<script setup lang="ts">
import type { NdmDecoderDiagInfo, NdmDecoderResultVO, Station } from '@/apis';
import { DeviceCommonCard, DeviceHardwareCard, DeviceHeaderCard, type DeviceCommonCardProps } from '@/components';
import destr from 'destr';
import { NFlex } from 'naive-ui';
import { computed, toRefs } from 'vue';

const props = defineProps<{
  ndmDevice: NdmDecoderResultVO;
  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 NdmDecoderDiagInfo;
});

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

  return [
    {
      title: '设备型号信息',
      items: [
        { label: '设备ID', value: 设备ID || '-' },
        { label: '软件版本', value: 软件版本 || '-' },
        { label: '设备厂商', value: 设备厂商 || '-' },
        { label: '设备别名', value: 设备别名 || '-' },
        { label: '设备型号', value: 设备型号 || '-' },
        { label: '硬件版本', value: 硬件版本 || '-' },
      ],
    },
  ];
});

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

<template>
  <NFlex vertical>
    <DeviceHeaderCard :ndm-device="ndmDevice" :station="station" />
    <DeviceCommonCard :common-info="commonInfo" />
    <DeviceHardwareCard :cpu-usage="cpuUsage" :mem-usage="memUsage" />
  </NFlex>
</template>

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