<script setup lang="ts">
import SystemInfoTable from '../SystemInfoTable/SystemInfoTable.vue';
import { ref, toRefs } from 'vue';
import type { PropType } from 'vue';
import { IOtherTab, IBackupBatteryUnits } 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 = 'bbuTable';
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): IBackupBatteryUnits {
  return {
    boardName: isOtherEmptyProperty(board.BoardName) || ' ',
    manufacturer: isOtherEmptyProperty(board.Manufacturer) || ' ',
    id: board.Id,
    serialNumber: isOtherEmptyProperty(board.SerialNumber) || ' ',
    firmwareVersion: isOtherEmptyProperty(board.FirmwareVersion) || ' ',
    workingState: isOtherEmptyProperty(board.WorkingState) || 0,
    capacity: isOtherEmptyProperty(board.Battery.RemainingPowerMilliWatts) || ' ',
    batteryModel: isOtherEmptyProperty(board.Battery.Model) || ' ',
    inPosition:
      (board.M2Device1Presence ? 'Presence' : 'Absence') +
      '/' +
      (board.M2Device2Presence ? 'Presence' : 'Absence'),
  };
}

const columns = [
  {
    title: 'COMMON_NAME',
    width: '10%',
    show: true,
    content: 'boardName',
  },
  {
    title: 'OTHER_ID',
    width: '5%',
    show: true,
    content: 'id',
  },
  {
    title: 'COMMON_FIXED_VERSION',
    width: '8%',
    show: true,
    content: 'firmwareVersion',
  },
  {
    title: 'OTHER_WORK_STATUS',
    width: '8%',
    show: true,
    content: 'workingState',
  },
  {
    title: 'OTHER_CAPATICY',
    width: '10%',
    show: true,
    content: 'capacity',
  },
  {
    title: 'OTHER_BATTERY_MODEL',
    width: '8%',
    show: true,
    content: 'batteryModel',
  },
  {
    title: 'COMMON_SERIALNUMBER',
    width: '12%',
    show: true,
    content: 'serialNumber',
  },
  {
    title: 'COMMON_MANUFACTURER',
    width: '10%',
    show: true,
    content: 'manufacturer',
  },
  {
    title: 'OTHER_M2_INPOSITION_1_2',
    width: '10%',
    show: true,
    content: 'inPosition',
  },
];
</script>

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

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