/* eslint-disable react-hooks/exhaustive-deps */
/* 维修工单
 * @Author: Li Hanlin
 * @Date: 2022-12-02 11:41:03
 * @Last Modified by: Li Hanlin
 * @Last Modified time: 2023-03-07 11:34:10
 */

import * as React from 'react';
import { useState, useMemo, useRef, useEffect } from 'react';
import DrawerPro from '@/components/DrawerPro';
import AutoTable from '@/components/AutoTable';
import PremButton from '@/components/PremButton';
import { useRequest } from 'ahooks';
import { doFetch } from '@/utils/doFetch';
import { Dropdown, Menu, Button, message } from 'antd';
import InitForm from '@/components/InitForm';
import OrderHandle from '../platform/RepairOrderHandle';
import getcolumns from './columns';
import Detail from '@/components/RepaireDetail/Detail';
import { useModel } from '@umijs/max';
import EditTable from '@/components/InitForm/EditTable';

function Failure(props) {
  let actionRef = useRef(),
    formRef = useRef();
  const { initialState, error, refresh, setInitialState } = useModel('@@initialState');
  console.log(initialState);
  const [drawer, setdrawer] = useState({
    open: false,
    SpecName: null,
  }),
    [activeTabKey, setactiveTabKey] = useState('1');

  useEffect(async () => {
    let res = await doFetch({
      url: '/base/pmBaseBusinessData/queryBySpecName',
      params: {
        businessSpecName: 'ORDER_RECEIVING_DISTRIBUTE',
      },
    });
    setdrawer((s) => ({
      ...s,
      SpecName: res?.data?.data,
    }));
  }, []);

  const { run, loading } = useRequest(doFetch, {
    manual: true,
    onSuccess: (res, params) => {
      if (res?.code == '0000') {
        actionRef?.current?.reload();
        setdrawer((s) => ({
          ...s,
          open: false,
        }));
      }
    },
  });

  const detail = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          type: 'link',
          onClick: () => {
            setdrawer((s) => ({
              ...s,
              open: true,
              item: row,
              title: '详情',
              val: 'detail',
              title: '详细信息',
            }));
          },
        }}
      >
        详情
      </PremButton>
    );
  };

  const edit = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: 'small',
          onClick: () => {
            setdrawer((s) => ({
              ...s,
              open: true,
              item: row,
              title: '编辑',
              val: 'edit',
            }));
          },
        }}
      >
        编辑
      </PremButton>
    );
  };

  const remove = (text, row, _, action) => {
    return (
      <PremButton
        pop={{
          title: '是否删除?',
          okText: '确认',
          cancelText: '取消',
          onConfirm: () => {
            run({ url: pathconfig?.delete || '/delete', params: { id: row?.id } });
          },
        }}
        btn={{
          size: 'small',
          type: 'danger',
        }}
      >
        删除
      </PremButton>
    );
  };

  // 更多
  const More = (text, row, _, action) => {
    let menulist = [];
    if (drawer?.SpecName?.businessData !== '2') {
      // 接单模式
      if (row?.status == '2') {
        menulist = [
          {
            label: (
              <PremButton
                access="umRepairOrder_completeRepair"
                btn={{
                  onClick: async () => {
                    let repairContent = '';
                    if (row?.faultDetailId) {
                      doFetch({
                        url: '/repair/umFaultSettingDetail/queryById',
                        params: {
                          id: row?.faultDetailId,
                        },
                      }).then((res) => {
                        setdrawer((s) => ({
                          ...s,
                          open: true,
                          type: 'wcwx',
                          item: {
                            ...row,
                            repairType: '101',
                            isShutdown: 1,
                            repairContent: res?.data?.data?.faultSolution,
                            repairAssistList: row?.repairAssistList.map((it) => it?.assistUserId),
                          },
                          val: 'only',
                          title: '完成维修',
                        }));
                      });
                    }
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'wcwx',
                      item: {
                        ...row,
                        isShutdown: 1,
                        repairType: '101',
                        repairAssistList: row?.repairAssistList.map((it) => it?.assistUserId),
                      },
                      val: 'only',
                      title: '完成维修',
                    }));
                  },
                }}
              >
                完成维修
              </PremButton>
            ),
            key: '1',
          },
          {
            label: (
              <PremButton
                access="umRepairOrder_transferTracking"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'zzz',
                      item: row,
                      val: 'only',
                      title: '转追踪',
                    }));
                  },
                }}
              >
                转追踪
              </PremButton>
            ),
            key: '2',
          },
          {
            label: (
              <PremButton
                access="umRepairOrder_transferOutsourcing"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'zwx',
                      item: row,
                      val: 'only',
                      title: '转外协',
                    }));
                  },
                }}
              >
                转外协
              </PremButton>
            ),
            key: '3',
          },
          {
            label: (
              <PremButton
                access="umRepairOrder_documentTransfer"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'zd',
                      item: row,
                      val: 'only',
                      title: '转单',
                    }));
                  },
                }}
              >
                转单
              </PremButton>
            ),
            key: '4',
          },
          {
            label: (
              <PremButton
                access="umRepairOrder_chargeback"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'td',
                      item: row,
                      val: 'only',
                      title: '退单',
                    }));
                  },
                }}
              >
                退单
              </PremButton>
            ),
            key: '5',
          },
        ];
      } else if (row?.status == '6') {
        menulist = [
          {
            label: (
              <PremButton
                access="umRepairOrder_transferTrackingAudit"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'zzsh',
                      item: { ...row, resultFlag: '1' },
                      val: 'only',
                      title: '追踪审核',
                    }));
                  },
                }}
              >
                追踪审核
              </PremButton>
            ),
            key: '7',
          },
        ];
      } else if (row?.status == '4') {
        menulist = [
          {
            label: (
              <PremButton
                access="umRepairOrder_transferAudit"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'wxsh',
                      item: { ...row, resultFlag: '1' },
                      val: 'only',
                      title: '外协审核',
                    }));
                  },
                }}
              >
                外协审核
              </PremButton>
            ),
            key: '8',
          },
        ];
      } else if (row?.status == '7') {
        menulist = [
          {
            label: (
              <PremButton
                access="umRepairOrder_chargebackAudit"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'tdsh',
                      item: { ...row, resultFlag: '1' },
                      val: 'only',
                      title: '退单审核',
                    }));
                  },
                }}
              >
                退单审核
              </PremButton>
            ),
            key: '9',
          },
        ];
      } else if (row?.status == '3') {
        menulist = [
          {
            label: (
              <PremButton
                access="umRepairOrder_completeRepairValidate"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'xgyz',
                      item: { ...row, resultFlag: '1' },
                      val: 'only',
                      title: '效果验证',
                    }));
                  },
                }}
              >
                效果验证
              </PremButton>
            ),
            key: '6',
          },
        ];
      } else {
        menulist = [
          {
            label: (
              <PremButton
                access="umRepairOrder_dispatchById"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'jd',
                      item: row,
                      val: 'only',
                      title: '接单',
                    }));
                  },
                }}
              >
                接单
              </PremButton>
            ),
            key: '0',
          },
        ];
      }
    } else {
      if (row?.status == '2') {
        menulist = [
          {
            label: (
              <PremButton
                access="umRepairOrder_completeRepair"
                btn={{
                  onClick: () => {
                    let repairContent = '';
                    if (row?.faultDetailId) {
                      doFetch({
                        url: '/repair/umFaultSettingDetail/queryById',
                        params: {
                          id: row?.faultDetailId,
                        },
                      }).then((res) => {
                        setdrawer((s) => ({
                          ...s,
                          open: true,
                          type: 'wcwx',
                          item: {
                            ...row,
                            repairType: '101',
                            isShutdown: 1,
                            repairContent: res?.data?.data?.faultSolution,
                            repairAssistList: row?.repairAssistList.map((it) => it?.assistUserId),
                          },
                          val: 'only',
                          title: '完成维修',
                        }));
                      });
                    }
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'wcwx',
                      item: {
                        ...row,
                        repairType: '101',
                        isShutdown: 1,
                        repairAssistList: row?.repairAssistList.map((it) => it?.assistUserId),
                      },
                      val: 'only',
                      title: '完成维修',
                    }));
                  },
                }}
              >
                完成维修
              </PremButton>
            ),
            key: '1',
          },
          {
            label: (
              <PremButton
                access="umRepairOrder_transferTracking"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'zzz',
                      item: row,
                      val: 'only',
                      title: '转追踪',
                    }));
                  },
                }}
              >
                转追踪
              </PremButton>
            ),
            key: '2',
          },
          {
            label: (
              <PremButton
                access="umRepairOrder_transferOutsourcing"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'zwx',
                      item: row,
                      val: 'only',
                      title: '转外协',
                    }));
                  },
                }}
              >
                转外协
              </PremButton>
            ),
            key: '3',
          },
          {
            label: (
              <PremButton
                access="umRepairOrder_documentTransfer"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'zd',
                      item: row,
                      val: 'only',
                      title: '转单',
                    }));
                  },
                }}
              >
                转单
              </PremButton>
            ),
            key: '4',
          },
          {
            label: (
              <PremButton
                access="umRepairOrder_chargeback"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'td',
                      item: row,
                      val: 'only',
                      title: '退单',
                    }));
                  },
                }}
              >
                退单
              </PremButton>
            ),
            key: '5',
          },
        ];
      } else if (row?.status == '6') {
        menulist = [
          {
            label: (
              <PremButton
                access="umRepairOrder_transferTrackingAudit"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'zzsh',
                      item: { ...row, resultFlag: '1' },
                      val: 'only',
                      title: '追踪审核',
                    }));
                  },
                }}
              >
                追踪审核
              </PremButton>
            ),
            key: '7',
          },
        ];
      } else if (row?.status == '4') {
        menulist = [
          {
            label: (
              <PremButton
                access="umRepairOrder_transferAudit"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'wxsh',
                      item: { ...row, resultFlag: '1' },
                      val: 'only',
                      title: '外协审核',
                    }));
                  },
                }}
              >
                外协审核
              </PremButton>
            ),
            key: '8',
          },
        ];
      } else if (row?.status == '7') {
        menulist = [
          {
            label: (
              <PremButton
                access="umRepairOrder_chargebackAudit"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'tdsh',
                      item: row,
                      val: 'only',
                      title: '退单审核',
                    }));
                  },
                }}
              >
                退单审核
              </PremButton>
            ),
            key: '9',
          },
        ];
      } else if (row?.status == '3') {
        menulist = [
          {
            label: (
              <PremButton
                access="umRepairOrder_completeRepairValidate"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'xgyz',
                      item: { ...row, resultFlag: '1' },
                      val: 'only',
                      title: '效果验证',
                    }));
                  },
                }}
              >
                效果验证
              </PremButton>
            ),
            key: '6',
          },
        ];
      } else {
        menulist = [
          {
            label: (
              <PremButton
                access="umRepairOrder_dispatchById"
                btn={{
                  onClick: () => {
                    setdrawer((s) => ({
                      ...s,
                      open: true,
                      type: 'pd',
                      item: row,
                      val: 'only',
                      title: '派单',
                    }));
                  },
                }}
              >
                派单
              </PremButton>
            ),
            key: '0',
          },
        ];
      }
    }

    const menu = <Menu items={menulist} />;
    return row?.status == '5' ? (
      <Button size="small" disabled>
        更多
      </Button>
    ) : (
      <Dropdown
        overlay={menu}
        trigger={['click']}
        arrow={true}
        getPopupContainer={() => containderef.current}
      >
        <Button size="small">更多</Button>
      </Dropdown>
    );
  };

  // ++++++下拉菜单+++++++
  // 接单
  const Recevive = () => {
    return (
      <OrderHandle
        type="wxjd"
        id={drawer?.item.id}
        actionRef={actionRef}
        url="/repair/umRepairOrder/orderReceiving"
        setdrawer={setdrawer}
      />
    );
  };
  // 派单
  const Disp = () => {
    return (
      <OrderHandle
        type="wxpd"
        id={drawer?.item.id}
        actionRef={actionRef}
        url="/repair/umRepairOrder/dispatchById"
        setdrawer={setdrawer}
      />
    );
  };
  // 完成维修
  const Finishrepair = () => {
    const columns_fsmjxh = [
      {
        title: '线边库',
        dataIndex: 'stockName',
        key: 'stockName',
        editable: false,
      },
      {
        title: '备件料号',
        dataIndex: 'sparePartNo',
        key: 'sparePartNo',
        span: 3,
        editable: false,
      },
      {
        title: '备件名称',
        dataIndex: 'sparePartName',
        key: 'sparePartName',
        span: 3,
        editable: false,
      },
      {
        title: '供应商编号',
        dataIndex: 'supplierNo',
        key: 'supplierNo',
        span: 3,
        editable: false,
      },
      {
        title: '供应商名称',
        dataIndex: 'supplierName',
        key: 'supplierName',
        span: 3,
        editable: false,
      },
      {
        title: '可用数量',
        dataIndex: 'usedStock',
        key: 'usedStock',
        search: false,
        span: 3,
        editable: false,
      },
      {
        title: (
          <div>
            消耗数量 <span style={{ color: 'red' }}>* </span>
          </div>
        ),
        search: false,
        dataIndex: 'operateNum',
        valueType: 'digit',
        fieldProps: {
          precision: 3,
        },
        key: 'operateNum',
      },
    ];
    const columns = [
      {
        title: '故障类型',
        dataIndex: 'faultSettingId',
        key: 'faultSettingId',
        formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
        valueType: 'select',
        options: {
          path: '/repair/umFaultSetting/selected/queryList',
          params: {
            equipmentId: drawer?.item.equipmentId,
          },
        },
      },
      {
        title: '故障名称',
        dataIndex: 'faultDetailId',
        key: 'faultDetailId',
        valueType: 'select',
        formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
        options: {
          path: '/repair/umFaultSettingDetail/selected/queryList',
          linkParams: {
            faultSettingId: '',
          },
        },
      },
      {
        title: '故障原因',
        dataIndex: 'faultCause',
        formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
        key: 'faultCause',
        valueType: 'textarea',
        formItemProps: {
          rules: [
            {
              required: true,
              message: '此项为必填项',
            },
          ],
        },
      },
      {
        title: '开始维修时间',
        dataIndex: 'repairTime',
        key: 'repairTime',
        fieldProps: {
          disabled: true,
        },
      },
      {
        title: '维修类型',
        dataIndex: 'repairType',
        key: 'repairType',
        formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
        valueType: 'select',
        options: {
          path: '/base/sysInitData/queryChildrenSelection',
          params: {
            parentId: 1,
          },
        },
        formItemProps: {
          rules: [
            {
              required: true,
              message: '此项为必填项',
            },
          ],
        },
      },
      {
        title: '是否停机维修',
        dataIndex: 'isShutdown',
        formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
        key: 'isShutdown',
        valueType: 'radio',
        options: [
          {
            value: 1,
            label: '是',
          },
          {
            value: 2,
            label: '否',
          },
        ],
        formItemProps: {
          rules: [
            {
              required: true,
              message: '此项为必填项',
            },
          ],
        },
      },
      {
        title: '维修内容',
        dataIndex: 'repairContent',
        key: 'repairContent',
        formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
        valueType: 'textarea',
        formItemProps: {
          rules: [
            {
              required: true,
              message: '此项为必填项',
            },
          ],
        },
      },
      {
        title: '协助维修人员',
        dataIndex: 'repairAssistList',
        key: 'repairAssistList',
        valueType: 'select',
        options: {
          path: '/repair/umRepairOrder/selection',
          params: { id: drawer?.item.id, permission: 'umRepairOrder:completeRepair' },
        },
        fieldProps: {
          placeholder: '请选择',
          showSearch: true,
          mode: 'multiple',
        },
      },
      {
        title: '非寿命件消耗',
        dataIndex: 'supplierList',
        key: 'supplierList',
        valueType: 'formSelectList',
        colProps: {
          span: 24,
        },
        span: 3,
        columns: columns_fsmjxh,
        hideInSearch: true,
        hideInTable: true,
        path: '/sparepart/lineStock/queryConsumeStock',
        rowName: 'supplierList',
        rowSelection: {
          type: 'checkbox',
          getCheckboxProps: (record) => ({
            disabled: record.isAsh == '1',
          }),
        },
      },
      {
        title: '寿命件更换',
        dataIndex: 'lifePieceStockUseList',
        key: 'lifePieceStockUseList',
        valueType: 'Expandable',
        colProps: {
          span: 24,
        },
        columns: [
          {
            title: '备件料号',
            dataIndex: 'sparePartNo',
            key: 'sparePartNo',
          },
          {
            title: '备件名称',
            dataIndex: 'sparePartName',
            key: 'sparePartName',
          },
          {
            title: '供应商编号',
            dataIndex: 'supplierNo',
            key: 'supplierNo',
            search: false,
          },
          {
            title: '供应商名称',
            dataIndex: 'supplierName',
            search: false,
            key: 'supplierName',
          },
          {
            title: '安装部位',
            dataIndex: 'installPosition',
            key: 'installPosition',
          },
          {
            title: '安装数量',
            dataIndex: 'installNum',
            search: false,
            key: 'installNum',
          },
          {
            title: '下次更换日期',
            dataIndex: 'nextReplaceDate',
            key: 'nextReplaceDateList',
            valueType: 'dateRange',
          },
        ],
        path: '/sparepart/lifePieceAccount/queryPageByEquipment',
        expandablePath: '/sparepart/lineStock/queryReplaceStockAll',
        extraparams: { equipmentId: drawer?.item?.equipmentId },
      },
    ];
    return (
      <>
        <InitForm
          formRef={formRef}
          defaultFormValue={drawer?.item}
          fields={columns}
          onValuesChange={(changedValues, allValues) => {
            if (Object.keys(changedValues)[0] == 'faultDetailId') {
              doFetch({
                url: '/repair/umFaultSettingDetail/queryById',
                params: {
                  id: changedValues?.faultDetailId,
                },
              }).then((res) => {
                formRef?.current?.setFieldsValue({
                  repairContent: res?.data?.data.faultSolution,
                });
              });
            }
          }}
          onFinish={async (vals) => {
            console.log(vals);
            let params = {
              repairComplete: {
                faultType: vals.faultSettingId,
                faultName: vals.faultDetailId,
                faultCause: vals.faultCause,
                repairType: vals.repairType,
                isShutdown: vals.isShutdown,
                repairContent: vals.repairContent,
              },
              id: drawer?.item.id,
              repairAssistList: vals?.repairAssistList?.map((it) => ({ assistUserId: it })),
              lineStockUseList: vals?.supplierList?.map((it) => ({
                lineStockId: it?.id,
                operateNum: it?.operateNum,
              })),
              lifePieceStockUseList: vals?.lifePieceStockUseList,
            };
            let res = await doFetch({
              url: '/repair/umRepairOrder/completeRepair',
              params,
            });
            if (res.code === '0000') {
              message.success('操作成功！');
              setdrawer((s) => ({
                ...s,
                open: false,
              }));
              actionRef.current.reload();
            }
          }}
          {...drawer}
        />
      </>
    );
  };
  // 转追踪
  const Toracking = () => {
    return (
      <>
        <InitForm
          defaultFormValue={drawer?.item}
          fields={[
            {
              title: '申请追踪原因',
              dataIndex: 'reason',
              key: 'reason',
              valueType: 'textarea',
              formItemProps: {
                rules: [
                  {
                    required: true,
                    message: '此项为必填项',
                  },
                ],
              },
            },
          ]}
          onFinish={async (vals) => {
            let params = {
              repairInfo: { ...vals },
              id: drawer?.item.id,
            };
            let res = await doFetch({
              url: '/repair/umRepairOrder/transferTracking',
              params,
            });
            if (res.code === '0000') {
              message.success('操作成功！');
              setdrawer((s) => ({
                ...s,
                open: false,
              }));
              actionRef?.current?.reload();
            }
          }}
          {...drawer}
        />
      </>
    );
  };
  // 转外协
  const Toout = () => {
    return (
      <>
        <InitForm
          defaultFormValue={drawer?.item}
          fields={[
            {
              title: '申请外协原因',
              dataIndex: 'reason',
              key: 'reason',
              valueType: 'textarea',
              formItemProps: {
                rules: [
                  {
                    required: true,
                    message: '此项为必填项',
                  },
                ],
              },
            },
          ]}
          onFinish={async (vals) => {
            let params = {
              repairInfo: { ...vals },
              id: drawer?.item.id,
            };
            let res = await doFetch({
              url: '/repair/umRepairOrder/transferOutsourcing',
              params,
            });
            if (res.code === '0000') {
              message.success('操作成功！');
              setdrawer((s) => ({
                ...s,
                open: false,
              }));
              actionRef.current.reload();
            }
          }}
          {...drawer}
        />
      </>
    );
  };
  // 转单
  const Transfer = () => {
    return (
      <>
        <InitForm
          defaultFormValue={drawer?.item}
          fields={[
            {
              title: '转单类型',
              dataIndex: 'reviewerType',
              key: 'reviewerType',
              valueType: 'select',
              options: {
                path: '/base/sysInitData/queryChildrenSelection',
                params: {
                  parentId: 2,
                },
              },
              formItemProps: {
                rules: [
                  {
                    required: true,
                    message: '此项为必填项',
                  },
                ],
              },
            },
            {
              title: '转单原因',
              dataIndex: 'reason',
              key: 'reason',
              valueType: 'textarea',
              formItemProps: {
                rules: [
                  {
                    required: true,
                    message: '此项为必填项',
                  },
                ],
              },
            },
          ]}
          onFinish={async (vals) => {
            let params = {
              repairInfo: { ...vals },
              id: drawer?.item.id,
            };
            let res = await doFetch({
              url: '/repair/umRepairOrder/documentTransfer',
              params,
            });
            if (res.code === '0000') {
              message.success('操作成功！');
              setdrawer((s) => ({
                ...s,
                open: false,
              }));
              actionRef.current.reload();
            }
          }}
          {...drawer}
        />
      </>
    );
  };
  // 退单
  const Singelback = () => {
    return (
      <>
        <InitForm
          defaultFormValue={drawer?.item}
          fields={[
            {
              title: '退单原因',
              dataIndex: 'reason',
              key: 'reason',
              valueType: 'textarea',
              formItemProps: {
                rules: [
                  {
                    required: true,
                    message: '此项为必填项',
                  },
                ],
              },
            },
          ]}
          onFinish={async (vals) => {
            let params = {
              repairInfo: { ...vals },
              id: drawer?.item.id,
            };
            let res = await doFetch({
              url: '/repair/umRepairOrder/chargeback',
              params,
            });
            if (res.code === '0000') {
              message.success('操作成功！');
              setdrawer((s) => ({
                ...s,
                open: false,
              }));
              actionRef.current.reload();
            }
          }}
          {...drawer}
        />
      </>
    );
  };
  // 效果验证
  const EffectCheck = () => {
    return (
      <>
        <InitForm
          defaultFormValue={drawer?.item}
          fields={[
            {
              title: '故障描述',
              dataIndex: 'faultDescription',
              key: 'faultDescription',
              valueType: 'textarea',
              fieldProps: {
                disabled: true,
              },
              formItemProps: {
                rules: [
                  {
                    required: true,
                    message: '此项为必填项',
                  },
                ],
              },
            },
            {
              valueType: 'textarea',
              title: '故障原因',
              dataIndex: 'faultCause',
              key: 'faultCause',
              fieldProps: {
                disabled: true,
              },
              formItemProps: {
                rules: [
                  {
                    required: true,
                    message: '此项为必填项',
                  },
                ],
              },
            },
            {
              valueType: 'textarea',
              title: '维修内容',
              dataIndex: 'repairContent',
              key: 'repairContent',
              fieldProps: {
                disabled: true,
              },
              formItemProps: {
                rules: [
                  {
                    required: true,
                    message: '此项为必填项',
                  },
                ],
              },
            },
            {
              title: '验证结果',
              dataIndex: 'resultFlag',
              key: 'resultFlag',
              valueType: 'radio',
              options: [
                { label: '通过', value: '1' },
                { label: '不通过', value: '0' },
              ],
              formItemProps: {
                rules: [
                  {
                    required: true,
                    message: '此项为必填项',
                  },
                ],
              },
            },
            {
              title: '备注',
              dataIndex: 'remark',
              key: 'remark',
              valueType: 'textarea',
              formItemProps: {
                rules: [
                  {
                    required: false,
                    message: '此项为必填项',
                  },
                ],
              },
            },
          ]}
          onFinish={async (vals) => {
            let params = {
              repairProcess: {
                reviewerResult: '',
                remark: vals?.remark,
              },
              resultFlag: vals?.resultFlag,
              id: drawer?.item.id,
            };
            let res = await doFetch({
              url: '/repair/umRepairOrder/completeRepairValidate',
              params,
            });
            if (res.code === '0000') {
              message.success('操作成功！');
              setdrawer((s) => ({
                ...s,
                open: false,
              }));
              actionRef.current.reload();
            }
          }}
          {...drawer}
        />
      </>
    );
  };
  // 追踪审核
  const Rackaudit = () => {
    return (
      <>
        <InitForm
          defaultFormValue={drawer?.item}
          fields={[
            {
              title: '申请追踪原因',
              dataIndex: 'reason',
              key: 'reason',
              fieldProps: {
                disabled: true,
              },
              formItemProps: {
                rules: [
                  {
                    required: false,
                    message: '此项为必填项',
                  },
                ],
              },
            },
            {
              title: '审核结果',
              dataIndex: 'resultFlag',
              key: 'resultFlag',
              valueType: 'radio',
              options: [
                { label: '通过', value: '1' },
                { label: '不通过', value: '0' },
              ],
              formItemProps: {
                rules: [
                  {
                    required: true,
                    message: '此项为必填项',
                  },
                ],
              },
            },
            {
              title: '备注',
              dataIndex: 'remark',
              key: 'remark',
              valueType: 'textarea',
              formItemProps: {
                rules: [
                  {
                    required: false,
                    message: '此项为必填项',
                  },
                ],
              },
            },
          ]}
          onFinish={async (vals) => {
            let params = {
              repairProcess: { reviewerResult: '', remark: vals?.remark },
              resultFlag: vals.resultFlag,
              id: drawer?.item.id,
            };
            let res = await doFetch({
              url: '/repair/umRepairOrder/transferTrackingAudit',
              params,
            });
            if (res.code === '0000') {
              message.success('操作成功！');
              setdrawer((s) => ({
                ...s,
                open: false,
              }));
              actionRef.current.reload();
            }
          }}
          {...drawer}
        />
      </>
    );
  };
  // 外协审核
  const Outaudit = () => {
    return (
      <>
        <InitForm
          defaultFormValue={drawer?.item}
          fields={[
            {
              title: '申请外协原因',
              dataIndex: 'reason',
              key: 'reason',
              fieldProps: {
                disabled: true,
              },
              formItemProps: {
                rules: [
                  {
                    required: false,
                    message: '此项为必填项',
                  },
                ],
              },
            },
            {
              title: '审核结果',
              dataIndex: 'resultFlag',
              key: 'resultFlag',
              valueType: 'radio',
              options: [
                { label: '通过', value: '1' },
                { label: '不通过', value: '0' },
              ],
              formItemProps: {
                rules: [
                  {
                    required: true,
                    message: '此项为必填项',
                  },
                ],
              },
            },
            {
              title: '备注',
              dataIndex: 'remark',
              key: 'remark',
              valueType: 'textarea',
              formItemProps: {
                rules: [
                  {
                    required: false,
                    message: '此项为必填项',
                  },
                ],
              },
            },
          ]}
          onFinish={async (vals) => {
            let params = {
              repairProcess: { reviewerResult: '', remark: vals?.remark },
              resultFlag: vals.resultFlag,
              id: drawer?.item.id,
            };
            let res = await doFetch({
              url: '/repair/umRepairOrder/transferAudit',
              params,
            });
            if (res.code === '0000') {
              message.success('操作成功！');
              setdrawer((s) => ({
                ...s,
                open: false,
              }));
              actionRef.current.reload();
            }
          }}
          {...drawer}
        />
      </>
    );
  };
  // 退单审核
  const Backaudit = () => {
    return (
      <>
        <InitForm
          defaultFormValue={drawer?.item}
          fields={[
            {
              title: '退单原因',
              dataIndex: 'reason',
              key: 'reason',
              fieldProps: {
                disabled: true,
              },
              formItemProps: {
                rules: [
                  {
                    required: false,
                    message: '此项为必填项',
                  },
                ],
              },
            },
            {
              title: '审核结果',
              dataIndex: 'resultFlag',
              key: 'resultFlag',
              valueType: 'radio',
              options: [
                { label: '通过', value: '1' },
                { label: '不通过', value: '0' },
              ],
              formItemProps: {
                rules: [
                  {
                    required: true,
                    message: '此项为必填项',
                  },
                ],
              },
            },
            {
              title: '备注',
              dataIndex: 'remark',
              key: 'remark',
              valueType: 'textarea',
              formItemProps: {
                rules: [
                  {
                    required: false,
                    message: '此项为必填项',
                  },
                ],
              },
            },
          ]}
          onFinish={async (vals) => {
            let params = {
              repairProcess: { reviewerResult: '', remark: vals?.remark },
              resultFlag: vals.resultFlag,
              id: drawer?.item.id,
            };
            let res = await doFetch({
              url: '/repair/umRepairOrder/chargebackAudit',
              params,
            });
            if (res.code === '0000') {
              message.success('操作成功！');
              setdrawer((s) => ({
                ...s,
                open: false,
              }));
              actionRef.current.reload();
            }
          }}
          {...drawer}
        />
      </>
    );
  };

  const basecolumns = useMemo(() => {
    if (activeTabKey == 3) {
      return [
        {
          title: '创建时间',
          dataIndex: 'createTime',
          key: 'createTime',
        },
        {
          title: '工单状态',
          dataIndex: 'statusName',
          key: 'statusName',
        },
        {
          title: '报修单号',
          dataIndex: 'repairNo',
          key: 'repairNo',
        },
        {
          title: '最新维修人员',
          dataIndex: 'newRepairUserName',
          key: 'newRepairUserName',
        },
        {
          title: '关单时间',
          dataIndex: 'repairCloseTime',
          key: 'repairCloseTime',
        },
      ];
    } else {
      return [
        {
          title: '创建时间',
          dataIndex: 'createTime',
          key: 'createTime',
        },
        {
          title: '工单状态',
          dataIndex: 'statusName',
          key: 'statusName',
        },
        {
          title: '报修单号',
          dataIndex: 'repairNo',
          key: 'repairNo',
        },
        {
          title: '最新维修人员',
          dataIndex: 'newRepairUserName',
          key: 'newRepairUserName',
        },
      ];
    }
  }, [activeTabKey]);

  const selectType = {
    jd: <Recevive />,
    pd: <Disp />,
    wcwx: <Finishrepair />,
    zzz: <Toracking />,
    zwx: <Toout />,
    zd: <Transfer />,
    td: <Singelback />,
    xgyz: <EffectCheck />,
    zzsh: <Rackaudit />,
    wxsh: <Outaudit />,
    tdsh: <Backaudit />,
    detail: (
      <Detail
        title="维修单号"
        titleno={drawer?.item?.repairOrderNo}
        detailpath="/repair/umRepairOperation/queryOperationList"
        params={{ repairOrderId: drawer?.item?.id }}
        baseRow={drawer?.item}
        basecolumns={basecolumns}
        type="wxgd"
      />
    ),
  };

  const columns = useMemo(() => {
    let defcolumn = getcolumns(setdrawer, drawer?.SpecName?.businessData).filter(
      (it) => it.key == activeTabKey,
    )[0]?.columns;
    let defpath =
      getcolumns(setdrawer, drawer?.SpecName?.businessData).filter(
        (it) => it.key == activeTabKey,
      )[0]?.pathconfig ?? {};
    if (activeTabKey == 1) {
      return defcolumn.concat({
        title: '操作',
        valueType: 'option',
        width: 150,
        render: (text, row, _, action) => [More(text, row, _, action)],
      });
    } else {
      return defcolumn;
    }
  }, [activeTabKey, drawer]);

  const pathconfig = useMemo(() => {
    let defpath = getcolumns(setdrawer).filter((it) => it.key == activeTabKey)[0]?.pathconfig ?? {};
    return defpath;
  }, [activeTabKey]);

  const containderef = useRef();
  return (
    <div ref={containderef}>
      <AutoTable
        pagetitle={<h3 className="page-title">维修工单</h3>}
        columns={columns}
        path={pathconfig?.list || '/ngic-auth/sysUser/query/page'}
        actionRef={actionRef}
        resizeable={false}
        tabList={getcolumns()}
        activeTabKey={activeTabKey}
        extraparams={activeTabKey == 1 ? { newUserId: initialState?.currentUser?.id } : null}
        onTabChange={(key) => {
          setactiveTabKey(key);
        }}
      />

      <DrawerPro
        fields={columns}
        detailpath={pathconfig?.detail || null}
        detailData={drawer?.item}
        defaultFormValue={drawer?.item}
        params={{ id: drawer?.item?.id }}
        formRef={formRef}
        placement="right"
        onClose={() => {
          setdrawer((s) => ({
            ...s,
            open: false,
          }));
        }}
        {...drawer}
        onFinish={(vals) => {
          if (drawer?.val == 'add') {
            run({ url: pathconfig?.add || '/add', params: { ...vals } });
          } else if (drawer?.val == 'edit') {
            run({ url: pathconfig?.edit || '/edit', params: { ...vals, id: drawer?.item?.id } });
          }
        }}
      >
        {selectType[drawer?.type]}
      </DrawerPro>
    </div>
  );
}

export default Failure;
