import { BasicColumn, FormSchema } from '/@/components/Table';
import { PackageField, PackageType } from './packageDetail.config';
import { dateUtil } from '/@/utils/dateUtil';
import { DEFAULT_DATE_FORMAT } from '/@/views/visual/board/detail/config/util';

export interface ModalPassRecord {
  isUpdate: boolean;
  record?: Recordable;
}

export enum OtaPermissionKey {
  CREATE = 'api:operation:ota:post',
  UPDATE = 'api:operation:ota:update',
  DELETE = 'api:operation:ota:delete',
  DOWNLOAD = 'api:operation:ota:download',
  DETAIL = 'api:operation:ota:detail',
}

export const columns: BasicColumn[] = [
  {
    title: '创建时间',
    dataIndex: PackageField.CREATE_TIME,
    format(text) {
      return dateUtil(text).format(DEFAULT_DATE_FORMAT);
    },
    width: 140,
  },
  {
    title: '标题',
    dataIndex: PackageField.TITLE,
    width: 120,
  },
  {
    title: '版本',
    dataIndex: PackageField.VERSION,
    width: 120,
  },
  {
    title: '版本标签',
    dataIndex: PackageField.VERSION_TAG,
    width: 120,
  },
  {
    title: '包类型',
    dataIndex: PackageField.PACKAGE_TYPE,
    format: (text) => {
      return text === PackageType.FIRMWARE ? '固件' : '软件';
    },
    width: 120,
  },
  {
    title: '直接URL',
    dataIndex: PackageField.URL,
    width: 120,
  },
  {
    title: '文件名',
    dataIndex: PackageField.FILE_NAME,
    width: 120,
  },
  {
    title: '文件大小',
    dataIndex: PackageField.FILE_SIZE,
    format(text, record) {
      return record[PackageField.FILE_SIZE]
        ? `${Math.ceil(((text as unknown as number) / 1024) * 100) / 100}kb`
        : '';
    },
    width: 120,
  },
  {
    title: '校验和',
    dataIndex: PackageField.CHECK_SUM,
    format(text, record) {
      return text ? `${record[PackageField.CHECK_SUM_ALG]}: ${text.slice(0, 11)}` : '';
    },
    width: 120,
  },
  {
    title: '操作',
    dataIndex: 'action',
    flag: 'ACTION',
    fixed: 'right',
    width: 200,
    slots: {
      customRender: 'action',
    },
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: PackageField.TITLE,
    label: '标题',
    component: 'Input',
    colProps: { span: 8 },
  },
];
