<template>
  <div>
    <nav
      v-if="showHeader"
      class="aui-navbar"
      :class="`aui-navbar--${$store.state.navbarLayoutType}`"
    >
      <div class="aui-navbar__header">
        <h1 class="aui-navbar__brand" @click="$router.push({ name: 'home' })">
          <a class="aui-navbar__brand-lg" href="javascript:;">{{
            customerName
          }}</a>
          <a class="aui-navbar__brand-mini" href="javascript:;">{{
            customerName.slice(0, 2)
          }}</a>
        </h1>
      </div>

      <div class="aui-navbar__body">
        <el-menu class="aui-navbar__menu mr-auto" mode="horizontal">
          <el-menu-item
            index="1"
            @click="$store.state.sidebarFold = !$store.state.sidebarFold"
          >
            <svg
              class="icon-svg aui-navbar__icon-menu aui-navbar__icon-menu--switch"
              aria-hidden="true"
            >
              <use xlink:href="#icon-outdent"></use>
            </svg>
          </el-menu-item>

          <el-menu-item index="2" @click="refresh()">
            <svg class="icon-svg aui-navbar__icon-menu" aria-hidden="true">
              <use xlink:href="#icon-sync"></use>
            </svg>
          </el-menu-item>
        </el-menu>

        <div class="m-nav mr-auto z-div">
          <el-menu
            :default-active="$store.state.LevelOneMenuActiveName"
            :unique-opened="true"
            :collapseTransition="false"
            mode="horizontal"
            class="nav"
          >
            <el-menu-item @click="toIndexPage">
              <span>首页</span>
            </el-menu-item>

            <el-menu-item
              v-show="menu.showFlag == 1"
              v-for="(menu, idx) in $store.state.sidebarMenuList"
              :key="menu.id"
              :menu="menu"
              :index="menu.id"
              @click="gotoRouteHandle(menu.id, idx)"
            >
              <span>{{ menu.name }}</span>
            </el-menu-item>
          </el-menu>
        </div>

        <el-menu
          class="aui-navbar__menu"
          mode="horizontal"
          style="min-width: 180px"
        >
          <el-menu-item index="2" @click="fullscreenHandle()">
            <img
              style="position: relative; top: 5px; width: 18px; height: 18px"
              src="~@/assets/imgs/common/message.png"
            />
          </el-menu-item>

          <el-menu-item index="1" class="aui-navbar__avatar">
            <el-dropdown placement="bottom" :show-timeout="0">
              <span class="el-dropdown-link">
                <img
                  v-if="$store.state.user.gender === '2'"
                  src="~@/assets/img/staff-default-avatar-girl.png"
                />
                <img v-else src="~@/assets/img/staff-default-avatar-boy.png" />
                <span>{{ $store.state.user.realName }}</span>
                <i class="el-icon-arrow-down"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="updatePasswordHandle()">{{
                  $t("updatePassword.title")
                }}</el-dropdown-item>
                <el-dropdown-item @click.native="logoutHandle()">{{
                  $t("logout")
                }}</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-menu-item>
        </el-menu>
      </div>
      <!-- 弹窗, 修改密码 -->
      <update-password-work
        v-if="updatePassowrdVisible"
        ref="updatePassowrd"
      ></update-password-work>
    </nav>
    <nav v-else class="aui-navbar main-line"></nav>
  </div>
</template>

