<template>
  <header class="screen-nav">
    <div class="screen-nav-left">
      <div
        class="screen-nav-left-item"
        v-for="item in leftNav"
        :key="item.name"
        @click="goNav(item)"
        :class="{ active: isActive(item) }"
      >
        <div class="screen-nav-item-zh">
          {{ item.zh }}
        </div>
        <div class="screen-nav-item-bo">{{ item.bo }}</div>
      </div>
    </div>
    <div class="screen-nav-year" @click.stop>
      <label class="screen-nav-year__label">统计年份</label>
      <select v-model="yearModel" class="screen-nav-year__select" aria-label="统计年份">
        <option v-for="y in availableYears" :key="y" :value="String(y)">{{ y }}年</option>
      </select>
    </div>
    <div class="screen-nav-right">
      <div
        class="screen-nav-right-item"
        v-for="item in rightNav"
        :key="item.name"
        @click="goNav(item)"
        :class="{ active: isActive(item) }"
      >
        <div class="screen-nav-item-zh">
          {{ item.zh }}
        </div>
        <div class="screen-nav-item-bo">
          {{ item.bo }}
        </div>
      </div>
    </div>
    <div class="logout" @click.stop="onLogout">
      <div class="screen-nav-item-zh">
        {{ logoutNav.zh }}
      </div>
      <div class="screen-nav-item-bo">
        {{ logoutNav.bo }}
      </div>
    </div>
  </header>
</template>

<script setup>
import { computed } from "vue";
import { useRoute, useRouter } from "vue-router";
import { logout } from "@/api/login";
import { removeToken } from "@/utils/auth";
import { resetScreenAutoLoginState } from "@/utils/screenAutoLogin";
import { useStatYear } from "@/stores/statYear";

const router = useRouter();
const route = useRoute();
const { yearModel, availableYears } = useStatYear();

const leftNav = [
  { name: "ScreenHome", path: "/home", zh: "首页", bo: "གཙོ་ངོས།" },
  {
    name: "LivestockResources",
    path: "/livestock-resources",
    zh: "畜牧资源",
    bo: "སྐྱེས་ཁམས་ཐོན་ཁུངས།",
  },
  {
    name: "EpidemicRisk",
    path: "/epidemic-risk",
    zh: "疫病风险",
    bo: "ནད་རིམས་ཉེན་ཁ།",
  },
];

const rightNav = [
  {
    name: "TradeSales",
    path: "/trade-sales",
    zh: "交易销售",
    bo: "འབྲེལ་གཏོང་ཚོང་འབུལ།",
  },
  {
    name: "CommonProsperity",
    path: "/common-prosperity",
    zh: "共同富裕",
    bo: "སྤྱི་འགྲོས་ཕྱུག་པོ།",
  },
];

const logoutNav = {
  name: "Logout",
  path: "/logout",
  zh: "退出登录",
  bo: "ཕྱིར་ལོག་འཛུལ་བ།",
};

const activeName = computed(() => route.name);

function isActive(item) {
  return item.name && activeName.value === item.name;
}

function goNav(item) {
  if (item.path) router.push(item.path);
}

function onLogout() {
  logout().catch(() => {});
  removeToken();
  resetScreenAutoLoginState();
  router.replace({ path: "/login" });
}
</script>


<style scoped>
.screen-nav {
  width: 100%;
  height: 100px;
  position: relative;
  font-family: inherit;
  pointer-events: none;
  background: url("../assets/header.png") no-repeat center center;
}
.screen-nav-left {
  position: absolute;
  left: 60px;
  top: 30px;
  display: flex;
}
.screen-nav-left-item {
  width: 179px;
  height: 63px;
  background: url("../assets/nav/btn.png") no-repeat center center;
  background-size: 100% 100%;
  text-align: center;
  box-sizing: border-box;
  padding: 5px 10px;
  cursor: pointer;
  margin-right: 10px;
  pointer-events: auto;
}
.screen-nav-left-item:hover {
  background: url("../assets/nav/btn_active.png") no-repeat center center;
  background-size: 100% 100%;
}
.screen-nav-left-item.active {
  background: url("../assets/nav/btn_active.png") no-repeat center center;
  background-size: 100% 100%;
}
.screen-nav-year {
  position: absolute;
  left: 38%;
  top: 58px;
  transform: translateX(-50%);
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 8px;
  pointer-events: auto;
}
.screen-nav-year__label {
  font-size: 13px;
  color: #a8d4c8;
  white-space: nowrap;
}
.screen-nav-year__select {
  min-width: 96px;
  height: 30px;
  padding: 0 8px;
  font-size: 14px;
  color: #e8eef5;
  background: rgba(4, 48, 40, 0.85);
  border: 1px solid rgba(61, 217, 176, 0.42);
  border-radius: 4px;
  outline: none;
  cursor: pointer;
}
.screen-nav-year__select:focus {
  border-color: rgba(61, 217, 176, 0.85);
}
.screen-nav-right {
  position: absolute;
  right: 240px;
  top: 30px;
  display: flex;
}
.screen-nav-right-item {
  width: 179px;
  height: 63px;
  background: url("../assets/nav/btn.png") no-repeat center center;
  background-size: 100% 100%;
  text-align: center;
  box-sizing: border-box;
  padding: 5px 10px;
  cursor: pointer;
  margin-right: 10px;
  pointer-events: auto;
}
.screen-nav-right-item:hover {
  background: url("../assets/nav/btn_active.png") no-repeat center center;
  background-size: 100% 100%;
}
.screen-nav-right-item.active {
  background: url("../assets/nav/btn_active.png") no-repeat center center;
  background-size: 100% 100%;
}
.logout {
  width: 179px;
  height: 63px;
  text-align: center;
  box-sizing: border-box;
  padding: 5px 10px;
  cursor: pointer;
  position: absolute;
  right: 70px;
  top: 30px;
  pointer-events: auto;
}
.screen-nav-item-zh {
  font-size: 18px;
  color: #fff;
}
.screen-nav-item-bo {
  font-size: 14px;
  color: #fff;
}
.logout:hover .screen-nav-item-zh {
  color: #16A769;
}
.logout:hover .screen-nav-item-bo {
  color: #16A769;
}

</style>
