<template>
  <!-- 顶部登录导航 -->
  <div class="LoginNav1">
    <div class="logo" v-if="configInfor">
      <img
        v-if="configInfor.logo_image_url"
        :src="configInfor.logo_image_url"
        alt=""
      />
      <span class="logo-img" v-else>logo</span>
      <el-menu
        :default-active="activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
        text-color="#fff"
        active-text-color="#FF9900"
      >
        <el-menu-item index="1">主页</el-menu-item>
        <el-menu-item index="2">
          <template v-if="projectList.length > 1">
            <el-dropdown trigger="click" @command="handleCommand">
              <span
                class="el-dropdown-link"
                :style="{ color: activeIndex == 2 ? '#FF9900' : '#fff' }"
              >
                {{ projectName }}
                <!-- SYSTEM -->
                <img
                  v-if="activeIndex == 2"
                  src="../assets/login/headerDownlod.png"
                  alt=""
                />
                <img v-else src="../assets/login/headerDownlod2.png" alt="" />
              </span>
              <el-dropdown-menu slot="dropdown" style="min-width: 278px">
                <el-dropdown-item
                  :class="i == LoginNavIndex ? 'menuActive' : ''"
                  :command="i"
                  :key="i"
                  class="projectList"
                  v-for="(item, i) in projectList"
                >
                  <img
                    style="position: relative; width: 24px"
                    :src="item.icon_url_memu_default"
                    alt=""
                  />
                  <span style="margin-left: 16px">
                    {{ item.name }}
                  </span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
          <template v-else>{{
            projectList.length > 0 && projectList[0].name
          }}</template>
        </el-menu-item>
      </el-menu>
    </div>
    <div class="userName">
      <template>
        <el-dropdown
          style="margin-right: 16px; cursor: pointer"
          trigger="click"
          @command="changeLang"
        >
          <span class="el-dropdown-link" style="color: #fff">
            {{ lang }}
            <img src="../assets/login/headerDownlod2.png" alt="" />
          </span>
          <el-dropdown-menu slot="dropdown" style="width: 200px">
            <el-dropdown-item
              v-for="item in language_list"
              :key="item.language_type"
              :command="item"
              >{{ item.language_name }}</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
      </template>
      <div v-if="!userMessage" class="selectLoginOrRegistration">
        <span @click="cutLoginReg">登录</span>
      </div>
      <!-- 用户头像和用户名 -->
      <div class="user" v-else>
        <img
          @click="userShow = !userShow"
          class="headPhoto"
          :src="
            userMessage.image_url
              ? userMessage.image_url
              : require('../assets/login/Group3214.png')
          "
          alt=""
        />
        <span @click="userShow = !userShow">{{
          userMessage.user_real_name
        }}</span>
        <div class="userShow" v-show="userShow">
          <p @click="gotoPersonalcenter">
            <img src="../assets/login/project7.png" alt="" />
            个人中心
          </p>
          <p @click="QuitLogin">
            <img src="../assets/login/Frame77.png" alt="" />
            退出登录
          </p>
        </div>
      </div>
      <!-- 消息 铃铛图片 -->
      <div class="message">
        <img
          @click="gotoPersonalcenter"
          src="../assets/login/Vector2.png"
          alt=""
        />
        <span class="redDot" v-if="is_exist == 'true'"></span>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getToken, removeToken } from "@/utils/auth";
