<template>
  <div class="headerOa">
    <div class="userHeader">
      <user-header></user-header>
    </div>
    <div class="head-bj">
      <div class="logo">
        <div class="logoIma"></div>
        <div class="logoWordes"></div>
      </div>
      <a href="/web/portal/#/dashboard"><el-button type="primary" class="outButton backPortal"><i class="iconfont iconshouyexuanzhong" style="margin-right: 5px;"></i>门户首页</el-button></a>
      <!-- http://lswz:88 -->
      <!--头部导航-->
      <div v-if="navData && navData.length != 0">
        <div class="menuBoxSty">
          <div
            class="icon-left"
            v-show="navData.length>6"
            v-on:click="translates(true)"
            :class="isleft?'isleft':''"
          ></div>
          <div class="menu">
            <ul class="menu-navHome">
              <!-- <li class="nav-child"><a href="http://lswz:88/web/portal/#/dashboard">返回门户</a></li> -->
              <li
                class="nav-child"
                v-for="(item,index) in navData"
                :key="index"
                v-on:click="showMenu(index,item.name,$event,item)"
                :class="isClick==index ?'click_back':''"
              >
                <i class="iconfont" :class="item.icon" style="font-size:14px;margin-right:0"></i>
                {{item.name}}
              </li>
            </ul>
          </div>
          <div
            class="icon-right"
            v-show="navData.length>6"
            v-on:click="translates(false)"
            :class="isright?'isright':''"
          ></div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import UserHeader from "./components/userHeader";
