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

import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import get from 'lodash.get';
import * as LaboratoryApi from '/@/api/device/laboratoryApi';

export enum DeviceTypeEnum {
  TEST_ROBOT = '复合机器人',
  CHART_SWITCHER = '测试卡切换器',
  LIGHTING_CONTROL = '光源',
  LUXMETER = '照度计',
  LUXMETER_LOCATOR = '照度计定位器',
  IMATEST = 'ImaTest测试软件',
}
export const DeviceTypeEnumOptions: any[] = [];
for (const key in DeviceTypeEnum) {
  DeviceTypeEnumOptions.push({
    value: key,
    label: DeviceTypeEnum[key],
  });
}
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: 'LabDevice',
  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: 'name',
      label: '名称',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '名称',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入名称！' }],
      },
      table: {
      },
    },
    {
      field: 'token',
      label: '设备Token',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '设备Token',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入设备Token！' }],
      },
      table: {
      },
    },
    {
      field: 'remoteId',
      label: '物联平台Id',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '物联平台Id',
        },
        colProps,
        component: 'Input',
      },
      table: {
      },
    },
    {
      field: 'type',
      label: '类型',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '类型',
          options: DeviceTypeEnumOptions,
        },
        colProps,
        component: 'Select',
        rules: [{ required: true, message: '请输入类型！' }],
      },
      table: {
        customRender: ({ text }) => {
          const option = DeviceTypeEnumOptions.find((item) => item.value === text)
          return option ? option.label : text;
        },
      },
    },
    {
      field: 'manufacturer',
      label: '厂家',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '厂家',
        },
        colProps,
        component: 'Input',
      },
      table: {
      },
    },
    {
      field: 'description',
      label: '描述',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '描述',
        },
        component: 'InputTextArea',
        colProps: { span: 24 },
      },
      table: {
      },
    },
    {
      field: 'contact',
      label: '联系人',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '联系人',
        },
        colProps,
        component: 'Input',
      },
      table: {
      },
    },
    {
      field: 'phone',
      label: '联系电话',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '联系电话',
        },
        colProps,
        component: 'Input',
      },
      table: {
      },
    },
    {
      field: 'address',
      label: '联系地址',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '联系地址',
        },
        colProps,
        component: 'Input',
      },
      table: {
      },
    },
    {
      field: 'isEnable',
      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;
        },
      },
    },
    {
      field: 'laboratoryId',
      label: '实验室',
      defaultValue: undefined,
      form: {
        itemProps: { validateTrigger: 'blur' },
        componentProps: {
          api: LaboratoryApi.all,
          params: {},
          labelField: 'name',
          valueField: 'id',
          allowClear: false,
          placeholder: '实验室',
        },
        colProps,
        component: 'ApiSelect',
        required: true,
        // rules: [{ required: true, message: '请输入实验室！' }],
      },
      table: {
        customRender: ({ text, record }) => get(record, 'laboratory.name') || 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 = ['name','type','manufacturer','isEnable','laboratoryId','createTime'];
const editFields = ['name','token','type','manufacturer','description','contact','phone','address','isEnable','laboratoryId'];
const tableFields = ['name','token','type','manufacturer','contact','phone','address','isEnable','laboratoryId','editorName','createTime','updateTime'];
const descriptionFields = ['name','token','remoteId','type','manufacturer','description','contact','phone','address','isEnable','laboratoryId','editorName','createTime','updateTime'];
const labFields = ['name','type','isEnable',];

export const searchFormSchema: FormSchema[] = schema.properties.filter(item => queryFields.includes(item.field))
  .map(
    ({ field, label, form: { rules = [], required, ...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 labColumns: BasicColumn[] = schema.properties.filter(item => labFields.includes(item.field))
  .map(
    ({ field, label, table }) =>
      ({
        dataIndex: field,
        title: label,
        ...table,
      } as BasicColumn)
  );
