import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  HouseWorkOrdersAssignStatusEnum,
  HouseWorkOrdersStatusEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space, Tag } from 'antd';
import WorkOrderShow from '../../work_order/list/modals/WorkOrderShow';
import Create from './modals/Create';
import Show from './modals/Show';
import Update from './modals/Update';

export default function Index({ title = '品质核查' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="qualitychecks"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle={title}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.QualityCheck.QualityChecks.List,
          )
        }
        toolBarRender={(action) => [
          <Create key="Create" reload={action?.reload} title={title} />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            title: '关联项目',
            dataIndex: ['asset_project', 'name'],
            search: {
              transform: (value) => {
                return { project_name: value };
              },
            },
          },
          {
            title: '核查对象',
            dataIndex: 'check_object',
            // search: false,
          },
          {
            title: '核查日期',
            dataIndex: 'check_date',
            search: false,
          },
          {
            title: '核查日期',
            dataIndex: 'check_date',
            valueType: 'dateRange',
            hidden: true,
          },
          {
            title: '核查得分',
            dataIndex: 'check_score',
            search: false,
            render: (value: any) => `${value?.slice(0, 2) || 0} 分`,
          },
          {
            title: '下发整改',
            dataIndex: 'is_rectification',
            search: false,
            render: (value) => (
              <Tag color={value ? 'red' : 'green'}>{value ? '是' : '否'}</Tag>
            ),
          },
          {
            title: '整改工单',
            dataIndex: 'house_work_orders_id',
            search: false,
            render: (value: any) => (
              <WorkOrderShow
                item={{ id: value, type: 'link' }}
                title={value + ' 查看'}
              />
            ),
          },
          MyColumns.EnumTag({
            title: '分配状态',
            dataIndex: ['house_work_order', 'assign_status'],
            valueEnum: HouseWorkOrdersAssignStatusEnum,
          }),
          MyColumns.EnumTag({
            title: '处理进度',
            dataIndex: ['house_work_order', 'status'],
            valueEnum: HouseWorkOrdersStatusEnum,
            search: false,
          }),
          {
            title: '核查人',
            dataIndex: 'employee_name',
            render: (_, record) => {
              return `${record?.employee_name || ''}-${
                record?.employee_phone || ''
              }`;
            },
          },
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <Show item={item} />
                <Update item={item} reload={action?.reload} title={title} />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.QualityCheck.QualityChecks.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
