<template>
  <!-- 顶部登录导航 -->
  <div class="LoginNav" :style="{ color: type == 'Live' ? 'black' : 'white' }">
    <div class="logo">
      <!-- <span> 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" v-if="projectListArr.length > 0">
          <el-dropdown trigger="click" @command="handleCommand">
            <span
              class="el-dropdown-link"
              :style="{ color: LoginNavIndex == 2 ? '#FF9900' : '#fff' }"
            >
              {{ projectName }}
              <img
                class="dropdownIcon"
                src="../../assets/login/dropdownIcon.png"
                alt=""
              />
            </span>
            <el-dropdown-menu slot="dropdown" style="width: 300px">
              <el-dropdown-item
                v-for="(item, i) in projectListArr"
                :key="i"
                :command="i"
                :class="projectDownIndex == i ? 'downsele' : ''"
              >
                <img
                  style="position: relative; top: 5px; 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>
        </el-menu-item>
      </el-menu> -->
      <img :src="configInfor.logo_image_url_home" class="logo-img" />
      <p class="p1">{{ configInfor.title }}</p>
    </div>
    <div class="userName">
      <el-dropdown style="margin: 0 16px" trigger="click" @command="changeLang">
        <span class="el-dropdown-link" style="color: #fff; cursor: pointer">
          {{ lang ? lang : "中文" }}<img src="../../assets/arrow-down.png" />
        </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>
      <!-- <div v-if="!userMessage" class="selectLoginOrRegistration">
        <span class="lrBtn" @click="cutLoginReg('login')">{{
          $t("Key9")
        }}</span>
        <span> | </span>
        <span class="lrBtn" @click="cutLoginReg('signin')">{{
          $t("Key10")
        }}</span>
      </div> -->
      <!-- 用户头像和用户名 -->
      <div class="user" v-if="userMessage">
        <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="" />{{
              $t("Key38")
            }}
            <!-- 个人中心 -->
          </p>
          <p @click="QuitLogin">
            <img src="../../assets/login/Frame77.png" alt="" />{{ $t("Key39") }}
            <!-- 退出登录 -->
          </p>
        </div>
      </div>
      <!-- 消息 铃铛图片 -->
      <div class="message" v-if="userMessage">
        <img
          @click="gotoPersonalcenter"
          src="../../assets/login/Vector2.png"
          alt=""
        />
      </div>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import { getStaticContent, getContent } from "@/api/api";

import { getToken, removeToken, setToken } from "@/utils/auth";
import { setI18nLang } from "@/utils/i18n";
export default {
  props: [
    "type",
    "changeLoginReg",
    "FatheruserMessage",
    "projectList",
    "configInfor",
  ],
  data() {
    return {
      activeIndex: "1",
      LoginNavIndex: "1",
      projectName: "",
      userMessage: null,
      userShow: false,
      projectDownIndex: 0,
      language_list: [],
      lang: "",
      projectListArr: [],
    };
  },
  watch: {
    FatheruserMessage(newval, oldval) {
      this.userMessage = null;
      this.userMessage = newval;
    },
    projectList: {
      handler: function (val, oldVal) {
        if (val.length > 0) {
          this.projectListArr = val;
          this.projectName = val[0].name;
        }
      },
      // 深度观察监听
      deep: true,
    },
  },
  methods: {
    // 切换导航
    handleSelect(key, keyPath) {
      this.LoginNavIndex = key;
      if (this.LoginNavIndex == "1") {
        this.$router.push("/");
      } else {
        let ulobj = document.getElementsByClassName("el-dropdown-menu")[0];
        ulobj.classList.add("LoginNavSeleProject");
      }
    },
    // 切换项目
    handleCommand(command) {
      let _this = this;
      _this.LoginNavIndex = "2";
      let userInfor = getToken();
      let user_code = "",
        user_type = "",
        session_id = "";
      if (userInfor) {
        userInfor = JSON.parse(getToken());
        user_code = userInfor.user_code;
        user_type = userInfor.user_type;
        session_id = userInfor.session_id;
      }
      if (!session_id || !user_code || !user_type || !_this.userMessage) {
        this.$message.warning("请先登录");
        this.projectName = "教学中心";
        return;
      }
      _this.projectDownIndex = command;
      let relative_path = _this.projectList[command].relative_path;
      location.href = relative_path;
    },
    // 切换语言
    async changeLang(command) {
      this.lang = command.language_name;
      let lang_type = command.language_type;
      console.log(lang_type);
      let userInfor = getToken();
      if (userInfor) {
        await setI18nLang(lang_type);
        userInfor = JSON.parse(getToken());
        userInfor.language_type = lang_type;
        setToken(userInfor);
      } else {
        localStorage.setItem("language_type", lang_type);
      }

      this.$router.go(0);
    },
    // 切换登录的注册
    cutLoginReg(value) {
      this.changeLoginReg(value);
    },
    // 前往个人中心
    gotoPersonalcenter() {
      let userInfor = getToken();
      let user_code = "",
        user_type = "",
        session_id = "";
      if (userInfor) {
        userInfor = JSON.parse(getToken());
        user_code = userInfor.user_code;
        user_type = userInfor.user_type;
        session_id = userInfor.session_id;
      }
      if (!session_id) {
        this.$message.warning("请先登录");
        return;
      }
      location.href = `/GCLS-Personal/#/EnterSys`;
    },
    QuitLogin() {
      Cookies.remove("userMessage");
      Cookies.remove("session_id");
      Cookies.remove("user_code");
      Cookies.remove("user_type");
      Cookies.remove("GCLSCode");
      sessionStorage.removeItem("useragent_root_close");
      removeToken();
      this.userShow = false;
      this.userMessage = null;
      this.changeLoginReg("login", null);
      this.projectListArr = [];
    },
    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;
          }
        }
      });
    },
  },
  created() {
    let _this = this;
    let language_type = localStorage.getItem("language_type");
    if (language_type) {
      _this.language_type = language_type;
    }

    if (this.FatheruserMessage) {
      _this.userMessage = this.FatheruserMessage;
    } else {
      _this.userMessage = null;
    }
    _this.getLangList();
  },
  mounted() {
    // this.projectName =
    //   this.projectList.length > 0 ? this.projectList[0].name : "";
  },
};
</script>

