/**
 * 项目：中核预埋件检测
 * 模型分组：数据管理
 * 模型名称：检测任务表
 * @Author: xiongwei
 * @Date: 2024-11-15 11:50:00
 */

import { BasicColumn } from '@/components/Table';
import { FormSchema } from '@/components/Table';
import * as DeviceApi from '@/api/data/deviceApi'



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

export const schema = {
  model: 'Task',
  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: 'deviceSn',
      label: '设备',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '请选择设备',
          labelInValue: false,
          api: DeviceApi.all,
          params: {},
          resultField: 'result',
          labelField: 'name',
          valueField: 'deviceSn',
        },
        colProps,
        component: 'ApiSelect',
        rules: [{ required: true, message: '请选择设备！' }],
      },
      table: {
      },
    },
    {
      field: 'paramJson',
      label: '任务参数',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '任务参数',
          autoSize: { minRows: 15, maxRows: 15 },
        },
        slot: 'paramJson',
        colProps: { span: 24 },
        rules: [{ required: true, message: '请输入任务参数！' }],
      },
      table: {
      },
    },
    {
      field: 'resultJson',
      label: '任务结果',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '任务结果',
          autoSize: {minRows: 15, maxRows: 15},
        },
        component: 'InputTextArea',
        colProps: { span: 24 },
      },
      table: {
      },
    },
    {
      field: 'startTimeQuery',
      label: '开始时间',
      defaultValue: undefined,
      form: {
        colProps,
        component: 'RangePicker',
        componentProps: {
          allowClear: false,
          placeholder: ['开始时间', '结束时间'],
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          showTime: false,
        },
        rules: [{ required: true, message: '请输入开始时间！' }],
      },
      table: {
      },
    },
    {
      field: 'startTime',
      label: '开始时间',
      defaultValue: undefined,
      form: {
        colProps,
        component: 'DatePicker',
        componentProps: {
          allowClear: false,
          placeholder: '开始时间',
          format: 'YYYY-MM-DD HH:mm:ss',
          valueFormat: 'YYYY-MM-DD HH:mm:ss',
          showTime: true,
        },
        rules: [{ required: true, message: '请输入开始时间！' }],
      },
      table: {
      },
    },
    {
      field: 'endTime',
      label: '结束时间',
      defaultValue: undefined,
      form: {
        colProps,
        component: 'DatePicker',
        componentProps: {
          allowClear: false,
          placeholder: '结束时间',
          format: 'YYYY-MM-DD HH:mm:ss',
          valueFormat: 'YYYY-MM-DD HH:mm:ss',
          showTime: true,
        },
        rules: [{ required: true, message: '请输入结束时间！' }],
      },
      table: {
      },
    },
    {
      field: 'state',
      label: '状态',
      defaultValue: undefined,
      form: {
        colProps,
        component: 'Select',
        componentProps: {
          allowClear: true,
          placeholder: '请选择状态',
          options: [{value: 0, label: '未开始'}, {value: 1, label: '进行中'}, {value: 2, label: '已完成'}],
          labelField: 'label',
          valueField: 'value',
        },
        rules: [{ required: true, message: '请选择状态！' }],
      },
      table: {
        customRender: ({ text, record }) => {
          if (record.state === 0)
            return '未开始';
          if (record.state === 1)
            return '进行中';
          if (record.state === 2)
            return '已完成';
        },
      },
    },
    {
      field: 'createTime',
      label: '创建日期',
      defaultValue: undefined,
      form: {
        colProps,
        componentProps: {
          allowClear: false,
          placeholder: ['开始时间', '结束时间'],
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          showTime: false,
        },
        component: 'RangePicker',
        rules: [{ required: true, message: '请输入CreateTime！' }],
      },
      table: {
      },
    },
    {
      field: 'updateTime',
      label: 'UpdateTime',
      defaultValue: undefined,
      form: {
        colProps,
        componentProps: {
          allowClear: false,
          placeholder: ['开始时间', '结束时间'],
          format: 'YYYY-MM-DD',
          valueFormat: 'YYYY-MM-DD',
          showTime: false,
        },
        component: 'RangePicker',
        rules: [{ required: true, message: '请输入UpdateTime！' }],
      },
      table: {
      },
    },
  ],
};

const queryFields = ['name','deviceSn','startTimeQuery','state'];
// const editFields = ['name','deviceSn','paramJson'];
const editFields = ['name', 'deviceSn'];
const tableFields = ['name','deviceSn','startTime','endTime','state','createTime'];
const descriptionFields = ['name','deviceSn','paramJson','resultJson','startTime','endTime','state'];

const queryFieldsIndexMap = new Map(queryFields.map((field, index) => [field, index]));
export const searchFormSchema: FormSchema[] = schema.properties.filter(item => queryFields.includes(item.field))
  .map(
    ({ field, label, form: { ...formProps } }) =>
      ({
        field,
        label,
        defaultValue: undefined,
        ...formProps,
        required: false,
        rules: [{ required: false }],
      } as FormSchema),
  )
 .sort((a, b) => {
   const indexA = queryFieldsIndexMap.get(a.field) as number;
   const indexB = queryFieldsIndexMap.get(b.field) as number;
   return indexA - indexB;
 });

const editFieldsIndexMap = new Map(editFields.map((field, index) => [field, index]));
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),
  )
  .sort((a, b) => {
      const indexA = editFieldsIndexMap.get(a.field) as number;
      const indexB = editFieldsIndexMap.get(b.field) as number;
      return indexA - indexB;
  });

const tableFieldsIndexMap = new Map(tableFields.map((field, index) => [field, index]));
export const columns: BasicColumn[] = schema.properties.filter(item => tableFields.includes(item.field))
  .map(
    ({ field, label, table }) =>
      ({
        dataIndex: field,
        title: label,
        ...table,
      } as BasicColumn)
  )
  .sort((a, b) => {
     const indexA = tableFieldsIndexMap.get(a.dataIndex as string) as number;
     const indexB = tableFieldsIndexMap.get(b.dataIndex as string) as number;
     return indexA - indexB;
  });

const descriptionFieldsIndexMap = new Map(descriptionFields.map((field, index) => [field, index]));
export const descriptionColumns: BasicColumn[] = schema.properties.filter(item => descriptionFields.includes(item.field))
  .map(
    ({ field, label, table }) =>
      ({
        dataIndex: field,
        title: label,
        ...table,
      } as BasicColumn),
  )
  .sort((a, b) => {
      const indexA = descriptionFieldsIndexMap.get(a.dataIndex as string) as number;
      const indexB = descriptionFieldsIndexMap.get(b.dataIndex as string) as number;
      return indexA - indexB;
  });
