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

import { selectAllSysStation } from '#/api/system/infoEntry/stationInfo/stationInfo';
import { getDictOptions } from '#/utils/dict';

// 字典标识常量
const DICT_KEYS = {
  // 预警类型字典标识
  WARNING_TYPE: 'warning_type',
  // 处理状态字典标识
  HANDLE_STATUS: 'handle_status',
};

// 获取预警类型字典选项
const getWarningTypes = () => getDictOptions(DICT_KEYS.WARNING_TYPE);

// 获取处理状态字典选项
const getHandleStatuses = () => getDictOptions(DICT_KEYS.HANDLE_STATUS);

export const queryFormSchema: FormSchemaGetter = () => [
  {
    component: 'ApiSelect',
    fieldName: 'stationId',
    label: '预警场站',
    componentProps: {
      placeholder: '请选择预警场站',
      api: async () => {
        const resp = await selectAllSysStation();
        const data = resp || [];
        return Array.isArray(data)
          ? data.map((item: any) => ({
              label: item.stationName,
              value: item.id.toString(),
            }))
          : [];
      },
    },
  },
  {
    component: 'DatePicker',
    componentProps: {
      type: 'daterange',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      startPlaceholder: '开始时间',
      endPlaceholder: '结束时间',
      style: {
        width: '100%',
      },
    },
    fieldName: 'alertTime',
    label: '检查时间',
  },
  {
    component: 'Select',
    fieldName: 'alertType',
    label: '预警场景',
    componentProps: {
      placeholder: '请选择预警场景',
      options: getWarningTypes(),
    },
  },
];

export const tableColumns: VxeGridProps['columns'] = [
  {
    field: 'status',
    title: '状态',
    minWidth: 100,
    fixed: 'left',
    formatter: (params: { cellValue: string }) => {
      const statusMap = {
        'unhandled': '未处理',
        'handling': '处理中',
        'resolved': '已解决',
        'false_alarm': '误报',
      };
      return statusMap[params.cellValue] || params.cellValue;
    },
  },
  {
    field: 'alertTime',
    title: '预警时间',
    minWidth: 180,
    fixed: 'left',
  },
  {
    field: 'stationName',
    title: '预警场站',
    minWidth: 120,
  },
  {
    field: 'stationManagerName',
    title: '场站站长',
    minWidth: 100,
  },
  {
    field: 'areaManagerName',
    title: '片区经理',
    minWidth: 100,
  },
  {
    field: 'location',
    title: '摄像头位置',
    minWidth: 120,
  },
  {
    field: 'alertType',
    title: '预警场景',
    minWidth: 100,
    formatter: (params: { cellValue: string }) => {
      const alertTypeMap = {
        'fire': '火焰',
        'smoke': '烟雾',
        'fire_danger': '火灾',
        'intrusion': '入侵',
      };
      return alertTypeMap[params.cellValue] || params.cellValue;
    },
  },
  {
    field: 'snapshotUrl',
    title: '异常图片',
    minWidth: 100,
    slots: {
      default: ({ row }: { row: any }) => {
        return row.snapshotUrl ? (
          <el-image
            src={row.snapshotUrl}
            style={{ width: '60px', height: '60px' }}
            fit="cover"
            preview-src-list={[row.snapshotUrl]}
          />
        ) : '-';
      },
    },
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 150,
  },
];

export const drawerFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'alertId',
  },
  {
    component: 'Select',
    fieldName: 'handleAction',
    label: '处理动作',
    componentProps: {
      placeholder: '请选择处理动作',
      options: [
        { label: '解决', value: 'resolve' },
        { label: '忽略', value: 'ignore' },
        { label: '误报', value: 'false_alarm' },
      ],
    },
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'handleContent',
    label: '处理内容',
    componentProps: {
      placeholder: '请输入处理内容',
      maxlength: 500,
      type: 'textarea',
      showWordLimit: true,
      rows: 4,
    },
    rules: 'required',
  }
];
