import BaseFormSelect from '@/components/BaseFormSelect';
import {
  ModalForm,
  ProFormDependency,
  ProFormRadio,
} from '@ant-design/pro-components';
import BaseFormText from '@/components/BaseFormText';
import { useRequest } from 'ahooks';
import { Form, message } from 'antd';
import React, { useRef } from 'react';
import { BED_STATUS, OPTIONS_BED_TYPE } from '../../../../constants/options';
import { BedController, CommonController, types } from '../../../../services';

interface EditFormProps {
  title: string;
  type: 'add' | 'update' | 'view';
  trigger: JSX.Element;
  value?: BedController.BedFields;
  onSubmit?: () => void;
  orgList: types.SelectOptionFields[];
}

const EditForm: React.FC<EditFormProps> = (props) => {
  const { trigger, title, value, type, orgList, onSubmit } = props;
  const [form] = Form.useForm<BedController.BedFields>();
  const deptLoaded = useRef<boolean>(false);
  const { data: deptList, run: getDeptOptions } = useRequest(
    CommonController.getDeptOptionsApi,
    {
      manual: true,
      onSuccess() {
        if (deptLoaded.current) {
          form.setFieldValue('deptCode', undefined);
        }
        deptLoaded.current = true;
      },
    },
  );
  return (
    <ModalForm<BedController.BedFields>
      layout="horizontal"
      labelCol={{ span: 4 }}
      wrapperCol={{ span: 18 }}
      width={800}
      title={title}
      trigger={trigger}
      form={form}
      autoFocusFirstInput
      modalProps={{
        destroyOnClose: true,
      }}
      onOpenChange={(open) => {
        if (open && value?.deptCode) {
          getDeptOptions({ orgCode: value.orgCode });
        }
        deptLoaded.current = false;
      }}
      disabled={type === 'view'}
      submitTimeout={2000}
      onFinish={async (values) => {
        if (type === 'add') {
          await BedController.addApi(values);
        } else if (type === 'update') {
          await BedController.updateApi(values);
        }
        message.success('提交成功');
        onSubmit?.();
        return true;
      }}
    >
      <BaseFormSelect
        label="机构名称"
        name="orgCode"
        initialValue={value?.orgCode}
        disabled={type !== 'add'}
        options={orgList}
        fieldProps={{
          options: orgList || [],
          showSearch: true,
          onChange: (orgCode: string) => {
            getDeptOptions({ orgCode });
          },
        }}
        rules={[{ required: true }]}
      />
      <ProFormDependency name={['orgCode']}>
        {({ orgCode }) => {
          return (
            <BaseFormSelect
              label="所属科室"
              name="deptCode"
              params={{ orgCode }}
              disabled={type !== 'add'}
              initialValue={value?.deptCode}
              fieldProps={{
                options: deptList || [],
                showSearch: true,
              }}
              rules={[{ required: true }]}
            />
          );
        }}
      </ProFormDependency>
      <ProFormRadio.Group
        name="bedStatus"
        disabled
        initialValue={
          value?.bedStatus === '4'
            ? '3'
            : value?.bedStatus
            ? value?.bedStatus
            : '3'
        }
        label="床位状态"
        options={BED_STATUS}
        rules={[{ required: true }]}
      />
      <BaseFormText
        label="床位编码"
        disabled={type !== 'add'}
        name="bedCode"
        initialValue={value?.bedCode}
        rules={[{ required: true }]}
      />
      <BaseFormText
        label="床号"
        name="bedNo"
        initialValue={value?.bedNo}
        rules={[{ required: true }]}
      />
      <BaseFormText
        label="版本号"
        name="version"
        initialValue={value?.version}
        rules={[{ required: true }]}
      />
      <ProFormRadio.Group
        label="床位类型"
        name="bedClass"
        initialValue={value?.bedClass || '1'}
        options={OPTIONS_BED_TYPE}
      />
    </ModalForm>
  );
};

export default EditForm;
