import BaseFormSelect from '../../../../components/BaseFormSelect';
import {
  ModalForm,
  ProCard,
  ProFormDependency,
  ProFormList,
  ProFormRadio,
} from '@ant-design/pro-components';
import BaseFormDatePicker from '../../../../components/BaseFormDatePicker';
import BaseFormText from '../../../../components/BaseFormText';
import validatorRules from '@cowin/validator-rules';
import { Col, Row, message } from 'antd';
import React, { useRef, useState } from 'react';
import { OPTIONS_TAG } from '../../../../constants/options';
import {
  CommonController,
  DoctorController,
  types,
} from '../../../../services';
interface AddLicenseProps {
  trigger: JSX.Element;
  values: DoctorController.DoctorFields;
  onSubmit?: () => void;
  orgList: types.SelectOptionFields[];
}
const defaultCol = {
  labelCol: { flex: '140px' },
};
const AddLicenseForm: React.FC<AddLicenseProps> = (props) => {
  const { trigger, values, orgList, onSubmit } = props;
  const [open, setOpen] = useState(false);
  const [sysDoctorJobList, setSysDoctorJobList] = useState<
    DoctorController.DoctorJobFields[]
  >([]);
  const deptLoaded = useRef<boolean>(false);
  return (
    <>
      {React.cloneElement(trigger, {
        async onClick() {
          const { data } = await DoctorController.getDetailsApi(values.uuid);
          setSysDoctorJobList(data?.sysDoctorJobList || []);
          setOpen(true);
        },
      })}
      <ModalForm
        title="医护人员执业情况"
        layout="horizontal"
        width={1200}
        open={open}
        modalProps={{
          destroyOnClose: true,
          onCancel: () => setOpen(false),
        }}
        onOpenChange={() => {
          if (open) {
            setTimeout(() => {
              deptLoaded.current = true;
            });
          } else {
            deptLoaded.current = false;
          }
        }}
        submitTimeout={2000}
        onFinish={async (formValues) => {
          await DoctorController.updateApi({ ...values, ...formValues });
          message.success('提交成功');
          onSubmit?.();
          setOpen(false);
        }}
      >
        <ProFormList
          name="sysDoctorJobList"
          initialValue={sysDoctorJobList}
          itemRender={({ listDom, action }) => {
            return (
              <ProCard
                bordered
                style={{
                  marginBlockEnd: 8,
                }}
              >
                {listDom}
                <div style={{ float: 'right' }}>{action}</div>
              </ProCard>
            );
          }}
        >
          {(f, index, action) => {
            return (
              <Row>
                <Col span={12}>
                  <BaseFormSelect
                    name="orgCode"
                    label="机构"
                    rules={[{ required: true }]}
                    {...defaultCol}
                    fieldProps={{
                      options: orgList || [],
                      showSearch: true,
                      onChange() {
                        action.setCurrentRowData({ deptCode: undefined });
                      },
                    }}
                  />
                </Col>
                <Col span={12}>
                  <ProFormDependency name={['orgCode']}>
                    {({ orgCode }) => {
                      return (
                        <BaseFormSelect
                          label="所属科室"
                          name="deptCode"
                          params={{ orgCode }}
                          fieldProps={{
                            showSearch: true,
                            fetchDataOnSearch: false,
                          }}
                          request={async ({ orgCode }) => {
                            const orgList =
                              await CommonController.getDeptOptionsApi({
                                orgCode,
                              });
                            return orgList;
                          }}
                          rules={[{ required: true }]}
                          {...defaultCol}
                        />
                      );
                    }}
                  </ProFormDependency>
                </Col>
                <Col span={12}>
                  <BaseFormSelect
                    name="userFlow"
                    label="人员流动情况"
                    fieldProps={{
                      showSearch: true,
                      fetchDataOnSearch: false,
                    }}
                    request={async () =>
                      CommonController.getSelectionsApi('QY99.00.015')
                    }
                    {...defaultCol}
                  />
                </Col>
                <Col span={12}>
                  <BaseFormSelect
                    name="preScription"
                    label="处方权"
                    fieldProps={{
                      showSearch: true,
                      fetchDataOnSearch: false,
                    }}
                    request={async () =>
                      CommonController.getSelectionsApi('QY99.02.02.008')
                    }
                    rules={[{ required: true }]}
                    {...defaultCol}
                  />
                </Col>
                <Col span={12}>
                  <BaseFormDatePicker
                    name="transInDate"
                    label="调入时间"
                    {...defaultCol}
                  />
                </Col>
                <Col span={12}>
                  <BaseFormDatePicker
                    name="transOutDate"
                    label="调出时间"
                    {...defaultCol}
                  />
                </Col>

                <Col span={12}>
                  <BaseFormSelect
                    fieldProps={{
                      showSearch: true,
                      fetchDataOnSearch: false,
                    }}
                    request={async () =>
                      CommonController.getSelectionsApi('CV08.30.001')
                    }
                    name="eStabMent"
                    label="编制情况"
                    {...defaultCol}
                  />
                </Col>
                <Col span={12}>
                  <BaseFormText
                    name="officePhone"
                    label="办公室电话"
                    rules={[
                      { required: true },
                      {
                        validator: (rules, value, fn) => {
                          if (!value) return fn();
                          const res = validatorRules(
                            ['Phone', 'Telephone'],
                            value,
                          );
                          if (!res.checkState) {
                            fn(res.message);
                          } else {
                            fn();
                          }
                        },
                      },
                    ]}
                    {...defaultCol}
                  />
                </Col>
                <Col span={12}>
                  <ProFormRadio.Group
                    name="reEmployPerson"
                    label="是否返聘人员"
                    options={OPTIONS_TAG}
                    {...defaultCol}
                  />
                </Col>
                <Col span={12}>
                  <ProFormRadio.Group
                    name="onJobCode"
                    label="在职情况"
                    options={OPTIONS_TAG}
                    {...defaultCol}
                  />
                </Col>
                <Col span={12}>
                  <ProFormRadio.Group
                    name="orgSign"
                    label="是否主要执业机构"
                    options={OPTIONS_TAG}
                    rules={[{ required: true }]}
                    {...defaultCol}
                  />
                </Col>
                <Col span={12}>
                  <BaseFormText
                    name="medStaId"
                    label="工号"
                    rules={[{ required: true }]}
                    {...defaultCol}
                  />
                </Col>
              </Row>
            );
          }}
        </ProFormList>
      </ModalForm>
    </>
  );
};

export default AddLicenseForm;
