import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  MyTableActions,
  MyToolBarActions,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  HouseWorkOrdersAssignStatusEnum,
  HouseWorkOrdersLevelEnum,
  HouseWorkOrdersLocationEnum,
  HouseWorkOrdersStatusEnum,
  HouseWorkOrdersTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useSearchParams } from '@umijs/max';
import { Space, Tooltip } from 'antd';
import WorkOrderAssign from './modals/WorkOrderAssign';
import WorkOrderCreate from './modals/WorkOrderCreate';
import WorkOrderShow from './modals/WorkOrderShow';
import WorkOrderUpdate from './modals/WorkOrderUpdate';

export default function Index({ title = '报修报事' }) {
  const [searchParams, setSearchParams]: any = useSearchParams();

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="workorders"
      tabLabel={title}
    >
      <ProTable<Record<any, any>>
        {...MyProTableProps.props}
        headerTitle="工单列表"
        request={async (params, sort) => {
          setSearchParams(params);
          return MyProTableProps.request(
            !searchParams?.get('page')
              ? {
                  status: searchParams?.get('status') || '',
                  is_rework: searchParams?.get('is_rework') || '',
                  ...params,
                  type: [
                    HouseWorkOrdersTypeEnum.Repair.value,
                    HouseWorkOrdersTypeEnum.Incident.value,
                  ],
                }
              : params,
            sort,
            Apis.WorkOrder.HouseWorkOrders.List,
          );
        }}
        toolBarRender={(action) => [
          <MyToolBarActions
            key="toolbar"
            actions={{
              create: (
                <WorkOrderCreate
                  key="Create"
                  reload={action?.reload}
                  title={title}
                />
              ),
            }}
          />,
        ]}
        columns={[
          MyColumns.ID({ search: false }),
          MyColumns.EnumTag({
            title: '处理状态',
            dataIndex: 'status',
            valueEnum: HouseWorkOrdersStatusEnum,
            fieldProps: {
              defaultValue: searchParams?.get('status') || undefined,
            },
          }),
          Selects?.AssetProjects({
            title: '选择项目',
            key: 'asset_projects_id',
            hidden: true,
          }),
          {
            title: '项目名称',
            dataIndex: 'project_name',
            hidden: true,
          },
          MyColumns.EnumTag({
            title: '分配状态',
            dataIndex: 'assign_status',
            valueEnum: HouseWorkOrdersAssignStatusEnum,
          }),
          MyColumns.EnumTag({
            title: '工单类型',
            dataIndex: 'type',
            valueEnum: HouseWorkOrdersTypeEnum,
            search: false,
          }),
          MyColumns.EnumTag({
            title: '报修位置',
            dataIndex: 'location',
            valueEnum: HouseWorkOrdersLocationEnum,
          }),
          {
            title: '位置信息',
            dataIndex: ['asset_house', 'full_name'],
            render: (_, record) => {
              return (
                <Space>
                  {record?.asset_house?.full_name
                    ? record?.asset_house?.full_name
                    : record?.asset_project?.name}
                </Space>
              );
            },
            search: {
              transform: (value) => {
                return { house_name: value };
              },
            },
          },
          {
            title: '工单描述',
            dataIndex: 'content',
            search: false,
            width: 200,
            render: (text) => (
              <Tooltip title={text} placement="topLeft">
                <div
                  style={{
                    width: '100%',
                    overflow: 'hidden',
                    textOverflow: 'ellipsis',
                    display: '-webkit-box',
                    WebkitBoxOrient: 'vertical',
                    WebkitLineClamp: 1,
                    cursor: 'help',
                  }}
                >
                  {text}
                </div>
              </Tooltip>
            ),
          },

          MyColumns.EnumTag({
            title: '优先级',
            dataIndex: 'level',
            valueEnum: HouseWorkOrdersLevelEnum,
          }),
          {
            title: '时间要求',
            dataIndex: 'predict_complete_at',
            render: (_, record) => {
              return record?.predict_complete_at?.substring(0, 10) || '-';
            },
            search: false,
          },
          MyColumns.Boolean({
            title: '是否回访',
            dataIndex: 'is_visited',
          }),
          MyColumns.Boolean({
            title: '是否评价',
            dataIndex: 'is_evaluated',
          }),
          {
            title: '处理人',
            dataIndex: ['assign_employee', 'name'],
            search: false,
            render: (_, record) => {
              return `${record?.assign_employee?.name || ''}-${
                record?.assign_employee?.phone || ''
              }`;
            },
          },
          MyColumns.Option({
            render: (_, item: any, __, action) => (
              <MyTableActions
                actions={{
                  show: (
                    <WorkOrderShow
                      item={item}
                      title="详情"
                      reload={action?.reload}
                    />
                  ),
                  update: (
                    <WorkOrderUpdate
                      item={{
                        ...item,
                        typeEnum: () => {
                          let obj: any = JSON.parse(
                            JSON.stringify(HouseWorkOrdersTypeEnum),
                          );
                          delete obj.RenovationAcceptance;
                          delete obj.EquipmentMaintenance;
                          delete obj.RenovationInspection;
                          delete obj.Emergency;
                          delete obj.EmergEquipmentMaintenancency;
                          delete obj.SecurityInspection;
                          delete obj.Complaint;
                          delete obj.QualityCheck;
                          return obj;
                        },
                      }}
                      reload={action?.reload}
                      title={title}
                    />
                  ),
                  assign: (
                    <WorkOrderAssign
                      item={item}
                      reload={action?.reload}
                      title="指派"
                    />
                  ),
                  delete: (
                    <MyButtons.Delete
                      disabled={item.status !== 'Pending'}
                      onConfirm={() =>
                        Apis.WorkOrder.HouseWorkOrders.Delete({
                          id: item.id,
                        }).then(() => action?.reload())
                      }
                    />
                  ),
                }}
              />
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
