/**
 * 项目：手机摄像头自动测试化平台
 * 模型分组：设备管理
 * 模型名称：实验室表
 * @Author: xiongwei
 * @Date: 2022-05-31 09:42:00
 */

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

export enum LaboratoryStateEnum {
  IDLE = '空闲中',
  BUSY = '使用中',
}
export const LaboratoryStateEnumOptions: any[] = [];
for (const key in LaboratoryStateEnum) {
  LaboratoryStateEnumOptions.push({
    value: key,
    label: LaboratoryStateEnum[key],
  });
}

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

export const schema = {
  model: 'Laboratory',
  viewInPage: true,
  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: false,
          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: 'description',
      label: '描述',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '描述',
        },
        component: 'InputTextArea',
        colProps: { span: 24 },
      },
      table: {
      },
    },
    {
      field: 'state',
      label: '状态',
      defaultValue: 'IDLE',
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '状态',
          options: LaboratoryStateEnumOptions,
        },
        colProps,
        component: 'Select',
        rules: [{ required: true, message: '请输入状态！' }],
      },
      table: {
        customRender: ({ text }) => {
          const option = LaboratoryStateEnumOptions.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: '请输入创建时间！' }],
      },
      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: {
      },
    },
  ],
};

const queryFields = ['code','name','state','createTime'];
const editFields = ['code','name','description','state'];
const tableFields = ['code','name','state','editorName','createTime','updateTime'];
const descriptionFields = ['code','name','description','state','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),
  );

export const useColumns: BasicColumn[] = [
  {
    title: '机器人名称',
    dataIndex: 'robotName',
  },
  {
    title: '操作人',
    dataIndex: 'editorName',
  },
  {
    title: '任务状态',
    dataIndex: 'state',
  },
];
