<script setup lang="ts">
import { storeToRefs } from "pinia";
import { useRoute, useRouter } from "vue-router";
import { useAppStore } from "@/store/modules/app";
import { useTagsViewStore } from "@/store/modules/tagsView";
import { useUserStore } from "@/store/modules/user";
import { SchoolList } from "@/api/school/types";
import { getSchoolSelect } from "@/api/school";
import { watch } from "vue";
import SvgIcon from "@/components/SvgIcon/index.vue";
import { CaretBottom } from "@element-plus/icons-vue";
const appStore = useAppStore();
const tagsViewStore = useTagsViewStore();
const userStore = useUserStore();
const route = useRoute();
const router = useRouter();
const { device } = storeToRefs(appStore); // 设备类型：desktop-宽屏设备 || mobile-窄屏设备

/**
 * 左侧菜单栏显示/隐藏
 */
function toggleSideBar() {
  appStore.toggleSidebar(true);
}
/**
 * vueUse 全屏
 */
const { isFullscreen, toggle } = useFullscreen();
/**
 * 注销
 */
function logout() {
  ElMessageBox.confirm("确定退出系统吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    userStore
      .logout()
      .then(() => {
        tagsViewStore.delAllViews();
      })
      .then(() => {
        router.push(`/login?redirect=${route.fullPath}`);
      });
  });
}
/**
 * 学校数据
 */
const schoolData = ref<SchoolList[]>();
const schoolId = ref(0);
const schoolNum = ref("");
async function getSchoolData() {
  getSchoolSelect()
    .then(({ data }) => {
      schoolData.value = data;
      if (schoolId.value == 0) {
        schoolId.value = data[0].school_id;
        schoolNum.value = data[0].num;
        userStore.changeSchool(schoolId.value, schoolNum.value);
      }
    })
    .catch((error) => {
      console.log(error);
    });
}
onMounted(() => {
  getSchoolData();
});
watch(
  () => schoolId.value,
  (newValue, oldValue) => {
    let num: string = "";
    schoolData.value?.some((school) => {
      if (newValue == school.school_id) {
        num = school.num;
        return true;
      }
    });
    //console.log("school", newValue, num);
    userStore.changeSchool(newValue, num);
  }
);
</script>

<template>
  <!-- 顶部导航栏 -->
  <div class="navbar">
    <!-- 左侧面包屑 -->
    <div class="flex">
      <Hamburger
        :is-active="appStore.sidebar.opened"
        @toggle-click="toggleSideBar"
      />
      <Breadcrumb />
    </div>
    <!-- 右侧导航设置 -->
    <div class="flex">
      <!-- 导航栏设置(窄屏隐藏)-->
      <div v-if="device !== 'mobile'" class="setting-container">
        <!--全屏 -->
        <div class="setting-item" @click="toggle">
          <svg-icon
            :icon-class="isFullscreen ? 'exit-fullscreen' : 'fullscreen'"
            size="16px"
          />
        </div>
      </div>
      <!-- 学校选择下拉框 -->
      <div class="nav-select">
        <el-select
          v-model="schoolId"
          size="large"
          placeholder="请选择学校"
          :suffix-icon="CaretBottom"
        >
          <el-option
            v-for="item in schoolData"
            :key="item.school_id"
            :label="item.name"
            :value="item.school_id"
          />
        </el-select>
      </div>
      <!-- 用户头像 -->
      <div class="avatar-container">
        <span>学校编码：{{ userStore.schoolNum }}</span>
        <span class="spl">|</span>
        <img src="../../assets/login/avatar.png" alt="头像" />
        <span class="">{{ userStore.nickname + " " + userStore.phone }}</span>
        <span @click="logout"
          ><svg-icon icon-class="exit" color="#006eff" size="20px"
        /></span>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 70px;
  background-color: #fff;
  box-shadow: 0 0 1px #0003;

  .setting-container {
    display: flex;
    align-items: center;

    .setting-item {
      display: inline-block;
      width: 30px;
      height: 70px;
      line-height: 70px;
      color: #5a5e66;
      text-align: center;
      cursor: pointer;

      &:hover {
        background: rgb(249 250 251 / 100%);
      }
    }
  }
  .avatar-container {
    display: flex;
    align-items: center;
    justify-items: center;
    margin: 0 20px 0 0;
    cursor: pointer;
    img {
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 20px;
      background: #494949;
    }
    span {
      margin-left: 12px;
      font-size: 16px;
      color: #494949;
      &.spl {
        margin: 0 12px 0 12px;
        color: #d7d8d8;
      }
    }
  }
  .svg-icon {
    margin-bottom: -2px;
  }
  .el-select {
    padding: 15px 0;
    width: 280px;
    margin-left: 12px;
  }
}
/* 自定义 el-select 样式 */
:deep(.el-input__wrapper) {
  background: #efefef;
  border-radius: 12px;
}

/* el-select 各种边框线隐藏**/
:deep(.el-select) {
  --el-select-input-focus-border-color: none !important;
}
:deep(.el-input__wrapper) {
  box-shadow: none !important;
}
:deep(.el-select .el-input.is-focus .el-input__wrapper) {
  box-shadow: none !important;
}
:deep(.el-select .el-input__wrapper.is-focus) {
  box-shadow: none !important;
}
:deep(.el-select:hover:not(.el-select--disabled) .el-input__wrapper) {
  box-shadow: none !important;
}
//移动端兼容
.mobile {
  .navbar {
    .nav-select {
      position: absolute;
      left: 0;
      top: 80px;
      z-index: 1;
      width: 100%;
      box-sizing: border-box;
      padding: 0 24px;
      .el-select {
        width: 100%;
        padding: 0;
        margin: 0;
      }
      :deep(.el-input__wrapper) {
        background: #ffffff;
      }
    }
  }
}
</style>
