<template>
  <div class="list">
     <div class="l-con" :class="showList?'active':''">
      <div class="pic-con">
        <ul class="pic-list" v-if="scenes.length>0">
          <li @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" :class="item.type!='4dkk'?'iconedit_type_panorama':'iconedit_type_3d'"></i>
              <rollName :offset="20" :active="selected.sceneCode==item.sceneCode" class="pic-name" :myref="'subw'+item.id" :name="item.sceneTitle"/>
            </div>
          </li>
        </ul>
        
        <ul class="tap " v-if="childTab.length > 1" >
          <li  @click="tabtowActive = item" v-for="(item,i) in childTab" :key="i">
              <rollName :offset="30" :mgin="20" :active="item.id == tabtowActive.id" class="btn pic-rect" :class="{active:item.id == tabtowActive.id}" :myref="'ww'+item.id" :name="item.name"/>
          </li>
        </ul>
      </div>
       <ul class="tap" v-if="showInfo.catalogRoot.length > 1">
          <li @click="taboneActive = item" :class="{ active: item.id == taboneActive.id }" 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 v-if="selected.type=='4dkk'" class="menu">
      <div class="menucon" :class="{shouqi:shouqi}">
        <ul>
          <li @click="handleDL" v-if="isShowDL">
            <i class="iconfont" :class="isDaolan?'iconshow_suspension':'iconshow_playback'"></i>
            <span title="导览" >导览</span>
          </li>
          <template v-if="currentActive!='2d'&&currentActive!='3d'">
            <li v-for="(item,i) in menu" @click="handleMenu(item)" :key="i">
              <i class="iconfont" :class="currentActive==item.id?item.active:item.icon"></i>
              <span :title="item.name" >{{item.name}}</span>
              <!-- <i style="font-size:10px; margin-left:6px" v-if="item.id == 'daolan'" class="iconfont iconshow_drop-down"></i> -->
            </li>
          </template>
        </ul>
        <i  @click="shouqi = !shouqi" class="iconfont iconshow_function_collect"></i>

      </div>

      <div class="scenedl">
        <i class="iconfont"  @click="showList=!showList" :class="showList?'iconnav_scene_down':'iconnav_scene_up'"></i>
      </div>
    </div>

    <div class="btn dl" @click="showList=!showList" v-else>
      <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";

export default {
  props:['select'],
  components:{rollName},
  
  data(){
    let menu = [{
      id:'pano',
      name:'漫游',
      icon:'iconshow_roaming_normal',
      active:'iconshow_roaming_selected',
      params:'pano'
    },{
      id:'2d',
      name:'平面',
      icon:'iconshow_plane_normal',
      active:'iconshow_plane_selected',
      params:'2d'
    },{
      id:'3d',
      name:'三维',
      icon:'iconshow_3d_normal',
      active:'iconshow_3d_selected',
      params:'3d'
    }]
    return{
      isDaolan:false,
      isShowDL:'',
      currentActive:'pano',
      taboneActive:{children:[]},
      tabtowActive:'',
      childTab:[],
      sceneNum: config.sceneNum,
      scenes:[],
      menu,
      showList:true,
      loadFirst: true,
      shouqi:false,
      canClick:true
    }
  },
  computed: {
      ...mapGetters({
        showInfo: "showInfo"
      }),
      selected(){
        return {...this.select}
      }
  },

  mounted(){
      this.$bus.on('guideRoomsData',data=>{
        if (data.length>0) {
          this.isShowDL = true
        }
      })

      this.$bus.on('currentMode',data=>{
        this.delHandle()
        this.currentActive = data.data.mode || 'pano'
        // this.menu.findItem(item=>item.params == data.data.mode)
      })
      
  },
  methods:{
    handleDL(){
      this.isDaolan = !this.isDaolan
      let ele = document.getElementById('showifr');
      ele.contentWindow.postMessage({
          source: "mingyuan",
          event: this.isDaolan?'guide-start':'guide-pause',
      },"*")
    },
    delHandle(){
      this.canClick = false
      setTimeout(() => {
        this.canClick = true
      }, 1800);
    },
    handleMenu(data){
      if (this.canClick) {
        this.currentActive = data.id
        let ele = document.getElementById('showifr');
        ele.contentWindow.postMessage({
            source: "mingyuan",
            event: 'mode',
            params: data.params,
        },"*")
        this.delHandle()
      }
    },
    handleVR(item){
      history.replaceState(null, null, "".concat(window.location.pathname, "?").concat(`id=${this.showInfo.id}&vr=${item.sceneCode}`));
      this.sceneNum = item.sceneCode
    }
  },
  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: {
      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: 26px;
  line-height: 26px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.5);
  color: rgba(255, 255, 255, 0.5);
  border-radius: 4px;
  min-width: 78px;
  max-width: 200px;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  &.active {
    border: 1px solid rgba(255, 255, 255, 1);
    color: rgba(255, 255, 255, 1);
  }
}

