<template>
  <!-- todo: 旧版的初始场景设置时弹窗，可以删了？ -->
  <div class="select-commodity">
    <a class="close" @click="$emit('cancle')">+</a>
    <h3 class="title">{{ title }}</h3>
    <div class="filtert">
      <div>
        <input
          type="text"
          placeholder="输入关键词"
          v-model="key"
          @keyup.enter="search"
        />
        <i
        @click="search"
          class="iconfont iconsearch"
        ></i>
      </div>
    </div>

    <template>
      <div class="scene-layer">
        <tabList :list="info.catalogRoot" :cls="'w-menu'" @clickItem="item=>{taboneActive = item}" :hiddenHover="true" :active="taboneActive" :id="'selct11'" :subId="'selct111'">
        </tabList>
        <tabList v-if="childTab.length > 1" :list="childTab" :cls="'w-menu'" :hiddenHover="true" @clickItem="item=>{tabtowActive = item}"
           :active="tabtowActive" :id="'subselct11'" :subId="'subselct111'">
        </tabList>
       
        <div>
          <div class="pano-con"  v-if="filterScenes.length > 0">
            <ul>
              <li
                v-for="(item, i) in filterScenes"
                @click="select = item"
                :class="{ 'li-active': select.sceneCode == item.sceneCode }"
                :key="i"
              >
                <div class="typeli">
                  <i
                    class="iconfont iconedit_type_3d"
                    :class="{'iconedit_type_panorama':item.type!=='4dkk'}"
                  ></i>
                </div>
                <div class="img">
                  <img :key="i" :src="`${item.icon}?rnd=${Math.random()}`" alt="" />
                </div>
                <div class="ui-title">
                  <span>{{
                   item.sceneTitle
                  }}</span>
                </div>
              </li>
            </ul>
          </div>
          <div class="nodata" v-else>
              <img :src="$noresult" alt="">
              <span>{{!!isClickSearch?'未搜索到结果~':'暂无素材'}}</span>
            </div>
        </div>
      </div>
    </template>

    <div class="btns">
      <a @click="$emit('cancle')">取消</a>
      <a :class="{ disable: disable }" @click="$emit('submit', select)">确定</a>
    </div>
  </div>
</template>

<script>
import tabList from "@/components/tablist";
import { mapGetters } from "vuex";
import { getTabSceneList } from "@/api";

export default {
components: {tabList},
 props: {
    selected: {
      default() {
        return {};
      },
      type: Object | String,
    },
    title: {
      default: "",
      type: String,
    }
  },
  watch: {
    taboneActive: {
      immediate:true,
      deep:true,
      handler: function (newVal,oldVal) {
        
        if (!newVal.id) {
          this.taboneActive = this.info.catalogRoot[0]
        }

        let temp = []
        newVal.children && newVal.children.forEach(item=>{
          this.info.catalogs.forEach(sub=>{
            if (item==sub.id) {
              temp.push(sub)
            }
          })
        })

        this.childTab = temp

        if (this.childTab.length == 1 || newVal!=oldVal) {
          this.tabtowActive = this.childTab[0]
        }
      },
    },
    tabtowActive: {
      immediate:true,
      handler: function (newVal) {
        if (!newVal) {
          this.tabtowActive = this.childTab[0]
        } else{
          this.$emit("catalog",newVal);
          this.scenes = this.info.scenes.filter(item=>{
            return newVal.id == item.category
          })
          this.key = ''

          this.search()
        }
      }
    }
  },
  computed: {
    ...mapGetters({
      tablist:'tablist',
      info: "info",
      backupInfo: "backupInfo"
    }),
    disable() {
      return !this.select.id;
    }
  },
  data() {
    return {
      taboneActive:{children:[]},
      tabtowActive:'',
      select:{...this.selected},
      list:[],
      childTab:[],
      scenes:[],
      filterScenes:[],
      isClickSearch:'',
      key: ""
    };
  },

  methods: {
    search(){
      let tmp = this.scenes.filter(item=>{
        if (item.sceneTitle.indexOf(this.key)>-1) {
          return item
        }
      })
      this.filterScenes = tmp.sort((a,b)=>a.weight-b.weight)
    },
    getTabSceneList(catalogId=null){
      if (!catalogId) {
        catalogId = this.taboneActive.children.length<=0 ? this.taboneActive.id : this.tabtowActive.id
      }
      getTabSceneList({catalogId},(data)=>{
          this.list = data.data||[]
      })
    },
  },
  mounted() {
    this.key = "";
  },
};
</script>

<style lang="less" scoped>
::-webkit-scrollbar-track {
  box-shadow: inset 0 0 5px rgb(0 0 0 / 20%);
  border-radius: 4px;
  background: #fff;
}
.select-commodity {
  position: fixed;
  z-index: 3;
  left: 50%;
  top: 50%;
  transform: translateX(-50%) translateY(-50%);
  color: #202020;
  background: #ffffff;
  max-width: 960px;
  width: 100%;
  border-radius: 6px;
}

.select-commodity > * {
  padding: 15px;
}

.title {
  font-size: 16px;
  line-height: 20px;
  font-weight: 400;
  margin: 0;
  font-weight: bold;
  border-bottom: 1px solid rgba(255, 255, 255, 0.3);
}

.close {
  position: absolute;
  right: -8px;
  top: -15px;
  font-size: 25px;
  color: #909090;
  transform: rotate(45deg);
  cursor: pointer;
}

.filtert {
  padding: 15px;
}

.filtert > div {
  width: 210px;
  height: 34px;
  background: rgba(22, 26, 26, 1);
  border-radius: 2px;
  display: inline-block;
  margin-right: 10px;
  position: relative;
  vertical-align: middle;
}

.filtert > div > select,
.filtert > div > input {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  border: none;
  background: none;
  color: #202020;
  padding: 8px 10px;
  outline: none;
  background: #ffffff;
  border: 1px solid #ebebeb;
}

.filtert > div > input {
  padding-right: 40px;
}

.filtert > div > i {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  right: 10px;
  cursor: pointer;
  color: @color;
}

.scene-layer {
  min-height: 400px;
  max-height: 60vh;
  overflow-y: auto;
  position: relative;
  .nodata {
    position: absolute;
    left: 50%;
    top: 40%;
    transform: translate(-50%, -50%);
  }
  .pano-con{
    background: #fff;
    padding: 0;
    > ul {
      > li{
        cursor: pointer;
        margin: 10px 10px 30px 0;
        .ui-title {
          > span {
            color: #202020;
          }
        }
        .typeli,
        .oper {
          background: rgba(0, 0, 0, 0.5);
          .iconfont{
            color:#fff
          }
        }
      }
    }
  }
}


.btns {
  text-align: center;
  > a {
    width: 120px;
    height: 40px;
    text-align: center;
    line-height: 40px;
    opacity: 1;
    border-radius: 20px;
    font-size: 12px;
    cursor: pointer;
    display: inline-block;
    color: @color;
    margin: 0 10px;
    &:nth-child(2) {
      background-color: @color;
      color: #fff;
    }
    &:nth-child(1) {
      border: 1px solid @color;
    }
  }
  .disable {
    pointer-events: none !important;
    opacity: 0.5 !important;
  }
}
</style>