import { MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import { HouseWorkOrdersStatusEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Image } from 'antd';
import { useEffect, useRef } from 'react';
import WorkLogsCreate from '../list/components/WorkLogsCreate';
import WorkVisited from '../list/components/WorkVisited';

interface WorkLogsListProps {
  workOrderId: any;
  workOrderStatus: any;
  workOrderProjectId: any;
  workOrderIsVisited: any;
  workOrderType: any;
  item?: any;
  reload?: () => void;
}

export default function WorkLogsList({
  workOrderId,
  workOrderStatus,
  workOrderProjectId,
  workOrderIsVisited,
  workOrderType,
  item,
  reload,
}: WorkLogsListProps) {
  const actionRef = useRef<any>();

  useEffect(() => {
    actionRef?.current?.reload();
  }, [workOrderId]);
  console.log(workOrderId, 'pwaoidas');
  return (
    <>
      <ProTable<Record<any, any>>
        {...MyProTableProps.props}
        actionRef={actionRef}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              house_work_orders_id: workOrderId || 0,
            },
            sort,
            Apis.WorkOrder.HouseWorkLogs.List,
          )
        }
        options={false}
        toolBarRender={(action) => [
          workOrderStatus === HouseWorkOrdersStatusEnum.Processing.value && (
            <WorkLogsCreate
              key="CreateWorkLog"
              workOrderId={workOrderId}
              workOrderProjectId={workOrderProjectId}
              item={item}
              reload={() => {
                action?.reload();
                reload?.();
              }}
            />
          ),
          workOrderStatus === HouseWorkOrdersStatusEnum.Completed.value &&
            !workOrderIsVisited &&
            (workOrderType === 'Repair' ||
              workOrderType === 'Incident' ||
              workOrderType === 'Complaint') && (
              <WorkVisited
                key="WorkVisited"
                workOrderId={workOrderId}
                reload={() => {
                  action?.reload();
                  reload?.();
                }}
              />
            ),
        ]}
        search={false}
        pagination={{
          pageSize: 10,
          showSizeChanger: false,
        }}
        columns={[
          MyColumns.ID({
            search: false,
          }),
          MyColumns.EnumTag({
            title: '工单状态',
            dataIndex: 'status',
            valueEnum: HouseWorkOrdersStatusEnum,
            search: false,
          }),
          {
            title: '进度描述',
            dataIndex: 'description',
            search: false,
            ellipsis: true,
          },

          {
            title: '附件',
            dataIndex: 'attachments',
            search: false,
            render: (_, record) => {
              if (
                !Array.isArray(record.attachments) ||
                record.attachments.length === 0
              ) {
                return '-';
              }
              return (
                <div style={{ display: 'flex', flexWrap: 'wrap', gap: 6 }}>
                  {record.attachments.map((item: any, index: number) => {
                    if (item.type && item?.type?.indexOf('image') > -1) {
                      return (
                        <Image
                          key={index}
                          width={40}
                          height={30}
                          src={item.url}
                          style={{
                            marginRight: 8,
                            marginBottom: 8,
                            objectFit: 'cover',
                          }}
                        />
                      );
                    } else if (item.type && item?.type?.indexOf('video') > -1) {
                      return (
                        <video
                          key={index}
                          width={40}
                          height={30}
                          controls
                          style={{
                            marginRight: 8,
                            marginBottom: 8,
                            objectFit: 'cover',
                          }}
                          src={item.url}
                        />
                      );
                    }
                    return null;
                  })}
                </div>
              );
            },
          },
          MyColumns.UpdatedAt(),
        ]}
      />
    </>
  );
}
