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

import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { getDistributorId } from '/@/commonSchemaProperty';

export enum StatusEnum {
  YES = '启用',
  NO = '禁用',
}
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: 'Hospital',
  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 }),
    {
      field: 'name',
      label: '医院名称',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '医院名称',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入医院名称！' }],
      },
      table: {},
    },
    {
      field: 'abbreviationName',
      label: '医院简称',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '医院简称',
        },
        colProps,
        component: 'Input',
        rules: [{ required: false, message: '请输入医院简称！' }],
      },
      table: {},
    },
    {
      field: 'code',
      label: '医院编号',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '医院编号',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入医院编号！' }],
      },
      table: {},
    },
    {
      field: 'tax',
      label: '税率',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '税率',
        },
        colProps,
        component: 'Input',
        rules: [{ required: false, message: '请输入税率！' }],
      },
      table: {},
    },
    {
      field: 'address',
      label: '医院地址',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '医院地址',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入医院地址！' }],
      },
      table: {},
    },
    {
      field: 'mobile',
      label: '电话',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '电话',
        },
        colProps,
        component: 'Input',
        rules: [{ required: false, message: '请输入电话！' }],
      },
      table: {},
    },
    {
      field: 'bedSize',
      label: '床位数',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '床位数',
        },
        colProps,
        component: 'InputNumber',
      },
      table: {},
    },
    {
      field: 'level',
      label: '医院等级',
      defaultValue: 1,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '等级',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: false, message: '请输入等级！' }],
      },
      table: {},
    },
    {
      field: 'licenseNumber',
      label: '营业执照代码',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '营业执照代码',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入代码！' }],
      },
      table: {},
    },
    {
      field: 'licensePic',
      label: '营业执照图片',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '营业执照图片',
          maxNumber: 1,
          multiple: false,
        },
        colProps,
        component: 'Upload',
        rules: [{ required: true, message: '请上传' }],
      },
      table: {},
    },
    {
      field: 'licenseExpireTime',
      label: '执照有效期',
      defaultValue: undefined,
      form: {
        colProps,
        component: 'DatePicker',
        componentProps: {
          allowClear: false,
          placeholder: '执照有效期',
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD HH:mm:ss',
          showTime: false,
        },
        rules: [{ required: true, message: '请输入执照有效期！' }],
      },
      table: {},
    },
    {
      field: 'status',
      label: '状态',
      defaultValue: 'YES',
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '状态',
          options: StatusEnumOptions,
        },
        colProps,
        component: 'Select',
        rules: [{ required: true, message: '请输入状态！' }],
      },
      table: {
        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 = [
  // 'distributorId',
  'name',
  'abbreviationName',
  'code',
  'tax',
  'address',
  'mobile',
  'bedSize',
  'level',
  'licenseNumber',
  'licenseExpireTime',
  'status',
  // 'editorId',
  'editorName',
  'createTime',
  'updateTime',
];
const editFields = [
  'name',
  'abbreviationName',
  'code',
  'tax',
  'address',
  'mobile',
  'bedSize',
  'level',
  'licenseNumber',
  'licensePic',
  'licenseExpireTime',
  'status',
];
const tableFields = [
  // 'distributorId',
  'name',
  'abbreviationName',
  'code',
  'tax',
  'address',
  'mobile',
  'bedSize',
  'level',
  'licenseNumber',
  'licensePic',
  'licenseExpireTime',
  'status',
  // 'editorId',
  'editorName',
  'createTime',
  'updateTime',
];
const descriptionFields = [
  'id',
  // 'distributorId',
  'name',
  'abbreviationName',
  'code',
  'tax',
  'address',
  'mobile',
  'bedSize',
  'level',
  'licenseNumber',
  'licensePic',
  'licenseExpireTime',
  'status',
  'editorId',
  'editorName',
  'createTime',
  'updateTime',
];

export const searchFormSchema: FormSchema[] = schema.properties
  .filter((item) => 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),
  );
