<template>
  <div class="user-layout">
    <el-container>
      <el-header class="header">
        <div class="header-left" @click="router.push('/')">
          <img :src="metaStore.logo" alt="logo" class="logo">
          <h2 class="title">{{metaStore.title}}</h2>
        </div>
        <el-menu
            :default-active="activeIndex"
            mode="horizontal"
            router
            @select="handleSelect"
            class="nav-menu"
        >
          <el-menu-item :index="menu.path" v-for="menu in menus" :key="menu.path">
            <el-icon><component :is="menu.meta?.icon"/></el-icon>
            <template #title>{{ menu.meta?.title }}</template>
          </el-menu-item>
          <el-menu-item v-if="isAdmin" index="/admin">
            <el-icon><Tools/></el-icon>
            <template #title>后台管理</template>
          </el-menu-item>
        </el-menu>
        <div class="header-right">
          <template v-if="login">
            <el-dropdown placement="bottom">
              <el-button class="user-btn" text>
                <el-icon class="user-avatar"><UserFilled /></el-icon>
                <span class="username">{{ loginUserStore.loginUser?.user?.name }}</span>
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>个人信息</el-dropdown-item>
                  <el-dropdown-item divided @click="logoutHandler">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
          <template v-else>
            <el-button type="primary" size="small" @click="loginHandler">登录</el-button>
            <el-button size="small" @click="router.push('/login')">注册</el-button>
          </template>
        </div>
      </el-header>

      <el-main>
        <router-view/>
      </el-main>

      <el-footer>Footer</el-footer>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import {computed, ref} from 'vue'
import {useLoginUserStore, useMetaStore} from "../store";
import router, {userRoutes} from "../router";
import {
  Tools,
  UserFilled
} from '@element-plus/icons-vue'
const metaStore = useMetaStore()
const loginUserStore = useLoginUserStore()
const login = computed(() => loginUserStore.loginUser.isLogin)
const isAdmin = computed(() => loginUserStore.loginUser.user?.role === 'admin')
const loginHandler = () => {
  router.push('/login')
}
let activeIndex = ref('/')
const menus = computed(()=>{
  return userRoutes[0].children;
})
const handleSelect = () => {
  //todo: 处理菜单选择
}
const logoutHandler = () => {
  loginUserStore.logoutUser()
  router.push('/')
}
</script>
<style scoped>
.user-layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
el-main{
  flex: 1;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}
.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}
.logo {
  width: 40px;
  height: 40px;
}
.title {
  margin: 0;
  font-size: 20px;
}
.nav-menu {
  flex: 1;
  display: flex;
  justify-content: center;
  border-bottom: none;
}
.header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}
.username {
  font-size: 14px;
  color: #606266;
}

.user-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  transition: color 0.2s;
}

.user-btn:hover {
  color: #db2777;
}

.user-avatar {
  font-size: 20px;
  color: #be185d;
}
</style>
