import type { VxeGridProps } from '#/adapter/vxe-table';

import { computed } from 'vue';

import dayjs from 'dayjs';

import { getCommonGetSelectList } from '#/api-client';
import { $t } from '#/locales';

export const querySchema = computed(() => [
  {
    component: 'ApiSelect',
    fieldName: 'IoTDataType',
    label: $t('abp.IoTDBBase.IoTDataType'),
    componentProps: {
      api: getCommonGetSelectList,
      params: {
        query: {
          typeName: 'IoTDBDataTypeConst',
        },
      },
      labelField: 'value',
      valueField: 'key',
      optionsPropName: 'options',
      immediate: true,
      allowClear: true,
      placeholder: $t('common.pleaseSelect') + $t('abp.IoTDBBase.IoTDataType'),
      afterFetch: (res: any) => {
        // 确保返回的是数组格式
        if (Array.isArray(res)) {
          return res;
        }
        // 如果是包装在 items 中的，提取出来
        if (res && Array.isArray(res.items)) {
          return res.items;
        }
        // 如果是包装在 data 中的，提取出来
        if (res && Array.isArray(res.data)) {
          return res.data;
        }
        // 如果都不是，返回空数组
        return [];
      },
    },
  },
  {
    component: 'TableModelDeviceSelect',
    fieldName: 'DeviceAddress',
    label: $t('abp.log.deviceInfo'),
    componentProps: {
      placeholder: $t('common.pleaseSelect') + $t('abp.log.deviceInfo'),
      allowClear: true,
    },
  },
  // 新增开始时间选择器
  {
    component: 'DatePicker',
    fieldName: 'StartCreationTime',
    label: '开始时间',
    componentProps: {
      placeholder: '开始时间',
      format: 'YYYY-MM-DD HH:mm:ss',
      showTime: true,
      allowClear: true,
    },
  },
  // 新增结束时间选择器
  {
    component: 'DatePicker',
    fieldName: 'EndCreationTime',
    label: '结束时间',
    componentProps: {
      placeholder: '结束时间',
      format: 'YYYY-MM-DD HH:mm:ss',
      showTime: true,
      allowClear: true,
    },
  },
]);

export const tableSchema: any = computed((): VxeGridProps['columns'] => [
  { title: $t('common.seq'), type: 'seq', width: 50, slots: {} },
  {
    field: 'timestampStr',
    title: $t('abp.IoTDBBase.Timestamps'),
    minWidth: '150',
    slots: {},
  },
  {
    field: 'formattedTimestamps',
    title: $t('abp.IoTDBBase.FormattedTimestamps'),
    minWidth: '150',
    formatter: ({ cellValue }) => {
      return cellValue ? dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss') : '';
    },
  },

  {
    field: 'ioTDataType',
    title: $t('abp.IoTDBBase.IoTDataType'),
    minWidth: '120',
    slots: {},
  },
  {
    field: 'deviceAddress',
    title: $t('abp.IoTDBBase.DeviceAddress'),
    minWidth: '120',
    slots: {},
  },
  {
    field: 'subDevice',
    title: $t('abp.IoTDBBase.SubDevice'),
    minWidth: '120',
    slots: {},
  },
  {
    field: 'messageType',
    title: $t('abp.OneNETLog.MessageType'),
    minWidth: '120',
    slots: {},
  },
  {
    field: 'protocol',
    title: $t('abp.OneNETLog.Protocol'),
    minWidth: '100',
    slots: {},
  },
  {
    field: 'formattedReceivedTimestamps',
    title: $t('abp.OneNETLog.ReceivedTime'),
    minWidth: '150',
    slots: {},
    formatter: ({ cellValue }) => {
      return cellValue ? dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss') : '';
    },
  },
  {
    field: 'isEncrypted',
    title: $t('abp.OneNETLog.IsEncrypted'),
    minWidth: '200',
    slots: {},
  },
  {
    field: 'plaintextMessage',
    title: $t('abp.OneNETLog.PlaintextMessage'),
    minWidth: '200',
    slots: {},
  },
  {
    field: 'rawMessage',
    title: $t('abp.OneNETLog.RawMessage'),
    minWidth: '200',
    slots: {},
  },
  {
    field: 'receivedPayload',
    title: $t('abp.OneNETLog.ReceivedPayload'),
    minWidth: '200',
    slots: {},
  },
]);
