<script setup lang="ts">
import { deepClone } from '@/utils/utils';
import { traduction } from '@/utils/language';
import { PropType, reactive, watch } from 'vue';
import {
  haveDataJudgments,
  dealRate,
  initShowData,
  initInterfaceType,
} from '@/pages/System/Info/InfoPages/Net/netFunctions';
import {
  ICartData,
  ILabelValue,
  IFields,
  IColumns,
  IBasicAttribute,
} from '@/model/System/net-interface';

import DataList from '@/components/DataList/DataList.vue';
import CanOpenRowTable from '@/pages/System/Info/InfoPages/Net/NetPages/components/CanOpenRowTable.vue';

const props = defineProps({
  showData: {
    type: Object as PropType<ICartData>,
    default: () => {
      return {};
    },
    requird: true,
  },
});

watch(
  () => props.showData,
  () => {
    initData();
  },
  { deep: true },
);

const nicDivieceData: ILabelValue[] = reactive([]);
let tableData: IBasicAttribute[] = reactive([]);

const columns: IColumns[] = [
  {
    title: traduction('SERVICE_PORT'),
    key: 'Name',
  },
  {
    title: 'FC_ID',
    key: 'FC_ID',
  },
  {
    title: traduction('NET_PORT_TYPE'),
    key: 'InterfaceType',
  },
  {
    title: traduction('EVENT_STATUS'),
    key: 'LinkStatus',
  },
];

const nicFields: IFields[] = [
  {
    key: 'CardName',
    lable: traduction('NET_NAME'),
  },
  {
    key: 'CardModel',
    lable: traduction('NET_TYPE'),
  },
  {
    key: 'ChipModel',
    lable: traduction('NET_CHIP_MODEL'),
  },
  {
    key: 'CardManufacturer',
    lable: traduction('COMMON_MANUFACTURER'),
  },
  {
    key: 'ChipManufacturer',
    lable: traduction('NET_CHIP_VENDOR'),
  },
  {
    key: 'BoardID',
    lable: traduction('OTHER_BOARD_ID'),
  },
  {
    key: 'PartNumber',
    lable: traduction('OTHER_PART_NUMBER'),
  },
  {
    key: 'PCBVersion',
    lable: traduction('COMMON_ALARM_PCB_VERSION'),
  },
  {
    key: 'AssociatedResource',
    lable: traduction('COMMON_RESOURCE_OWNER'),
  },
  {
    key: 'SerialNumber',
    lable: traduction('COMMON_SERIALNUMBER'),
  },
  {
    key: 'PcieSlotId',
    lable: traduction('PCIE_SLOTID'),
  },
];

const openRowFields: IFields[] = [
  {
    key: 'WWPN',
    lable: 'WWPN',
  },
  {
    key: 'TxBbCredit',
    lable: traduction('NET_RIGHT_CERITICAL'),
  },
  {
    key: 'RxBbCredit',
    lable: traduction('NET_LOCAL_CERITICAL'),
  },
  {
    key: 'TXRateGbps',
    lable: traduction('NET_SEND_RATE'),
    unit: 'Gbps',
  },
  {
    key: 'RXRateGbps',
    lable: traduction('NET_RECIVE_RATE'),
    unit: 'Gbps',
  },
  {
    key: 'RateNegotiationStage',
    lable: traduction('NET_RATE_LEVEL'),
    fun: dealRate,
  },
  {
    key: 'PortStatus',
    lable: traduction('NET_CONNENT_STATUS'),
    judgments: [traduction('STORE_ENABLED'), traduction('DISABLED')],
  },
  {
    key: 'WorkingRate',
    lable: traduction('NET_WORK_RATE'),
    unit: 'Gbps',
  },
  {
    key: 'LinkSpeedGbps',
    lable: traduction('NET_SPEED_RATE'),
    unit: 'Gbps',
  },
  {
    key: 'WWNN',
    lable: 'WWNN',
  },
  {
    key: 'FirmwareVersion',
    lable: traduction('COMMON_FIXED_VERSION'),
  },
  {
    key: 'DriverName',
    lable: traduction('NET_DRIVER_NAME'),
  },
  {
    key: 'DriverVersion',
    lable: traduction('NET_DRIVER_VERSION'),
  },
  {
    key: 'WorkMode',
    lable: traduction('COMMON_WORKING_MODE'),
  },
];

// 初始化数据
function initData(): void {
  nicDivieceData.splice(0);
  tableData.splice(0);
  const data = deepClone(props.showData);
  // 网卡设备
  initShowData(data, nicFields, nicDivieceData);

  // 端口属性
  if (data.PortProperties && data.PortProperties.length > 0) {
    data.PortProperties.forEach((item: IBasicAttribute) => {
      if (item.BasicAttribute) {
        item.BasicAttribute.Name = haveDataJudgments(item.BasicAttribute.Name);
        item.BasicAttribute.FC_ID = haveDataJudgments(item.BasicAttribute.FC_ID);
        item.BasicAttribute.InterfaceType = initInterfaceType(item.BasicAttribute.InterfaceType);
        item.BasicAttribute.LinkStatus = haveDataJudgments(item.BasicAttribute.LinkStatus);
      }

      if (item.NetworkAttribute) {
        let arr: ILabelValue[] = reactive([]);
        initShowData(item.NetworkAttribute, openRowFields, arr);
        item.NetworkAttribute.dataList = arr;
      }

      tableData.push({ ...item.BasicAttribute, extendData: item });
    });
  }
}

initData();
</script>

<template>
  <div class="card-box">
    <div class="n-i-c-device">
      <div class="left">
        <img src="@/assets/common/image/network_card.svg" class="img" />
        <div class="part-title">{{ $t('NET_FC_DEVICE') }}</div>
      </div>

      <div class="right-key-value-box">
        <DataList id="fcCardAttr" :data-list="nicDivieceData" />
      </div>
    </div>

    <div v-if="tableData.length" class="port-attributes">
      <div class="title">
        {{ $t('OTHER_PORT_ATTR') }}
      </div>

      <div class="table-box">
        <CanOpenRowTable :columns="columns" :table-data="tableData" :open-row-type="'normal'" />
      </div>
    </div>
  </div>
</template>

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