/* 接单、派单窗口
 * @Author: Li Hanlin
 * @Date: 2022-12-05 11:13:26
 * @Last Modified by: Li Hanlin
 * @Last Modified time: 2022-12-30 16:25:12
 */

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 }) => {
  console.log(type);
  const columns = {
    wxjd: [
      {
        title: '协助维修人员',
        dataIndex: 'repairAssistList',
        key: 'repairAssistList',
        valueType: 'select',
        options: { path: '/repair/umRepairOrder/selection', params: { id } },
        fieldProps: {
          placeholder: '请选择',
          showSearch: true,
          mode: 'multiple',
        },
      },
    ],
    wxpd: [
      {
        title: '维修人员',
        dataIndex: 'repairId',
        key: 'repairId',
        valueType: 'select',
        options: { path: '/auth/sysUser/selection' },
        formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
      },
      {
        title: '协助维修人员',
        dataIndex: 'repairAssistList',
        key: 'repairAssistList',
        valueType: 'select',
        options: { path: '/repair/umRepairOrder/selection', params: { id } },
        fieldProps: {
          placeholder: '请选择',
          showSearch: true,
          mode: 'multiple',
        },
      },
    ],
    zzpd: [
      {
        title: '追踪人员',
        dataIndex: 'trackAssistList',
        key: 'trackAssistList',
        valueType: 'select',
        options: { path: '/auth/sysUser/selection' },
        formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
      },
    ],
    wpd: [
      {
        title: '外协人员',
        dataIndex: 'otherUnitsAssistList',
        key: 'otherUnitsAssistList',
        valueType: 'select',
        options: { path: '/repair/umOtherUnitsOrder/selection', params: { id } },
        formItemProps: { rules: [{ required: true, message: '此项为必填项' }] },
      },
    ],
  };
  const finishHandle = {
    wxjd: async (vals) => {
      //console.log(vals);
      vals.repairAssistList = vals?.repairAssistList.map((it) => ({
        assistUserId: it,
      }));
      let params = {
        ...vals,
        id,
      };
      let res = await doFetch({
        url,
        params,
      });
      if (res.code === '0000') {
        message.success('操作成功！');
        setdrawer((s) => ({
          ...s,
          open: false,
        }));
        actionRef.current.reload();
      }
    },
    wxpd: async (vals) => {
      //console.log(vals);
      vals.repairAssistList = vals?.repairAssistList.map((it) => {
        return {
          assistUserId: it,
        };
      });
      let params = {
        ...vals,
        id,
      };
      let res = await doFetch({
        url,
        params,
      });
      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;
