<script setup lang="ts">
import SystemInfoTable from '../SystemInfoTable/SystemInfoTable.vue';
import { ref, toRefs } from 'vue';
import type { PropType } from 'vue';
import { IOtherTab, ICpuBoard } from '../../product.datatype';
import { getBoard } from '../../service/system.service';
import { parseValue } from '../../utils/system.util';

const props = defineProps({
  data: {
    type: Object as PropType<IOtherTab>,
    required: true,
    default: () => {
      return {};
    },
  },
});
const tableId = 'cpuBoard';
const { data } = toRefs(props);
const boardData: IOtherTab = data?.value;
const boardTableData = ref([] as any);
const boardShow = ref(false);

if (boardData.currentNumber && boardData.currentNumber > 0) {
  getBoard(boardData.detailLink).then((res: any) => {
    if (res.data && res.data.length > 0) {
      res.data.forEach((borad: any) => {
        const d = parseData(borad);
        if (d) {
          boardTableData.value.push(d);
        }
      });
    }
    boardShow.value = true; // 异步请求，等请求结束才能显示
  });
} else {
  boardShow.value = true; // 为了显示表头
}

function parseData(board: any): ICpuBoard {
  return {
    boardName: parseValue(board.BoardName),
    manufacturer: parseValue(board.Manufacturer),
    id: parseValue(board.CardNo),
    deviceType: 'CpuBoard',
    pCBVersion: parseValue(board.PCBVersion),
    cPLDVersion: parseValue(board.CPLDVersion),
    boardId: parseValue(board.BoardID),
    power: parseValue(board.PowerWatts),
  };
}

const columns = [
  {
    title: 'COMMON_NAME',
    show: true,
    content: 'boardName',
  },
  {
    title: 'COMMON_MANUFACTURER',
    show: true,
    content: 'manufacturer',
  },
  {
    title: 'OTHER_SLOT_POSITION',
    show: true,
    content: 'id',
  },
  {
    title: 'COMMON_TYPE',
    show: true,
    content: 'deviceType',
  },
  {
    title: 'COMMON_ALARM_PCB_VERSION',
    show: true,
    content: 'pCBVersion',
  },
  {
    title: 'COMMIN_CPLD_VERSION',
    show: true,
    content: 'cPLDVersion',
  },
  {
    title: 'OTHER_BOARD_ID',
    show: true,
    content: 'boardId',
  },
  {
    title: 'OTHER_POWER',
    show: true,
    content: 'power',
  },
];
</script>

<template>
  <SystemInfoTable
    v-if="boardShow"
    :id="tableId"
    :data="boardTableData"
    :columns="columns"
    :num="boardData.maxNumber"
  />
</template>

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