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

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

export enum YesNoEnum {
  YES = '是',
  NO = '否',
}
export const YesNoEnumOptions: any[] = [];
for (const key in YesNoEnum) {
  YesNoEnumOptions.push({
    value: key,
    label: YesNoEnum[key],
  });
}

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

export const schema = {
  model: 'SurgicalType',
  viewInPage: false,
  properties: [
    {
      field: 'id',
      label: 'ID',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: 'ID',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入ID！' }],
      },
      table: {},
    },
    {
      field: 'code',
      label: '编码',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '编码',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入编码！' }],
      },
      table: {},
    },
    {
      field: 'name',
      label: '名称',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '名称',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入名称！' }],
      },
      table: {},
    },
    {
      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: '请输入创建时间！' }],
      },
      table: {},
    },
    {
      field: 'updateTime',
      label: '开始时间',
      defaultValue: undefined,
      form: {
        colProps,
        componentProps: {
          allowClear: false,
          placeholder: ['开始时间', '结束时间'],
          format: 'YYYY-MM-DD',
          showTime: false,
        },
        component: 'RangePicker',
        rules: [{ required: true, message: '请输入开始时间！' }],
      },
      table: {},
    },
    {
      field: 'status',
      label: '是否启用',
      defaultValue: 'YES',
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '是否启用',
          options: YesNoEnumOptions,
        },
        colProps,
        component: 'Select',
        rules: [{ required: true, message: '请输入是否启用！' }],
      },
      table: {
        customRender: ({ text }) => {
          const option = YesNoEnumOptions.find((item) => item.value === text);
          return option ? option.label : text;
        },
      },
    },
  ],
};

const queryFields = ['code', 'name', 'editorName', 'createTime', 'updateTime', 'status'];
const editFields = ['code', 'name', 'status'];
const tableFields = ['id', 'code', 'name', 'editorName', 'createTime', 'updateTime', 'status'];
const descriptionFields = ['id', 'code', 'name', 'editorId', 'editorName', 'createTime', 'updateTime', 'status'];

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),
  );
