<script setup lang="ts">
import { deepClone } from '@/utils/utils';
import { traduction } from '@/utils/language';
import { PropType, reactive, watch, Ref, ref } 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';
import { getNetDataPort } from '@/pages/System/Info/InfoPages/Net/net.server';

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

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

const nicDivieceData: ILabelValue[] = reactive([]);
const tableData: Ref<any[]> = ref([]);
let openRow = reactive([] as any);

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'),
  },
  {
    key: 'Presence',
    lable: traduction('STORE_IN_POSITION'),
  },
  {
    key: 'Position',
    lable: traduction('MEMORY_POSITION'),
  },
];

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.value.splice(0);
  const data = deepClone(props.showData);
  if (data.Presence === 0 || data.Presence) {
    data.Presence = data.Presence === 0 ? traduction('COMMON_NO') : traduction('COMMON_YES');
  }
  // 网卡设备
  initShowData(data, nicFields, nicDivieceData);

  // 端口属性
  if (data.PortProperties && data.PortProperties.length > 0) {
    data.PortProperties.forEach((item: IBasicAttribute) => {
      item.Name = haveDataJudgments(item.Name);
      item.FC_ID = haveDataJudgments(item.FC_ID);
      item.InterfaceType = initInterfaceType(item.InterfaceType);
      item.LinkStatus = haveDataJudgments(item.LinkStatus);
      const checkItem = openRow.find((i: any) => i.PhysicalPortNumber === item.PhysicalPortNumber);
      if (checkItem) {
          tableData.value.push({ ...checkItem, ...item, adapterId: data.adapterId});
        } else {
          tableData.value.push({ ...item, adapterId: data.adapterId, extendData: { showExpendData: {} } });
        }
    });
  }
  resetPort(data.adapterId);
}

function expandChange(obj: any) {
  openRow = JSON.parse(JSON.stringify(obj.expandedRows));
  const checkItem = openRow.find((item: any) => item.PhysicalPortNumber === obj.row.PhysicalPortNumber);
  if (checkItem) {
    getPortData(
      checkItem.adapterId,
      obj.row.PhysicalPortNumber,
      obj.row,
      obj
    );
  }
}

function getPortData(id: string, port: number, row: any, obj?: any) {
  getNetDataPort(id, port)
    .then((res: any) => {
      const checkIndex = tableData.value.findIndex(
        (item: any) => item.PhysicalPortNumber === row.PhysicalPortNumber
      );
      if (checkIndex > -1) {
        tableData.value[checkIndex].extendData = JSON.parse(JSON.stringify(res));
        if (tableData.value[checkIndex].extendData?.NetworkAttribute) {
          let arr: ILabelValue[] = reactive([]);
          initShowData(tableData.value[checkIndex].extendData.NetworkAttribute, openRowFields, arr);
          tableData.value[checkIndex].extendData.NetworkAttribute.dataList = arr;
        }
      }
    })
    .finally(() => {
      if (obj) {
        openRow = JSON.parse(JSON.stringify(obj.expandedRows));
      }
    });
}

function resetPort(adapterId: string) {
  openRow.forEach((element: any) => {
    getPortData(
      adapterId,
      element.PhysicalPortNumber,
      element
    );
  });
}

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'" @expand-change="expandChange"/>
      </div>
    </div>
  </div>
</template>

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