<template>
  <div id="app">
    <div class="app_head" :class="{ userWidth: $route.path == '/userManage' }">
      <div class="logo"></div>
      <span style="margin-left: 10px; color: #fff; font-weight: 600"
        >PBL教育平台-后台管理</span
      >
      <div class="user_head" v-if="!this.$store.state.isLogin">
        <div>
          <el-button type="text" style="color: white" @click="Login()"
            >登录</el-button
          >
        </div>
      </div>
      <div class="user_head" v-else>
        <span class="user_name">{{this.$store.state.userInfo.username ? this.$store.state.userInfo.username : "管理员"}}</span>
        <div>
          <i></i>
          <el-button type="text" style="color: white" @click="exit"
            >退出</el-button
          >
        </div>
      </div>
    </div>
    <div class="app">
      <!-- main 内容 -->
      <el-container style="height: 100%">
        <!-- <el-header class="daoh"> </el-header> -->
        <el-container>
          <footer-nav
            v-if="isShowNav"
            :luyou="this.$store.state.luyou"
          ></footer-nav>
          <keep-alive v-if="$route.meta.keepAlive">
            <!-- 这里是会被缓存的视图组件 -->
            <el-main>
              <router-view
                v-if="$route.meta.keepAlive"
                :class="{ pb_body: isShowNav }"
              />
            </el-main>
          </keep-alive>
          <!-- 这里是不被缓存的视图组件 -->
          <router-view
            v-if="!$route.meta.keepAlive"
            :class="{ pb_body: isShowNav }"
          />
        </el-container>
      </el-container>
      <!-- 底部导航 -->
    </div>
  </div>
</template>

<script>
import leftBar from "./components/tools/leftBar";
export default {
  name: "App",
  data() {
    return {
      isShowNav: true, // 是否显示导航 Tab
      navTabs: [
        "/",
        "/curriculum",
        "/notice",
        "/tutor",
        "/grouping",
        "/organization",
        "/jurisdiction",
        "/find",
        "/course",
        "/userManage",
        "/works",
        "/class",
        "/template",
      ], // 导航
      loading: "",
    };
  },
  components: {
    "footer-nav": leftBar,
  },
  created: function () {
    // debugger
    console.log(this.$store.state);
    // this.$store.commit("update", ["isLogin", 123]);
    this.routerP();
  },
  methods: {
    Login() {
      this.$router.push("/login");
    },
    isLogin() {
      const loading = this.$loading.service({
        background: "rgba(255, 255, 255)",
        target: document.querySelector("body"),
      });
      //&& window.sessionStorage.getItem("userInfo")
      var isLogin = this.$cookies.get("Alogin");
      var info = this.$cookies.get("AuserInfo");
      if (isLogin == "1") {
        loading.close();
        this.$store.commit("update", ["isLogin", true]);
        // var info = JSON.parse(window.sessionStorage.getItem("userInfo"));
        this.$store.commit("update", ["userInfo", info]);
      } else {
        this.$store.commit("update", ["isLogin", false]);
        this.$message({
          message: "未登录，请登录",
          type: "warning",
        });
        setTimeout(() => {
          loading.close();
          this.$router.push("/login");
        }, 2000);
      }
    },
    routerP() {
      const { navTabs } = this.$data;
      let router_path = this.$route.path;
      console.log(router_path);
      if (router_path != "/login") {
        this.isLogin();
      }
      var a = 0;
      for (var i = 0; i < navTabs.length; i++) {
        if (router_path == navTabs[i]) {
          this.isShowNav = true;
          a = 1;
          break;
        }
      }
      if (a == 0) {
        this.isShowNav = false;
      }
    },
    exit() {
      sessionStorage.clear();
      this.$cookies.remove("Alogin");
      this.$cookies.remove("AuserInfo");
      this.$store.commit("update", ["isLogin", false]);
      this.$store.commit("update", ["userInfo", {}]);
      this.$message({
        message: "退出成功",
        type: "success",
      });
      this.$router.push("/login");
    },
    goTo(path) {
      this.$router.push(path);
    },
  },
  watch: {
    $route(to, from) {
      const { navTabs } = this.$data;
      const toPath = to.path;
      const fromName = from.name;
      var a = 0;
      for (var i = 0; i < navTabs.length; i++) {
        if (toPath == navTabs[i]) {
          this.isShowNav = true;
          a = 1;
          break;
        }
      }
      if (a == 0) {
        this.isShowNav = false;
      }
    },
  },
};
</script>

<style>
body {
  background-color: #e9e9e9;
}
* {
  margin: 0;
  padding: 0;
}

.daoh {
  background: #3d67bc;
}
#app {
  height: 100%;
  width: 100%;
  background: #e6eaf0;
  /* min-width: 1380px; */
  min-width: 1000px;
}
.app {
  /* height: 100%; */
  width: 100%;
  background: #e6eaf0;
  /* min-width: 1380px; */
  min-width: 1000px;
  height: calc(100% - 67.5px);
}
.app_head {
  height: 67.5px;
  width: 100%;
  background-color: rgb(61, 103, 188);
  display: flex;
  align-items: center;
  min-width: 1000px;
}
.logo {
  height: 48px;
  width: 73px;
  background: url("./assets/logo.png");
  background-size: 100% 100%;
  margin-left: 20px;
}
.pb_body {
  margin-left: 220px;
  width: calc(100% - 240px) !important;
  /* min-width: 1140px; */
  min-width: 750px;
  display: inline-block;
  /* height: calc(100% - 100px); */
  min-height: 750px;
  background: #fff;
  margin-top: 20px;
  border-radius: 5px;
  position: absolute;
  height: calc(100% - 100px);
  overflow: auto;
}
.user_head {
  display: flex;
  align-items: center;
  margin-left: auto;
  margin-right: 20px;
  font-size: 18px;
  font-weight: 600;
}
.user_head .user_name {
  color: #fff;
  margin-right: 10px;
}
.user_head div {
  display: flex;
  align-items: center;
  cursor: pointer;
}
.user_head i {
  background-image: url("./assets/exit.png");
  width: 25px;
  height: 25px;
  background-size: 100% 100%;
  margin-top: 1px;
  line-height: 25px;
  vertical-align: text-top;
  background-repeat: no-repeat;
}

.pb_buttonBox {
  display: flex;
  align-content: center;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
}
.pb_buttonBox button {
  margin: 2px 2px !important;
}
.noticeBox {
  position: relative;
  margin-right: 10px;
}

.user_head .noticeI {
  background-image: url("./assets/icon/noticeA.png");
  width: 25px;
  height: 25px;
  background-size: 100% 100%;
  margin-top: 1px;
  line-height: 25px;
  vertical-align: text-top;
  background-repeat: no-repeat;
  cursor: pointer;
}
.noticeBox span {
  position: absolute;
  background: red;
  width: 15px;
  height: 15px;
  border-radius: 30px;
  color: #fff;
  text-align: center;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  top: -3px;
  right: -3px;
}
.userWidth {
  min-width: 1420px;
}
</style>
