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

const props = defineProps({
  data: {
    type: Object as PropType<IOtherTab>,
    required: true,
    default: () => {
      return {};
    },
  },
});
const tableId = 'mezzTable';
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): IMezzCard {
  return {
    productName: isOtherEmptyProperty(board.ProductName) || ' ',
    description: isOtherEmptyProperty(board.Description) || ' ',
    cardNo: isOtherEmptyProperty(board.CardNo) || 0,
    pCBVersion: isOtherEmptyProperty(board.PCBVersion) || ' ',
    boardId: isOtherEmptyProperty(board.BoardID) || ' ',
    associatedResource: isOtherEmptyProperty(board.AssociatedResource) || ' ',
    linkWidth: isOtherEmptyProperty(board.LinkWidth),
    linkSpeed: isOtherEmptyProperty(board.LinkSpeed),
    linkWidthAbility: isOtherEmptyProperty(board.LinkWidthAbility),
    linkSpeedAbility: isOtherEmptyProperty(board.LinkSpeedAbility),
    partNumber: isOtherEmptyProperty(board.PartNumber) || ' ',
    serialNumber: isOtherEmptyProperty(board.SerialNumber) || ' ',
  };
}

const columns = [
  {
    title: 'COMMON_NAME',
    width: '6%',
    show: true,
    content: 'productName',
  },
  {
    title: 'COMMON_DESCRIPTION',
    width: '10%',
    show: true,
    content: 'description',
  },
  {
    title: 'OTHER_SLOT_POSITION',
    width: '6%',
    show: true,
    content: 'cardNo',
  },
  {
    title: 'COMMON_ALARM_PCB_VERSION',
    width: '6%',
    show: true,
    content: 'pCBVersion',
  },
  {
    title: 'OTHER_BOARD_ID',
    width: '6%',
    show: true,
    content: 'boardId',
  },
  {
    title: 'COMMON_RESOURCE_OWNER',
    width: '6%',
    show: true,
    content: 'associatedResource',
  },
  {
    title: 'OTHER_CURRENT_BROAD_BAND',
    width: '10%',
    show: true,
    content: 'linkWidth',
  },
  {
    title: 'OTHER_CURRENT_RATE',
    width: '10%',
    show: true,
    content: 'linkSpeed',
  },
  {
    title: 'OTHER_BOARD_SPECIFICATIONS',
    width: '10%',
    show: true,
    content: 'linkWidthAbility',
  },
  {
    title: 'OTHER_RATE_SPECIFICATIONS',
    width: '10%',
    show: true,
    content: 'linkSpeedAbility',
  },
  {
    title: 'OTHER_PART_NUMBER',
    width: '10%',
    show: true,
    content: 'partNumber',
  },
  {
    title: 'COMMON_SERIALNUMBER',
    width: '10%',
    show: true,
    content: 'serialNumber',
  },
];
</script>

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

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