import { MyButtons, MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import { HouseChargeTasksTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import Assign from './modals/Assign';
import CreateMaintenances from './modals/CreateMaintenances';
import MyShow from './modals/Show';

export default function Index({ title = '维保工单' }) {
  return (
    <ProTable
      {...MyProTableProps.props}
      request={async (params, sort) =>
        MyProTableProps.request(
          params,
          sort,
          Apis.Asset.AssetItemMaintenances.List,
        )
      }
      headerTitle={title}
      toolBarRender={(action) => [
        <CreateMaintenances
          key="maintenances"
          reload={action?.reload}
          title="添加维保"
        />,
      ]}
      columns={[
        MyColumns.ID({
          search: false,
        }),
        // MyColumns.EnumTag({
        //   title: '状态',
        //   dataIndex: 'status',
        //   valueEnum: AssetItemMaintenanceStatusEnum,
        // }),
        // MyColumns.EnumTag({
        //   title: '类型',
        //   dataIndex: 'type',
        //   search: false,
        //   valueEnum: AssetItemMaintenanceTypeEnum,
        // }),
        MyColumns.EnumTag({
          title: '创建方式',
          dataIndex: 'source',
          valueEnum: HouseChargeTasksTypeEnum,
        }),
        {
          title: '资产名称',
          dataIndex: 'name',
        },
        {
          title: '资产型号',
          dataIndex: 'specification',
          search: false,
        },
        {
          title: '资产分类',
          render: (_, item: any) => {
            return `${item?.asset_item_category_level1_name}-${item?.asset_item_category_level2_name}-${item?.asset_item_category_level3_name}`;
          },
          search: false,
        },
        {
          title: '完成时间',
          dataIndex: 'deadline',
          search: false,
        },
        {
          title: '维保人',
          dataIndex: ['assigned_employee', 'name'],
          search: false,
        },
        MyColumns.CreatedAt(),
        MyColumns.Option({
          render: (_, item: any, index, action) => (
            <Space key={`item_${index}`}>
              <MyShow item={item} title="查看" />
              <Assign item={item} reload={action?.reload} title="指派" />
              <MyButtons.Default
                key="close"
                isConfirm
                title="关闭"
                disabled={
                  item?.status !== 'Assigned' && item?.status !== 'Pending'
                }
                description="确认关闭维保工单吗？"
                onConfirm={() =>
                  Apis.Asset.AssetItemMaintenances.Close({ id: item.id }).then(
                    () => action?.reload(),
                  )
                }
              />
            </Space>
          ),
        }),
      ]}
    />
  );
}
