<template>
  <div class="front-layout">
    <header class="front-header">
      <div class="header-left">
        <img src="@/assets/images/tba-gxpt-logo.png" alt="logo" class="logo" />
      </div>
      <nav class="front-nav">
        <el-menu :default-active="activeMenu" mode="horizontal" background-color="transparent" text-color="#fff" active-text-color="#ffd700">
          <el-menu-item 
            v-for="menu in frontMenus" 
            :key="menu.key" 
            :index="menu.key" 
            @click="navigate(menu.key)"
          >
            {{ menu.title }}
          </el-menu-item>
        </el-menu>
      </nav>
      <div class="header-right">
        <el-button type="primary" @click="goAdmin">后台管理</el-button>
        <el-dropdown @command="handleCommand" class="avatar-container" trigger="hover">
          <div class="avatar-wrapper">
            <svg-icon icon-class="user" class="user-icon" />
            <span class="user-name">{{ userStore.nickName || userStore.name }}</span>
            <el-icon class="dropdown-icon">
              <ArrowDown />
            </el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <router-link to="/user/profile">
                <el-dropdown-item>个人中心</el-dropdown-item>
              </router-link>
              <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </header>

    <main class="front-content">
      <router-view />
    </main>

    <footer class="front-footer">
      <p>主办单位：太湖流域管理局水文局（信息中心） | 技术支持：北京金水信息技术发展有限公司</p>
      <p>沪ICP备2022022055号-1</p>
    </footer>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ArrowDown } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'
import useUserStore from '@/store/modules/user'
import usePermissionStore from '@/store/modules/permission'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const permissionStore = usePermissionStore()

const activeMenu = ref('/index')

const frontMenus = computed(() => {
  const routes = permissionStore.frontRouters || []
  const menuList = []
  routes.forEach(item => {
    const orderNum = item.meta?.orderNum || 0
    if (item.children && item.children.length > 0) {
      item.children.forEach(child => {
        if (child.meta?.title && !child.hidden) {
          menuList.push({
            key: item.path,
            title: child.meta.title,
            orderNum: child.meta?.orderNum || orderNum
          })
        }
      })
    } else if (item.meta?.title && !item.hidden) {
      menuList.push({
        key: item.path,
        title: item.meta.title,
        orderNum: orderNum
      })
    }
  })
  return menuList.sort((a, b) => {
    return (a.orderNum || 0) - (b.orderNum || 0)
  })
})

onMounted(() => {
  activeMenu.value = route.path
})

watch(() => route.path, (newPath) => {
  activeMenu.value = newPath
})

function navigate(path) {
  activeMenu.value = path
  router.push(path)
}

function goAdmin() {
  router.push('/admin/index')
}

function handleCommand(command) {
  switch (command) {
    case 'logout':
      ElMessageBox.confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        userStore.logOut().then(() => {
          location.href = '/login'
        })
      }).catch(() => {})
      break
    default:
      break
  }
}
</script>

<style scoped>
.front-layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.front-header {
  height: 70px;
  background: linear-gradient(135deg, #0066cc 0%, #0099ff 100%);
  display: flex;
  align-items: center;
  padding: 0 30px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 15px;
  flex-shrink: 0;
}

.logo {
  height: 50px;
  width: auto;
}

.title {
  font-size: 20px;
  font-weight: bold;
  color: #fff;
  letter-spacing: 2px;
}

.front-nav {
  flex: 1;
  display: flex;
  align-items: center;
  margin-left: 20px;
  overflow-x: auto;
}

.front-nav :deep(.el-menu) {
  border-bottom: none;
  background-color: transparent;
  width: 100%;
}

.front-nav :deep(.el-menu-item) {
  height: 60px;
  line-height: 60px;
  font-size: 15px;
}

.front-nav :deep(.el-menu-item:hover) {
  background-color: rgba(255, 255, 255, 0.1);
}

.front-nav :deep(.el-menu-item.is-active) {
  background-color: rgba(255, 255, 255, 0.2);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-shrink: 0;
}

.user-icon {
  width: 20px;
  height: 20px;
  color: #fff;
}

.user-name {
  color: #fff;
  font-size: 14px;
}

.dropdown-icon {
  color: #fff;
  font-size: 14px;
}

.front-content {
  flex: 1;
  background: #f5f7fa;
  padding: 20px;
}

.front-footer {
  background: #2c3e50;
  color: #fff;
  text-align: center;
  padding: 15px 0;
  font-size: 13px;
}

.front-footer p {
  margin: 5px 0;
}
</style>
