<style lang="scss">
@import "./main.scss";
</style>
<template>
  <div class="main">
    <!-- 左侧菜单 -->
    <div class="sidebar-menu-con menu-bar">
      <div class="logo-con flex flex_justify_content flex_align_item">
        <img
          style="text-align: center; height: 34px"
          src="https://yun.tuisnake.com/shopmanager/161b5462-7aea-4cb3-970b-769ba0699472.png"
          key="max-logo"
        />
      </div>
      <shrinkable-menu></shrinkable-menu>
    </div>
    <!-- 顶部标题栏主体 -->
    <div class="main-header-con">
      <div class="main-header">
        <div
          class="flex fz_16 fw_bold"
          style="line-height: 60px; padding: 2px 20px"
        >
          <div
            class="flex flex_justify_content flex_align_item pointer"
            :style="{
              marginRight: '25px',
              color: curModel == 1 ? '#1890FF' : '',
            }"
            @click="handleChangeModel(1)"
          >
            <img
              style="width: 22px; height: 22px; margin-right: 8px"
              :src="modelImg1"
            />
            商城模块
          </div>
          <div
            class="flex flex_justify_content flex_align_item pointer"
            :style="{ color: curModel == 2 ? '#1890FF' : '' }"
            @click="handleChangeModel(2)"
          >
            <img
              style="width: 22px; height: 22px; margin-right: 8px"
              :src="modelImg2"
            />
            系统管理
          </div>
        </div>
        <div
          :class="{
            'header-avator-con': navType != 4,
            'header-avator-con nav4': navType == 4,
          }"
        >
          <!-- 通知消息 -->
          <!-- <message-tip v-if="tipsMessage" :res="tipsMessage"></message-tip> -->
          <!-- 用户头像 -->
          <div class="user-dropdown-menu-con">
            <Row
              type="flex"
              justify="end"
              align="middle"
              class="user-dropdown-innercon"
            >
              <Dropdown
                transfer
                trigger="hover"
                @on-click="handleClickUserDropdown"
              >
                <div class="dropList">
                  <span class="main-user-name">{{ userInfo.nickName }}</span>
                  <Icon type="md-arrow-dropdown" />
                  <Avatar
                    :src="avatarPath"
                    style="background: #fff; margin-left: 10px"
                  ></Avatar>
                </div>
                <DropdownMenu slot="list">
                  <DropdownItem name="personalCenter">{{
                    $t("userCenter")
                  }}</DropdownItem>
                  <DropdownItem name="changePass">{{
                    $t("changePass")
                  }}</DropdownItem>
                  <DropdownItem name="loginOut" divided>{{
                    $t("logout")
                  }}</DropdownItem>
                </DropdownMenu>
              </Dropdown>
            </Row>
          </div>
        </div>
      </div>
      <!-- 已打开的页面标签 -->
      <div class="tags-con">
        <tags-page-opened :pageTagsList="pageTagsList"></tags-page-opened>
      </div>
    </div>
    <div class="single-page-con">
      <div class="single-page">
        <router-view></router-view>
      </div>
    </div>
    <!-- 全局加载动画 -->
    <circleLoading class="loading-position" v-show="loading" />
  </div>
</template>

<script>
import shrinkableMenu from "./main-parts/shrinkable-menu/shrinkable-menu-new.vue";
import tagsPageOpened from "./main-parts/tags-page-opened.vue";
import messageTip from "./main-parts/message-tip.vue";
import circleLoading from "@/components/lili/circle-loading.vue";
import Cookies from "js-cookie";
import util from "@/libs/util.js";
import { getNoticePage, logout } from "@/api/index";
import { getBaseSite } from "@/api/common";

