<template>
  <nav class="site-navbar" :class="'site-navbar--' + navbarLayoutType">
    <div class="site-navbar__header">
      <h1 class="site-navbar__brand" @click="$router.push({ name: 'home' })">
        <a class="site-navbar__brand-lg" href="javascript:;"
          >大妈集市商户端</a
        >
        <a
          class="site-navbar__brand-mini"
          href="javascript:;"
          style="font-size: 14px;"
          >商户端</a
        >
      </h1>
    </div>
    <div class="site-navbar__body clearfix">
      <el-menu class="site-navbar__menu" mode="horizontal">
        <el-menu-item
          class="site-navbar__switch"
          index="0"
          @click="sidebarFold = !sidebarFold"
        >
          <icon-svg name="zhedie"></icon-svg>
        </el-menu-item>
        <el-tooltip :open-delay="800" :content="'刷新【'+mainTabsActiveTitle+'】页面'" placement="bottom" effect="light">
          <el-menu-item
            class="site-navbar__switch"
            index="1"
            @click="refresh()"
          >
            <i class="el-icon-refresh"></i>
          </el-menu-item>
        </el-tooltip>
      </el-menu>
      <el-menu
        class="site-navbar__menu site-navbar__menu--right"
        mode="horizontal"
      >
        <!-- <el-menu-item index="0" @click="openBigScreen">
          <template slot="title">
            <el-badge value="">
              <i class="el-icon-s-data"></i>
              数据驾驶舱
            </el-badge>
          </template>
        </el-menu-item> -->
        <el-menu-item index="1" @click="$router.push({ name: 'theme' })">
          <template slot="title">
            <!-- 暂时不要右上角的图标 -->
            <el-badge value="">
              <icon-svg name="shezhi" class="el-icon-setting"></icon-svg>
            </el-badge>
          </template>
        </el-menu-item>
        <el-menu-item index="2" @click="fullscreen">
          <template slot="title">
            <!-- 暂时不要右上角的图标 -->
            <el-badge value="" @click="fullscreen">
              <icon-svg name="fullscreen" class="el-icon-scan"></icon-svg>
            </el-badge>
          </template>
        </el-menu-item>
        <el-menu-item class="site-navbar__avatar" index="3">
          <el-dropdown :show-timeout="0" placement="bottom">
            <span class="el-dropdown-link">
              <img src="~@/assets/img/avatar.png" :alt="userName" />{{
                userName
              }}
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="updatePasswordHandle()"
                >修改密码</el-dropdown-item
              >
              <el-dropdown-item @click.native="logoutHandle()"
                >退出</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password
      v-if="updatePassowrdVisible"
      ref="updatePassowrd"
    ></update-password>
  </nav>
</template>

<script>
import screenfull from "screenfull";
import UpdatePassword from "./main-navbar-update-password";
import { clearLoginInfo } from "@/utils";
export default {
  inject: ["refresh"],
  data() {
    return {
      updatePassowrdVisible: false,
    };
  },
  components: {
    UpdatePassword,
  },
  computed: {
    navbarLayoutType: {
      get() {
        return this.$store.state.common.navbarLayoutType;
      },
    },
    sidebarFold: {
      get() {
        return this.$store.state.common.sidebarFold;
      },
      set(val) {
        this.$store.commit("common/updateSidebarFold", val);
      },
    },
    mainTabs: {
      get() {
        return this.$store.state.common.mainTabs;
      },
      set(val) {
        this.$store.commit("common/updateMainTabs", val);
      },
    },
    userName: {
      get() {
        return this.$store.state.user.name;
      },
    },
    mainTabsActiveTitle(){
      return this.$store.state.common.mainTabs.find(item=>{
        return item.name==this.$store.state.common.mainTabsActiveName
      })?.title||'';
    }
  },
  methods: {
    // 修改密码
    updatePasswordHandle() {
      this.updatePassowrdVisible = true;
      this.$nextTick(() => {
        this.$refs.updatePassowrd.init();
      });
    },
    // 退出
    logoutHandle() {
      this.$confirm(`确定进行[退出]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$api.logout().then(({ data }) => {
            clearLoginInfo();
            this.$router.push({ name: "login" });
          });
        })
        .catch(() => {});
    },
    fullscreen() {
      screenfull.toggle();
    },
    //打开大屏展示-部署在另一个项目中
    openBigScreen() {
      if (process.env.NODE_ENV !== "production") {
        window.location.href = `http://localhost:8080?t=${this.$cookie.get(
          "token"
        )}`;
      } else {
        window.location.href = `http://red-admin.yingruikeji.cn/screen?t=${this.$cookie.get(
          "token"
        )}`;
      }
    },
  },
};
</script>
