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

import { selectAllSysStation } from '#/api/system/infoEntry/stationInfo/stationInfo';
import { selectAllSysStationAreaList } from '#/api/system/infoEntry/stationInfo/stationInfo';

export const queryFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'location',
    label: '摄像头位置',
    componentProps: {
      placeholder: '请输入摄像头位置',
    },
  },
  {
    component: 'Input',
    fieldName: 'httpUrl',
    label: '通道地址',
    componentProps: {
      placeholder: '请输入通道地址',
    },
  },
  {
    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(),
            }))
          : [];
      },
    },
  },
];

export const tableColumns: VxeGridProps['columns'] = [
  {
    type: 'checkbox',
    width: 80,
    fixed: 'left',
  },
  {
    field: 'location',
    title: '摄像头位置',
    minWidth: 150,
    fixed: 'left',
  },
  {
    field: 'stationName',
    title: '所属场站',
    minWidth: 120,
    fixed: 'left',
  },
  {
    field: 'httpUrl',
    title: '通道地址',
    minWidth: 200,
  },
  {
    field: 'cameraName',
    title: '摄像头名称',
    minWidth: 120,
  },
  {
    field: 'cameraCode',
    title: '摄像头编码',
    minWidth: 120,
  },
  {
    field: 'cameraType',
    title: '摄像头类型',
    minWidth: 100,
  },
  {
    field: 'ipAddress',
    title: 'IP地址',
    minWidth: 120,
  },
  {
    field: 'aiEnabled',
    title: '是否启用AI',
    minWidth: 100,
    formatter: (params: { cellValue: string }) => {
      return params.cellValue === '1' ? '是' : '否';
    },
  },
  {
    field: 'status',
    title: '设备状态',
    minWidth: 100,
    formatter: (params: { cellValue: string }) => {
      const statusMap = {
        '0': '在线',
        '1': '离线',
        '2': '故障',
      };
      return statusMap[params.cellValue] || params.cellValue;
    },
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 220,
  },
];

export const drawerFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'cameraId',
  },
  {
    component: 'Input',
    fieldName: 'cameraCode',
    label: '设备编号',
    componentProps: {
      placeholder: '请输入设备编号（唯一）',
      maxlength: 100,
    },
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'cameraName',
    label: '设备名称',
    componentProps: {
      placeholder: '请输入设备名称',
      maxlength: 100,
    },
  },
  {
    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: 'Input',
    fieldName: 'cameraType',
    label: '摄像头类型',
    componentProps: {
      placeholder: '请输入摄像头类型（IPC枪机/PTZ球机/DOME半球）',
      maxlength: 50,
    },
  },
  {
    component: 'Input',
    fieldName: 'ipAddress',
    label: 'IP地址',
    componentProps: {
      placeholder: '请输入IP地址',
      maxlength: 50,
    },
  },
  {
    component: 'InputNumber',
    fieldName: 'port',
    label: '端口号',
    componentProps: {
      placeholder: '请输入端口号',
      style: {
        width: '100%',
      },
    },
  },
  {
    component: 'Input',
    fieldName: 'username',
    label: '登录用户名',
    componentProps: {
      placeholder: '请输入登录用户名',
      maxlength: 50,
    },
  },
  {
    component: 'Input',
    fieldName: 'password',
    label: '登录密码',
    componentProps: {
      placeholder: '请输入登录密码',
      maxlength: 100,
    },
  },
  {
    component: 'Input',
    fieldName: 'rtspUrl',
    label: 'RTSP流地址',
    componentProps: {
      placeholder: '请输入RTSP视频流地址',
      maxlength: 200,
    },
  },
  {
    component: 'Input',
    fieldName: 'httpUrl',
    label: 'HTTP流地址',
    componentProps: {
      placeholder: '请输入HTTP视频流地址',
      maxlength: 200,
    },
  },
  {
    component: 'Input',
    fieldName: 'location',
    label: '摄像头位置',
    componentProps: {
      placeholder: '请输入摄像头位置',
      maxlength: 100,
    },
  },
  {
    component: 'InputNumber',
    fieldName: 'longitude',
    label: '经度',
    componentProps: {
      placeholder: '请输入经度',
      style: {
        width: '100%',
      },
    },
  },
  {
    component: 'InputNumber',
    fieldName: 'latitude',
    label: '纬度',
    componentProps: {
      placeholder: '请输入纬度',
      style: {
        width: '100%',
      },
    },
  },
  {
    component: 'Select',
    fieldName: 'aiEnabled',
    label: '是否启用AI识别',
    componentProps: {
      placeholder: '请选择是否启用AI识别',
      options: [
        { label: '是', value: '1' },
        { label: '否', value: '0' },
      ],
    },
  },
  {
    component: 'Select',
    fieldName: 'status',
    label: '设备状态',
    componentProps: {
      placeholder: '请选择设备状态',
      options: [
        { label: '在线', value: '0' },
        { label: '离线', value: '1' },
      ],
    },
  },
];