<script>
import { messages } from "@/i18n";
import { mapGetters } from "vuex";
import screenfull from "screenfull";
import UpdatePasswordWork from "./main-navbar-update-password-work";
import { clearLoginInfo } from "@/utils";
export default {
  inject: ["refresh"],
  data() {
    return {
      showHeader: true,
      i18nMessages: messages,
      updatePassowrdVisible: false,
      customerName: "",
    };
  },
  components: {
    UpdatePasswordWork,
  },
  created() {
    let platformToken = localStorage.getItem("showHeader") || "";
    if (
      typeof platformToken !== "undefined" &&
      platformToken !== "undefined" &&
      platformToken !== ""
    ) {
      this.showHeader = false;
    }
    this.changeCustomerName();
    console.log("=============================", this.userType);
  },
  computed: {
    userType() {
      return localStorage.getItem("userType");
    },
  },
  methods: {
    toIndexPage() {
      this.$router.replace("/indexWork");
    },
    // 通过menuId与动态(菜单)路由进行匹配跳转至指定路由
    gotoRouteHandle(menuId, idx) {
      var route = window.SITE_CONFIG["dynamicMenuRoutes"].filter(
        (item) => item.meta.menuId === menuId
      )[0];
      if (route) {
        if (route.meta.iframeURL.endsWith("【跳转新页面】")) {
          return window.open(route.meta.iframeURL.slice(0, -7));
        } else {
          this.$router.push({ name: route.name });
        }
      }
      this.$store.state.LevelOneMenuActiveName = menuId;
      this.$store.state.sidebarActiveSubMenuList =
        this.$store.state.sidebarMenuList[idx].children || [];
    },
    changeCustomerName(customerName) {
      this.customerName =
        localStorage.getItem("customerName") || "党建引领基层治理互联管理平台";
    },
    // 全屏
    fullscreenHandle() {
      if (!screenfull.enabled) {
        return this.$message({
          message: this.$t("fullscreen.prompt"),
          type: "warning",
          duration: 500,
        });
      }
      screenfull.toggle();
    },
    // 修改密码
    updatePasswordHandle() {
      this.updatePassowrdVisible = true;
      this.$nextTick(() => {
        this.$refs.updatePassowrd.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(() => {
          //关闭所有标签页
          // tabs, 关闭全部
          this.$store.state.contentTabs = this.$store.state.contentTabs.filter(
            (item) => item.name === "home"
          );

          this.$http
            .post("/auth/login/logout")
            .then(({ data: res }) => {
              if (res.code !== 0) {
                //别处登陆时 退出不提示 直接跳登陆页即可
                if (res.code !== 10007) {
                  this.$message.error(res.msg);
                }
              }

              clearLoginInfo();
              this.$router.push({ name: "login" });
              // if (localStorage.getItem('userType') === 'work') {
              //   this.$router.push({ name: 'loginWork' })
              // } else {
              //   this.$router.push({ name: 'login' })
              // }
            })
            .catch(() => {});
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.main-line {
  height: 50px;
  background: #eeeeee;
}

.m-nav {
  position: relative;
  bottom: 0;
  padding: 0 75px;
  margin-top: 30px;

  .nav {
    height: 38px;
    line-height: 38px;
    background-image: url("~@/assets/imgs/common/nav-bg-center.png");
    background-size: 225px 38px;
    background-repeat: repeat no-repeat;
    color: #ffffff;
    border-bottom: none;

    &::before {
      content: "";
      display: block;
      position: absolute;
      z-index: -1;
      background-image: url("~@/assets/imgs/common/nav-bg-left.png");
      background-size: 90px 38px;
      left: -75px;
      width: 90px;
      height: 38px;
      background-repeat: no-repeat;
    }

    &::after {
      content: "";
      display: block;
      position: absolute;
      z-index: -1;
      background-image: url("~@/assets/imgs/common/nav-bg-right.png");
      background-size: 90px 38px;
      right: -75px;
      width: 90px;
      height: 38px;
      background-repeat: no-repeat;
    }
    ::v-deep .el-menu-item {
      position: relative;
      height: 38px;
      line-height: 34px;
      color: #ffffff;
      font-size: 13px;
      border-bottom: none;
      text-align: center;
      span {
        position: relative;
        z-index: 2;
      }

      &.is-active {
        background-color: transparent;
        border-bottom: none;
        &::before {
          position: absolute;
          z-index: 0;
          width: 105px;
          height: 30px;
          display: block;
          content: "";
          top: -100px;
          left: -100px;
          right: -100px;
          bottom: -100px;
          margin: auto;
          background-image: url("~@/assets/imgs/common/nav-bg-on.png");
          background-size: 105px 30px;
          background-position: center;
          background-repeat: no-repeat;
        }
      }
      &:hover,
      &:active {
        background-color: transparent;
      }
    }
  }
}
</style>
