import BaseFormSelect from '@/components/BaseFormSelect';
import { ModalForm, ProFormRadio } from '@ant-design/pro-components';
import BaseFormText from '@/components/BaseFormText';
import BaseFormDatePicker from '@/components/BaseFormDatePicker';
import validatorRules from '@cowin/validator-rules';
import { Col, Form, Row, message } from 'antd';
import React from 'react';
import { BED_STATUS } from '../../../../constants/options';
import { CommonController, DoctorController } from '../../../../services';

interface EditFormProps {
  title: string;
  type: 'add' | 'update' | 'view';
  trigger: JSX.Element;
  values?: DoctorController.DoctorFields;
  onSubmit?: () => void;
}

const EditForm: React.FC<EditFormProps> = (props) => {
  const { trigger, title, values, type, onSubmit } = props;
  const [form] = Form.useForm<DoctorController.DoctorFields>();
  return (
    <ModalForm<DoctorController.DoctorFields>
      layout="horizontal"
      labelCol={{ flex: '160px' }}
      width={1200}
      title={title}
      trigger={trigger}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
      }}
      disabled={type === 'view'}
      submitTimeout={2000}
      onFinish={async (values) => {
        if (type === 'add') {
          await DoctorController.addApi(values);
        } else if (type === 'update') {
          await DoctorController.updateApi(values);
        }
        message.success('提交成功');
        onSubmit?.();
        return true;
      }}
    >
      <BaseFormText name="uuid" hidden initialValue={values?.uuid} />
      <Row gutter={0}>
        <Col span={12}>
          <BaseFormText
            label="姓名"
            name="name"
            rules={[{ required: true }]}
            initialValue={values?.name}
          />
        </Col>
        <Col span={12}>
          <BaseFormDatePicker
            label="出生日期"
            rules={[{ required: true }]}
            name="birthDate"
            initialValue={values?.birthDate}
          />
        </Col>
        <Col span={12}>
          <ProFormRadio.Group
            label="性别"
            rules={[{ required: true }]}
            name="sexCode"
            request={async () =>
              CommonController.getSelectionsApi('GB/T 2261.1-2003')
            }
            initialValue={values?.sexCode}
          />
        </Col>
        <Col span={12}>
          <BaseFormText
            label="出生地"
            name="birthPlace"
            initialValue={values?.birthPlace}
          />
        </Col>
        <Col span={12}>
          <BaseFormSelect
            label="身份证类别"
            name="idClass"
            fieldProps={{
              showSearch: true,
              fetchDataOnSearch: false,
            }}
            rules={[{ required: true }]}
            initialValue={values?.idClass}
            showSearch
            request={async () =>
              CommonController.getSelectionsApi('CV02.01.101')
            }
          />
        </Col>
        <Col span={12}>
          <BaseFormText
            label="身份证号"
            name="idno"
            rules={[
              { required: true },
              {
                validator: (rules, value, fn) => {
                  if (!value) return fn();
                  const res = validatorRules('CardID', value);
                  if (!res.checkState) {
                    fn(res.message);
                  } else {
                    fn();
                  }
                },
              },
            ]}
            initialValue={values?.idno}
          />
        </Col>
        <Col span={12}>
          <BaseFormSelect
            label="专业技术职务"
            name="technicaljobCode"
            initialValue={values?.technicaljobCode}
            rules={[{ required: true }]}
            fieldProps={{
              showSearch: true,
              fetchDataOnSearch: false,
            }}
            request={async () =>
              CommonController.getSelectionsApi('GB/T 8561-2001')
            }
          />
        </Col>
        <Col span={12}>
          <BaseFormSelect
            label="民族"
            name="peopleEthnicCode"
            rules={[{ required: true }]}
            initialValue={values?.peopleEthnicCode}
            fieldProps={{
              showSearch: true,
              fetchDataOnSearch: false,
            }}
            request={async () =>
              CommonController.getSelectionsApi('GB/T 3304-1991')
            }
          />
        </Col>
        <Col span={12}>
          <BaseFormDatePicker
            label="初期参加工作日期"
            name="firstWorkingDate"
            initialValue={values?.firstWorkingDate}
            placeholder="请选择日期"
          />
        </Col>
        <Col span={12}>
          <BaseFormText
            label="手机号码"
            name="cellPhone"
            initialValue={values?.cellPhone}
            placeholder="手机号码"
            rules={[
              { required: true },
              {
                validator: (rules, value, fn) => {
                  if (!value) return fn();
                  const res = validatorRules('Phone', value);
                  if (!res.checkState) {
                    fn(res.message);
                  } else {
                    fn();
                  }
                },
              },
            ]}
          />
        </Col>
        <Col span={12}>
          <BaseFormSelect
            label="证书类别"
            fieldProps={{
              showSearch: true,
              fetchDataOnSearch: false,
            }}
            name="certiClass"
            initialValue={values?.certiClass}
            request={async () => CommonController.getSelectionsApi('GBT001')}
          />
        </Col>
        <Col span={12}>
          <BaseFormSelect
            label="从事专业类别"
            fieldProps={{
              showSearch: true,
              fetchDataOnSearch: false,
            }}
            name="proClassCode"
            initialValue={values?.proClassCode}
            request={async () =>
              CommonController.getSelectionsApi('QY99.00.006')
            }
          />
        </Col>
        <Col span={12}>
          <BaseFormText
            label="医师执业证书编码"
            name="medCertiCode"
            initialValue={values?.medCertiCode}
            placeholder="医师执业证书编码"
          />
        </Col>
        <Col span={12}>
          <BaseFormSelect
            label="医师执业类别"
            name="medCertiClass"
            fieldProps={{
              showSearch: true,
              fetchDataOnSearch: false,
            }}
            initialValue={values?.medCertiClass}
            request={async () =>
              CommonController.getSelectionsApi('QY99.00.007')
            }
          />
        </Col>
        <Col span={12}>
          <BaseFormSelect
            label="行政/业务管理职务"
            name="adminPostCode"
            fieldProps={{
              showSearch: true,
              fetchDataOnSearch: false,
            }}
            initialValue={values?.adminPostCode}
            request={async () =>
              CommonController.getSelectionsApi('QY99.00.001')
            }
          />
        </Col>
        <Col span={12}>
          <BaseFormText
            label="住院医师规范化培训合格证书编码"
            name="hosCertiCode"
            initialValue={values?.hosCertiCode}
            labelCol={{ flex: '240px' }}
            placeholder="住院医师规范化培训合格证书编码"
          />
        </Col>
        <Col span={12}>
          <BaseFormText
            label="注册护士执业证书编码"
            name="nurseCertiCode"
            initialValue={values?.nurseCertiCode}
            placeholder="注册护士执业证书编码"
          />
        </Col>
        <Col span={12}>
          <BaseFormSelect
            label="专业技术资格（评）"
            name="proQualCode"
            fieldProps={{
              showSearch: true,
              fetchDataOnSearch: false,
            }}
            initialValue={values?.proQualCode}
            request={async () =>
              CommonController.getSelectionsApi('CV08.30.005')
            }
          />
        </Col>
        <Col span={12}>
          <BaseFormSelect
            label="学历"
            name="eduCode"
            fieldProps={{
              showSearch: true,
              fetchDataOnSearch: false,
            }}
            initialValue={values?.eduCode}
            request={async () =>
              CommonController.getSelectionsApi('GB/T 4658-2006')
            }
          />
        </Col>
        <Col span={12}>
          <BaseFormSelect
            label="学位"
            fieldProps={{
              showSearch: true,
              fetchDataOnSearch: false,
            }}
            name="acaCode"
            initialValue={values?.acaCode}
            request={async () =>
              CommonController.getSelectionsApi('GB/T 6864-2003')
            }
          />
        </Col>
        <Col span={12}>
          <BaseFormSelect
            label="所学专业"
            fieldProps={{
              showSearch: true,
              fetchDataOnSearch: false,
            }}
            name="stuPostCode"
            initialValue={values?.stuPostCode}
            request={async () =>
              CommonController.getSelectionsApi('GB/T 16835-1997')
            }
          />
        </Col>
        <Col span={12}>
          <ProFormRadio.Group
            label="是否注册为全科医学专业"
            name="whoMedpostTag"
            labelCol={{ flex: '180px' }}
            initialValue={values?.whoMedpostTag}
            options={[
              { label: '是', value: '1' },
              { label: '否', value: '0' },
            ]}
          />
        </Col>
        <Col span={12}>
          <BaseFormText
            label="版本号"
            name="version"
            initialValue={values?.version}
            rules={[{ required: true }]}
          />
        </Col>
        <Col span={12}>
          <ProFormRadio.Group
            name="docStatus"
            disabled
            initialValue={
              values?.docStatus === '4'
                ? '3'
                : values?.docStatus
                ? values?.docStatus
                : '3'
            }
            label="医护状态"
            options={BED_STATUS}
            rules={[{ required: true }]}
          />
        </Col>
      </Row>
    </ModalForm>
  );
};

export default EditForm;
