<script setup lang="ts">
import type { NdmNvrDiagInfo, NdmNvrResultVO, Station } from '@/apis';
import { DeviceCommonCard, DeviceHardwareCard, DeviceHeaderCard, NvrDiskCard, NvrEnvCard, NvrRecordCheckCard, type DeviceCommonCardProps } from '@/components';
import { isNvrCluster } from '@/helpers';
import destr from 'destr';
import { NFlex } from 'naive-ui';
import { computed, toRefs } from 'vue';

const props = defineProps<{
  ndmDevice: NdmNvrResultVO;
  station: Station;
}>();

const { ndmDevice, station } = toRefs(props);

const lastDiagInfo = computed(() => {
  const result = destr<any>(ndmDevice.value.lastDiagInfo);
  if (!result) return null;
  if (typeof result !== 'object') return null;
  return result as NdmNvrDiagInfo;
});

const commonInfo = computed<DeviceCommonCardProps['commonInfo']>(() => {
  const { ipAddress } = ndmDevice.value;
  const { ethInfo, ipInfo, stCommonInfo } = lastDiagInfo.value ?? {};
  const { 设备ID, 软件版本, 生产厂商, 设备别名, 设备型号, 硬件版本 } = stCommonInfo ?? {};

  let operStatus = '-';
  if (!!ethInfo?.operStatus) {
    operStatus = ethInfo?.operStatus === '1' ? '正常' : '异常';
  }

  return [
    {
      title: '设备型号信息',
      items: [
        { label: '设备ID', value: 设备ID || '-' },
        { label: '软件版本', value: 软件版本 || '-' },
        { label: '生产厂商', value: 生产厂商 || '-' },
        { label: '设备别名', value: 设备别名 || '-' },
        { label: '设备型号', value: 设备型号 || '-' },
        { label: '硬件版本', value: 硬件版本 || '-' },
      ],
    },
    {
      title: '设备网卡信息',
      items: [
        { label: 'IP地址', value: ipAddress || '-' },
        { label: '子网掩码', value: ipInfo?.mASK || '-' },
        { label: 'MAC地址', value: ethInfo?.macAddress || '-' },
        { label: '连接速率', value: ethInfo?.speed || '-' },
        { label: 'MTU', value: ethInfo?.mTU || '-' },
        { label: '运行状态', value: operStatus },
      ],
    },
  ];
});

const cpuUsage = computed(() => lastDiagInfo.value?.stCommonInfo?.CPU使用率);
const memUsage = computed(() => lastDiagInfo.value?.stCommonInfo?.内存使用率);

const diskHealth = computed(() => lastDiagInfo.value?.info?.diskHealth);
const diskArray = computed(() => lastDiagInfo.value?.info?.groupInfoList);

const fanInfo = computed(() => lastDiagInfo.value?.cdFanInfo);
const powerSupplyInfo = computed(() => lastDiagInfo.value?.cdPowerSupplyInfo);
</script>

<template>
  <NFlex vertical>
    <DeviceHeaderCard :ndm-device="ndmDevice" :station="station" />
    <DeviceCommonCard :common-info="commonInfo" />
    <DeviceHardwareCard :cpu-usage="cpuUsage" :mem-usage="memUsage" />
    <NvrEnvCard :fan-info="fanInfo" :power-supply-info="powerSupplyInfo" />
    <NvrDiskCard :disk-health="diskHealth" :disk-array="diskArray" />
    <template v-if="isNvrCluster(ndmDevice)">
      <NvrRecordCheckCard :ndm-device="ndmDevice" :station="station" />
    </template>
  </NFlex>
</template>

<style scoped lang="scss"></style>
