<script lang="ts" setup>
import useStore from '@/stores';
import { reactive, computed, nextTick, ref } from 'vue';
import { traduction } from '@/utils/language';
import { showElMessage } from '@/utils/utils';
import { getNetWorkData } from './network.server';
import { initNetworkData } from './networkFunctions/networkDataFactory';
import { UserPrivil } from '@/model/base-enum';
import { checkPrivil } from '@/apps/app-bmc/router/routes';

import Dns from './components/Dns.vue';
import Vlan from './components/Vlan.vue';
import Lldp from './components/Lldp.vue';
import Hostname from './components/Hostname.vue';
import IpInput from '@/components/IpInput/IpInput.vue';
import NetworkPortMode from './components/NetworkPortMode.vue';
import NetworkProtocols from './components/NetworkProtocols.vue';
import GlobalStoreService from '@/services/gloabal-store-service';

const globalStoreService = new GlobalStoreService();
const errorMsg = traduction('COMMON_FAILED');

// 权限
const permission = checkPrivil(UserPrivil.baseConfig);
const store = useStore();
const isSystemLock = computed(() => {
  return store.state.glob.systemLocked;
});

const networkPort: any = reactive({});
const networkProtocol: any = reactive({});
const networkDns: any = reactive({});
const networkVlan: any = reactive({});
const dedicatedVlan: any = reactive({});
const networkLldp: any = reactive({});
const multiSupported = ref(false);
let networkData: any = reactive([]);
const networkPortMap: any = {
  Dedicated: traduction('IBMC_DEDICATED_NETWORK'),
  Aggregation: traduction('IBMC_DEDICATED_NETWORK01'),
  LOM: traduction('IBMC_DEDICATED_NETWORK02'),
  FlexIO: traduction('IBMC_DEDICATED_NETWORK03'),
  ExternalPCIe: traduction('IBMC_DEDICATED_NETWORK04'),
  OCP: traduction('IBMC_DEDICATED_NETWORK05'),
  OCP2: traduction('IBMC_DEDICATED_NETWORK06'),
};

// 设置网口模式、网络协议、DNS、VLAN、LLDP的label的宽度相同，取其中的最大值
function initLabelWidth() {
  const list: any = document.querySelectorAll('.network-page-label');
  const resDomList: any = [];
  let resWidth = 0;
  list.forEach((item: any) => {
    if (
      item.classList.contains('el-form-item') &&
      item.children &&
      item.children[0] &&
      item.children[0].classList.contains('el-form-item__label-wrap')
    ) {
      resWidth = Math.max(item.children[0].clientWidth, resWidth);
      resDomList.push(item.children[0]);
    } else {
      resWidth = Math.max(item.clientWidth, resWidth);
      resDomList.push(item);
    }
  });

  resDomList.forEach((item: any) => {
    item.style.width = resWidth + 'px';
  });
}

// 初始化展示数据
function initShowData(data: any) {
  const showData = initNetworkData(data);
  Object.assign(networkPort, showData.networkPort);
  Object.assign(networkProtocol, showData.networkProtocol);
  Object.assign(networkDns, showData.networkDns);
  Object.assign(networkVlan, showData.networkVlan);
  Object.assign(dedicatedVlan, showData.dedicatedVlan);
  Object.assign(networkLldp, showData.networkLldp);
  networkData = networkData.concat(showData.networkData);
  multiSupported.value = networkData[0].multiSupported;
}

// 获取数据
function getData() {
  globalStoreService.setValue('isLoading', true);
  getNetWorkData()
    .then((data: any) => {
      if (data) {
        initShowData(data);
      }
      nextTick(() => {
        initLabelWidth();
      });
      globalStoreService.setValue('isLoading', false);
    })
    .catch(() => {
      globalStoreService.setValue('isLoading', false);
    });
}

// 修改主机名成功
function setHostName(value: string) {
  networkPort.hostName = value;
}
// 获取数据
getData();
</script>

<template>
  <div id="networkViewId" class="network-box">
    <!-- 主要内容区域 -->
    <div class="content-details">
      <!-- 主机名 -->
      <Hostname
        :data="networkPort"
        :permission="permission"
        :lock="isSystemLock"
        @set-host-name="setHostName"
      />

      <!-- 网口模式 -->
      <NetworkPortMode
        v-if="networkPort.productType"
        :permission="permission"
        :lock="isSystemLock"
        :data="networkPort"
        :multi-supported="multiSupported"
        @init-show-data="initShowData"
      />

      <!-- 网络协议 -->
      <div v-for="(item, index) in networkData" :key="index" class="net-configs">
        <div class="net-config">
          <span class="net-title">{{ networkPortMap[item?.title?.Type] }}</span>
          <el-tag class="ml-8" effect="dark" type="info" :round="true">
            {{ item?.title?.PortName }}
          </el-tag>
        </div>
        <NetworkProtocols
          :data="item?.networkProtocol"
          :permission="permission"
          :lock="isSystemLock"
          :address="item?.MACAddress"
          :multi-supported="multiSupported"
          @init-show-data="initShowData"
        />
        <!-- Vlan配置 -->
        <Vlan
          v-if="multiSupported && item?.networkVlan.productTypeVlan"
          :permission="permission"
          :lock="isSystemLock"
          :data="item?.networkVlan"
          :address="item?.MACAddress"
          :multi-supported="multiSupported"
          :net-type="item?.title?.Type"
          @init-show-data="initShowData"
        />
              <!-- Ncsi VLAN -->
        <Vlan
          v-if="!multiSupported && networkVlan.productTypeVlan"
          :permission="permission"
          :lock="isSystemLock"
          :data="networkVlan"
          :is-n-c-s-i="true"
          @init-show-data="initShowData"
        />

        <!-- Dedicated VLAN -->
        <Vlan
          v-if="!multiSupported && dedicatedVlan.productTypeVlan"
          :permission="permission"
          :lock="isSystemLock"
          :data="dedicatedVlan"
          :is-n-c-s-i="false"
          @init-show-data="initShowData"
        />
      </div>

      <!-- DNS -->
      <Dns
        :data="networkDns"
        :permission="permission"
        :lock="isSystemLock"
        @init-show-data="initShowData"
      />

      <!-- LLDP -->
      <Lldp
        v-if="networkLldp.lldpSupport"
        :permission="permission"
        :lock="isSystemLock"
        :data="networkLldp"
        @init-show-data="initShowData"
      />
    </div>
  </div>
</template>

<style lang="scss" scoped>
.network-box {
  padding-right: 24px;

  :deep(.el-radio.el-radio--large .el-radio__label) {
    font-size: 12px;
  }

  :deep(.el-popper) {
    max-width: 250px !important;
    min-width: auto !important;
    width: auto !important;
  }

  :deep(.el-form-item) {
    .el-form-item__label {
      position: relative;

      &::before {
        position: absolute;
        left: -6px;
      }
    }
  }

  :deep(.network-page-label) {
    margin-right: 0 !important;

    .el-form-item__label-wrap {
      margin-right: 0 !important;
    }
  }
  .net-configs {
    margin-top: 8px;
    padding: 24px;
    background-color: var(--o-bg-color-base);
    .ml-8 {
      margin-left: 8px;
    }
    .net-config {
      display: flex;
      align-items: center;
      margin-bottom: 16px;
      .net-title {
        font-size: 16px;
        line-height: 24px;
        font-weight: medium;
        color: #000000;
      }
      .el-tag.is-round {
        border-radius: 20px !important;
      }
    }
  }
}
</style>
