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

const i18n = useI18n();
const props = defineProps({
  data: {
    type: Object as PropType<IOtherTab>,
    required: true,
    default: () => {
      return {};
    },
  },
});
const tableId = 'cicTable';
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): ICicBoard {
  const extendTitleArr: IExtendTitle[] = [
    {
      title: 'OTHER_EXTEND_INFO',
      data: [
        {
          label: 'COMMON_M2_DISK1',
          content: 'm2Device1Presence',
          hide: board.M2Device1Presence === undefined,
        },
        {
          label: 'COMMON_M2_DISK2',
          content: 'm2Device2Presence',
          hide: board.M2Device2Presence === undefined,
        },
      ],
    },
  ];
  return {
    boardName: isOtherEmptyProperty(board.BoardName),
    manufacturer: isOtherEmptyProperty(board.Manufacturer),
    pcbVer: isOtherEmptyProperty(board.PcbVer),
    boardId: isOtherEmptyProperty(board.BoardId) || 0,
    description: isOtherEmptyProperty(board.Description),
    serialNumber: isOtherEmptyProperty(board.SerialNumber),
    partNum: isOtherEmptyProperty(board.PartNum),
    m2Device1Presence:
      isOtherEmptyProperty(board.M2Device1Presence) === 0
        ? i18n.t('STORE_NO_IN_POSITION')
        : i18n.t('STORE_IN_POSITION'),
    m2Device2Presence:
      isOtherEmptyProperty(board.M2Device2Presence) === 0
        ? i18n.t('STORE_NO_IN_POSITION')
        : i18n.t('STORE_IN_POSITION'),
    extendTitleArr,
  };
}

const columns = [
  {
    title: 'OTHER_BOARDNAME',
    width: '10%',
    show: true,
    content: 'boardName',
  },
  {
    title: 'COMMON_MANUFACTURER',
    width: '5%',
    show: true,
    content: 'manufacturer',
  },
  {
    title: 'COMMON_ALARM_PCB_VERSION',
    width: '8%',
    show: true,
    content: 'pcbVer',
  },
  {
    title: 'OTHER_BOARD_ID',
    width: '8%',
    show: true,
    content: 'boardId',
  },
  {
    title: 'COMMON_DESCRIPTION',
    width: '10%',
    show: true,
    content: 'description',
  },
  {
    title: 'COMMON_SERIALNUMBER',
    width: '8%',
    show: true,
    content: 'serialNumber',
  },
  {
    title: 'OTHER_PART_NUMBER',
    width: '12%',
    show: true,
    content: 'partNum',
  },
];
</script>

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

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