<template>
  <div
    class="bg-menu"
    :class="{
      transparent: transparentBg,
      'is-front': isFront,
    }"
  >

    <!-- LOGO -->
    <router-link to="/" class="bg-logo">
      <img style="width:140px;height:22px;" src="../assets/imgs/img_logo.png"/>
      <!-- <img style="width:40px;height:40px;" :src="logo"/> -->
      <!-- <span>{{name}}</span> -->
    </router-link>

    <!-- 菜单 -->
    <div class="main-menu" v-if="userInfo&& userInfo.user">
      <!-- 前台菜单 -->
      <div class="font-menu" v-if="fontMenu&&fontMenu.length">
        <div class="nav-item" :style="path==item.path?{backgroundColor:'#2b4695'}:''" v-show="item.show" v-for="(item,index) in fontMenu" :key="'font'+index">
          <span @click="menuAction(item.path,item)">
            {{item.menuName}}
            <bg-icon v-if="item.children&&item.children.length" icon="#bg-ic-arrow-down" style="font-size:8px;margin-left: 3px;"></bg-icon>
          </span>
          <div class="menu-virtual"></div>
          <div class="menu-list" v-if="item.children&&item.children.length">
            <div class="menu-item text_clip" @click="childMenuAction(it,item)" v-show="it.show" v-for="(it,idx) in item.children" :key="'font-children'+idx">{{it.menuName}}</div>
          </div>
        </div>
      </div>

      <!-- 后管菜单 -->
      <div class="manager-menu" v-if="managerMenu&&managerMenu.length">
        <div class="nav-item" :style="path==item.path?{backgroundColor:'#2b4695'}:''" v-show="item.show" v-for="(item,index) in managerMenu" :key="'manager'+index">
          <span @click="menuAction(item.path,item)">
            {{item.menuName}}
            <bg-icon v-if="item.children&&item.children.length" icon="#bg-ic-arrow-down" style="font-size:8px;margin-left: 3px;"></bg-icon>
          </span>
          <div class="menu-virtual"></div>
          <div class="menu-list" v-if="item.children&&item.children.length">
            <div class="menu-item text_clip" @click="childMenuAction(it,item)" v-show="it.show" v-for="(it,idx) in item.children" :key="'manager-children'+idx">{{it.menuName}}</div>
          </div>
        </div>
      </div>
    </div>

    <div class="person-menu" v-if="userInfo&& userInfo.user">
      <!-- 信息 -->
      <!-- 购物车 -->
      <!-- 用户中心 -->
      <div class="user-center nav-item">
        <span class="user-icon">
          <img style="width:24px;height:24px;border-radius:50%;vertical-align:middle;" :src="userInfo&&userInfo.headPortrait?userInfo.headPortrait:'/src/assets/imgs/home_ic_user.png'" />
        </span>
        <span class="user-name" v-if="userInfo && userInfo.user">
          {{ userInfo.name || userInfo.user }}
  
          <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.user" class="menu-list">
          <div
            v-for="(item, index) in user_arr"
            class="menu-item text_clip"
            :key="index + 700"
            @click="menuAction(item.path,item)"
          >
            {{ item.menuName }}
          </div>
          <div class="menu-item text_clip" @click="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';

export default {
  name: "BgMenu",
  components: {  },
  props:{
    path:{
      type:String,
      default:''
    }
  },
  data() {
    return {
      documentScrollTop: 0,
      user_arr: [],
      cartList: [],
      showCart: true,
      showShopMenu: false,
      logo:'../assets/imgs/img_logo.png',
      name:''
    };
  },
  computed: {
    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.menuType=='font'){
            temp.push(e)
          }
        });
        return temp
      },
      managerMenu(state){
        let temp = []
        state.menu.forEach(e => {
          if(e.menuType=='system'){
            temp.push(e)
          }
        });
        return temp
      }
    })
    
  },
  watch: {
    nameFlag(n,o){
      this.getLogo()
    }
    // cartState() {
    //   this.getCartList();
    // },
    // routePath(val) {
    //   return "/"; // FIXME: 当前选中的路由
    // },
  },
  methods: {
    ...mapMutations(["setCartNum"]),
    navAction(path) {
      this.$router.push(path);
    },
    changezz(item){
      this.$trace.setOptionValue('appID',item.value)
    },
    getLogo(){
      // this.$axios.get(`/apaas/backmgt/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.get(`/apaas/backmgt/logout`).then(res=>{
          if(res.data.code=='200'){
            this.$router.push('/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 = `/login?redirect=${path}`;
      } else {
        //true 同区域跳转
        //false 跨系统跳转
        if(parent.menuType==this.$store.state.menuObj[this.path].menuType){
          this.$router.push(n);
        }else{
          let temp = ''
          if(this.$store.state.menuObj[n].menuType=='font'||this.$store.state.menuObj[n].menuType=='system'){
            temp = '/apaas/ui/#'
          }else if(this.$store.state.menuObj[n].menuType=='manager'){
            temp = '/apaas/system/uir/#'
          }
          window.location.href = temp+n
        }
      }
    },
    childMenuAction(v,parent) {
      if(v.children&&v.children.length){
        if(v.children[0].children){
          this.pathToUrl(v.children[0].children[0].path,parent)
        }else{
          for (let index = 0; index < v.children.length; index++) {
            const e = v.children[index];
            if(e.show){
              this.pathToUrl(e.path,parent)
              break
            }
          }
        }
      }else{
        this.pathToUrl(v.path,parent)
      }
    },
    pathToUrl(path,parent){
      //判断是否是外链，是则新开窗口跳转
      if(path.includes('http')){
        window.open(path,'_blank')
      }else{
        debugger
        //判断是否是同区域内跳转
        //非同区域跳转需要改前缀，如apaas/ui跳转到apaas/manager
        if(parent.menuType==this.$store.state.menuObj[this.path].menuType){
          this.$router.push(path);
        }else{
          let temp = ''
          if(this.$store.state.menuObj[path].menuType=='font'||this.$store.state.menuObj[path].menuType=='system'){
            temp = '/apaas/ui/#'
          }else if(this.$store.state.menuObj[path].menuType=='manager'){
            temp = '/apaas/system/uir/#'
          }
          window.location.href = temp+path
        }
      }
    },
    intoCartDetail() {
      this.$router.push({ name: "shopCart" });
    },
    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 ''
      }
    },
    
  },
  created() {
    console.log(this.userInfo);
    this.getLogo()
    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);
  },
  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;
      }
    }
  }

  // 超市菜单
  > .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%;
    }
    .manager-menu{
      height: 100%;
    }

    .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: #2b4695;
      }
    }
  }

  // 分割线
  > .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;

    &::before{
      content:'';
      width: 1px;
      height: 24px;
	    background-color: #5c6da4;
      position: absolute;
      left: 0;
      top: 16px;
    }
  }
  // 用户中心
  .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>
