import React, { useCallback, useEffect, useRef, useState } from "react";
import Edit from "@/components/Page/Edit";
import { Radio, Form } from "antd";
import {
  ProForm,
  ProFormDatePicker,
  ProFormText,
  ProFormSelect,
  ProFormTextArea,
  ProFormDigit,
  ProFormRadio,
} from "@ant-design/pro-components/es";
import {
  postTaMandatoryLeave,
  putTaMandatoryLeave,
  getTaMandatoryLeaveById,
} from "@/service/taMandatoryLeave";
import { getSysPosition } from "@/service/sysposition";
import { queryDict } from "@/utils/request";
import { getSysOrg } from "@/service/sysorg";
import { useSearchParams } from "react-router-dom";

const queryPosition = queryDict(getSysPosition, {
  labelKey: "name",
  valueKey: "positionId",
});
const getOrgId = queryDict(getSysOrg, {
  labelKey: "orgId",
  valueKey: "orgId",
});
const getOrgName = queryDict(getSysOrg, {
  labelKey: "name",
  valueKey: "name",
});
const request = {
  save: postTaMandatoryLeave,
  update: putTaMandatoryLeave,
  get: getTaMandatoryLeaveById,
};

export default (props) => {
  const formRef = useRef();
  const [params] = useSearchParams();
  const disabled = params.get("disabled");
  const selectChange = [
    { label: "是", value: "是" },
    { label: "否", value: "否" },
  ];

  const counterInstitutionsLevelOpt = [
    { label: "1.总行部门、直属机构", value: "1.总行部门、直属机构" },
    { label: "2.分行各级管理机构本部", value: "2.分行各级管理机构本部" },
    { label: "3.基层营业网点(含营业部)", value: "3.基层营业网点(含营业部)" },
  ];

  const onChangeOrg = (value, e) => {
    formRef.current.setFieldValue("orgNum", e.orgId);
    formRef.current.setFieldValue("orgName", e.name);
  };

  return (
    <Edit
      formRef={formRef}
      request={request}
      disabled={disabled}
      rowKey="leaveId"
      labelCol={{ span: 6 }}
      renderItems={() => (
        <>
          {/* <ProFormDigit name="sort" label=" 序 号 ：" /> */}
          <ProFormText name="headOfficeOrg" label="总 行 部 门" />
          <ProFormText
            name="employeeNum"
            label="员 工 工 号"
            rules={[{ required: true }]}
          />
          <ProFormText
            name="employeeName"
            label="员 工 姓 名"
            rules={[{ required: true }]}
          />
          <ProFormSelect
            onChange={onChangeOrg}
            request={getOrgId}
            name="orgNum"
            label="原单位机构号"
          />
          <ProFormSelect
            rules={[{ required: true }]}
            onChange={onChangeOrg}
            name="orgName"
            label="原单位名称"
            request={getOrgName}
          />

          <ProFormSelect
            name="counterInstitutionsLevel"
            label="对应机构层次"
            options={counterInstitutionsLevelOpt}
          />
          <ProFormText name="currentPosition" label="当 前 职 位" />

          <ProFormSelect
            name="counterImpPost"
            label="对应重要岗位"
            request={queryPosition}
          />
          <ProFormText
            name="counterHeadOfficeLine"
            label="对应总行条线"
            // options={[{ label: "个人数字金融部", value: "个人数字金融部" }]}
          />
          <ProFormDatePicker
            name="startTimeVacation"
            label="休假起始时间"
            width="100%"
          />
          <ProFormDatePicker
            name="vacationDeadline"
            label="休假截止时间"
            width="100%"
          />
          <ProFormDigit name="mandatoryVacationDay" label="已强制休假天数" />
          <ProFormText
            name="depSbtDepartureCheck"
            label="开展离岗检查/代职检查情况"
          />
          <ProFormTextArea name="remark" label="备注填写" />
          <ProFormRadio.Group
            name="checkChange"
            label="立 查 立 改："
            options={selectChange}
          />
          <ProFormRadio.Group
            name="checkCorrect"
            label="自 查 自 纠："
            options={selectChange}
          />
        </>
      )}
    />
  );
};
