/**
 * @file 通用表单schema字段
 */
import * as DistributorApi from '/@/api/system/distributorApi';
import get from 'lodash.get';
import * as HospitalApi from '/@/api/system/hospitalApi';
import * as DoctorApi from '/@/api/system/doctorApi';

interface gerPropType {
  colProps?: any;
  onChange?: any;
}

export const getDistributorText = (text) => {
  if (text === 0) {
    return '供应商创建';
  } else {
    return '（已删除/不存在）';
  }
};
const getDistributorId = ({ colProps }) => {
  return {
    field: 'distributorId',
    label: '经销商',
    defaultValue: undefined,
    form: {
      itemProps: {
        validateTrigger: 'blur',
      },
      componentProps: {
        labelInValue: true,
        api: DistributorApi.all,
        params: {},
        resultField: 'records',
        labelField: 'name',
        valueField: 'id',
        allowClear: false,
        placeholder: '经销商',
        optionFilterProp: 'label',
        showSearch: true,
      },
      colProps,
      component: 'ApiSelect',
      rules: [{ required: true, type: 'object', message: '请选择经销商！' }],
    },
    table: {
      customRender: ({ text, record }) => {
        return get(record, 'distributor.name') || getDistributorText(text);
      },
    },
  };
};

const getHospitalId = ({ colProps, onChange }: gerPropType) => {
  return {
    field: 'hospitalId',
    label: '医院',
    defaultValue: undefined,
    form: {
      itemProps: {
        validateTrigger: 'blur',
      },
      componentProps: ({ formActionType, formModel }) => {
        return {
          labelInValue: true,
          api: HospitalApi.all,
          params: {},
          resultField: 'records',
          labelField: 'name',
          valueField: 'id',
          allowClear: false,
          placeholder: '医院',
          optionFilterProp: 'label',
          showSearch: true,
          onChange: (e: any) => {
            onChange && onChange({ formActionType, formModel, e });
          },
        };
      },
      colProps,
      component: 'ApiSelect',
      rules: [{ required: true, type: 'object', message: '请选择医院！' }],
    },
    table: {
      customRender: ({ record }) => get(record, 'hospital.name') || '（已删除/不存在）',
    },
  };
};

const getDoctorId = ({ colProps }) => {
  return {
    field: 'doctorId',
    label: '医生',
    defaultValue: undefined,
    form: {
      itemProps: {
        validateTrigger: 'blur',
      },
      componentProps: {
        labelInValue: true,
        api: DoctorApi.all,
        params: {},
        resultField: 'records',
        labelField: 'name',
        valueField: 'id',
        allowClear: false,
        placeholder: '医生',
        optionFilterProp: 'label',
        showSearch: true,
        disabled: true,
      },
      colProps,
      component: 'ApiSelect',
      rules: [{ required: true, type: 'object', message: '请选择医生！' }],
    },
    table: {
      customRender: ({ record }) => get(record, 'hospital.name') || '（已删除/不存在）',
    },
  };
};

const getHospitalIdAndDoctorId = ({ colProps }) => {
  return [
    getHospitalId({
      colProps,
      onChange: ({ formActionType, formModel, e }) => {
        const { updateSchema } = formActionType;
        updateSchema([
          { field: 'doctorId', componentProps: { params: { hospitalId: e.value } } },
          { field: 'doctorId', componentProps: { disabled: false } },
        ]);
        formModel.doctorId = undefined; //  清空医生
      },
    }),
    getDoctorId({ colProps }),
  ];
};
export { getDistributorId, getHospitalId, getDoctorId, getHospitalIdAndDoctorId };
