/**
 * 项目：视频监控平台
 * 模型分组：用户管理
 * 模型名称：日志表
 * @Author: xiongwei
 * @Date: 2024-09-26 11:50:00
 */

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




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

export const schema = {
  model: 'Log',
  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: 'userId',
      label: '操作人',
      defaultValue: 0,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '操作人',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入操作人！' }],
      },
      table: {
      },
    },
    {
      field: 'description',
      label: '描述',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '描述',
        },
        component: 'Input',
        colProps,
        rules: [{ required: true, message: '请输入描述！' }],
      },
      table: {
        ellipsis: true,
      },
    },
    {
      field: 'remark',
      label: '说明',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '说明',
        },
        colProps,
        component: 'Input',
      },
      table: {
        width: 400,
        ellipsis: true,
      },
    },
    {
      field: 'username',
      label: '操作人',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '操作人',
        },
        colProps,
        component: 'Input',
      },
      table: {
        width: 150,
      },
    },
    {
      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: '请选择日志时间！' }],
      },
      table: {
        width: 150,
      },
    },
  ],
};

const queryFields = ['createTime'];
const editFields = ['description','userId','remark'];
const tableFields = ['description','remark','username','createTime'];
const descriptionFields = ['description','userId','username','remark','createTime'];

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