<template>
  <div
    class="bg-menu"
    :class="{
      transparent: transparentBg,
      'is-front': isFront,
    }">
    <!-- LOGO -->
    <div @click="gotoPage" class="bg-logo">
      <img
        style="width: 140px; height: 22px"
        :src="systemLogo ? JSON.parse(systemLogo)[0].url : getImageUrl('img_logo.png')" />
    </div>

    <div class="person-menu" v-if="userInfo && userInfo.system_id">
      <!-- 用户中心 -->
      <div class="user-center nav-item">
        <span class="user-icon">
          <img
            style="width: 24px; height: 24px; border-radius: 50%; vertical-align: middle"
            v-if="userInfo && userInfo.logo"
            :src="userInfo && userInfo.logo" />
          <img
            style="width: 24px; height: 24px; border-radius: 50%; vertical-align: middle"
            v-else
            src="../assets/imgs/home_ic_user.png" />
        </span>
        <span class="user-name" v-if="userInfo && userInfo.system_id">
          {{ userInfo.system_account || userInfo.system_id }}

          <bg-icon icon="#bg-ic-arrow-down" style="font-size: 8px; margin-left: 3px"></bg-icon>
        </span>
        <span class="user-name" v-else> 请登录 </span>
        <div class="menu-virtual"></div>
        <div v-if="userInfo && userInfo.system_id" class="menu-list">
          <div class="menu-item text_clip" @click.stop="menuAction('logout')">退出登录</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import { clearCookie } from "../services/cookie.js";
import { resetRouter } from "../router/index";
import { normalizeProps } from "vue-demi";
import { getImageUrl } from "@/services/helper.js";

export default {
  name: "BgMenu",
  components: {},
  props: {
    path: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      getImageUrl,
      documentScrollTop: 0,
      cartList: [],
      showCart: true,
      showShopMenu: false,
      logo: "../assets/imgs/img_logo.png",
      name: "",
    };
  },
  computed: {
    msgUnreadNum() {
      return this.$store.state.msgUnreadNum;
    },
    nameFlag() {
      return this.$store.state.nameFlag;
    },
    userInfo() {
      return this.$store.state.userInfo || {};
    },
    unreadMsgCount() {
      return this.$store.state.unreadMessageCount;
    },
    transparentBg() {
      return (
        this.documentScrollTop < 180 &&
        (this.$route.name === "shopRecommend" || this.$route.name === "shopSearch")
      );
    },
    isFront() {
      let path = this.$route.path;
      return false;
    },
    systemLogo() {
      return this.$store.state.systemLogo;
    },
    ...mapState({
      fontMenu(state) {
        let temp = [];
        // state.menu.forEach(e => {
        //   if(e.dict_group_id=='2cb4f767-fad7-44f2-afa3-f055e15dd2b6'){//font
        //     temp.push(e)
        //   }
        // });
        return temp;
      },
      managerMenu(state) {
        let temp = [];
        // state.menu.forEach(e => {
        //   if(e.dict_group_id=='64c156e0-bfff-4bfc-a63a-56effe130a25'){//system

        //     // 暂时处理运营管控中心-调用日志，后期规划位置后修改
        //     if (e.menuName !== '运营看板') {
        //       temp.push(e)
        //     }

        //     // temp.push(e)
        //   }
        // });
        return temp;
      },
      systemMenu(state) {
        let temp = [];
        state.menu.forEach((e) => {
          if (e.dict_group_id == "09938937-3db9-47de-b967-7777ea4ebb2d") {
            //manager
            temp.push(e);
          }
        });
        return temp;
      },
    }),
  },
  watch: {
    nameFlag(n, o) {
      this.getLogo();
    },
    // cartState() {
    //   this.getCartList();
    // },
    // routePath(val) {
    //   return "/"; // FIXME: 当前选中的路由
    // },
  },
  methods: {
    openMsg() {
      this.$emit("openMsg", true);
    },
    ...mapMutations(["setCartNum"]),
    navAction(path) {
      this.$router.push(path);
    },
    gotoPage() {
      window.open("/so/manage/ui/#/", "_blank");
    },
    changezz(item) {
      this.$trace.setOptionValue("appID", item.value);
    },
    getLogo() {
      // this.$axios.get(`/apaas/system/getPicAndSystemName`).then(res=>{
      //   if(res.data.code==200){
      //     let data = res.data.data
      //     this.name=data.systemName
      //     this.logo = data.picture
      //     document.title = this.name
      //     var link = document.querySelector("link[rel*='icon']")||document.createElement('link');
      //     link.type = 'image/x-icon';
      //     link.rel = 'shortcut icon';
      //     link.href = data.picture;
      //     document.getElementsByTagName('head')[0].appendChild(link);
      //   }
      // })
    },
    menuAction(n, parent) {
      if (n == "logout") {
        this.$axios.post(`/apaas/system/v5/user/logout`).then((res) => {
          if (res.data.code == "200") {
            window.location.href = `/so/manage/ui/#/login`;
            this.$store.commit("setUserInfo", null);
            clearCookie("bgToken");
            this.$message.success("退出成功");
            resetRouter();
          } else {
            this.$message.error("退出失败");
          }
        });
      } else if (n == "login") {
        let path = this.$route.path;
        window.location.href = `/so/manage/ui/#/login?redirect=${path}`;
      } else {
        //是否有子路由，有则寻找子路由的第一个进行跳转，无则直接进行跳转
        //使用childMenuAction是模拟template中，子路径点击跳转使用childMenuAction，
        //防止出现层级错误的问题
        if (parent.children && parent.children.length && parent.menuType !== 1) {
          this.childMenuAction(parent.children[0], parent);
        } else {
          this.pathToUrl(n, parent);
        }
      }
    },
    childMenuAction(v, parent) {
      //最多只有2层目录，childMenuAction已经进入第一层，只需判断是否还有第二层即可
      //有第二层则继续找第二层的第一个跳转
      //无第二层则直接跳转
      if (v.children && v.children.length) {
        for (let index = 0; index < v.children.length; index++) {
          const e = v.children[index];
          if (e.menuType !== 2) {
            this.pathToUrl(e, parent);
            break;
          }
        }
      } else {
        this.pathToUrl(v, parent);
      }
    },
    pathToUrl(item, parent) {
      //判断是否是外链，是则新开窗口跳转
      if (item.path.includes("http")) {
        window.open(item.path, "_blank");
      } else {
        //判断是否是新窗口打开
        if (item.new_window) {
          window.open(item.path, "_blank");
        } else {
          this.$router.push(item.path);
        }
      }
    },
    shopMenuAction() {
      this.showShopMenu = false;
    },
    shopMenEventListener() {
      this.showShopMenu = false;
    },
    scrollAction() {
      this.documentScrollTop = document.documentElement.scrollTop || 0;
    },
  },
  created() {
    this.getLogo();
    if (this.userInfo) {
      // this.getUserDetail(this.userInfo.user_id)
    } else {
      // this.getUserInfo()
    }
  },
  mounted() {
    this.$nextTick(this.scrollAction);
    window.addEventListener("scroll", this.scrollAction, true);
    window.addEventListener("click", this.shopMenEventListener);
  },
  destroyed() {
    window.removeEventListener("scroll", this.scrollAction, true);
    window.removeEventListener("click", this.shopMenEventListener);
  },
};
</script>

