/**
 * 项目：-
 * 模型分组：系统管理
 * 模型名称：医生管理
 * @Author: xiongwei
 * @Date: 2022-06-28 11:50:00
 */

import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { getDistributorId, getHospitalId } from '/@/commonSchemaProperty';
import { usePermission } from '/@/hooks/web/usePermission';
import { BatchVerifyParams } from '/@/api/model/doctor';
import { useUserStore } from '/@/store/modules/user';
const userStore = useUserStore();
const { hasPermission } = usePermission();
export enum SexEnum {
  MALE = '男',
  FEMALE = '女',
}
export const SexEnumOptions: any[] = [];
for (const key in SexEnum) {
  SexEnumOptions.push({
    value: key,
    label: SexEnum[key],
  });
}
export enum StatusEnum {
  PENDING_REVIEW = '待审核',
  PASSED = '已通过',
  REJECT = '已拒绝',
  FORBIDDEN = '禁用',
}
export enum StatusColorEnum {
  PENDING_REVIEW = 'processing',
  PASSED = 'success',
  REJECT = 'error',
  FORBIDDEN = 'default',
}
export enum StatusValEnum {
  PENDING_REVIEW = 'PENDING_REVIEW',
  PASSED = 'PASSED',
  REJECT = 'REJECT',
  FORBIDDEN = 'FORBIDDEN',
}

export const isShowHandlePassed = (status: string): boolean => {
  return hasPermission('AUTH_SYSTEM_DOCTOR:PASSED') && status === 'PENDING_REVIEW';
};
export const isShowHandleReject = (status: string): boolean => {
  return hasPermission('AUTH_SYSTEM_DOCTOR:REJECT') && status === 'PENDING_REVIEW';
};
export const isShowHandleForbidden = (status: string): boolean => {
  return hasPermission('AUTH_SYSTEM_DOCTOR:FORBIDDEN') && status === 'PASSED';
};
export const isShowHandleEdit = (status: string): boolean => {
  return hasPermission('AUTH_SYSTEM_DOCTOR:EDIT') && status !== 'FORBIDDEN';
};
export const isShowHandleDelete = (): boolean => {
  return hasPermission('AUTH_SYSTEM_DOCTOR:DELETE');
};
/**
 * @description: 返回审核所需参数
 * @param record
 * @param status
 */
export const getVerifyParams = (record: Recordable, status: string): BatchVerifyParams => {
  const params: BatchVerifyParams = {
    idList: [record.id],
    status,
  };
  return params;
};

export const StatusEnumOptions: any[] = [];
for (const key in StatusEnum) {
  StatusEnumOptions.push({
    value: key,
    label: StatusEnum[key],
  });
}

const colProps = { xs: { span: 24 }, sm: { span: 24 }, lg: { span: 8 } };
const colPropsInDrawer = { span: 24 };