import { setTimeout } from 'timers';
export default {
  data() {
    return {
      form: {
        username: "",
        name: "",
        roleName: [], // 角色名称
        departmentName: "", // 部门名称
        positionName: "", // 职位名称
        // sex: '',
        email: "",
        phone: "",
        id: "",
        photo: ""
      },
      isClick: 0,
      leftNum: 0,
      rightNum: 0,
      isleft: false,
      isright: true,
      breadStr: []//点击导航默认侧边栏选中的是第一个菜单时的面包屑
    };
  },
  components: {
    UserHeader
  },
  computed: {
    ...mapGetters(["data", "sidebar", "index"]),
    navData() {
      console.log("navData", this.data[0].children);
      return this.data[0].children;
    }
  },
  methods: {
    logout() {
      this.$store.dispatch("LogOut").then(() => {
        // 为了重新实例化vue-router对象 避免bug
        // window.location.href = 'http://lswz:88/web/portal/#/login'
        window.close();
      });
    },

    translates(isLeft) {
      const ulContainer = document.querySelector(".menu-navHome");
      //点击限制
      const limitNum = this.navData.length - 6;
      let num;

      if (isLeft) {
        //isLeft 为true 向左平移
        this.leftNum++;
        num = this.leftNum - this.rightNum;
        if (num === 0) {
          this.isleft = false;
        }
        if (num > 0) {
          num = 0;
          this.leftNum--;
        }
        this.isright = true;

        ulContainer.style.transform = "translateX(" + 130 * num + "px)";
      } else {
        //向右
        this.rightNum++;
        num = this.rightNum - this.leftNum;
        if (num === limitNum) {
          this.isright = false;
        }
        if (num > limitNum) {
          num = limitNum;
          this.rightNum--;
        }
        this.isleft = true;

        ulContainer.style.transform = "translateX(-" + 130 * num + "px)";
      }
      ulContainer.style.transition = "transform 0.5s";
    },

    //点击哪一个导航内容，就显示哪一个内容下的二级菜单
    showMenu(val, navTextName, e,itemVal) {
      // const hrefIfr = itemVal.url
      // this.getCommonFun(itemVal.url)
      let arr = [];
      //  arr.push(e.toElement.outerText.trim())
      arr.push(e.srcElement.innerText.trim());
      window.sessionStorage.setItem("levelList", arr);
      window.sessionStorage.setItem("levelListTit", arr);
      //导航菜单索引
      // window.localStorage.setItem("menuIndex", val);
      if (itemVal.url == "/dashboard") {//首页
        //首页时隐藏侧边栏
        this.isClick = 0;
        this.sidebar.asideState = false;
      } else {
        this.sidebar.asideState = true;
      }
      if (navTextName == "库级监管") {//使侧边栏变窄，只显示图标
        this.sidebar.opened = false;
        // console.log(document.getElementsByClassName("app-main")[0])
        document.getElementsByClassName("app-main")[0].style.paddingLeft=0;
      } else {
        this.sidebar.opened = true;
        document.getElementsByClassName("app-main")[0].style.paddingLeft="10px";
      }
      this.isClick = val;
      this.$store.dispatch("ChangeMenu", val);
      //动态跳转
      // console.log('侧边栏和导航',this.navData)
      this.breadStr = []//清空面包屑（点击导航默认侧边栏选中的是第一个菜单时）
      this.navData.map((item, index) => {
        if (index === val) {
          const secondLevel = this.navData[index].children//二级菜单集合
          if(secondLevel) {//有二级菜单
            this.getLastNode(secondLevel)
          }else {//没二级菜单
              this.getCommonFun(this.navData[index].url)
              this.$router.push({
                path: this.navData[index].url
              })
              return
          }
        }
      });
    },
    getCommonFun(hrefIfr) {
      if (hrefIfr.length > 30) {
        var newHrefLast1
        const hrefLast1 = hrefIfr.substr(hrefIfr.indexOf('?') + 1)//问号后边所有的参数
        if(hrefLast1.search('breadCrumbs') == -1) {
          newHrefLast1 = hrefLast1.substring(hrefLast1.indexOf('=')+1)
        }else{
          newHrefLast1 = hrefLast1.substring(hrefLast1.indexOf('=')+1,hrefLast1.lastIndexOf('&'))
        }
        console.log('参数参数参数',newHrefLast1)
        // console.log('传参传参传参',newHrefLast1.replace(/"/g,''))
        localStorage.setItem('href', newHrefLast1.replace(/"/g,''))//url地址
      }
    },
    getLastNode(needArr) {//获取叶子节点二级菜单第一项的叶子节点(递归调用)
      if(needArr) {
        if(needArr[0].children) {
          this.breadStr.push(needArr[0].name)
          this.getLastNode(needArr[0].children)
        }else {
          this.getCommonFun(needArr[0].url)
          this.$router.push({
            path: needArr[0].url
          })
          this.breadStr.push(needArr[0].name)
          window.sessionStorage.setItem("levelList", this.breadStr)
          return
        }
      }else {
        this.getCommonFun(needArr[0].url)
        this.$router.push({
          path: needArr[0].url
        })
        return
      }
    },
    handleOver(event) {
      console.log(event.target);
    },
    slideNavMethod(path) {
      //控制侧边栏和导航的方法
      if (path === "/kjjg/home") {
        //从门户进来
        this.sidebar.opened = false; //让侧边栏变窄
        console.log(document.getElementsByClassName("app-main")[0])
        // document.getElementsByClassName("app-main")[0].style.paddingLeft=0;
        // document.getElementsByClassName("app-main")[0].style.paddingLeft=0;
      } else {
        this.sidebar.opened = true;
      }
      //导航赋值
      this.navData.map((item, index) => {
        if (item.url === path) {
          this.isClick = index;
          //跳转的二级菜单显示
          this.$store.dispatch("ChangeMenu", index);
          return
        }else if(window.sessionStorage.getItem('levelListTit') == item.name){//为了判断当前的url对应的是哪个一级导航
          this.isClick = index;
          //跳转的二级菜单显示
          this.$store.dispatch("ChangeMenu", index);
          console.log('indexindex',index)
          return
        }
      });
      // console.log('走循环下边',1111111)
      //显示侧边栏
      this.sidebar.asideState = true;
    }
  },
  watch: {
    $route() {
      const path = this.$route.fullPath;
      if(path == '/statisticalReview/statisticalReviewIndex' || path == '/dashboard') {//针对来自该平台首页统计审核的更多 或者 首页统计审核列表的查看（点击返回时）
        this.navData.map((item, index) => {
          if (item.url === path) {
            window.sessionStorage.setItem("levelListTit", item.name);//一级导航
            if(path == '/statisticalReview/statisticalReviewIndex') {
              window.sessionStorage.setItem("levelList", '任务管理,统计审核');//面包屑
              this.sidebar.asideState = true
            } else if(path == '/dashboard'){
              window.sessionStorage.setItem("levelList", item.name);//面包屑
              this.sidebar.asideState = false
            }
            this.isClick = index;
            //跳转的二级菜单显示
            this.$store.dispatch("ChangeMenu", index);
            return
          }
        });
      }else if(path.indexOf('/statisticalReview/statisticalReviewSearch') != -1) {//针对该平台首页统计审核列表的详情页
        this.navData.map((item, index) => {
          if(window.sessionStorage.getItem('levelListTit') == item.name){//为了判断当前的url对应的是哪个一级导航
            this.isClick = index;
            //跳转的二级菜单显示
            this.$store.dispatch("ChangeMenu", index);
            this.sidebar.asideState = true
            return
          }
        });
      }
    }
  },
  created() {
    console.log('一进来的下标',this.isClick)
    //默认跳到第一个
    const path = decodeURIComponent(this.$route.fullPath);
    // if(this.isClick ===0){
    if (path === "/dashboard") {
      if(this.navData[0].url == "/dashboard") {//针对从门户导航'粮食业务平台'进来时做的判断（可能业务平台内的导航第一个不是首页）
        this.$router.push({
          path: "/dashboard"
        });
        window.sessionStorage.setItem("levelListTit", this.navData[0].name);//一级导航
        window.sessionStorage.setItem("levelList", this.navData[0].name);//面包屑
        //隐藏侧边栏
        this.sidebar.asideState = false;
        this.isClick = 0;
      }else {
        const secondLevel = this.navData[0].children//二级菜单集合
        if(secondLevel) {//有二级菜单
          this.getLastNode(secondLevel)
        }else {//没二级菜单
            this.getCommonFun(this.navData[0].url)
            this.$router.push({
              path: this.navData[0].url
            })
        }
        window.sessionStorage.setItem("levelListTit", this.navData[0].name);//一级导航
        //跳转的二级菜单显示
        this.$store.dispatch("ChangeMenu", 0);
        //展开侧边栏
        setTimeout(() => {
          this.sidebar.asideState = true;
        },10)
        this.isClick = 0;
      }
    } else {
      this.slideNavMethod(path);
    }
    // }
  },
 mounted() {
    if (this.navData && this.navData.length != 0) {
      const ulContainer = document.querySelector(".menu-navHome");
      ulContainer.style.width = 130 * this.navData.length + "px";
    }
  }
};
</script>
<style lang="scss" scoped>
.headerOa {
  height: 100%;
  background-size: 100%;
  background-repeat: no-repeat;
  background-image: url("../indexBusinessImages/Hbg.png");
  .backPortal{
    position: absolute;
    top: 3px;
    right: 840px;
  }
  .userHeader {
    width: 100%;
    height: 38px;
    // background-color: rgba(0, 0, 0, 0.15);
    // padding: 0 20px;
  }

  // ---------头部--------/-------
  .head-bj {
    height: calc(100% - 38px);
    position: relative;
  }
  .head-bj .logo {
    width: 400px;
    position: absolute;
    left: 15px;
    top: -9px;
    overflow: hidden;
    // float: left;
  }
  .head-bj .logo .logoIma {
    font-size: 34px;
    color: #f3f3f3;
    width: 59px;
    height: 58px;
    // padding: 12px 0 0 65px;
    background-image: url("../indexBusinessImages/logo.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    float: left;
  }
  .head-bj .logo .logoWordes {
    width: 293px;
    height: 43px;
    background: url("../indexBusinessImages/lsyw.png") center top;
    float: left;
    margin: 10px 0 0 15px;
  }
  .head-bj .logo-on {
    width: 226px;
    height: 42px;
    color: #f3f3f3;
    font-size: 14px;
    background-color: #5fac9e;
    border-radius: 15px;
    position: absolute;
    top: 47px;
    right: 124px;
    line-height: 42px;
  }
  .logo-on span:nth-child(1) {
    padding-left: 51px;
    margin-right: 45px;
  }
  .head-bj .head-nav {
    width: 100%;
    height: 50px;
    position: absolute;
    bottom: 0px;
    background-color: rgba(0, 0, 0, 0.5);
    line-height: 50px;
  }
  .head-nav ul {
    padding-left: 116px;
    list-style: none;
    margin-top: 0px;
  }
  .head-nav li {
    color: #fff;
    font-size: 18px;
    float: left;
    margin-right: 52px;
  }
  .menuBoxSty {
    width: 820px;
    height: 40px;
    position: absolute;
    top: 3px;
    right: 4px;
  }
  // ------注销功能----/----
  .greetPub {
    font-size: 14px;
    font-weight: 510;
    position: absolute;
    cursor: pointer;
  }
  .greet {
    top: 2px;
    left: 55px;
  }
  .mail {
    top: 1px;
    left: 145px;
  }
  .logFf {
    top: 2px;
    left: 175px;
  }
  .icon-xinfeng {
    font-size: 20px;
  }
  // ------注销功能----\----
  // ---------头部--------\-------
  .icon-menhushouye:before{
    margin-right: 5px;
  }
  //门户首页按钮
  .outButton{
    padding: 12px 18px;
    background: #3fa9b5;
    border-color: #3fa9b5;
    border: none;
  }
  .outButton.el-button--primary:focus, .outButton.el-button--primary:hover{
    background-color: #038794;
    border-color: #038794;
  }
  .outButton.el-button--primary.is-disabled, .outButton.el-button--primary.is-disabled:active, .outButton.el-button--primary.is-disabled:focus, .outButton.el-button--primary.is-disabled:hover{
    background-color: #aed6da;
    border-color: #aed6da;
  }
}
.head-bj .menu {
  width: 780px;
  height: 40px;
  line-height: 40px;
  background-color: #3fa9b5;
  // position: absolute;
  // top: 35px;
  // right: 40px;
  float: left;
  overflow: hidden;
  list-style: none;
  padding-left: 0px;
  margin: 0;
}
.icon-right {
  width: 20px;
  height: 40px;
  float: left;
  // position: absolute;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  // top: 35px;
  // right: 20px;
  background-color: #3fa9b5;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: 8px;
}

.isright {
  background-image: url("../indexBusinessImages/right-arrow.png");
}
.icon-left {
  width: 20px;
  height: 40px;
  float: left;
  // position: absolute;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  // top: 35px;
  // right: 820px;
  background-color: #3fa9b5;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: 8px;
}
.icon-left:before {
  content: "";
  width: 1px;
  height: 50%;
  background-image: url("../indexBusinessImages/nav_x.png");
  background-repeat: no-repeat;
  background-size: 50% 100%;
  position: absolute;
  top: 25%;
  right: 0px;
}
.isleft {
  background-image: url("../indexBusinessImages/left-arrow.png");
}
.menu .menu-navHome {
  /*left: 114px;*/
  list-style: none;
  padding-left: 0px;
  margin: 0;

  /* Firefox 17- */
}
.nav-child {
  width: 130px;
  box-sizing: border-box;
  padding: 0 10px;
  height: 40px;
  float: left;
  line-height: 40px;
  color: #fff;
  text-align: center;
  position: relative;
  font-size: 14px;
}
.nav-child:before {
  content: "";
  width: 1px;
  height: 50%;
  background-image: url("../indexBusinessImages/nav_x.png");
  background-repeat: no-repeat;
  background-size: 50% 100%;
  position: absolute;
  top: 25%;
  right: 0px;
}
.nav-child:hover {
  background-color: #fff;
  color: #3eaf9e;
  border-radius: 4px;
  cursor: pointer;
}

.click_back {
  background-color: #fff;
  background-origin: padding-box;
  color: #3eaf9e;
  border-radius: 4px;
  cursor: pointer;
}
.user-avatar {
  display: flex;
  align-items: center;
  position: absolute;
  right: 20px;
  top: 7px;
}
</style>

