/**
 * @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';
import * as UserApi from '/@/api/system/userApi';
import { StatusValEnum as DoctorStatusValEnum } from '/@/views/system/doctor/enum';
import { StatusValEnum as UserStatusValEnum } from '/@/views/system/user/enum';
import { RoleEnum } from '/@/views/system/role/enum';
interface gerPropType {
  colProps?: any;
  onChange?: any;
}
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: {
          status: UserStatusValEnum.PASSED,
        },
        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: {
          status: DoctorStatusValEnum.PASSED,
        },
        resultField: 'records',
        labelField: 'name',
        valueField: 'id',
        allowClear: false,
        placeholder: '医生',
        optionFilterProp: 'label',
        showSearch: true,
        disabled: true,
        immediate: false,
      },
      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, disabled: false } } }]);
        formModel.doctorId = undefined; //  清空医生
      },
    }),
    getDoctorId({ colProps }),
  ];
};
const getSaleUserId = ({ colProps }) => {
  return {
    field: 'saleUserId',
    label: '业务员',
    defaultValue: undefined,
    form: {
      itemProps: {
        validateTrigger: 'blur',
      },
      componentProps: {
        labelInValue: true,
        api: UserApi.all,
        params: {
          role: RoleEnum.SALESMAN,
          status: UserStatusValEnum.PASSED,
        },
        resultField: 'records',
        labelField: 'realName',
        valueField: 'id',
        allowClear: false,
        placeholder: '业务员',
        optionFilterProp: 'label',
        showSearch: true,
      },
      colProps,
      component: 'ApiSelect',
      rules: [{ required: true, type: 'object', message: '请选择业务员' }],
    },
    table: {
      customRender: ({ record }) => {
        return get(record, 'user.realName') || '（已删除/不存在）';
      },
    },
  };
};
export { getDistributorText, getDistributorId, getHospitalId, getDoctorId, getHospitalIdAndDoctorId, getSaleUserId };
