<template>
  <div class="list">
     <div class="l-con" :class="showList?'active':''"  v-if="!(showInfo.catalogRoot.length == 1 && scenes.length == 1 && showInfo.catalogs.length == 1)">
      <div class="pic-con">

        <div class="clip-scroller" ref="sw" v-swiper:mySwiper="swiperOptions"  v-if="scenes.length>0">
          <ul class="pic-list swiper-wrapper">
            <li class="swiper-slide" @click="handleVR(item,i)" v-for="(item,i) in scenes" :key="i">
              <div :class="{active:selected.sceneCode==item.sceneCode}">
                <img :src="handleImg(item) + `?${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>
        </div>

        <div ref="sw1" class="clip-scroller" v-swiper:mySwipera="swiperOptions" v-if="childTab.length > 1">
          
        <ul class="tap pp-tap swiper-wrapper"  >
          <li class="swiper-slide" @click="handleTabtow(item,i)" 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>
      </div>
      
      <div class="clip-scroller" ref="sw2"  v-swiper:mySwiperb="swiperOptions" v-if="showInfo.catalogRoot.length > 0 && showInfo.catalogs.length > 1">
         <ul class="tap swiper-wrapper" v-if="showInfo.catalogRoot.length > 1">
          <li class="swiper-slide" @click="handleTabone(item,i)" :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>


    <div v-if="selected.type=='4dkk' && isShowDL" class="menu">
        <div  @click="handleDL" v-if="isShowDL">
            <i class="iconfont" :class="isDaolan?'iconshow_suspension':'iconshow_playback'"></i>
        </div>
    </div>

    <div class="btn dl" :class="{deepbg:showList}" v-if="!(showInfo.catalogRoot.length == 1 && scenes.length == 1 && showInfo.catalogs.length == 1)" @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";
import { directive } from "vue-awesome-swiper";
// import style (<= Swiper 5.x)
import "swiper/css/swiper.css";

export default {
  props:['select','mapvisit'],
  components:{rollName},
  directives: {
    swiper: directive,
  },
  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"
      }),
      swiperOptions() {
        return {
          slidesPerView: "auto",
          // observer:true,
          // observeParents: true,
          centeredSlides : true,
          centerInsufficientSlides: true,
          centeredSlidesBounds: true,
          freeMode:true
        };
      },
      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:{
    handleImg(item){
      let url = ''
      if (item.type == "4dkk") {
        url = `local/4dkk/${item.sceneCode}/wwwroot/images/images${item.sceneCode}/thumbSmallImg.jpg`
      }
      else{
        if (item.icon.indexOf('preview.jpg')>-1) {
          url = item.icon.replace('https://4dkk.4dage.com/720yun_fd_manage/image/',`local/pano/${item.sceneCode}/`)
        }
        else{
          url = item.icon.replace('https://4dkk.4dage.com/720yun_fd_manage/image/',`local/pano/images/`)
        }
      }
      return url || item.icon
    },
    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()
      }
    },

     fixPosit(ref,i,prenum,nexnum){
         if (i!==null) {
        let acidx = this.$refs[ref].swiper.activeIndex
        if (i - acidx > nexnum ) {
          this.$refs[ref].swiper.slideNext()
        }

        if (acidx - i > prenum ) {
          this.$refs[ref].swiper.slidePrev()
        }
      }
    },

    handleTabtow(item,i=null){
      this.tabtowActive = item
      this.fixPosit('sw1',i,1,1)
    },

    handleTabone(item,i=null){
      this.taboneActive = item
      this.fixPosit('sw2',i,1,1)
    },

    handleVR(item,i=null){
      history.replaceState(null, null, "".concat(window.location.pathname, "?").concat(`id=${this.showInfo.id}&vr=${item.sceneCode}`));
      this.sceneNum = item.sceneCode

      this.fixPosit('sw',i,1,1)

    }
  },
  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 = true
          setTimeout(() => {
            this.showList = false
          }, 800);
        }
      }
    }
  }
}
</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: 44px;
  left: 0;
  text-align: right;
  width: 100%;

  .clip-scroller{
    padding: 0 15px;
  }

  .dl{
    background: rgba(0, 0, 0, 0.3);
    border-radius: 18px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    margin-top: 18px;
    margin-right: 15px;
    padding: 0 10px;
    height: 36px;
    line-height: 36px;
    color: #fff;
    vertical-align: middle;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 120px;
    >i{
      margin-right: 8px;
      vertical-align: bottom;
      font-size: 18px;
    }
  }

  .deepbg{
    background: rgba(0, 0, 0, 0.5);
  }
  .menu{
      position: fixed;
      left: 15px;
      bottom: 40px;
      >div{
        width: 36px;
        height: 36px;
        display: inline-block;
        background: rgba(0, 0, 0, 0.5);
        border: 1px solid rgba(255, 255, 255, 0.2);
        opacity: 1;
        border-radius: 18px;
        position: relative;
        cursor: pointer;
        >i{
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
        }
      }
  }
  .l-con{
    width: 100%;
    transition: all ease 0.3s;
    max-height: 0px;
    overflow: hidden;
    .pic-con{
      background: rgba(0, 0, 0, 0.5);
    }
    ul{
      align-items: center;
      display: flex;
      li{
        margin: 0 5px;
        text-align: center;
        
      }
    }
    .tap{
      padding: 12px 0 0;
      &::-webkit-scrollbar{
        display: none;
      }
       > li {
         font-size: 0;
        position: relative;
        width: 104px;

        .btn{
          font-size: 14px;
          width: 100%;
          padding: 0;
        }
        // &.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%);
        //   }
        // }
      }
    }

    .pp-tap{
      padding: 8px 0 18px;
       > li {
         width: 88px;
      }
    }
    .pic-list{
      padding: 10px 0;
      &::-webkit-scrollbar {
        display: none;
      }
      >li{
        cursor: pointer;
        width: 70px;
        height: 70px;
        >div{
          width: 100%;
          height: 100%;
          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;
            &::after{
              background: rgba(0, 0, 0, 0.3);
              content: '';
              width: 14px;
              height: 14px;
              display: inline-block;
              position: absolute;
              top: 50%;
              left: 50%;
              transform: translate(-50%,-50%);
              z-index: -1;
              filter: blur(4px);
            }
          }
          >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;
          font-size: 0;
          /deep/ span{
            font-size: 12px;
            word-break: keep-all;
            white-space:nowrap;
            padding: 3px 0;
          }
        }
      }
    }
    &.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>