/* 接单、派单窗口
 * @Author: Li Hanlin
 * @Date: 2022-12-05 11:13:26
 * @Last Modified by: Li Hanlin
 * @Last Modified time: 2023-02-02 13:42:49
 */

import React, { useState, useEffect } from 'react';
import InitForm from '@/components/InitForm';
import { doFetch } from '@/utils/doFetch';
import { message } from 'antd';
const App = ({ type, id, actionRef, url, setdrawer }) => {
  const columns = {
    wxjd: [
      {
        title: '协助维修人员',
        dataIndex: 'repairAssistList',
        key: 'repairAssistList',
        valueType: 'select',
        options: {
          path: '/repair/umRepairOrder/selection',
          params: { id, permission: 'umRepairOrder:completeRepair' },
        },
        fieldProps: {
          placeholder: '请选择',
          showSearch: true,
          mode: 'multiple',
        },
      },
    ],
    wxpd: [
      {
        title: '维修人员',
        dataIndex: 'repairId',
        key: 'repairId',
        valueType: 'select',
        options: { path: '/repair/umRepairOrder/selectionRepair', params: { id } },
        formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
      },
      {
        title: '协助维修人员',
        dataIndex: 'repairAssistList',
        key: 'repairAssistList',
        valueType: 'select',
        options: {
          path: '/repair/umRepairOrder/selection',
          linkParams: { repairId: 'repairId' },
          extraParams: {
            id,
            permission: 'umRepairOrder:completeRepair',
          },
        },
        fieldProps: {
          placeholder: '请选择',
          showSearch: true,
          mode: 'multiple',
        },
      },
    ],
    zzpd: [
      {
        title: '追踪人员',
        dataIndex: 'trackAssistList',
        key: 'trackAssistList',
        valueType: 'select',
        options: {
          path: '/repair/umTrackOrder/selection',
          params: { permission: 'umTrackOrder:trackById', id },
        },
        formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
      },
    ],
    wpd: [
      {
        title: '外协人员',
        dataIndex: 'otherUnitsAssistList',
        key: 'otherUnitsAssistList',
        valueType: 'select',
        options: {
          path: '/repair/umOtherUnitsOrder/selection',
          params: { id, permission: 'umOtherUnitsOrder:finishOtherUnits' },
        },
        formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
      },
    ],
  };
  const finishHandle = {
    wxjd: async (vals) => {
      //console.log(vals);
      let params = {};
      if (vals.repairAssistList) {
        params.repairAssistList = vals?.repairAssistList.map((it) => ({
          assistUserId: it,
        }));
      }
      let res = await doFetch({
        url,
        params: {
          ...params,
          id,
        },
      });
      if (res.code === '0000') {
        message.success('操作成功！');
        setdrawer((s) => ({
          ...s,
          open: false,
        }));
        actionRef.current.reload();
      }
    },
    wxpd: async (vals) => {
      let params = {
        repairId: vals?.repairId,
      };
      if (vals.repairAssistList) {
        params.repairAssistList = vals?.repairAssistList.map((it) => {
          return {
            assistUserId: it,
          };
        });
      }
      let res = await doFetch({
        url,
        params: { ...params, id },
      });
      if (res.code === '0000') {
        message.success('操作成功！');
        setdrawer((s) => ({
          ...s,
          open: false,
        }));
        actionRef.current.reload();
      }
    },
    zzpd: async (vals) => {
      //console.log(vals);
      let params = {
        trackAssistList: [{ assistUserId: vals?.trackAssistList }],
        id,
      };
      //console.log(params);
      let res = await doFetch({
        url,
        params,
      });
      if (res.code === '0000') {
        message.success('操作成功！');
        setdrawer((s) => ({
          ...s,
          open: false,
        }));
        actionRef.current.reload();
      }
    },
    wpd: async (vals) => {
      let params = {
        otherUnitsAssistList: [{ assistUserId: vals?.otherUnitsAssistList }],
        id,
      };
      let res = await doFetch({
        url,
        params,
      });
      if (res.code === '0000') {
        message.success('操作成功！');
        setdrawer((s) => ({
          ...s,
          open: false,
        }));
        actionRef.current.reload();
      }
    },
  };

  return (
    <>
      <InitForm fields={columns[type]} onFinish={finishHandle[type]} />
    </>
  );
};

export default App;
