import { formatToDateTime } from '/@/utils/dateUtil';
import { FormSchema } from '/@/components/Form';
import { BasicColumn } from '/@/components/Table';
import { DeviceTypeEnum } from '/@/api/device/model/deviceModel';
import { getCustomerList } from '/@/api/device/deviceManager';
import { DescItem } from '/@/components/Description/index';
import moment from 'moment';
import { h } from 'vue';
import { Button } from 'ant-design-vue';
import { TypeEnum } from './data';
import { PageEnum } from '/@/enums/pageEnum';
import { useGo } from '/@/hooks/web/usePage';

// 设备详情的描述
export const descSchema = (emit: EmitType): DescItem[] => {
  return [
    {
      field: 'createTime',
      label: '创建时间',
    },
    {
      field: 'name',
      label: '设备名称',
      render(val, data: Record<'alias' | 'name', string>) {
        return h('span', {}, data.alias || val);
      },
    },
    {
      field: 'label',
      label: '设备标签',
    },
    {
      field: 'deviceProfile.name',
      label: '产品',
      render(val) {
        const go = useGo();
        return h(
          Button,
          {
            type: 'link',
            style: { padding: 0 },
            onClick: () => go(PageEnum.DEVICE_PROFILE + '?name=' + String(val)),
          },
          { default: () => val }
        );
      },
    },
    {
      field: 'gatewayName',
      label: '所属网关',
      show: (data) => !!data.gatewayName,
      render(val, data) {
        if (TypeEnum.SENSOR !== data.deviceType) return val;
        return h(
          Button,
          { type: 'link', style: { padding: 0 }, onClick: () => emit('open-gateway-device', data) },
          { default: () => data.gatewayAlias || val }
        );
      },
    },
    {
      field: 'deviceType',
      label: '设备类型',
      render: (text) => {
        return text === DeviceTypeEnum.GATEWAY
          ? '网关设备'
          : text == DeviceTypeEnum.DIRECT_CONNECTION
          ? '直连设备'
          : '网关子设备';
      },
    },
    {
      field: 'description',
      label: '描述',
      // span: 2,
    },
  ];
};

// 实时数据表格
export const realTimeDataColumns: BasicColumn[] = [
  {
    title: '键',
    dataIndex: 'key',
    width: 100,
  },
  {
    title: '值',
    dataIndex: 'value',
    width: 160,
  },
  {
    title: '最后更新时间',
    dataIndex: 'time',
    width: 120,
    format: (text) => formatToDateTime(text, 'YYYY-MM-DD HH:mm:ss'),
  },
];

// 告警
export const alarmSearchSchemas: FormSchema[] = [
  {
    field: 'status',
    label: '告警状态',
    component: 'Select',
    colProps: { span: 6 },
    componentProps: {
      options: [
        {
          label: '清除未确认',
          value: 'CLEARED_UNACK',
        },
        {
          label: '激活未确认',
          value: 'ACTIVE_UNACK',
        },
        {
          label: '清除已确认',
          value: 'CLEARED_ACK',
        },
        {
          label: '激活已确认',
          value: 'ACTIVE_ACK',
        },
      ],
    },
  },
  {
    field: 'alarmType',
    label: '告警场景',
    component: 'Input',
    colProps: { span: 6 },
    componentProps: {
      maxLength: 36,
    },
  },
  {
    field: 'alarmTime',
    label: '告警时间',
    component: 'RangePicker',
    componentProps: {
      showTime: { defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('23:59:59', 'HH:mm:ss')] },
    },
    colProps: { span: 6 },
  },
];
export const alarmColumns: BasicColumn[] = [
  {
    title: '告警时间',
    dataIndex: 'createdTime',
    width: 120,
  },
  {
    title: '告警设备',
    dataIndex: 'deviceName',
    width: 100,
  },
  {
    title: '告警场景',
    dataIndex: 'type',
    width: 160,
  },
  {
    title: '告警级别',
    dataIndex: 'severity',
    width: 160,
    format: (text) => alarmLevel(text),
  },
  {
    title: '状态',
    dataIndex: 'status',
    format: (text) => statusType(text),
    width: 160,
  },
];

export const alarmSchemasForm: FormSchema[] = [
  {
    field: 'deviceName',
    label: '告警设备',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
  },

  {
    field: 'startTs',
    label: '开始时间',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'endTs',
    label: '结束时间',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'ackTs',
    label: '处理时间',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
    ifShow: ({ values }) => values.status === '激活已确认' || values.status === '清除已确认',
  },
  {
    field: 'clearTs',
    label: '清除时间',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
    ifShow: ({ values }) => values.status === '清除已确认' || values.status === '清除未确认',
  },
  {
    field: 'type',
    label: '告警场景',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'severity',
    label: '严重程度',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Input',
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'details',
    label: '详情',
    component: 'InputTextArea',
    componentProps: {
      maxLength: 255,
    },
  },
];
// 子设备
export const childDeviceSchemas: FormSchema[] = [
  {
    field: 'deviceState',
    label: '设备状态',
    colProps: { span: 6 },
    component: 'Select',
    componentProps: {
      maxLength: 255,
      options: [
        { label: '待激活', value: 'INACTIVE' },
        { label: '在线', value: 'ONLINE' },
        { label: '离线', value: 'OFFLINE' },
      ],
    },
  },
  {
    field: 'name',
    label: '设备名称',
    component: 'Input',
    colProps: { span: 6 },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入设备名称',
    },
  },
];
export const childDeviceColumns: BasicColumn[] = [
  {
    title: '名称',
    dataIndex: 'tbDeviceName',
    width: 120,
    slots: {
      customRender: 'tbDeviceName',
    },
  },
  {
    title: '标签',
    dataIndex: 'label',
    width: 160,
  },
  {
    title: '状态',
    dataIndex: 'deviceState',
    slots: { customRender: 'deviceState' },
    width: 160,
  },
  {
    title: '最后连接时间',
    dataIndex: 'lastOnlineTime',
    format: (text) => formatToDateTime(text, 'YYYY-MM-DD HH:mm:ss'),
    width: 160,
  },
  {
    title: '更新时间',
    dataIndex: 'createdTime',
    format: (text) => formatToDateTime(text, 'YYYY-MM-DD HH:mm:ss'),
    width: 160,
  },
];

export const alarmLevel = (type: string): string => {
  if (type === 'CRITICAL') {
    return '紧急';
  } else if (type === 'MAJOR') {
    return '重要';
  } else if (type === 'MINOR') {
    return '次要';
  } else if (type === 'WARNING') {
    return '警告';
  } else {
    return '不确定';
  }
};
export const statusType = (type: string): string => {
  if (type === 'CLEARED_UNACK') {
    return '清除未确认';
  } else if (type === 'CLEARED_ACK') {
    return '清除已确认';
  } else if (type === 'ACTIVE_ACK') {
    return '激活已确认';
  } else {
    return '激活未确认';
  }
};

export const customerForm: FormSchema[] = [
  {
    field: 'customerId',
    label: '分配客户',
    component: 'ApiSelect',
    componentProps: {
      api: getCustomerList,
      immediate: false,
      labelField: 'realName',
      valueField: 'customerId',
    },
    required: true,
    colProps: { span: 12 },
  },
];