.pic-rect {
  border-radius: 4px;
  height: 24px;
  line-height: 24px;
  padding: 0;
  padding-bottom: 10px;
  background: none;
  border: none;
  &.active {
    border: none;
    color: rgba(255, 255, 255, 1);
    position: relative;
    &::before {
      content: "";
      display: inline-block;
      position: absolute;
      bottom: 0;
      width: 20px;
      height: 2px;
      z-index: 9999;
      left: 50%;
      transform: translateX(-50%);
      background: #0076f6;
    }
  }
}

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

.list{
  position: fixed;
  bottom: 10px;
  left: 0;
  text-align: right;
  width: 100%;
  .dl{
    background: rgba(0, 0, 0, 0.3);
    border-radius: 18px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    margin-top: 10px;
    margin-right: 10px;
    padding: 0 10px;
    height: 36px;
    line-height: 36px;
    color: #fff;
    >i{
      margin-right: 8px;
      vertical-align: bottom;
      font-size: 18px;
    }
  }

  .menu{
    margin: 10px 0 12px;
    .menucon{
      float: left;
      display: flex;
      border: 1px solid rgba(255, 255, 255, 0.10196078431372549);
      border-radius: 0px 24px 24px 0px;
      background: rgba(0, 0, 0, 0.3);
      padding-right: 30px;
      position: relative;
      transition: all 0.3s ease;
      >ul{
        display: flex;
        overflow: hidden;
        >li{
            color: #fff;
            margin: 0;
            height: 46px;
            line-height: 46px;
            padding: 0 5px;
            background: none;
            >span{
              display: inline-block;
              margin-left: 6px;
            }
            
          }
      }
      >i{
          font-size: 10px;
          position: absolute;
          right: 14px;
          top: 50%;
          transform: translateY(-50%);
        }
    }

    .shouqi{
      >ul{
        max-width: 0;
        width: 0;
      }
      >i{
        transform: translateY(-50%) rotate(180deg);
      }
    }
    .scenedl{
      width: 48px;
      height: 48px;
      background: rgba(0, 0, 0, 0.3);
      border: 1px solid rgba(255, 255, 255, 0.1);
      display: inline-block;
      border-radius: 50%;
      position: relative;
      margin-right: 10px;
      >i{
        top: 50%;
        left: 50%;
        position: absolute;
        transform: translate(-50%,-50%);
        font-size: 18px;
      }
    }
  }
  .l-con{
    width: 100%;
    transition: all ease 0.3s;
    max-height: 0px;
    overflow: hidden;
    .pic-con{
      background: rgba(0, 0, 0, 0.5);
      border-radius: 4px;
    }
    ul{
      padding-left: 10px!important;
      align-items: center;
      display: flex;
      li{
        margin: 0 5px;
        text-align: center;
        
      }
    }
    .tap{
      padding: 12px 10px;
      overflow-x: auto;
      &::-webkit-scrollbar{
        display: none;
      }
       > li {
        position: relative;
        &.active {
          @size:8px;
          &::before {
            content: "";
            display: inline-block;
            width: 0;
            height: 0;
            border-left:  @size * 1.5 solid transparent;
            border-right:  @size * 1.5 solid transparent;
            border-top:  @size*2 solid rgba(0, 0, 0, 0.5);
            position: absolute;
            top: -18px;
            left: 50%;
            transform: translateX(-50%);
          }
        }
      }
    }
    .pic-list{
      padding: 12px 0;
      overflow-x: auto;
      &::-webkit-scrollbar {
        display: none;
      }
      >li{
        cursor: pointer;
        >div{
          width: 72px;
          height: 72px;
          border: 2px solid #FFFFFF;
          opacity: 1;
          border-radius: 4px;
          overflow: hidden;
          position: relative;
          cursor: pointer;
          >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.3);
            position: absolute;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            left: 0;
            bottom: 0;
            width: 100%;
          }
          &.active{
              border: 2px solid #0076F6;
          }
        }
        .pic-name{
          background: rgba(0, 0, 0, 0.3);
          position: absolute;
          left: 0;
          bottom: 0;
          /deep/ span{
            word-break: keep-all;
            white-space:nowrap;
          }
        }
      }
    }
    &.active{
      max-height: 200px;
    }
  }
}

@media screen and (max-width: 350px) {
  .list{
  .menu{
    .menucon{
      padding-right: 10px;
      >ul{
        >li{
            color: #fff;
            margin: 0;
            height: 36px;
            line-height: 36px;
            padding: 0 4px;
            background: none;
            >span{
              display: inline-block;
              margin-left: 6px;
            }
            &:last-of-type{
              padding-right: 14px;
            }
          }
      }
      >i{
          font-size: 12px;
          right: 8px;
          top: 50%;
          transform: translateY(-50%);
        }
    }

    .shouqi{
      padding-right: 30px;
      >ul{
        max-width: 0;
        width: 0;
      }
      >i{
        transform: translateY(-50%) rotate(180deg);
      }
    }
    .scenedl{
      width: 36px;
      height: 36px;
      >i{
        top: 50%;
        left: 50%;
        position: absolute;
        transform: translate(-50%,-50%);
        font-size: 14px;
      }
    }
  }
}
}
</style>