<template>
  <div
    class="bg-menu"
    :class="{
      transparent: transparentBg,
      'is-front': isFront,
    }">
    <!-- LOGO -->
    <router-link to="/" class="bg-logo">
      <img
        style="width: 22px; height: 22px"
        :src="systemLogo ? JSON.parse(systemLogo)[0].url : getImageUrl('img_logo.png')" />
      <span>智能运维管理平台</span>
    </router-link>

    <!-- 菜单 -->
    <div class="main-menu">
      <!-- 前台菜单 -->
      <div v-if="fontMenu && fontMenu.length" class="font-menu">
        <div
          v-for="(item, index) in fontMenu"
          v-show="item.menuType !== 2"
          :key="'font' + index"
          class="nav-item"
          :style="path == item.path ? { backgroundColor: '#3753a4' } : ''"
          @click="menuAction(item, item)">
          <span>
            {{ item.menuName }}
          </span>
        </div>
      </div>

      <!-- 后管菜单 -->
      <!-- <div v-if="managerMenu && managerMenu.length" class="manager-menu">
        <div
          v-for="(item, index) in managerMenu"
          v-show="item.menuType !== 2"
          :key="'manager' + index"
          class="nav-item"
          :style="path == item.path ? { backgroundColor: '#3753a4' } : ''"
          @click="menuAction(item, item)">
          <span>
            {{ item.menuName }}
          </span>
        </div>
      </div> -->
    </div>

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

          <bg-icon icon="#bg-ic-arrow-down" style="font-size: 8px; margin-left: 3px" />
        </span>
        <span v-else class="user-name"> 请登录 </span>
        <div class="menu-virtual"></div>
        <div v-if="userInfo" class="menu-list">
          <div
            v-for="(item, index) in systemMenu"
            :key="index + 700"
            class="menu-item text_clip"
            @click.stop="menuAction(item, item)">
            {{ item.menuName }}
          </div>
          <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 { getImageUrl } from "@/services/helper.js";
