<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 NetworkPortSelect from './components/NetworkPortSelect.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 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 = ref([]);
let activeTab = 'tab1';

// 设置网口模式、网络协议、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) {
  networkData.value = [];
  const showData = initNetworkData(data);
  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.value = networkData.value.concat(showData.networkData);
  multiSupported.value = networkData.value[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) {
  networkData.value[0].networkPort.hostName = value;
}
// 获取数据
getData();
function handleClick(tab: any) {
  getData();
}
</script>

<template>
  <div id="networkViewId" class="network-box">
    <!-- 主要内容区域 -->
    <div class="content-details">
      <!-- 主机名 -->
      <Hostname
        :data="networkData[0]?.networkPort"
        :permission="permission"
        :lock="isSystemLock"
        @set-host-name="setHostName"
      />
      <div v-if="multiSupported" class="net-configs">
        <p class="part-title">{{ $t('IBMC_NETWORK_INTERFACE') }}</p>
        <el-tabs v-model="activeTab" class="tabs" @tab-click="handleClick">
          <el-tab-pane
            v-for="(tab, index) in networkData"
            :key="index"
            :label="`${$t('IBMC_NETWORK_GROUP')}${index + 1}`"
            :name="`tab${index + 1}`"
          >
            <div class="net-tip">
              <el-alert :title="$t('IBMC_NETWORK_TIP')" class="ml-4" type="info" show-icon />
            </div>
            <!-- 网口选择 -->
            <NetworkPortSelect
              :permission="permission"
              :lock="isSystemLock"
              :data="tab.ManagementNetworkPorts"
              :address="tab?.MACAddress"
              :multi-supported="multiSupported"
              @get-data="getData"
            />

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

            <!-- 网络协议 -->
            <NetworkProtocols
              :data="tab.networkProtocol"
              :permission="permission"
              :lock="isSystemLock"
              :address="tab?.MACAddress"
              :multi-supported="multiSupported"
              @init-show-data="initShowData"
            />
          </el-tab-pane>
        </el-tabs>
      </div>
      <div v-else>
        <!-- 网口模式 -->
        <NetworkPortMode
          v-if="networkData[0]?.networkPort.productType"
          :permission="permission"
          :lock="isSystemLock"
          :data="networkData[0]?.networkPort"
          @init-show-data="initShowData"
        />

        <!-- 网络协议 -->
        <NetworkProtocols
          :data="networkProtocol"
          :permission="permission"
          :lock="isSystemLock"
          @init-show-data="initShowData"
        />
      </div>

      <!-- DNS -->
      <Dns
        :data="networkDns"
        :permission="permission"
        :lock="isSystemLock"
        @init-show-data="initShowData"
      />
      <!-- Ncsi VLAN -->
      <Vlan
        v-if="networkVlan.productTypeVlan"
        :permission="permission"
        :lock="isSystemLock"
        :data="networkVlan"
        :is-n-c-s-i="true"
        @init-show-data="initShowData"
      />

      <!-- Dedicated VLAN -->
      <Vlan
        v-if="dedicatedVlan.productTypeVlan"
        :permission="permission"
        :lock="isSystemLock"
        :data="dedicatedVlan"
        :is-n-c-s-i="false"
        @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);
    .part-title {
      font-size: 16px;
      font-weight: bold;
      color: var(--o-text-color-primary);
      margin-bottom: 16px;
    }
    .tabs {
      :deep(.el-tabs__content) {
        overflow: visible; // 修改overflow属性, 展示ip地址
      }
    }
    .net-tip {
      margin-top: 16px;
    }
    .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>
