<template>
  <div class="outer-person-container">
    <div class="container-title">
      <span class="line"></span>
      <span class="title">管理规定与通知</span>
    </div>
    <div class="tab-content">
      <el-tabs v-model="activeName">
        <el-tab-pane label="保卫" name="security">
          <div class="item-content">
            <div class="regulation-item" v-for="item in securityRegulationData" :key="item.id">
              <div class="round" @click="handleItemRedirect(item)"> </div>
              <div class="item-name" @click="handleItemRedirect(item)">{{ item.name }}</div>
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane label="保密" name="confidentiality">
          <div class="item-content">
            <div class="regulation-item" v-for="item in confidentialityRegulationData" :key="item.id">
              <div class="round" @click="handleItemRedirect(item)"> </div>
              <div class="item-name" @click="handleItemRedirect(item)">{{ item.name }}</div>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
    <div class="more">
      <span style="padding-right: 5px; cursor: pointer" @click="handleMoreRedirect">更多</span>
      <img
        style="cursor: pointer"
        @click="handleMoreRedirect"
        src="@/assets/images/security-confidientiality/overview-more.png"
        alt=""
      />
    </div>
  </div>
</template>

<script setup lang="ts">
  import { onUnmounted, ref, watch } from 'vue';
  import { useRouter } from 'vue-router';
  import type { RegulationItem } from '../types';
  import { getSecurityOverview, getConfidentialityOverview } from '@/api/security-confidentiality-overview';

  const router = useRouter();
  const activeName = ref(sessionStorage.getItem('security-confidentiality-overview-active') || 'security');

  const securityRegulationData = ref<RegulationItem[]>([]);
  const confidentialityRegulationData = ref<RegulationItem[]>([]);

  async function getSecurityRegulationData() {
    securityRegulationData.value = await getSecurityOverview();
  }

  async function getConfidentialityRegulationData() {
    confidentialityRegulationData.value = await getConfidentialityOverview();
  }

  onUnmounted(() => {
    sessionStorage.setItem('security-confidentiality-overview-active', activeName.value);
  });

  watch(
    () => activeName.value,
    (val) => {
      if (val === 'security') {
        getSecurityRegulationData();
      } else if (val === 'confidentiality') {
        getConfidentialityRegulationData();
      }
    },
    {
      immediate: true,
    },
  );

  function handleItemRedirect(item: RegulationItem) {
    sessionStorage.setItem(`${activeName.value}-regulation-active`, 'regulation');
    if (item.managementType === 1) {
      router.push({
        path: `/security-confidentiality/${activeName.value}-regulation-notice`,
      });
    } else if (item.managementType === 2) {
      router.push({
        path: `/security-confidentiality/${activeName.value}-regulation-notice-item`,
        query: { id: item.id, operate: 'notice-view' },
      });
    }
  }
  function handleMoreRedirect() {
    sessionStorage.setItem(`${activeName.value}-regulation-active`, 'regulation');
    router.push({
      path: `/security-confidentiality/${activeName.value}-regulation-notice`,
    });
  }
</script>

<style scoped lang="scss">
  .container-title {
    height: 24px;
    display: flex;
    align-items: center;
    font-weight: 500;
    font-size: 16px;
    color: #000000;

    .line {
      width: 3px;
      height: 16px;
      background: #1777ff;
      margin-right: 10px;
    }

    .title {
      margin-right: 12px;
    }
  }

  .outer-person-container {
    width: 100%;
    height: 100%;
    padding-top: 14px;
  }
  .tab-content {
    height: calc(100% - 50px);
    padding: 0 10px;
  }
  .regulation-item {
    padding: 7px 0;
    display: flex;
    align-items: center;
  }
  .round {
    width: 6px;
    height: 6px;
    background: #1777ff;
    border-radius: 50%;
    margin-right: 6px;
    cursor: pointer;
  }
  .item-name {
    max-width: calc(100% - 12px);
    color: rgba($color: #000000, $alpha: 0.85);
    font-size: 12px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
  }
  .more {
    text-align: end;
    color: #007bff;
    font-size: 12px;
    padding: 0 10px;
  }

  :deep(.el-tabs__header) {
    margin: 0;
  }
  :deep(.el-tabs__item) {
    font-size: 14px !important;
  }
</style>