<style lang="scss" scoped>
.bg-menu {
  width: 100%;
  height: 56px;
  background-color: #253b7e;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;

  // 二级菜单公有样式
  .nav-item {
    font-size: 14px;
    color: #fff;
    cursor: pointer;
    position: relative;

    > p {
      > i {
        font-size: 12px;
        line-height: 56px;
        color: #8899aa;
        margin-left: 5px;
      }
    }

    > .menu-virtual {
      width: 128px;
      height: 10px;
      position: absolute;
      top: 49px;
      right: 0;
      z-index: 9;
      background-color: rgba(0, 0, 0, 0);
    }

    > .menu-list {
      box-shadow: 0px 4px 12px 0px rgba(18, 30, 63, 0.1);
      background-color: #ffffff;
      border-radius: 4px;
      overflow: hidden;
      padding: 4px 0;
      position: absolute;
      top: 59px;
      right: 0;
      z-index: 9;
      text-align: center;
      display: none;

      > .menu-item {
        width: 144px;
        height: 34px;
        font-size: 14px;
        line-height: 34px;
        padding: 0 16px;
        background-color: #fff;
        color: #202531;
        text-align: left;
        white-space: nowrap;

        > .width-num {
          display: inline-block;
          vertical-align: middle;
          min-width: 30px;
          background-color: #ff6a00;
          font-size: 10px;
          line-height: 16px;
          color: #fff;
          padding: 0 8px;
          margin-left: 5px;
          box-sizing: border-box;
          border-radius: 8px;
          text-align: center;
          overflow: hidden;
        }

        &:hover {
          background-color: #eff2fa;

          /* > .width-num {
            background-color: #fcefd6;
            color: #ff6a00;
          } */
        }
      }
    }

    &:hover {
      > .menu-list {
        display: block;
      }
    }
  }

  // LOGO
  > .bg-logo {
    display: flex;
    justify-content: center;
    align-items: center;
    line-height: 1;
    margin: 0 50px 0 20px;
    text-decoration: none;

    > span {
      font-size: 24px;
      font-weight: bolder;
      margin-left: 10px;
      background: linear-gradient(to bottom, #d6e0fc, #9cb0e5);
      -webkit-background-clip: text;
      color: transparent;
      line-height: 40px;
    }
  }

  //个人菜单
  .person-menu {
    height: 100%;
    padding-left: 32px;
    position: inherit;
    display: flex;
    align-items: center;
    column-gap: 35px;

    .message {
      height: 16px;
      line-height: 16px;
      background-color: #d14731;
      border-radius: 8px;
      padding: 0 6px;
      font-size: 12px;
      color: #ffffff;
      position: absolute;
      top: -8px;
      left: 12px;
    }
  }
  // 用户中心
  .user-center {
    height: 100%;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    line-height: 1;
    margin-right: 35px;
    position: relative;

    > .user-icon {
      position: relative;
      margin-right: 10px;

      > .has_msg {
        width: 8px;
        height: 8px;
        border-radius: 50%;
        background-color: #ff6a00;
        position: absolute;
        top: -5px;
        right: -5px;
      }
    }

    > .user-name {
      min-width: 4em;
      max-width: 8em;
      word-break: break-all;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      font-size: 14px;
    }
  }

  &.transparent {
    background-color: transparent;

    > .bg-shop-menu {
      > .shop-menu-btn {
        background-color: transparent;
      }
    }

    &::after {
      background-color: #2f607f;
    }
  }

  &.is-front {
    height: 40px;

    .bg-logo {
      display: none;
    }

    .nav-item {
      height: 40px;
      line-height: 40px;

      > p {
        > i {
          line-height: 40px;
        }
      }

      > .menu-list {
        top: 35px;
      }

      &.user-center {
        line-height: 1;

        .user-icon {
          > .has_msg {
            top: 0;
          }
        }
      }
    }
  }
}
</style>
