<template>
  <div :class="isCollapse?'menuCollapse topMenu':'topMenu'" :style="{ width: isCollapse ? (menuCollapseShowName==true?'58px':'48px') : '200px' }">
    <el-menu :unique-opened="true" :default-active="$route.path" router :class="menuCollapseShowName == true ? 'el-menue-mine-new menuCollapseShowName':'el-menue-mine-new'" :collapse="true"
      :style="{ width: isCollapse ? (menuCollapseShowName==true?'58px':'48px') : '200px' }">
      <div class="linkOut" :style="{ width: isCollapse ? (menuCollapseShowName==true?'58px':'48px') : '200px'}">
        <div class="navigateMenu navigateMenuBottom" v-for="(v,i) in myUserinfo.url_help || []" :key="i" @click="tapClick2(v.label_url)" v-show="loginInfo.isSingleLogin !== '1' && v.label_url && callPortal">
          <div class="swidth" v-if="!isCollapse"></div>
          <div class="navigateMenu">
            <img :src="v.label_icon || require('../img/u9.png')" class="listImgMenu" />
          </div>
          <div class="first-menu" v-if="menuCollapseShowName==true || !isCollapse">{{ v.label_name }}</div>
        </div>
      </div>
      <div class="openAction">
        <i class="el-icon-qz-menu" @click="setMenuw"></i>
      </div>
      <el-submenu :index="String(i)" @mouseenter.native="mouseenter" v-for="(v,i) in leftMenuList" :key="i"
        :class="[v.positionVal&&v.positionVal=='left-bottom' ? 'firstLevelMenueSet':'','firstLevelMenue']" :popper-class="v.is_active === '3' ? 'menu-popup__hidden' : ''"
        :title="v.is_active === '3' ? translate(v) || (v.name?v.name:v.res_na) : ''" 
        @click.native.stop=" see(v, i)"
         v-show="v.is_active!='2'"
        :style="{ width: isCollapse ? (menuCollapseShowName==true?'58px':'48px') : '200px' }">
        <template slot="title">
          <div class="swidth" v-if="!isCollapse"></div>
          <div class="navigateMenu" v-if="v.resIconNo">
            <img :src="syncUrl ? syncUrl(v.resIconNo) : v.resIconNo" class="listImgMenu" />
          </div>
          <div class="navigateMenu" v-else>
            <img src="../img/u9.png" class="listImgMenu" />
          </div>
          <div class="first-menu" v-if="menuCollapseShowName==true || !isCollapse" :title="translate(v) || (v.name?v.name:v.res_na)">{{get_menu_name(translate(v) || (v.name?v.name:v.res_na))}}</div>
        </template>
        <!-- hover -->
        <div class="firstMenu">{{translate(v) || (v.name?v.name:v.res_na)}}</div>
        <div class="el-menu-new-hover" v-for="(vv,ii) in v.children?v.children:v.childsList" :key="ii+'-second'" v-show="vv.is_active!='2'">
          <div class="hover_secondMenu_left">
            <!-- <el-tooltip class="item" effect="dark" :content="translate(vv) || (vv.name?vv.name:vv.res_na)" placement="bottom"> -->
            <div @click.stop="see(vv)" class="child" :title="translate(vv) || (vv.name?vv.name:vv.res_na)">{{translate(vv) || (vv.name?vv.name:vv.res_na)}}</div>
            <!-- </el-tooltip> -->
            <div class="jiantou">&nbsp;&gt;</div>
          </div>
          <div class="hover_secondMenu_Right">
            <span class="hoverColor" @click.stop="see(vvv)" @contextmenu.prevent="mouseclick(String(vvv.name?vvv.name.split('/#')[1]:vvv.res_url_ca.split('/#')[1]))"
              :index="String(vvv.name?vvv.name.split('/#')[1]:vvv.res_url_ca.split('/#')[1])" v-for="(vvv,iii) in vv.children?vv.children:vv.childsList"
              :key="iii+'-three'">{{translate(vvv) || (vvv.name?vvv.name:vvv.res_na)}}

              <div class="topChild" @click.stop="clickNew(vvv)" :class="[{'topChildShow':moseMenu==String(vvv.name?vvv.name.split('/#')[1]:vvv.res_url_ca.split('/#')[1])}]">
                {{ t(`asideMenu.openNewTab`) }}</div>
            </span>
          </div>
        </div>
      </el-submenu>
    </el-menu>
  </div>
</template>

