<template>
  <nav class="aui-navbar">
    <div class="aui-navbar__header">
      <div class="aui-navbar__brand">
        <span>创衡智能体平台</span>
      </div>
    </div>
    <div class="aui-navbar__body">
      <div class="grt_header_menu">
        <el-menu
          :default-active="activeIndex"
          class="aui-navbar__menu"
          text-color="#D8D8D8"
          active-text-color="#ffffff"
          style="flex: 1; margin-left: 0rem"
        >
          <el-menu-item
            :index="index + 1 + ''"
            v-for="(item, index) in leftMenu"
            :key="index"
            class="yijimenu"
            @click="tab_click(index)"
          >
            {{ item }}
          </el-menu-item>
        </el-menu>
        <el-select :class="{'selected':moreMenuValue!==''}" :value="moreMenuValue" placeholder="更多菜单" 
          v-if="moreMenu.length" style="width: 125px;height: 100%;font-size: 16px;" @change="handleChangeTab">
          <el-option
            v-for="item in moreMenu"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </div>

      <el-menu class="aui-navbar__menu exit">
        <el-menu-item index="4" @click="fullscreenHandle()">
          <svg class="icon-svg aui-navbar__icon-menu" aria-hidden="true">
            <use xlink:href="#icon-fullscreen"></use>
          </svg>
        </el-menu-item>
        <el-menu-item index="5" class="aui-navbar__avatar">
          <el-dropdown placement="bottom" :show-timeout="0">
            <span class="el-dropdown-link">
              <span>您好,{{ $store.state.user.name }}</span>
              <i class="el-icon-arrow-down"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="logoutHandle()">{{
                $t("logout")
              }}</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
    </div>
  </nav>
</template>
<script>
import screenfull from "screenfull";
import { clearLoginInfo } from "@/utils";
import SubMenu from "./main-sidebar-sub-menu";

export default {
  inject: ["refresh"],
  components: {
    SubMenu,
  },
  data() {
    return {
      yijiMenu: [],
      leftMenu: [],
      moreMenu: [],
      activeIndex: "1",
      updatePasswordVisible: false,
      moreMenuValue:''
    };
  },
  watch: {
    "$store.state.currentMenuIndex": "changeMenuIndex",
  },
  created() {
    let allMenu = window.SITE_CONFIG["menuList"];
    for (let i = 0; i < allMenu.length; i++) {
      this.yijiMenu.push(allMenu[i].name);
      if(i<9){
        this.leftMenu.push(allMenu[i].name);
      }else {
        this.moreMenu.push({value:i-9,label:allMenu[i].name});
      }
    }
    this.$store.state.sidebarMenuActiveName = [];
    var proute = window.SITE_CONFIG["menuList"].filter(
      (item) =>
        item.children.filter((child) => child.name == this.$route.meta.title)
          .length > 0
    );
    this.$store.state.sidebarMenuActiveName.push(
      proute.length > 0 ? proute[0].name : ""
    );
    let currentYijiIndex = localStorage.getItem("currentMenuIndex");
    localStorage.setItem("currentYiJiIndex", currentYijiIndex);
    this.$store.state.currentMenuIndex = currentYijiIndex;
    this.$store.state.sidebarMenuActiveName.push(this.$route.meta.title);
    this.$store.state.erjismenu = allMenu[currentYijiIndex].children;
    this.activeIndex = parseInt(currentYijiIndex) + 1 + "";

    if(currentYijiIndex>=9){
      this.activeIndex = '';
      this.moreMenuValue = currentYijiIndex-9;
    }
  },
  methods: {
    handleChangeTab(val){
      this.moreMenuValue = val;
      this.activeIndex = '';
      let index = val+9;
      this.$emit("changeYijiInd", index);
      localStorage.setItem("currentYiJiIndex", index);
    },
    changeMenuIndex(newval) {
      this.activeIndex = parseInt(newval) + 1 + "";
    },
    tab_click(index) {
      this.moreMenuValue = '';
      //当前菜单一样不执行操作
      this.$emit("changeYijiInd", index);
      localStorage.setItem("currentYiJiIndex", index);
    },
    // 全屏
    fullscreenHandle() {
      if (!screenfull.enabled) {
        return this.$message({
          message: this.$t("fullscreen.prompt"),
          type: "warning",
          duration: 500,
        });
      }
      screenfull.toggle();
    },

    // 修改密码
    updatePasswordHandle() {
      this.updatePasswordVisible = true;
      this.$nextTick(() => {
        this.$refs.updatePassword.init();
      });
    },
    // 退出
    logoutHandle() {
      this.$confirm(
        this.$t("prompt.info", { handle: this.$t("logout") }),
        this.$t("prompt.title"),
        {
          confirmButtonText: this.$t("confirm"),
          cancelButtonText: this.$t("cancel"),
          type: "warning",
        }
      )
        .then(() => {
          this.$http
            .post("/logout")
            .then(({ data: res }) => {
              if (res.code !== 0) {
                localStorage.removeItem();
                return this.$message.error(res.msg);
              }
              clearLoginInfo();
              this.$router.push({ name: "login" });
            })
            .catch(() => {});
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped lang="scss">
  .grt_header_menu{
    width: calc(100% - 240px);
    display: flex;
  }

  .grt_header_menu .el-select{
    width: 125px;
    flex: 1;
  }
  ::v-deep .grt_header_menu .el-input{
    width: 125px;
    height: 98%;
  }
  ::v-deep .grt_header_menu .el-input::placeholder{
    font-size: 16px;
    color: rgb(216, 216, 216);
  }
  ::v-deep .grt_header_menu .el-input__inner{
    background: transparent;
    height: 100%;
    border: none;
    font-size: 16px;
    color: rgb(216, 216, 216);
  }
  ::v-deep .grt_header_menu .selected .el-input{
    background: linear-gradient(360deg, #2E69EB 0%, #5BA2F7 100%) !important;
    color: #FFFFFF;
  }
</style>
