/**
 * @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 { StatusValEnum as ReceiverStatusValEnum } from '/@/views/system/receiver/enum';
import { RoleEnum } from '/@/views/system/role/enum';
import * as ThirdApi from '/@/api/system/thirdApi';
import * as ReceiverApi from '/@/api/system/receiverApi';
import { useUserStore } from '/@/store/modules/user';
const userStore = useUserStore();
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,
          distributorId: userStore.getIsDistributor ? userStore.getDistributorId : undefined,
        },
        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,
          distributorId: userStore.getIsDistributor ? userStore.getDistributorId : undefined,
        },
        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') || '（已删除/不存在）';
      },
    },
  };
};

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

const getReceiverId = ({ colProps }) => {
  return {
    field: 'receiveId',
    label: '收货人',
    defaultValue: undefined,
    form: {
      itemProps: {
        validateTrigger: 'blur',
      },
      componentProps: ({ formModel }) => {
        return {
          labelInValue: true,
          api: () => {
            return ReceiverApi.all().then((res) => {
              // @ts-ignore
              res.forEach((item) => {
                item.mergeLabel = `${item.receiveName}-${item.address}`;
              });
              return res;
            });
          },
          onChange: (e: any) => {
            const opt = e.option;
            if (formModel.receiveName) {
              formModel.receiveName = opt.receiveName;
            }
            if (formModel.receivePhone) {
              formModel.receivePhone = opt.receivePhone;
            }
            if (formModel.address) {
              formModel.address = opt.address;
            }
          },
          params: {
            status: ReceiverStatusValEnum.PASSED,
            distributorId: userStore.getIsDistributor ? userStore.getDistributorId : undefined,
          },
          // resultField: 'records',
          labelField: 'mergeLabel',
          valueField: 'id',
          allowClear: false,
          placeholder: '收货人',
          optionFilterProp: 'label',
          showSearch: true,
        };
      },
      colProps,
      component: 'ApiSelect',
      rules: [{ required: true, type: 'object', message: '请选择收货人！' }],
    },
    table: {
      customRender: ({ record }) => record.receiveName || '（已删除/不存在）',
    },
  };
};

export {
  getDistributorText,
  getDistributorId,
  getHospitalId,
  getDoctorId,
  getHospitalIdAndDoctorId,
  getSaleUserId,
  getProductLineName,
  getReceiverId,
};
