<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="../assets/imgs/img_logo.png"/>
    </div>

    <!-- 菜单 -->
    <div class="main-menu" v-if="userInfo&& userInfo.system_id">
      <!-- 前台菜单 -->
      <div class="font-menu" v-if="fontMenu&&fontMenu.length">
        <div class="nav-item" :style="path==item.path?{backgroundColor:'#2b4695'}:''" v-show="item.menuType!==2" 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.menuType!==2" 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.menuType!==2" 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.menuType!==2" 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.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
            v-for="(item, index) in systemMenu"
            class="menu-item text_clip"
            :key="index + 700"
            @click.stop="menuAction(item.path,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 { normalizeProps } from 'vue-demi';

export default {
  name: "BgMenu",
  components: {  },
  props:{
    path:{
      type:String,
      default:''
    }
  },
  data() {
    return {
      documentScrollTop: 0,
      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.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
        //     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: {
    ...mapMutations(["setCartNum"]),
    navAction(path) {
      this.$router.push(path);
    },
    gotoPage(){
      window.open('/apaas/portal/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 = `/apaas/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 = `/apaas/manage/ui/#/login?redirect=${path}`;
      } else {
        //是否有子路由，有则寻找子路由的第一个进行跳转，无则直接进行跳转
        //使用childMenuAction是模拟template中，子路径点击跳转使用childMenuAction，
        //防止出现层级错误的问题
        if(parent.children&&parent.children.length){
          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.path,parent)
            break
          }
        }
      }else{
        this.pathToUrl(v.path,parent)
      }
    },
    pathToUrl(path,parent){
      //判断是否是外链，是则新开窗口跳转
      if(path.includes('http')){
        window.open(path,'_blank')
      }else{
        //判断是否是同区域内跳转
        //非同区域跳转需要改前缀，如apaas/portal/ui跳转到apaas/manage/ui
        if(this.$store.state.menuObj[this.path]&&(parent.dict_group_id==this.$store.state.menuObj[this.path].dict_group_id||(['2cb4f767-fad7-44f2-afa3-f055e15dd2b6','64c156e0-bfff-4bfc-a63a-56effe130a25'].includes(parent.dict_group_id)&&['2cb4f767-fad7-44f2-afa3-f055e15dd2b6','64c156e0-bfff-4bfc-a63a-56effe130a25'].includes(this.$store.state.menuObj[this.path].dict_group_id)))){
          this.$router.push(path);
        }else{
          let temp = ''
          if(this.$store.state.menuObj[path].dict_group_id=='2cb4f767-fad7-44f2-afa3-f055e15dd2b6'||this.$store.state.menuObj[path].dict_group_id=='64c156e0-bfff-4bfc-a63a-56effe130a25'){
            temp = '/apaas/portal/ui/#'
          }else if(this.$store.state.menuObj[path].dict_group_id=='09938937-3db9-47de-b967-7777ea4ebb2d'){
            temp = '/apaas/manage/ui/#'
          }
          window.open(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>
