<template>
  <div class="navbar">
    <div :class="[role !== 3 ? 'navTitle' : 'TnavTitle']">
      <span class="nacber-name" v-if="role !== 3">电商互联网产品开发实训系统</span>
      <span class="nacber-name" v-else>数 字 营 销 实 训 系 统 - 教 师 端</span>
    </div>
    <div :class="['el-ment', { 'platform-nav': role === 1 }]">
      <template v-for="item in roleList" :key="item.name">
        <div :class="['el-ment-item', { 'el-ment-item-active': isActive(item) }]" @click="go(item)">{{ item.name }}</div>
      </template>
    </div>
    <div class="right-menu">
      <div class="avatar-container">
        <el-dropdown @command="handleCommand" class="right-menu-item hover-effect" trigger="click">
          <div class="avatar-wrapper">
            <img src="../../assets/images/头像.webp" class="user-avatar" />
            <el-icon><caret-bottom /></el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <!-- <router-link to="/user/profile">
                <el-dropdown-item>个人中心</el-dropdown-item>
              </router-link>-->
              <el-dropdown-item command="setLayout">
                <span>个人中心</span>
              </el-dropdown-item>
              <el-dropdown-item divided command="logout">
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
    <el-dialog title="个人中心" custom-class="gdialog" v-model="showModel" width="40%">
      <div class="dialog-content">
        <el-form :model="form" label-width="80px" class="dialog-form">
          <el-form-item :label="userStore.userInfo.roleId == 4 ? '姓名' : '教师姓名'">
            <el-input v-model="form.username" disabled></el-input>
          </el-form-item>
          <el-form-item :label="userStore.userInfo.roleId == 4 ? '学号' : '工号'">
            <el-input v-model="form.name" disabled></el-input>
          </el-form-item>
          <el-form-item :label="userStore.userInfo.roleId == 4 ? '院系' : '院系名称'">
            <el-input v-model="form.schoolFacultyId" disabled></el-input>
          </el-form-item>
          <el-form-item :label="userStore.userInfo.roleId == 4 ? '专业' : '专业名称'">
            <el-input v-model="form.schoolMajorId" disabled></el-input>
          </el-form-item>
          <el-form-item label="班级:">
            <el-input v-model="form.schoolClassId" disabled></el-input>
          </el-form-item>
          <el-form-item label="密码:">
            <el-input v-model="form.password" disabled></el-input>
          </el-form-item>
          <el-form-item label="新密码:">
            <el-input v-model="form.passwords"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footers">
          <el-button @click="showModel = false">取消</el-button>
          <el-button @click="changePassword">修改密码</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ElMessageBox } from "element-plus";
