<template>
  <el-container class="admin-layout">
    <el-aside width="200px" class="aside">
      <div class="logo">调压通管理后台</div>
      <el-menu
        :default-active="activeMenu"
        class="el-menu-vertical"
        router
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
      >
        <el-menu-item index="/dashboard">
          <el-icon><DataLine /></el-icon>
          <span>首页</span>
        </el-menu-item>

        <template v-if="userStore.role === 'SYSTEM_ADMIN'">
          <el-sub-menu index="/organization">
            <template #title>
              <el-icon><OfficeBuilding /></el-icon>
              <span>组织架构</span>
            </template>
            <el-menu-item index="/organization/tree">结构图视图</el-menu-item>
            <el-menu-item index="/organization/hospitals"
              >医院管理</el-menu-item
            >
            <el-menu-item index="/organization/departments"
              >科室管理</el-menu-item
            >
            <el-menu-item index="/organization/groups">小组管理</el-menu-item>
          </el-sub-menu>
        </template>
        <template v-else-if="canAccessOrgTree">
          <el-menu-item index="/organization/tree">
            <el-icon><Share /></el-icon>
            <span>组织架构图</span>
          </el-menu-item>
          <el-menu-item
            v-if="canAccessDepartments"
            index="/organization/departments"
          >
            <el-icon><OfficeBuilding /></el-icon>
            <span>科室管理</span>
          </el-menu-item>
          <el-menu-item index="/organization/groups">
            <el-icon><Connection /></el-icon>
            <span>小组管理</span>
          </el-menu-item>
        </template>

        <el-menu-item v-if="canAccessUsers" index="/users">
          <el-icon><User /></el-icon>
          <span>{{ usersMenuLabel }}</span>
        </el-menu-item>

        <el-menu-item v-if="canAccessDictionaries" index="/dictionaries">
          <el-icon><CollectionTag /></el-icon>
          <span>字典管理</span>
        </el-menu-item>

        <el-menu-item v-if="canAccessDevices" index="/devices">
          <el-icon><Monitor /></el-icon>
          <span>设备管理</span>
        </el-menu-item>

        <el-menu-item v-if="canAccessUploads" index="/uploads">
          <el-icon><Picture /></el-icon>
          <span>影像库</span>
        </el-menu-item>

        <el-menu-item v-if="canAccessTasks" index="/tasks">
          <el-icon><List /></el-icon>
          <span>任务管理</span>
        </el-menu-item>

        <el-menu-item v-if="canAccessPatients" index="/patients">
          <el-icon><Avatar /></el-icon>
          <span>患者管理</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header class="header">
        <div class="header-left">
          <!-- 这里预留面包屑区域 -->
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link user-info">
              {{ userStore.userInfo?.name || '用户' }}（{{
                getRoleLabel(userStore.role) || '未分配角色'
              }}）
              <el-icon class="el-icon--right">
                <arrow-down />
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-main class="main">
        <router-view v-slot="{ Component }">
          <transition name="fade-transform" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { getRoleLabel } from '../constants/role-labels';
import { useUserStore } from '../store/user';
import {
  ROLE_PERMISSIONS,
  hasRolePermission,
} from '../constants/role-permissions';
import {
  DataLine,
  OfficeBuilding,
  User,
  List,
  Avatar,
  ArrowDown,
  Connection,
  Share,
  Monitor,
  Picture,
  CollectionTag,
} from '@element-plus/icons-vue';

const route = useRoute();
const router = useRouter();
const userStore = useUserStore();

const activeMenu = computed(() => {
  return route.path;
});

const canAccessUsers = computed(() =>
  hasRolePermission(userStore.role, ROLE_PERMISSIONS.USERS),
);
const canAccessDevices = computed(() =>
  hasRolePermission(userStore.role, ROLE_PERMISSIONS.DEVICES),
);
const canAccessUploads = computed(() =>
  hasRolePermission(userStore.role, ROLE_PERMISSIONS.UPLOADS),
);
const canAccessDictionaries = computed(() =>
  hasRolePermission(userStore.role, ROLE_PERMISSIONS.DICTIONARIES),
);
const canAccessOrgTree = computed(() =>
  hasRolePermission(userStore.role, ROLE_PERMISSIONS.ORG_TREE),
);
const canAccessDepartments = computed(() =>
  hasRolePermission(userStore.role, ROLE_PERMISSIONS.ORG_DEPARTMENTS),
);
const canAccessTasks = computed(() =>
  hasRolePermission(userStore.role, ROLE_PERMISSIONS.TASKS),
);
const canAccessPatients = computed(() =>
  hasRolePermission(userStore.role, ROLE_PERMISSIONS.PATIENTS),
);
const usersMenuLabel = computed(() =>
  userStore.role === 'DIRECTOR'
    ? '成员查看'
    : userStore.role === 'LEADER'
      ? '组员列表'
      : '用户管理',
);

const handleCommand = (command) => {
  if (command === 'logout') {
    userStore.logout();
    router.push('/login');
  }
};
</script>

<style scoped>
.admin-layout {
  height: 100vh;
  width: 100vw;
}
.aside {
  background-color: #304156;
  color: white;
  display: flex;
  flex-direction: column;
}
.logo {
  height: 60px;
  line-height: 60px;
  text-align: center;
  font-size: 20px;
  font-weight: bold;
  border-bottom: 1px solid #1f2d3d;
}
.el-menu-vertical {
  border-right: none;
}
.header {
  background-color: white;
  border-bottom: 1px solid #dcdfe6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}
.user-info {
  cursor: pointer;
  display: flex;
  align-items: center;
}
.main {
  background-color: #f0f2f5;
  padding: 20px;
}

/* fade-transform transition */
.fade-transform-leave-active,
.fade-transform-enter-active {
  transition: all 0.3s;
}
.fade-transform-enter-from {
  opacity: 0;
  transform: translateX(-30px);
}
.fade-transform-leave-to {
  opacity: 0;
  transform: translateX(30px);
}
</style>