export const schema = {
  model: 'Doctor',
  viewInPage: true,
  properties: [
    {
      field: 'id',
      label: 'ID',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: 'ID',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入ID！' }],
      },
      table: {},
    },
    getDistributorId({ colProps }),
    getHospitalId({ colProps }),
    {
      field: 'name',
      label: '医生名称',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '医生名称',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入医生名称！' }],
      },
      table: {},
    },
    {
      field: 'sex',
      label: '性别',
      defaultValue: 'MALE',
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '性别',
          options: SexEnumOptions,
        },
        colProps,
        component: 'Select',
        rules: [{ required: false, message: '请输入性别！' }],
      },
      table: {
        customRender: ({ text }) => {
          const option = SexEnumOptions.find((item) => item.value === text);
          return option ? option.label : text;
        },
      },
    },
    {
      field: 'mobile',
      label: '手机号',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '手机号',
        },
        colProps,
        component: 'Input',
        rules: [{ pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码！', trigger: 'blur' }],
      },
      table: {},
    },
    {
      field: 'code',
      label: '编码',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '编码',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      field: 'job',
      label: '职务',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '职务',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      field: 'bed',
      label: '床位',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '床位',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      field: 'status',
      label: '状态',
      defaultValue: 'PENDING_REVIEW',
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '状态',
          options: StatusEnumOptions,
        },
        colProps,
        component: 'Select',
        rules: [{ required: true, message: '请输入状态！' }],
      },
      table: {
        fixed: 'right',
        customRender: ({ text }) => {
          const option = StatusEnumOptions.find((item) => item.value === text);
          return option ? option.label : text;
        },
      },
    },
    {
      field: 'editorId',
      label: '操作人Id',
      defaultValue: 1,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '操作人Id',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入操作人Id！' }],
      },
      table: {},
    },
    {
      field: 'editorName',
      label: '操作人',
      defaultValue: 'admin',
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '操作人',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入操作人！' }],
      },
      table: {},
    },
    {
      field: 'createTime',
      label: '创建时间',
      defaultValue: undefined,
      form: {
        colProps,
        componentProps: {
          allowClear: false,
          placeholder: ['开始时间', '结束时间'],
          format: 'YYYY-MM-DD',
          showTime: false,
        },
        component: 'RangePicker',
        rules: [{ required: true, message: '请输入CreateTime！' }],
      },
      table: {},
    },
    {
      field: 'updateTime',
      label: '更新时间',
      defaultValue: undefined,
      form: {
        colProps,
        componentProps: {
          allowClear: false,
          placeholder: ['开始时间', '结束时间'],
          format: 'YYYY-MM-DD',
          showTime: false,
        },
        component: 'RangePicker',
        rules: [{ required: true, message: '请输入UpdateTime！' }],
      },
      table: {},
    },
  ],
};

const queryFields = [
  // 'id',
  'distributorId',
  'hospitalId',
  'name',
  'sex',
  'mobile',
  'code',
  'job',
  'bed',
  'status',
  // 'editorId',
  'editorName',
  'createTime',
  'updateTime',
];
const editFields = ['hospitalId', 'name', 'sex', 'mobile', 'code', 'job', 'bed'];
const tableFields = [
  'id',
  'distributorId',
  'hospitalId',
  'name',
  'sex',
  'mobile',
  'code',
  'job',
  'bed',
  'status',
  // 'editorId',
  'editorName',
  'createTime',
  'updateTime',
];
const descriptionFields = [
  'id',
  'distributorId',
  'hospitalId',
  'name',
  'sex',
  'mobile',
  'code',
  'job',
  'bed',
  'status',
  'editorId',
  'editorName',
  'createTime',
  'updateTime',
];

export const searchFormSchema: FormSchema[] = schema.properties
  .filter((item) => {
    if (item.field === 'distributorId') {
      return queryFields.includes(item.field) && userStore.getDistributorId === 0;
    } else {
      return queryFields.includes(item.field);
    }
  })
  .map(
    ({ field, label, form: { rules = [], ...formProps } }) =>
      ({
        field,
        label,
        defaultValue: undefined,
        rules: rules.filter((r) => !r.required),
        ...formProps,
      } as FormSchema),
  );

export const formSchema: FormSchema[] = schema.properties
  .filter((item) => editFields.includes(item.field))
  .map(
    ({ field, label, defaultValue, form }) =>
      ({
        field,
        label,
        defaultValue,
        ...form,
        colProps: colPropsInDrawer,
      } as FormSchema),
  );

export const columns: BasicColumn[] = schema.properties
  .filter((item) => tableFields.includes(item.field))
  .map(
    ({ field, label, table }) =>
      ({
        dataIndex: field,
        title: label,
        ...table,
      } as BasicColumn),
  );

export const descriptionColumns: BasicColumn[] = schema.properties
  .filter((item) => descriptionFields.includes(item.field))
  .map(
    ({ field, label, table }) =>
      ({
        dataIndex: field,
        title: label,
        ...table,
      } as BasicColumn),
  );
