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

interface WorkLogsListProps {
  workOrderId: number;
  reload?: () => void;
}

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

  useEffect(() => {
    actionRef?.current?.reload();
  }, [workOrderId]);

  return (
    <>
      <ProTable<Record<any, any>>
        {...MyProTableProps.props}
        actionRef={actionRef}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              house_work_orders_id: workOrderId,
            },
            sort,
            Apis.WorkOrder.HouseWorkLogs.List,
          )
        }
        toolBarRender={(action) => [
          <WorkLogsCreate
            key="CreateWorkLog"
            workOrderId={workOrderId}
            reload={() => {
              action?.reload();
              reload?.();
            }}
          />,
        ]}
        search={false}
        pagination={{
          pageSize: 10,
          showSizeChanger: false,
        }}
        columns={[
          MyColumns.ID(),
          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' }}>
                  {record.attachments.map((item: any, index: number) => {
                    if (item.type && item.type.includes('image')) {
                      return (
                        <Image
                          key={index}
                          width={40}
                          height={30}
                          style={{
                            marginRight: 8,
                            marginBottom: 8,
                            objectFit: 'cover',
                          }}
                        />
                      );
                    } else if (item.type && item.type.includes('video')) {
                      return (
                        <video
                          key={index}
                          width={40}
                          height={30}
                          controls
                          style={{
                            marginRight: 8,
                            marginBottom: 8,
                            objectFit: 'cover',
                          }}
                          src={item.url}
                        />
                      );
                    }
                    return null;
                  })}
                </div>
              );
            },
          },
          {
            title: '创建时间',
            dataIndex: 'created_at',
            valueType: 'dateTime',
            search: false,
            width: 160,
          },
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.WorkOrder.HouseWorkLogs.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
