<script setup lang="ts">
import SystemInfoTable from '../SystemInfoTable/SystemInfoTable.vue';
import { ref, toRefs } from 'vue';
import type { PropType } from 'vue';
import { IOtherTab, IRaidCard } 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 = 'raidTable';
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((board: any) => {
        const d = parseData(board);
        if (d) {
          boardTableData.value.push(d);
        }
      });
    }
    boardShow.value = true; // 异步请求，等请求结束才能显示
  });
} else {
  boardShow.value = true; // 为了显示表头
}

function parseData(board: any): IRaidCard {
  return {
    productName: isOtherEmptyProperty(board.ProductName) || ' ',
    location: isOtherEmptyProperty(board.Location) || ' ',
    manufacturer: isOtherEmptyProperty(board.Manufacturer) || ' ',
    cardNo: isOtherEmptyProperty(board.CardNo) || 0,
    description: isOtherEmptyProperty(board.Description) || ' ',
    pCBVersion: isOtherEmptyProperty(board.PCBVersion) || ' ',
    cPLDVersion: isOtherEmptyProperty(board.CPLDVersion) || ' ',
    partNumber: isOtherEmptyProperty(board.PartNumber) || ' ',
    serialNumber: isOtherEmptyProperty(board.SerialNumber) || ' ',
    boardId: isOtherEmptyProperty(board.BoardID) || 0,
    associatedResource: isOtherEmptyProperty(board.AssociatedResource) || ' ',
  };
}

const columns = [
  {
    title: 'COMMON_NAME',
    width: '10%',
    show: true,
    content: 'productName',
  },
  {
    title: 'OTHER_POSITION',
    width: '8%',
    show: true,
    content: 'location',
  },
  {
    title: 'COMMON_MANUFACTURER',
    width: '8%',
    show: true,
    content: 'manufacturer',
  },
  {
    title: 'COMMON_NUMBER',
    width: '5%',
    show: true,
    content: 'cardNo',
  },
  {
    title: 'COMMON_TYPE',
    width: '10%',
    show: true,
    content: 'description',
  },
  {
    title: 'COMMON_ALARM_PCB_VERSION',
    width: '7%',
    show: true,
    content: 'pCBVersion',
  },
  {
    title: 'COMMIN_CPLD_VERSION',
    width: '9%',
    show: true,
    content: 'cPLDVersion',
  },
  {
    title: 'OTHER_PART_NUMBER',
    width: '10%',
    show: true,
    content: 'partNumber',
  },
  {
    title: 'COMMON_SERIALNUMBER',
    width: '15%',
    show: true,
    content: 'serialNumber',
  },
  {
    title: 'OTHER_BOARD_ID',
    width: '8%',
    show: true,
    content: 'boardId',
  },
  {
    title: 'COMMON_RESOURCE_OWNER',
    width: '10%',
    show: true,
    content: 'associatedResource',
  },
];
</script>

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

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