<script>
import Locale from 'element-ui-qz/src/mixins/locale';
import qrcodeImg from '../img/qrcode.png';
import moImg from '../img/heart.svg';
import ElTooltip from 'element-ui-qz/packages/tooltip';

export default {
  name: 'ElMenueMineNew',
  mixins: [Locale],
  props: {
    data: {
      type: Object,
      default: () => {
        return {
          JID: localStorage.getItem('__JID__'),
          Url: 'https://192.168.2.215:30700/ks-prj/menu/getFETreeList',
          reqType: 'get'
        };
      }
    }
  },
  computed: {
    loginInfo() {
      return (
        (localStorage.getItem('loginInfo') &&
          JSON.parse(localStorage.getItem('loginInfo'))) ||
        {}
      );
    }
  },
  data() {
    return {
      isCollapse: this.$store.state.basic_global_sql.get('menuState') ? !this.$store.state.basic_global_sql.get('menuState') : true,
      // isCollapse:true,
      leftMenuList: [],
      qrcodeImg: qrcodeImg,
      moImg: moImg,
      myUserinfo: '',
      defaultPic: '',
      bottomMenu: '',
      menuUrl: '/ks-prj/menu/getFETreeList',
      // menuUrl: "https://192.168.2.215:30700" + "/ks-prj/menu/getFETreeList",
      moseMenu: '',
      callPortal: true,
      menuCollapseShowName: ''
    };
  },
  components: {
    ElTooltip
  },
  methods: {
    mouseleave() {// 鼠标移出
      console.log('鼠标移出');
    },
    mouseclickLeft(value) {// 鼠标左键
      console.log('左键');
      this.moseMenu = '';
    },
    mouseclick(vakue) {// 鼠标右键
      // console.log(vakue)
      this.moseMenu = vakue;
    },
    mouseOver() {
      console.log('subiao');
    },
    defalutPic() {
      if (this.myUserinfo) {
        let parmas = {};
        parmas.prn = this.data.JID;
        this.$http
          .post(this.data.defaultPic, parmas, { emulateJSON: true })
          .then((res) => {
            this.defaultPic = res.data.obj[0].kings_lims;
          });
      }
    },
    mouseenter() {
      var that = this;
      setTimeout(function() {
        console.log('mouseente');
        that.moseMenu = '';
        // if (this.isCollapse) {
        var doms = document.body.getElementsByClassName('el-menu--vertical');
        var selectDom = '';
        for (var key = 0; key <= doms.length; key++) {
          if (doms[key] && doms[key].style.display === '') {
            doms[key].addEventListener('mousedown', function(event) {
              console.log('eventevent', event);
              if (event.button == 0) {
                setTimeout(() => {
                  that.moseMenu = '';
                }, 400);
              }
            });
          }
        }
      }, 400);
    },
    see(vvv, i,isactive) {
      const element = document.querySelector('.el-submenu__title.active-color');
      const elements = document.querySelectorAll('.el-submenu__title');
      element && element.classList.remove('active-color');
      if(vvv.isactive!==undefined  && isactive!=='3') return;
      if (i !== undefined) {
        let menuData = JSON.parse(localStorage.getItem('menuData')) || [];
        menuData.forEach(item => {
          if (item.childsList.length === 0) {
            elements[i] && elements[i].classList.add('active-color');
          }
        });
      }
      // 判断是否是ks
      console.log('vvv.res_url_ca', vvv.res_url_ca);
      if (vvv.res_url_ca) {
        if (window.location.href.includes('/ks-www')) {
          let path = vvv.res_url_ca.split('/#')[1];
          if (this.$route.path === path) {
            return;
          }
          localStorage.setItem('menu_id', vvv.id);
          const isCollapse = this.$store.state.basic_global_sql.get('menuClickType');
          if (isCollapse && isCollapse == true || isCollapse && isCollapse == 'true') {
            window.open(vvv.res_url_ca, '_blank');
            return;
          }

          //  if(vvv.win_open_style&&vvv.win_open_style===1||vvv.win_open_style&&vvv.win_open_style==='1'){
          //      window.open(vvv.res_url_ca, '_blank')
          //  }else{
          //   this.$router.push({ path });
          //  }
          this.$router.push({ path });
          // this.$emit('selectMenue', vvv);
        } else {
          // location.href = `${vvv.res_url_ca}?JID=${JID}`;
          let menu_id = '';
          if (vvv.outMenuId != '' && vvv.outMenuId != null) {
            menu_id = vvv.outMenuId;
          } else {
            menu_id = vvv.treeId;
          }
          localStorage.setItem('menu_id', menu_id);
          location.href = vvv.res_url_ca;
        }
      } else {
        // location.href = `${vvv.uri}?JID=${JID}`;
        let menu_id = '';
        if (vvv.outMenuId != '' && vvv.outMenuId != null) {
          menu_id = vvv.outMenuId;
        } else {
          menu_id = vvv.treeId;
        }
        localStorage.setItem('menu_id', menu_id);
        location.href = vvv.uri;
      }
    },
    clickNew(vvv) {// zai
      console.log('099');
      let JID = localStorage.getItem('__JID__');
      // 判断是否是ks
      console.log('vvv.res_url_ca', vvv.res_url_ca);
      if (vvv.res_url_ca) {
        if (window.location.href.includes('/ks-www')) {
          let path = vvv.res_url_ca.split('/#')[1];
          if (this.$route.path === path) {
            return;
          }
          localStorage.setItem('menu_id', vvv.id);
          window.open(vvv.res_url_ca, '_blank');
          return;
        } else {
          // location.href = `${vvv.res_url_ca}?JID=${JID}`;
          let menu_id = '';
          if (vvv.outMenuId != '' && vvv.outMenuId != null) {
            menu_id = vvv.outMenuId;
          } else {
            menu_id = vvv.treeId;
          }
          localStorage.setItem('menu_id', menu_id);
          window.open(vvv.res_url_ca, '_blank');
        }
      } else {
        // location.href = `${vvv.uri}?JID=${JID}`;
        let menu_id = '';
        if (vvv.outMenuId != '' && vvv.outMenuId != null) {
          menu_id = vvv.outMenuId;
        } else {
          menu_id = vvv.treeId;
        }
        localStorage.setItem('menu_id', menu_id);
        window.open(vvv.uri, '_blank');
      }
    },
    get_menu_name(name) {
      let len = name.length;
      if (len > 6) {
        return name.substring(0, 6) + '...';
      } else {
        return name;
      }
    },
    // 获取数据
    getMenueList() {

      let lData = localStorage.getItem('menuData');
      if (lData) {
        let lDataNew = JSON.parse(lData);
        let leftDataCont = [];
        for (let v of lDataNew) {
          if (v.positionVal != 'sys_home') {
            leftDataCont.push(v);
          }
        }
        this.leftMenuList = leftDataCont;
        this.bottomMenu = this.leftMenuList.length - 1;
        return;
      }
      this.$http
        .get(
          this.menuUrl,
          {
            params: Object.assign({
              JID: this.data.JID
            })
          },
          {
            emulateJSON: true
          }
        )
        .then((resp) => {
          if (resp === undefined || resp.data.obj === undefined) {
            location.href = resp.data.redirectUrl;
            localStorage.setItem('userInfo', '');
            localStorage.setItem('__JID__', '');
            return;
          }
          let leftData = resp.data.obj.childsList;
          localStorage.setItem('menuData', JSON.stringify(leftData));
          let leftDataCont = [];
          for (let v of leftData) {
            if (v.positionVal != 'sys_home') {
              leftDataCont.push(v);
            }
          }
          this.leftMenuList = leftDataCont;
          this.bottomMenu = this.leftMenuList.length - 1;
        });
    },
    tapClick1() {
      if (this.myUserinfo.url2 !== '' && this.myUserinfo.url2 !== undefined) {
        location.href = location.origin.this.myUserinfo.url2;
      }
    },
    tapClick2(url) {
      if (url) {
        window.open(url, '_blank');
      }
    },
    getStorage() {
      let that = this;
      let orignalSetItem = localStorage.setItem;
      localStorage.setItem = function(key, newValue) {
        let setItemEvent = new Event('setItemEvent');
        setItemEvent.key = key;
        setItemEvent.newValue = newValue;
        window.dispatchEvent(setItemEvent);
        orignalSetItem.apply(this, arguments);
      };
      window.addEventListener('setItemEvent', function(e) {
        console.log(e);
        if (e.key === 'myUserinfo') {
          that.myUserinfo = JSON.parse(e.newValue);
        }
      });
    },
    setMenuw() {
      this.isCollapse = !this.isCollapse;
	   this.$emit('collapseChange', this.isCollapse);
    }
  },
  created() {
    let that = this;
    let myUserinfos = localStorage.getItem('myUserinfo');
    if (myUserinfos) {
      that.myUserinfo = JSON.parse(myUserinfos);
    } else {
      this.getStorage();
    }
    // bug60460 菜单优化功能
    this.menuCollapseShowName = window.configs && window.configs.menuCollapseShowName || false;
  },
  mounted() {
	  // const isCollapse=this.$store.state.basic_global_sql.get('menuState') ? !this.$store.state.basic_global_sql.get('menuState') : true
    this.$emit('collapseChange', this.isCollapse);
    this.callPortal = window.configs ? window.configs.callPortal : true;
    this.getMenueList();
    // ks
    // var path = this.$route.path;
    // var value = path && (path.includes('/ks') || path.includes('/preview/'));
    // if (value) {
    //   this.$emit('selectMenue', value);
    // }
  }
};
</script>

