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: 'Input',
    fieldName: 'searchKeyword',
    label: '搜索关键字',
    componentProps: {
      placeholder: '请输入搜索关键字',
    },
  },
  {
    component: 'ApiSelect',
    fieldName: 'upgradeSource',
    label: '升级来源',
    componentProps: {
      api: getCommonGetSelectList,
      params: {
        query: {
          typeName: 'DeviceUpgradeSourceTypeEnum',
        },
      },
      labelField: 'value',
      valueField: 'key',
      optionsPropName: 'options',
      immediate: true,
      allowClear: true,
      placeholder: '请选择升级来源',
      afterFetch: (res: any) => {
        if (Array.isArray(res)) {
          return res;
        }
        if (res && Array.isArray(res.items)) {
          return res.items;
        }
        if (res && Array.isArray(res.data)) {
          return res.data;
        }
        return [];
      },
    },
  },
  {
    component: 'ApiSelect',
    fieldName: 'upgradeResult',
    label: '升级结果',
    componentProps: {
      api: getCommonGetSelectList,
      params: {
        query: {
          typeName: 'DeviceUpgradeStatusTypeEnum',
        },
      },
      labelField: 'value',
      valueField: 'key',
      optionsPropName: 'options',
      immediate: true,
      allowClear: true,
      placeholder: '请选择升级结果',
      afterFetch: (res: any) => {
        if (Array.isArray(res)) {
          return res;
        }
        if (res && Array.isArray(res.items)) {
          return res.items;
        }
        if (res && Array.isArray(res.data)) {
          return res.data;
        }
        return [];
      },
    },
  },
  {
    component: 'Input',
    fieldName: 'upgradeIdentifier',
    label: '升级标识符号',
    componentProps: {
      placeholder: '请输入升级标识符号',
      onInput: (e: Event) => {
        const target = e.target as HTMLInputElement;
        // 只允许输入数字
        const value = target.value.replace(/[^\d]/g, '');
        if (target.value !== value) {
          target.value = value;
          // 触发 input 事件，确保表单值更新
          target.dispatchEvent(new Event('input', { bubbles: true }));
        }
      },
    },
  },
  {
    component: 'DatePicker',
    fieldName: 'upgradeDate',
    label: '升级日期',
    componentProps: {
      placeholder: '请选择升级日期',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD',
      allowClear: true,
    },
  },
]);

export const tableSchema: any = computed((): VxeGridProps['columns'] => [
  { title: $t('common.seq'), type: 'seq', width: 50 },
  // 勾选列，用于批量操作
  { title: '', type: 'checkbox', width: 50 },
  {
    field: 'deviceName',
    title: '设备名称',
    minWidth: '150',
  },
  {
    field: 'deviceAddress',
    title: '设备地址',
    minWidth: '150',
  },
  {
    field: 'oldFirmwareVersion',
    title: '旧固件版本',
    minWidth: '150',
  },
  {
    field: 'nowFirmwareVersion',
    title: '当前固件版本',
    minWidth: '150',
  },
  {
    field: 'upgradeDate',
    title: '升级日期',
    minWidth: '180',
    formatter: ({ cellValue }) => {
      return cellValue ? dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss') : '';
    },
  },
  {
    field: 'upgradeSourceTypeName',
    title: '升级来源',
    minWidth: '120',
  },
  {
    field: 'upgradeStatusName',
    title: '升级状态',
    minWidth: '120',
    slots: { default: 'upgradeStatus' },
  },
  {
    field: 'upgradeResultName',
    title: '升级结果',
    minWidth: '120',
    slots: { default: 'upgradeResult' },
  },
  {
    field: 'upgradeIdentifier',
    title: '升级标识符',
    minWidth: '120',
  },
  {
    field: 'upgradeMessage',
    title: '升级信息',
    minWidth: '200',
    showOverflow: 'tooltip',
  },
  {
    field: 'firmwareSignature',
    title: '签名校验值',
    minWidth: '200',
    showOverflow: 'tooltip',
  },
  {
    field: 'creationTime',
    title: '创建时间',
    minWidth: '180',
    formatter: ({ cellValue }) => {
      return cellValue ? dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss') : '';
    },
  },
  {
    title: $t('common.action'),
    field: 'action',
    fixed: 'right',
    width: '150',
    slots: { default: 'action' },
  },
]);
