<script setup lang="ts">
import { ref, reactive } from 'vue';
import { traduction } from '@/utils/language';
import Version from './components/version/Version.vue';
import Manual from './components/manual/Manual.vue';
import { getUpgradeInfo } from './upgrade.services';
import { IVersion, IFirmwareVersion, IUpgradeVersion } from '@/model/Manager/upgrade-interface';
import { getStoreData } from '@/utils/composition';
const activeName = ref('0');
const firmwareVersion: IFirmwareVersion = reactive({
  activeBMCVersion: '',
  backupBMCVersion: '',
  availableBMCVersion: '',
});
const upgradeVersion: IUpgradeVersion = reactive({
  availableBMC: '',
  unavailable: false,
});
const versionList: IVersion[] = reactive([]);

const tabList: any[] = [
  {
    id: 'firmwareTabs0',
    index: '0',
    label: 'IBMC_UPGRADE_VERSION',
  },
  {
    id: 'firmwareTabs1',
    index: '1',
    label: 'IBMC_UPGRADE_MANUAL',
  },
];
const language: string = getStoreData('loct', 'locale');
function transformName(name: string) {
  return name
    .replace(/\bCpuBoard(?<idx>\d*)(?<tail>.*?)$/g, 'CPU板$<idx>$<tail>')
    .replace(/\bDiskBP(?<idx>\d*)(?<tail>.*?)$/g, '硬盘背板$<idx>$<tail>')
    .replace(/\bExpBoard(?<idx>\d*)(?<tail>.*?)$/g, '扩展板$<idx>$<tail>')
    .replace(/\bFanBoard(?<idx>\d*)(?<tail>.*?)$/g, '风扇板$<idx>$<tail>')
    .replace(/\bNIC\s*(?<idx>\d*)(?<tail>.*?)$/g, '网络接口控制器$<idx>$<tail>')
    .replace(/\bCSR\b/g, '组件自描述')
    .replace(/\bPSR\b/g, '产品自描述文件')
    .replace(/\bRiser(?<idx>\d*)\s*CSR\b/g, 'Riser卡$<idx> 组件自描述');
}
getUpgradeInfo().then(res => {
  firmwareVersion.activeBMCVersion = res.data.FirmwareVersion.ActiveBMCVersion;
  firmwareVersion.backupBMCVersion = res.data.FirmwareVersion.BackupBMCVersion;
  firmwareVersion.availableBMCVersion = res.data.FirmwareVersion.AvailableBMCVersion;
  if (firmwareVersion.availableBMCVersion) {
    upgradeVersion.availableBMC = firmwareVersion.availableBMCVersion;
    upgradeVersion.unavailable = true;
  } else {
    upgradeVersion.availableBMC = '';
    upgradeVersion.unavailable = false;
  }
  // 版本列表数据
  for (const item of res.data.UpgradeResource) {
    const versions: IVersion = {
      name: language === 'zh' ? transformName(item.Name) || item.Name : item.Name,
      currentVersion: item.Version || '--',
      type: item.Type,
    };
    versionList.push(versions);
  }
});

function handleSelect(val: string) {
  activeName.value = val;
}
</script>
<template>
  <RightTabs
    :id="'firmwareTabs'"
    :check-tab-index="activeName"
    :tab-list="tabList"
    @handle-select="handleSelect"
  />
  <Version
    v-show="activeName === '0'"
    :firmware-version="firmwareVersion"
    :upgrade-version="upgradeVersion"
    :version-list="versionList"
  />
  <Manual v-show="activeName === '1'" />
</template>

<style lang="scss" scoped>
.page-container {
  background-color: var(--o-bg-color-base);
  padding: 16px 24px 24px;
  margin-right: 24px;
  border-radius: 0 4px 4px 4px;
}
</style>