<style lang='scss'>
.el-menu--vertical {
  max-height: 100%;
  overflow: auto;
  .el-menu {
    background: #349eff;
    padding: 0 15px 24px 15px;
    margin-left: 0;
  }
}
.el-menu {
  position: 0 !important;
}
.el-menu-new-hover {
  color: #ffffff !important;
  p {
    display: block;
    -webkit-margin-before: 1em;
    -webkit-margin-after: 1em;
    -webkit-margin-start: 0px;
    -webkit-margin-end: 0px;
  }
  .hoverColor {

    &:hover {
      cursor: pointer;
      color: aqua !important;
    }
  }
}

.topMenu {
  overflow-y: auto;
  background: #1a90fd;
  overflow-x: hidden;
  width: 100%;
  .first-menu{
	  font-size:16px;
	  padding-left:8px;
  }
  &::-webkit-scrollbar {
    display: none;
  }
  .el-menue-mine-new {
    background: #1a90fd;
    margin-top: 18px;
    margin-bottom: 96px;
    border: 0;
    .firstLevelMenueSet {
      position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
      height: 48px;
      z-index: 200;
      background: #1a90fd;
    }
    .linkOut{
      position: fixed;
      bottom: 48px;
      left: 0;
      width: 100%;
      z-index: 200;
    }
    .navigateMenuBottom {
      width: 100%;
      height: 48px;
      background: #1a90fd;
      display: -webkit-box;
      /* Chrome 4+, Safari 3.1, iOS Safari 3.2+ */
      display: -moz-box;
      /* Firefox 17- */
      display: -webkit-flex;
      /* Chrome 21+, Safari 6.1+, iOS Safari 7+, Opera 15/16 */
      display: -moz-flex;
      /* Firefox 18+ */
      display: -ms-flexbox;
      /* IE 10 */
      display: flex;
      -webkit-box-align: center;
      -ms-flex-align: center;
      -webkit-align-items: center;
      align-items: center;
      color: #fff;
      &:hover {
        background: #006ac3;
        cursor: pointer;
      }
      .first-menu {
        flex: 1;
        -webkit-box-flex: 1;
        /* OLD - iOS 6-, Safari 3.1-6 */
        -moz-box-flex: 1;
        /* OLD - Firefox 19- */
        -webkit-flex: 1;
        /* Chrome */
        -ms-flex: 1;
        /* IE 10 */
        flex: 1;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        text-align: left;
      }
    }
    .navigate {
      i {
        font-size: 20px;
        color: #ffffff !important;
      }
      span {
        margin-left: 5px;
        font-weight: normal;
        color: #303133;
      }
    }
    .el-submenu__icon-arrow {
      display: none;
    }
    li {
      text-align: left;
    }
    .el-submenu__title {
      /* padding:0; */
      padding-left: 0 !important;
      color: white;
      height: 48px;
      line-height: 48px;
    }
    .el-submenu__title:hover {
      background: #006ac3;
    }
   .active-color {
        background: #006ac3;
      }
    .navigate is-active {
      background: #006ac3;
    }

    .firstLevelMenue .el-submenu__title {
      display: -webkit-box;
      /* Chrome 4+, Safari 3.1, iOS Safari 3.2+ */
      display: -moz-box;
      /* Firefox 17- */
      display: -webkit-flex;
      /* Chrome 21+, Safari 6.1+, iOS Safari 7+, Opera 15/16 */
      display: -moz-flex;
      /* Firefox 18+ */
      display: -ms-flexbox;
      /* IE 10 */
      display: flex;
      -webkit-box-align: center;
      -ms-flex-align: center;
      -webkit-align-items: center;
      align-items: center;
      .first-menu {
        flex: 1;
        -webkit-box-flex: 1;
        /* OLD - iOS 6-, Safari 3.1-6 */
        -moz-box-flex: 1;
        /* OLD - Firefox 19- */
        -webkit-flex: 1;
        /* Chrome */
        -ms-flex: 1;
        /* IE 10 */
        flex: 1;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
    .firstLevelMenue .el-submenu__title span {
      font-weight: 700;
      display: inline-block;
      width: calc(100% - 22px);
      height: 100%;
      padding: 0 12px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .secondLevelMenue .el-submenu__title {
      padding: 0 0 0 47px !important;
      font-weight: 700;
      font-size: 12px;
      span {
        padding-left: 0;
        width: 100%;
      }
    }
    .threeLevelMenue {
      padding-right: 12px;
      span {
        padding: 0 0 0 3px !important;
        display: inline-block;
        width: 100%;
        height: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}

.el-menu-new-hover {
  display: flex;
  justify-content: center;
  .hover_secondMenu_left {
    display: flex;
    .child {
      width: 90px !important;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      display: inline-block;
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      height: 32px;
      line-height: 32px;
    }
    .jiantou {
      height: 32px;
      line-height: 32px;
    }
    span {
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      margin-right: 4px;
    }
  }
  .hover_secondMenu_Right {
    display: inline-block !important;
    float: left;
    width: 280px;
    margin-left: 20px;
    .hoverColor {
      padding: 0 8px;
      border-left: 1px solid #ddd;
      border-left-style: 0.8;
      font-size: 12px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: 20px;
      color: rgba(255, 255, 255, 1);
      min-width: 75px;
      display: inline-block;
      text-align: center;
      margin: 6px 0;
      position: relative;
      .topChild{
         width:150px;
         background: #ffffff;
         color: #303133;
         padding:10px;
         border-radius:4px;
         position:absolute;
         top:20px;
         left:40%;
         z-index:200;
         display:none;
         font-size:12px;
      }
      .topChild.topChildShow{
        display: block;
      }
    }
    span:last-child {
      border-right: none;
    }
  }
}

.bootomMenu {
  background: #1a90fd;
  width: 100%;
  text-align: center;
}
.navigateMenu {
  height: 48px;
  line-height: 48px;
  padding-left: 0 !important;
  width: 48px;
  text-align: center;
  vertical-align: middle;
  position: relative;
/*  &:hover {
    background: #006ac3;
    cursor: pointer;
  } */
  .listImgMenu {
    display: inline-block;
    width: 32px;
    height: 32px;
    margin-top: -16px;
    top: 50%;
    position: absolute;
    left: 50%;
    margin-left: -16px;
  }
}
.el-tooltip__popper.is-dark {
  background: #ffffff;
  border: 1px solid rgba(0, 0, 0, 0);
  box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.15);
  font-size: 12px;
  color: #595959;
  text-align: center;
}
.qrcodeCont {
}
.listImg {
  width: 22px;
  height: 22px;
  background-color: #ffffff;
  mask-repeat: no-repeat;
  -webkit-mask-repeat: no-repeat;
  mask-position: center;
  mask-size: 100% 100%;
  -webkit-mask-position: center;
  -webkit-mask-size: 100% 100%;
  position: absolute;
  top: 50%;
  margin-top: -11px;
  left: 50%;
  margin-left: -11px;
}
.el-menu-Content {
  background: #1a90fd;
  width: 100%;
  .topMenu {
    background: #1a90fd;
    overflow-x: hidden;
    width: 100%;
    &::-webkit-scrollbar {
      display: none;
    }
  }
}

.firstMenu {
  font-size: 16px;
  color: #ffffff;
  height: 38px;
  line-height: 38px;
  border-bottom: 1px solid rgba(255, 255, 255, 1);
  margin-bottom: 22px;
}
.menu-popup__hidden {
  display: none !important;
}

.el-submenu__title {
  padding: 0px;
}

.openAction {
  padding-left: 16px;
  color: #ffffff;
  font-size: 18px;
  cursor: pointer;
  height: 30px;
  .el-icon-qz-menu {
    display: none;
  }
  &:hover {
    .el-icon-qz-menu {
      display: block;
    }
  }
}

.swidth{
	display: inline-block;
	width:8px;
	height:8px
}
.menuCollapse .el-menue-mine-new.menuCollapseShowName{
  .navigateMenu .listImgMenu{
    margin-top: -23px;
  }
  .el-submenu__title {
      height: 52px;
      line-height: 52px;
  }
  .first-menu {
    position: absolute;
    font-size: 12px;
    text-align: center;
    width: 100%;
    top: 36px;
    height: 14px;
    line-height: 14px;
    padding: 0 4px;
  }
  .navigateMenu{
    width: 100%;
  }
}
</style>