<style lang="scss" scoped>
.LoginNav {
  height: 64px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 60px 0 40px;
  z-index: 999;
  // background: linear-gradient(180deg, #000000 0%, rgba(44, 44, 44, 0) 100%);
  .logo {
    display: flex;
    align-items: center;
    .el-menu-demo {
      background: rgba(0, 0, 0, 0);
      // margin-left: 100px;
      li:hover {
        background: none;
      }
      li {
        background: none;
      }
    }
    .dropdownIcon {
      margin-left: 8px;
      width: 8px;
      height: 4px;
    }
    // 取消组件默认的样式
    .el-menu.el-menu--horizontal {
      border-bottom: none;
    }
    .logo-img {
      width: 48px;
      height: 48px;
    }
    .p1 {
      font-size: 18px;
      line-height: 26px;
      color: #ffffff;
      margin: 0 10px;
    }
  }
  .userName {
    display: flex;
    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 {
      position: relative;
      margin-left: 16px;
      img {
        width: 32px;
        height: 32px;
        cursor: pointer;
      }
      .redDot {
        width: 6px;
        height: 6px;
        position: absolute;
        display: inline-block;
        background: red;
        border-radius: 50%;
        right: 0;
      }
    }
    .selectLoginOrRegistration {
      display: flex;
      height: 32px;
      border: 1px solid #ffffff;
      box-sizing: border-box;
      border-radius: 4px;
      align-items: center;
      .lrBtn {
        min-width: 64px;
        padding: 0 8px;
        cursor: pointer;
        text-align: center;
      }
    }
    .user {
      display: flex;
      align-items: center;
      cursor: pointer;
      position: relative;
      > img {
        width: 34px;
        height: 34px;
      }
      > span {
        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: 0;
        color: black;
        img {
          width: 24px;
          height: 24px;
          margin-right: 10px;
        }
        p {
          height: 40px;
          display: flex;
          align-items: center;
          padding-left: 20px;
          margin: 0;
          margin-top: 5px;
        }
        > p:hover {
          background: #f2f2f2;
        }
      }
    }
  }
}
</style>
<style lang="scss">
.downsele {
  background: #ff9900;
}
.LoginNav {
  .el-menu--horizontal > .el-menu-item {
    height: 64px;
  }
  .el-menu-item {
    font-size: 16px;
  }
  .is-active {
    font-weight: bold;
    line-height: 64px;
  }
  .el-menu-item i {
    color: white;
  }
  .el-dropdown {
    font-size: 16px;
    img {
      width: 24px;
      height: 24px;
    }
    .el-dropdown-link {
      display: flex;
      align-items: center;
    }
  }
}
</style>