import { normalizeProps } from "vue-demi";

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;
    },
    subscribeNum() {
      return this.$store.state.subscribeNum;
    },
    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;
    },
    ...mapState({
      fontMenu(state) {
        let temp = [];
        state.menu?.forEach((e) => {
          if (e.dict_group_id == "f2779b4a-92c9-4dd0-b4c5-6734d416f331") {
            //font
            temp.push(e);
          }
        });
        return temp;
      },
      // managerMenu(state) {
      //   let temp = [];
      //   state.menu?.forEach((e) => {
      //     if (e.dict_group_id == "f2779b4a-92c9-4dd0-b4c5-6734d416f331") {
      //       temp.push(e);
      //     }
      //   });
      //   return temp;
      // },
      systemMenu(state) {
        let temp = [];
        state.menu?.forEach((e) => {
          if (e.dict_group_id == "475f4e5e-c953-469b-8314-24821a241d1d") {
            temp.push(e);
          }
        });
        return temp;
      },
    }),
    systemLogo() {
      return this.$store.state.systemLogo;
    },
  },
  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);
    },
    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("/v1/api/user/logout").then((res) => {
          if (res.data.code == "200") {
            window.location.href = "/so/manage/ui/#/login";
            this.$store.commit("setUserInfo", null);
            this.$store.commit("setMenu", 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.menuType !== 1 && 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.new_window) {
        window.open(item.sourcePath ? item.sourcePath : item.path, "_blank");
      } else {
        // 当前窗口打开
        if (item.path.indexOf("#") > -1) {
          // 携带#说明是其它项目，使用window.location.href
          window.location.href = item.path;
        } else {
          // 不携带#说明是本项目，使用router.push
          this.$router.push(item.path);
        }
      }
    },
    intoCartDetail() {
      this.$router.push({ name: "shopCart" });
    },
    goSubscription() {
      window.location.href = "/apaas/portal/ui/#/my-subscription";
    },
    deleteItem(id) {
      this.$api.serviceShop.delShoppingCart([id]).then((response) => {
        if (response.data.success == 1) {
          this.$store.commit("setCartState");
        }
      });
    },
    shopMenuAction() {
      this.showShopMenu = false;
    },
    shopMenEventListener() {
      this.showShopMenu = false;
    },
    scrollAction() {
      this.documentScrollTop = document.documentElement.scrollTop || 0;
    },
    getFirstPath(item) {
      if (item.path) {
        if (item.children) {
          if (item.children[0].children) {
            if (item.children[0].children[0].children) {
              return item.children[0].children[0].children[0].path;
            } else {
              return item.children[0].children[0].path;
            }
          } else {
            return item.children[0].path;
          }
        } else {
          return item.path;
        }
      } else {
        return "";
      }
    },
    // getSubscriptNum() {
    //   this.$axios.get("/apaas/service/v5/subscription/list").then((res) => {
    //     if (res.data.code == 200) {
    //       let data = res.data.data;
    //       this.$store.commit("setSubscribeNum", data.total);
    //     }
    //   });
    // },
  },
  created() {
    // console.log(this.userInfo);
    this.getLogo();
    // this.getSubscriptNum();
    if (this.userInfo) {
      // this.getUserDetail(this.userInfo.user_id)
    } else {
      // this.getUserInfo()
    }
  },
  mounted() {
    // console.log(this.currentMenuIndex);

    this.$nextTick(this.scrollAction);
    window.addEventListener("scroll", this.scrollAction, true);
    window.addEventListener("click", this.shopMenEventListener);
  },
  unmounted() {
    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: 0 4px 12px 0 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;
      }
    }
  }

  // 超市菜单
  > .bg-shop-menu {
    height: 100%;

    > .shop-menu-btn {
      width: 105px;
      height: 100%;
      background-color: #062a44;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;

      > span {
        font-size: 16px;
        line-height: 20px;
        color: #ffffff;
        margin-left: 10px;
      }
    }

    > .shop-menu {
      position: fixed;
      top: 56px;
      left: 0;
      width: 900px;
      height: calc(100vh - 56px);
    }
  }

  // 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;
    }
  }

  // 主菜单
  > .main-menu {
    height: 100%;
    flex-grow: 1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-right: 8px;

    .font-menu {
      height: 100%;
      display: flex;
    }
    .manager-menu {
      height: 100%;
      display: flex;
    }

    .nav-item {
      font-size: 14px;
      height: 56px;
      padding: 0 24px;
      line-height: 56px;
      text-align: center;
      position: relative;

      > .menu-virtual {
        left: 0;
        right: auto;
      }
      > .menu-list {
        left: 0;
        right: auto;
        > .menu-item {
          cursor: pointer;
        }
      }

      &:hover {
        background-color: #3753a4;
      }
    }
  }

  // 分割线
  > .shop-line {
    width: 1px;
    height: 24px;
    background-color: #536883;
    margin-right: 50px;
  }

  // 购物车
  > .shop-cart {
    height: 100%;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    margin-right: 50px;

    &:hover {
      .cart-detail {
        display: block;
      }
    }

    > .cart-info {
      display: flex;
      justify-content: space-between;
      align-items: center;

      > span {
        min-width: 36px;
        height: 18px;
        background-color: #ff6a00;
        border-radius: 9px;
        padding: 0 9px;
        box-sizing: border-box;
        font-size: 12px;
        line-height: 18px;
        color: #ffffff;
        text-align: center;
        margin-left: 5px;
      }
    }

    > .cart-detail {
      width: 280px;
      background-color: #ffffff;
      box-shadow: 0px 3px 6px 0px rgba(15, 19, 65, 0.05);
      border-radius: 8px;
      position: absolute;
      top: 56px;
      left: -20px;
      display: none;
      z-index: 3;
      padding: 10px 20px;

      > .cart-title {
        color: #8890a7;
        font-size: 14px;
        height: 30px;
        line-height: 30px;
      }

      > .cart-list {
        height: 80px;
        padding: 10px 0;
        display: flex;
        line-height: 26px;
        border-bottom: #f4f7fc 2px solid;

        &:nth-last-child(2) {
          border-bottom: 0;
        }

        > .shop_img {
          width: 60px;
          height: 60px;
          border-radius: 8px;
        }

        > .shop_cell_msgs {
          width: calc(100% - 80px);
          margin-left: 10px;

          > .shop_cell_name {
            color: #2d3867;
            font-size: 14px;
            line-height: 24px;

            &:hover {
              color: #0d0807;
            }
          }

          > .shop_cell_msg {
            color: #8890a7;
            font-size: 12px;
            line-height: 18px;
          }

          > .over_one {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
      }

      > .cart-action {
        width: 100%;
        height: 50px;
        text-align: right;
        padding: 15px 0;
        line-height: 30px;

        > .el-button {
          background-color: #ff6a00;
          color: #fcefd6;
        }
      }

      &::before {
        content: "";
        width: 16px;
        height: 16px;
        background-color: #fff;
        position: absolute;
        top: -7px;
        left: 38px;
        transform: rotate(45deg);
      }
    }
  }

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

    &::before {
      content: "";
      width: 1px;
      height: 24px;
      background-color: #5c6da4;
      position: absolute;
      left: 0;
      top: 16px;
    }

    .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;
      line-height: 20px;
      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>
