<template>
  <div class="layout">
    <!-- 固定导航栏 -->
    <nav class="navbar">
      <div class="logo">
        <img src="~/assets/img/KEP001.svg" class="logo-img" />
        <span class="log-text">KE Platform</span>
      </div>
      <ul class="nav-links" ref="el">
        <li v-for="item in navItems" :key="item.path">
          <NuxtLink :to="item.path" :class="{ active: activeTab === item.path }">
            {{ item.name }}
          </NuxtLink>
        </li>
      </ul>

      <!-- <div class="user-info">{{ user }}</div> -->
      <div class="div-area">
        当前工区：<el-select v-model="selectedAreaData" placeholder="请选择" class="workArea">
          <el-option v-for="item in areaData" :key="item.AreaID" :label="item.AreaName" :value="item.AreaID" />
        </el-select>
        &nbsp;
        当前用户：
        <el-dropdown @command="handleUser">
          <el-button link style="font-size: 20px;" :type="userBtnType">{{ userName }}
            <i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <template v-slot:dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </nav>

    <!-- 内容区域 - 会根据当前路由自动变化 -->
    <div class="content">
      <slot />
    </div>
  </div>
</template>

<script setup>
import { useAuthStore } from "~/stores/user";
import { ref, onMounted, onBeforeUnmount } from "vue";
import { useRoute } from "vue-router";

import { getWorkArea, setWorkArea } from "~/services/workAreaService";
import { useCurrentTabStore } from "~/stores/currentTabStore";
import { emitter } from "~/utils/eventBus";

const authStore = useAuthStore();
// authStore.initializeAuth();

const useTabStore = useCurrentTabStore();

const route = useRoute();
const activeTab = ref("/");

const areaData = ref(null);

const selectedAreaData = ref('');

const userName = ref('未登录');
const userBtnType = computed(() => { userName.value === '未登录' ? 'success' : 'danger' });


const navItems = ref([
  { name: "工区管理", path: "/" },
  { name: "数据管理", path: "/dataManage" },
  { name: "井位智能推荐", path: "/smartWellRecommend" },
  { name: "参数图形绘制", path: "/drawParamGdi" },
  { name: "井段数据分析", path: "/wellDataAnalysis" },
  { name: "水平井", path: "/horizontalWell" },
  { name: "微地震云图", path: "/microEarthQuake" },

]);

// 监听路由变化更新激活状态
watchEffect(() => {
  activeTab.value = route.path;
  useTabStore.changeTab(route.path);
  console.log("当前路径：", route.path);
});

const handleUser = ((val) => {
  switch (val) {
    case 'logout':
      authStore.logout();
      navigateTo('/login');
      break;
  }

});

watch(selectedAreaData, async (newVal) => {
  if (newVal) {
    await setData();
  }
}, { immediate: true });

onMounted(() => {
  emitter.on('addWorkArea', () => {
    getData();
  });
  getData();
  const userStr = localStorage.getItem("userInfo");
  const user = userStr ? JSON.parse(userStr) : null;
  console.log("当前用户信息", user);
  if (user) {
    userName.value = user.UserName;
  }
});

onBeforeUnmount(() => {
  emitter.off('addWorkArea');
})

const getData = async () => {
  const response = await getWorkArea();
  console.log(response);
  if (response.Count > 0) {
    areaData.value = response.Data;
    const savedAreaData = localStorage.getItem('workarea');
    if (savedAreaData) {
      selectedAreaData.value = savedAreaData;
    } else {
      selectedAreaData.value = areaData.value[0].AreaID;
      await setData();
    }
  }
};

const setData = async () => {
  if (!selectedAreaData) {
    return;
  }
  const response = await setWorkArea(selectedAreaData.value);
  console.log("设置工区", response);
  if (response.Code === 1) {
    localStorage.setItem("workarea", selectedAreaData.value);
  }
}
</script>

<style scoped>
.layout {
  height: 100vh;
  min-height: 700px;
  min-width: 1366px;
  width: 100%;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}

.navbar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  min-width: 1366px;
  display: flex;
  align-items: center;
  padding: 1rem;
  background: #ffffff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  z-index: 1000;
  min-height: 60px;
  /* 设置最小高度防止内容过小时高度塌陷 */
  height: 60px;
  /* justify-content: space-around; */
}

.logo {
  font-weight: bold;
  margin-right: 1rem;
  display: flex;
  justify-content: center;
  flex-direction: column;
}

.logo-img {
  width: 80px;
  height: 30px;
}

.log-text {
  font-size: 13px;
}

.nav-links {
  display: flex;
  list-style: none;
  gap: 1.5rem;
}

.nav-links a {
  text-decoration: none;
  color: #333;
  padding: 0.5rem;
  transition: color 0.3s;
}

.nav-links a:hover {
  color: #42b983;
}

.nav-links a.active {
  color: #42b983;
  border-bottom: 2px solid #42b983;
  font-weight: bold;
}

.user-info {
  flex-grow: 1;
  justify-content: flex-end;
}

.content {
  margin-top: 65px;
  /* 导航栏高度 + 额外间距 */
  /* padding-top: 20px; */
  flex-grow: 1;
  overflow: hidden;
  height: 100%;
  width: 100%;
}

.div-area {
  justify-content: flex-end;
  margin-left: auto;
  margin-right: 50px;
}

.workArea {
  width: 100px;
  z-index: 1001;
  margin-right: 10px;
}
</style>