import { getLogin, getStaticContent, getLearnWebContent } from "@/api/ajax";
import { setI18nLang } from "@/utils/i18n";
import { getConfig } from "@/utils/auth";
import { saveSession, getSession, removeSession } from "@/utils/role";
export default {
  name: "LayoutHeader",
  props: [],
  data() {
    return {
      activeIndex: "2", // 主导航索引
      LoginNavIndex: 0, //下拉框导航索引
      projectName: "教师培训中心",
      is_exist: "false",
      projectList: [],
      teacherProList: [
        //普通教师
        {
          id: 0,
          name: "教学中心",
          img: "project1",
        },

        {
          id: 2,
          name: "教培中心",
          img: "project3",
        },
        {
          id: 3,
          name: "教研中心",
          img: "project4",
        },
        {
          id: 5,
          name: "学习中心",
          img: "project6",
        },
        {
          id: 4,
          name: "考试中心",
          img: "project5",
        },
        {
          id: 6,
          name: "个人中心",
          img: "project7",
        },
      ],
      stuProList: [
        {
          id: 0,
          name: "教学中心",
          img: "project1",
        },
        {
          id: 5,
          name: "学习中心",
          img: "project6",
        },
        {
          id: 6,
          name: "个人中心",
          img: "project7",
        },
      ],
      userMessage: null,
      userShow: false,
      language_list: [],
      lang: "",
      headTimer: null,
      configInfor: null,
    };
  },
  watch: {},
  computed: {
    ...mapGetters(["language_type"]),
  },
  methods: {
    // 前往个人中心
    gotoPersonalcenter() {
      if (!this.userMessage) {
        this.$message.warning("请先登录");
        return;
      }
      location.href = `/GCLS-Personal/#/EnterSys`;
    },

    // 切换导航
    handleSelect(key, keyPath) {
      this.activeIndex = key;
      if (this.activeIndex == 1) {
        window.location.href = "/";
        // let token = getToken();
        // if (token) {
        //   if (JSON.parse(token).popedom_code_list.indexOf(2000006) != -1) {
        //     this.$router.push("/teacherEduEnteringIndex");
        //     // window.location.href = "/teacherEduEnteringIndex";
        //   } else {
        //     this.$router.push({ path: "/" });
        //     // window.location.href = "/";
        //   }
        // }
      }
    },
    // 切换项目
    handleCommand(command) {
      let _this = this;
      _this.LoginNavIndex = command;
      if (!_this.userMessage) {
        this.$message.warning("请先登录");
        this.projectName = "教培中心";
        window.location.href = "/";
        return;
      }
      _this.projectName = _this.projectList[command].name;
      let relative_path = _this.projectList[command].relative_path;
      location.href = relative_path;
    },
    // 切换登录的注册
    cutLoginReg() {
      window.location.href = "/";
    },
    QuitLogin() {
      window.location.href = "/";
      removeSession("SysList");
      //removeToken();
      this.userShow = false;
      this.userMessage = null;
    },
    getLangList() {
      let MethodName = "language_manager-GetLanguageList";
      let data = {};
      getStaticContent(MethodName, data).then((res) => {
        this.language_list = res.language_list;
        for (let i = 0; i < this.language_list.length; i++) {
          let item = this.language_list[i];
          if (item.language_type == this.language_type) {
            this.lang = item.language_name;
            break;
          }
        }
      });
    },
    async changeLang(command) {
      console.log(command);
      this.lang = command.language_name;
      let lang_type = command.language_type;
      await setI18nLang(lang_type);
      this.$router.go(0);
    },

    getNotReadMessage() {
      let MethodName = "message-message_manager-IsExistMyMessage_NotRead";
      let data = {};
      getLearnWebContent(MethodName, data).then((res) => {
        this.is_exist = res.is_exist;
      });
    },

    getChildSysList() {
      let _this = this;
      let MethodName = "login_control-GetChildSysList_CanEnter_PC";
      getLogin(MethodName, {})
        .then((res) => {
          if (res.child_sys_list && res.child_sys_list.length > 0) {
            _this.projectList = res.child_sys_list;
            _this.projectList.forEach((item, index) => {
              _this.LoginNavIndex = index;
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  created() {
    let _this = this;
    _this.getChildSysList();
    console.log("v2021.09.09.17:38");
  },
  mounted() {
    let _this = this;
    let user = getToken();
    if (user) {
      _this.userMessage = JSON.parse(user);
    }
    let config = getConfig();
    if (config) {
      _this.configInfor = JSON.parse(config);
    }
    _this.getLangList();
    // _this.headTimer = _this.getNotReadMessage();
    // setInterval(() => {
    //   _this.getNotReadMessage();
    // }, 120000);
  },
  beforeDestroy() {
    if (this.headTimer) {
      //如果定时器还在运行 或者直接关闭，不用判断
      clearInterval(this.headTimer); //关闭
    }
  },
};
</script>

<style lang="scss" scoped>
.LoginNav1 {
  // height: 74px;
  height: 64px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-sizing: border-box;
  padding: 0px 24px;
  z-index: 999;
  // background: #ffffff;
  // box-shadow: 0px 1px 0px rgba(0, 0, 0, 0.15);
  font-family: "sourceR";
  height: 64px;
  background: #424141;
  .el-dropdown-link {
    img {
      width: 8px;
      height: 4px;
      vertical-align: middle;
    }
  }
  .logo {
    > img {
      width: 188px;
      height: 64px;
    }
    &-img {
      font-style: normal;
      font-weight: 600;
      font-size: 30px;
      font-variant: small-caps;
      // color: #000000;
      color: #fff;
      margin-right: 29px;
      line-height: 64px;
    }
    display: flex;
    align-items: center;
    .el-menu-demo {
      background: rgba(0, 0, 0, 0);
      // margin-left: 100px;
      li:hover {
        background: none;
      }
      li {
        font-size: 16px;
        background: none;
        height: 64px;
        line-height: 64px;
      }
    }
    // 取消组件默认的样式
    .el-menu.el-menu--horizontal {
      border-bottom: none;
    }
  }
  .userName {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    .seek {
      margin-right: 100px;
      position: relative;
      img {
        left: 10px;
        top: 11px;
        position: absolute;
      }
    }
    .flag {
      position: relative;
      top: 5px;
    }
    .headPhoto {
      width: 50px;
      height: 50px;
      // background: url("../assets/teacherTrain/image 4.png") no-repeat 100% 100%;
      // background-size: 100%;
      border-radius: 50%;
      // margin-right: 10px;
    }
    .message {
      height: 100%;
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-left: 23px;
      img {
        width: 24px;
        cursor: pointer;
      }
      .redDot {
        position: absolute;
        top: 2px;
        right: -2px;
        width: 6px;
        height: 6px;
        display: inline-block;
        background: red;
        border-radius: 50%;
      }
    }
    .selectLoginOrRegistration {
      display: flex;
      height: 32px;
      border: 1px solid #ffffff;
      box-sizing: border-box;
      border-radius: 4px;
      justify-content: space-evenly;
      align-items: center;
      padding: 0 16px;
      > span {
        cursor: pointer;
        font-size: 16px;
        color: #fff;
      }
    }
    .user {
      display: flex;
      align-items: center;
      cursor: pointer;
      position: relative;
      color: #fff;
      > img {
        width: 34px;
        height: 34px;
      }
      > span {
        font-size: 20px;
        padding-left: 10px;
      }
      .userShow {
        position: absolute;
        width: 156px;
        height: 96px;
        background: #ffffff;
        box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.15);
        border-radius: 8px;
        bottom: -100px;
        left: -5px;
        color: black;
        img {
          width: 24px;
          height: 24px;
          margin-right: 10px;
        }
        p {
          font-size: 16px;
          height: 40px;
          display: flex;
          align-items: center;
          padding-left: 20px;
          margin: 0;
          margin-top: 5px;
        }
        > p:hover {
          background: #f2f2f2;
        }
      }
    }
  }
}
</style>
<style lang="scss">
.LoginNav1 {
  .el-dropdown-menu__item {
    line-height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    > span {
      font-family: "sourceR";
      font-size: 16px;
    }
  }
  .el-menu--horizontal > .el-menu-item {
    height: 60px;
    line-height: 60px;
  }
  .el-dropdown {
    display: block;
    > span {
      font-family: "sourceR";
      font-size: 16px;
    }
  }
  .el-menu-item.is-active i {
    color: white;
  }
}
.projectList {
  &.el-dropdown-menu__item {
    line-height: 40px;
    display: flex;
    align-items: center;
    color: #000000;
    > img {
      top: 0;
    }
    > span {
      line-height: 40px;
      font-family: "sourceR";
    }
  }
  &:hover {
    background: #ff9900 !important;
    // background: rgba(255, 153, 0, 0.1) !important;
    color: #000 !important;
    // color: #fff;
  }
  &.menuActive {
    background: #ff9900;
  }
}
</style>