<template>
  <div class="list">
     <div class="l-con" :class="showList?'active':''">
       <div class="clip-scroller">
        <ul class="pic-list" v-if="scenes.length>0" ref="pic">
          <li :title="item.sceneTitle" @click="handleVR(item)" v-for="(item,i) in scenes" :key="i">
            <div :class="{active:selected.sceneCode==item.sceneCode}">
              <img :src="item.icon+`?${Math.random()}`" alt="" />
              <i class="iconfont iconscene_map_3d" :class="{'iconjump':item.type!=='4dkk'}"></i>
              <rollName :offset="20" :active="selected.sceneCode==item.sceneCode" class="pic-name" :myref="'subw'+item.id" :name="item.sceneTitle"/>
            </div>
          </li>
        </ul>
       </div>
      
      <div class="clip-scroller" v-if="childTab.length > 1">
        <ul class="tap">
          <li @click="tabtowActive = item" v-for="(item,i) in childTab" :key="i">
            <rollName :offset="30" :mgin="20" :active="item.id == tabtowActive.id" class="btn rect" :class="{active:item.id == tabtowActive.id}" :myref="'ww'+item.id" :name="item.name"/>
          </li>
        </ul>
      </div>

      <div class="clip-scroller" v-if="showInfo.catalogRoot.length > 0 && showInfo.catalogs.length>1">
       <ul class="tap">
        <li @click="taboneActive = item" v-for="(item,i) in showInfo.catalogRoot" :key="i">
          <rollName :offset="30" :mgin="20" :active="item.id == taboneActive.id" class="btn rect" :class="{active:item.id == taboneActive.id}" :myref="'zw'+item.id" :name="item.name"/>
        </li>
      </ul>
      </div>

    </div>
    <div class="btn dl" @click="showList=!showList">
      <i class="iconfont " :class="showList?'iconnav_scene_down':'iconnav_scene_up'"></i>场景导航</div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import config from "@/config";
import rollName from "@/components/rollName";

let $scroll = null;

export default {
  props:['select'],
  components:{rollName},
  
  data(){
    return{
      taboneActive:{children:[]},
      tabtowActive:'',
      childTab:[],
      scenes:[],
      showList:false,
      loadFirst: true,
      menuWidth:0,
      sceneNum: config.sceneNum
    }
  },
  computed: {
      ...mapGetters({
        showInfo: "showInfo"
      }),
      selected(){
        return {...this.select}
      }
  },

  methods:{
    getCWidth(ref){
      if (this.$refs[ref]) {
        return this.$refs[ref][0].clientWidth
      }
    },
    handleVR(item){
      history.replaceState(null, null, "".concat(window.location.pathname, "?").concat(`id=${this.showInfo.id}&vr=${item.sceneCode}`));
      this.sceneNum = item.sceneCode
    }
  },

  mounted(){
    this.$nextTick(() => {
      if ($scroll == null) {
        let eles = $(".clip-scroller")
        Array.from(eles).forEach(item=>{
          let frame = new PerfectScrollbar(item, {
            useBothWheelAxes: true,
            suppressScrollY: true,
            wheelSpeed: 0.8,
          });
          $(item).data("scrollbar", frame);
        })
      }
    });
  },
  
  watch:{
    taboneActive: {
      handler: function (newVal) {
        let temp = []
        newVal.children && newVal.children.forEach(item=>{
          this.showInfo.catalogs.forEach(sub=>{
            if (item==sub.id) {
              temp.push(sub)
            }
          })
        })
        this.childTab = temp
        if (!this.loadFirst) {
            this.tabtowActive = ''
        }
      },
    },
    tabtowActive: {
      deep:true,
      handler: function (newVal) {
        if (!newVal) {
          this.tabtowActive = this.childTab[0]
        } else{
          let arr = this.showInfo.scenes.filter(item=>{
            return newVal.id == item.category
          })
          this.scenes = arr.sort((a,b)=>a.weight-b.weight)
        }
      }
    },

    sceneNum:{
      deep:true,
      immediate:true,
      handler:function (newVal) {
          if (!newVal) {
            let tmp = this.showInfo.firstScene||this.showInfo.scenes[0]
            this.handleVR(tmp)
            return
          }
          let val = this.showInfo.scenes.find(item=>item.sceneCode==newVal)
          let tmp = this.showInfo.catalogs.find(item=>item.id==val.category)
          let rootTmp = this.showInfo.catalogRoot.find(item => {
            return item.children.indexOf(tmp.id)>-1
          });
          this.taboneActive = rootTmp
          this.tabtowActive = tmp

        setTimeout(() => {
          this.$emit('select',val)
          this.loadFirst = false 
        });
      }
    },
   
    selected:{
      handler:function (newVal) {
        this.handleVR(newVal)
        if (newVal.type == '4dkk') {
          this.showList = false
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>

.btn{
    display: inline-block;
    margin: 0 auto;
    padding: 0 20px;
    height: 36px;
    line-height: 36px;
    background: rgba(0, 0, 0, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    opacity: 1;
    cursor: pointer;
    word-break: keep-all;
    max-width: 150px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
    &.active{
      background: rgba(0, 0, 0, 0.3);
      border: 1px solid #1FE4DC;
      color: #1FE4DC;
    }
}

.rect{
  border-radius: 4px;
  height: 24px;
  line-height: 24px;
  padding: 0;
  & /deep/ span{
    margin: 0 20px;
  }
}

.line{
  background:#1FE4DC;
  width: 100%;
  height: 0.1px;
  opacity: 0.5;
}

.list{
  position: fixed;
  bottom: 10px;
  left: 0;
  z-index: 999;
  text-align: center;
  width: 100%;
  pointer-events: none;
  .dl{
    background: rgba(0, 0, 0, 0.3);
    border-radius: 18px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    margin-top: 10px;
    pointer-events: auto;
    >i{
      margin-right: 8px;
      vertical-align: bottom;
      font-size: 18px;
    }
  }
  .l-con{
    width: 100%;
    transition: all ease 0.3s;
    max-height: 0px;
    overflow: hidden;
    pointer-events: auto;
    .clip-scroller{
      position: relative;
      background: rgba(0, 0, 0, 0.3);
      margin-bottom: 2px;
    }
    ul{
      display: inline-block;
      white-space: nowrap;
      position: relative;
      li{
        margin: 0 5px;
        display: inline-block;
        
      }
    }
    .tap{
      padding: 12px 0;
    }
    .pic-list{
      padding: 12px 0;
      >li{
        cursor: pointer;
        >div{
          width: 72px;
          height: 72px;
          border: 2px solid #FFFFFF;
          opacity: 1;
          border-radius: 4px;
          overflow: hidden;
          position: relative;
          cursor: pointer;
          .pic-name{
            background: rgba(0, 0, 0, 0.5);
            position: absolute;
            left: 0;
            bottom: 0;
          }
          >img{
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
          }
          .iconfont{
            position: absolute;
            left: 4px;
            top: 4px;
            z-index: 99;
          }
          >span{
            display: inline-block;
            background: rgba(0, 0, 0, 0.5);
            position: absolute;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            left: 0;
            bottom: 0;
            width: 100%;
          }
           &.active{
              border: 2px solid #1FE4DC;
          }
        }
      }
    }

    .onecls{
      background: rgba(0, 0, 0, 0.3);
      display: inline-block;
      width: 100%;
    }

    .towcls{
      background: rgba(0, 0, 0, 0.3);
    }
    &.active{
      max-height: 200px;
    }
  }
}
</style>