<script setup lang="ts">
import SystemInfoTable from '../SystemInfoTable/SystemInfoTable.vue';
import { ref, toRefs } from 'vue';
import type { PropType } from 'vue';
import { IOtherTab, ISecurity } 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 = 'securityTable';
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): ISecurity {
  let selfTestResult = '';
  if (board.SelfTestResult === null || board.SelfTestResult === undefined) {
    selfTestResult = '--';
  } else {
    selfTestResult =
      board.SelfTestResult === 'OK' ? i18n.t('COMMON_SUCCESS') : i18n.t('COMMON_FAILED');
  }
  return {
    protocolType: isOtherEmptyProperty(board.ProtocolType) || ' ',
    protocolVersion: isOtherEmptyProperty(board.ProtocolVersion) || ' ',
    manufacturer: isOtherEmptyProperty(board.Manufacturer) || ' ',
    firmwareVersion: isOtherEmptyProperty(board.FirmwareVersion) || ' ',
    selfTestResult,
  };
}

const columns = [
  {
    title: 'OTHER_PROTOCOL_TYPE',
    show: true,
    content: 'protocolType',
  },
  {
    title: 'OTHER_PROTOCOL_VERSION',
    show: true,
    content: 'protocolVersion',
  },
  {
    title: 'COMMON_MANUFACTURER',
    show: true,
    content: 'manufacturer',
  },
  {
    title: 'OTHER_VENDOR_VERSION',
    show: true,
    content: 'firmwareVersion',
  },
  {
    title: 'OTHER_SELFCHECK',
    show: true,
    content: 'selfTestResult',
  },
];
</script>

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

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