var client;
export default {
  components: {
    shrinkableMenu,
    tagsPageOpened,
    messageTip,
    circleLoading,
  },
  data() {
    return {
      sliceNum: 5, // 展示nav数量
      userInfo: "", // 用户信息
      navType: 1, // nav类型
      tipsMessage: "", // 通知消息
      domainLogo: "",
    };
  },
  computed: {
    loading() {
      return this.$store.state.app.loading;
    },
    pageTagsList() {
      return this.$store.state.app.pageOpenedList; // 打开的页面的页面对象
    },
    avatarPath() {
      return localStorage.avatorImgPath;
    },
    lang() {
      return this.$store.state.app.lang;
    },
    curModel() {
      return this.$store.state.app.curModel;
    },
    modelImg1() {
      return this.curModel == 1
        ? "https://yun.tuisnake.com/shopmanager/de555eba-0c7c-4ce6-b04c-d9a59d83d1b5.png"
        : "https://yun.tuisnake.com/shopmanager/af05a2bf-1311-4599-9ed0-3aec738e704c.png";
    },
    modelImg2() {
      return this.curModel == 2
        ? "https://yun.tuisnake.com/shopmanager/6b3434ff-bbe6-4cf7-877c-044873781b99.png"
        : "https://yun.tuisnake.com/shopmanager/07350a2b-683c-4a3c-beec-0a0b379af55e.png";
    },
  },

  methods: {
    init() {
      // 菜单初始化
      let userInfo = JSON.parse(Cookies.get("userInfoManager"));

      this.userInfo = userInfo;
      this.checkTag(this.$route.name);
      let currWidth = document.body.clientWidth;
      if (currWidth <= 1200) {
        this.sliceNum = 2;
      }
      // this.domainLogo = localStorage.getItem("icon");
      // let link =
      //   document.querySelector("link[rel*='icon']") ||
      //   document.createElement("link");
      // link.type = "image/x-icon";
      // link.href = '';
      // link.rel = "shortcut icon";
      // document.getElementsByTagName("head")[0].appendChild(link);
      window.document.title = localStorage.getItem("title") + " - 运营后台";
      // 读取未读消息数
      getNoticePage({}).then((res) => {
        if (res.success) {
          this.tipsMessage = res.result;

          this.$store.state.notices = res.result;
        }
      });
    },
    handleChangeModel(type) {
      this.$store.commit("setCurModel", type);
    },
    //用户头像下方抽屉点击
    handleClickUserDropdown(name) {
      //个人中心
      if (name === "personalCenter") {
        util.openNewPage(this, "personal-center");
        this.$router.push({
          name: "personal-center",
        });
      }
      //修改密码
      else if (name === "changePass") {
        util.openNewPage(this, "change-password");
        this.$router.push({
          name: "change_password",
        });
      }
      // 退出登录
      else if (name === "loginOut") {
        logout().then((res) => {
          this.$store.commit("logout", this);
          this.$store.commit("setAdded", false);
          this.setStore("accessToken", "");
          this.setStore("refreshToken", "");
          this.$router.push({ path: "/login" });
        });
      }
    },
    //切换标签
    checkTag(name) {
      let openpageHasTag = this.pageTagsList.some((item) => {
        if (item.name == name) {
          return true;
        }
      });
      if (!openpageHasTag) {
        //  解决关闭当前标签后再点击回退按钮会退到当前页时没有标签的问题
        util.openNewPage(
          this,
          name,
          this.$route.params || {},
          this.$route.query || {}
        );
      }
    },
    //宽度动态计算
    resize() {
      let currWidth = document.body.clientWidth;
      let count = currWidth / 300;
      if (count > 6) {
        this.sliceNum = 6;
      } else {
        this.sliceNum = count;
      }
    },
  },
  watch: {
    $route(to, from) {
      this.checkTag(to.name);
      localStorage.currentPageName = to.name;
    },
  },
  mounted() {
    this.init();
    let that = this;
    this.resize();
    window.addEventListener("resize", function () {
      that.resize();
    });
  },
  created() {
    // 显示打开的页面的列表
    this.$store.commit("setOpenedList");
  },
};
</script>