import Breadcrumb from "@/components/Breadcrumb";
import TopNav from "@/components/TopNav";
import Hamburger from "@/components/Hamburger";
import Screenfull from "@/components/Screenfull";
import SizeSelect from "@/components/SizeSelect";
import HeaderSearch from "@/components/HeaderSearch";
import RuoYiGit from "@/components/RuoYi/Git";
import RuoYiDoc from "@/components/RuoYi/Doc";
import useAppStore from "@/store/modules/app";
import useUserStore from "@/store/modules/user";
import useSettingsStore from "@/store/modules/settings";
import * as indexApi from "@/api/teacher";
const { proxy } = getCurrentInstance();
const router = useRouter();
const appStore = useAppStore();
const userStore = useUserStore();
const settingsStore = useSettingsStore();
const showModel = ref(false);
const form = ref({});
const role = computed(() => Number(userStore.userInfo.roleId));
const mentList = ref([
  {
    name: "首页",
    path: "/index",
  },
  {
    name: "学校管理",
    path: "/platform-schools/index",
    role: 1,
    activePrefix: "/platform-schools",
  },
  {
    name: "学校管理员",
    path: "/platform-school-admins/index",
    role: 1,
    activePrefix: "/platform-school-admins",
  },
  {
    name: "实验报告",
    path: "/report/index",
    role: 4,
  },
  {
    name: "实验数据源",
    path: "/data/index",
    role: 4,
  },
  {
    name: "BI可视化",
    path: "/bi",
    role: 4,
  },
  {
    name: "AI学情分析",
    path: "/analysis/index",
  },
  {
    name: "AI实训教案",
    path: "/ai/teaching-plan",
    role: 3,
  },
]);
const activement = ref(mentList.value[0]);
function toggleSideBar() {
  appStore.toggleSideBar();
}
function handleCommand(command) {
  switch (command) {
    case "setLayout":
      setLayout();
      break;
    case "logout":
      logout();
      break;
    default:
      break;
  }
}
function logout() {
  ElMessageBox.confirm("确定注销并退出系统吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      userStore.logOut().then(() => {
        location.href = "/index";
      });
    })
    .catch(() => {});
}
// const emits = defineEmits(["setLayout"]);
function setLayout() {
  showModel.value = true;
}
const go = (item) => {
  activement.value = item;
  router.push(item.path);
};
const isActive = (item) => {
  return item.activePrefix ? router.currentRoute.value.path.startsWith(item.activePrefix) : item.path === router.currentRoute.value.path;
};
// 根据角色显示菜单
const roleList = computed(() => {
  const currentRole = role.value;
  return mentList.value.filter((item) => {
    if (currentRole === 1) {
      return item.path === "/index" || item.role === 1;
    }
    if (currentRole === 2) {
      return item.path === "/index";
    }
    if (!item.role || item.path === "/index" || item.path === "/analysis/index") return true;
    return item.role == currentRole;
  });
});
// 获取用户详情
const getUserInfo = () => {
  if (role.value === 1) {
    form.value = {
      username: userStore.userInfo.name,
      name: userStore.userInfo.username,
      schoolFacultyId: "平台级账号",
      schoolMajorId: "平台级账号",
      schoolClassId: "无",
      password: "******",
      passwords: "",
    };
    return;
  }
  indexApi
    .getInfo({ userId: userStore.userInfo.userId })
    .then((res) => {
      form.value = res.data;
    })
    .catch(() => {});
};
// 修改密码
const changePassword = () => {
  indexApi
    .updatePassword({ userId: userStore.userInfo.userId, password: form.value.passwords })
    .then(() => {
      proxy.$modal.msgSuccess("修改成功");
      showModel.value = false;
      getUserInfo();
    })
    .catch(() => {});
};
onMounted(() => {
  getUserInfo();
});
</script>
<style lang="scss" scoped>
.navbar {
  width: 100%;
  height: 50px;
  overflow: hidden;
  position: fixed;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  background: url("@/assets/images/top.png") no-repeat;
  background-size: 100% 100%;
  .navTitle {
    z-index: 1;
    position: fixed;
    top: 0;
    background-size: 100% 100%;
    .nacber-name {
      height: 42px;
      display: flex;
      //  justify-content: center;
      align-items: center;
      font-family: Source Han Sans CN;
      font-weight: bold;
      color: #ffffff;
      font-size: 27px;
      margin-left: 10px;
      margin-top: 3px;
      letter-spacing: 4px;
    }
  }
  .TnavTitle {
    background: url("../assets/images/top1.png");
    background-size: 100% 100% !important;
    box-shadow: 0 1px 4px #00152914;
    position: fixed;
    width: 100%;
    z-index: 1000;
    padding: 5px 20px;
    span {
      font-size: 26px;
      font-family: Source Han Sans CN;
      font-weight: 700;
      color: #fff;
      line-height: 50px;
    }
  }
  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background 0.3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }
  .breadcrumb-container {
    float: left;
  }
  .topmenu-container {
    position: absolute;
    left: 50px;
  }
  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }
  .el-ment {
    position: absolute;
    left: 45%;
    top: 50%;
    transform: translate(-50%, -50%);
    font-size: 20px;
    color: #fff;
    font-weight: bold;
    display: flex;
    gap: 18px;
    align-items: center;
    white-space: nowrap;
    .el-ment-item {
      padding: 6px 16px;
      border: 1px solid transparent;
      border-radius: 999px;
      cursor: pointer;
      line-height: 1;
      transition: color 0.2s ease, border-color 0.2s ease, background 0.2s ease, box-shadow 0.2s ease;
      &:hover {
        color: #9ee7ff;
        border-color: rgba(0, 170, 255, 0.42);
        background: rgba(0, 124, 204, 0.18);
      }
    }
    .el-ment-item-active {
      color: #9ee7ff;
      border-color: rgba(0, 170, 255, 0.68);
      background: linear-gradient(90deg, rgba(0, 143, 254, 0.32), rgba(10, 92, 134, 0.2));
      box-shadow: inset 0 0 12px rgba(0, 170, 255, 0.12);
    }
    &.platform-nav {
      gap: 14px;
    }
  }
  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;
    display: flex;
    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }

    .avatar-container {
      margin-right: 40px;

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        i {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
