<template>
  <div class="works con">
    <div class="tab">
      <span>我的全景作品</span>
      <div class="tab-r">
        <button @click="add" class="ui-button ui-button-rect submit"><span>创建作品</span></button>
         <div class="filter">
          <div>
            <input
              type="text"
              placeholder="搜索全景作品"
              v-model="searchKey"
              @keyup.enter="(paging.current = 1) && getWorksList()"
            >
            <i @click="(paging.current = 1) && getWorksList()" class="iconfont iconbs_search"></i>
          </div>
        </div>
      </div>
    </div>
    <ul class="w-list">
      <li v-for="(item,i) in list" :key="i">
        <div class="view" :title="item.visit">
          <i class="iconfont iconlist_views"></i>{{item.visit>10000?'1w+':item.visit}}
        </div>
        <div class="share" @click="openShare(item)">
          <i class="iconfont iconbs_share"></i>
        </div>
        <div class="img" @click="handlePreview(item)">
          <img class="real" :src="item.icon||$thumb" alt="" />
        </div>
        <div class="li-info">
          <span class="shenglve" :title="item.name" @click="handlePreview(item)">{{item.name||'无标题'}}</span>
          <div>
            <span>{{item.createTime}}</span>
            <div>
              <span @click="edit(item)" class="edit">编辑</span>
              <span @click="del(item)" class="del">删除</span>
            </div>
          </div>
        </div>
      </li>
    </ul>
    <div class="nodata" v-if="list.length == 0">
        <img :src="$noresult" alt="">
        <span>
          {{isClickSearch ? "未搜索到结果" : "暂无作品"}}
        </span>
      </div>
    <div class="paging">
      <Paging
        v-if="paging.total>0"
        :paging="paging"
        @changeCurrent="changeCurrent"
      />
    </div>
    <share :show='showShare' :item="shareItem" @close="showShare=false"></share>
    <preview
      v-if="showItem"
      :name="showItem.name"
      :show="showPreview"
      :ifr="`./show.html?id=${showItem.id}`"
      @close="showPreview = false"
    />
  </div>
</template>

<script>
import Paging from '../components/paging'
import share from '../popup/share'
import preview from "@/components/preview";

import {
  addWorks,
  getWorksList,
  delWorks,
  getPanoInfo
} from "@/api";

export default {
  components:{
    Paging,
    share,
    preview
  },
  data(){
    return {
      list:[],
      searchKey:'',
      showShare:false,
      showPreview:false,
      showItem:'',
      shareItem:'',
      isClickSearch: "",
      paging: {
        pageSize: 8,
        pageNum: 1,
        total: 0,
        showSize: 4,
        current: 1
      }
    }
  },
  mounted(){
    this.getWorksList()
  },
  watch:{
    'paging.pageNum':function () {
      this.getWorksList()
    }
  },
  methods:{

    openShare(data){
      getPanoInfo(data.id, (data) => {
        if (data.scenes.length<=0) {
          return this.$tips({
            content: "链接未生成，请编辑作品添加素材"
          });
        }
        this.showShare = true
        this.shareItem = data
      })
    },
    
    handlePreview(item){
      getPanoInfo(item.id, (data) => {
        if (data.scenes.length<=0) {
          return this.$tips({
            content: "链接未生成，请编辑作品添加素材"
          });
        }
        this.showItem = {
          ...item,
          ...data
        }
        this.showPreview=true
      })
    },
    changeCurrent(data){
      this.paging.pageNum = data
    },
    add(){
      addWorks({},(data)=>{
        window.open(`./edit.html?id=${data.data.id}`)
      })
    },
    edit(item){
        window.open(`./edit.html?id=${item.id}`)
    },
    del(item){
      this.$confirm({
          content: "是否删除？",
          ok: () => {
              delWorks(item.id,()=>{
                this.$tips({ content: "删除成功",icon:'ok' });
                this.getWorksList()
              });
          }
      });
    },
    getWorksList() {
      this.isClickSearch = !!this.searchKey;
      getWorksList(
        {
          pageNum: this.paging.pageNum,
          pageSize: this.paging.pageSize,
          searchKey: this.searchKey,
        },
        (data) => {
          this.paging.pageNum = data.data.pageNum;
          this.paging.pageSize = data.data.pageSize;
          this.paging.total = data.data.total;
          this.list =data.data.list
        }
      );
    }
  }
}
</script>

<style lang="less" scoped>
.works{
  width: 100%;
  flex-direction: column;
  .tab{
    width: 100%;
    display: flex;
    background: #fff;
    justify-content: space-between;
    align-items: center;
    padding: 24px 30px;
    >span{
      font-size: 16px;
      font-weight: bold;
    }
    .tab-r{
      align-items: center;
      display: flex;
      .ui-button{
        margin-right: 20px;
      }
    }
  }
  .w-list{
    width: 100%;
    flex-wrap: wrap;
    margin-top: 30px;
    display: flex;
    @gap:27px;
    >li{
      width: calc((100% - @gap * 3) / 4);
      margin-bottom: @gap;
      box-shadow: 0px 1px 6px rgba(0, 0, 0, 0.16);
      background: #fff;
      position: relative;
      margin-right: @gap;

      &:nth-of-type(4n){
        margin-right: 0;
      }

     
      &:hover{
        .img{
          .real{
            height: 108%;
          }  
        }
      }
      >.view{
        position: absolute;
        top: 7px;
        left: 7px;
        color: #fff;
        z-index: 99;
        font-size: 12px;
        text-shadow: 0px 1px 3px rgba(0, 0, 0, 0.3);
        .iconfont{
          font-size: 12px;
          margin-right: 8px;
        }
      }
      .share{
        position: absolute;
        bottom: 82px;
        right: 10px;
        width: 40px;
        height: 40px;
        cursor: pointer;
        background: rgba(0, 0, 0, 0.5);
        color: #fff;
        z-index: 99;
        .iconfont{
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%,-50%);
          font-size: 24px;
        }
      }
      .img{
        width: 100%;
        height: 300px;
        position: relative;
        overflow: hidden;
        cursor: pointer;
        .real{
          height: 100%;
          position: absolute;
          top: 0;
          left: 50%;
          transform: translateX(-50%);
          z-index: 0;
          transition: all ease 0.3s;
        }   
      }
         
      .li-info{
        font-size: 14px;
        padding: 10px;
        >span{
          font-weight: bold;
          margin-bottom: 10px;
          display: inline-block;
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
          width: 100%;
          cursor: pointer;
        }
        >div{
          display: flex;
          justify-content: space-between;
          align-items: center;
          >span{
            font-size: 12px;
            color: #909090;
          }
          >div{
            .edit{
              margin-right: 10px;
              cursor: pointer;
              color: @color;
            }
            .del{
              cursor: pointer;
              color: #F56C6C;
            }
          }
        }
      }
    }
  }
}
</style>

<style lang="less" scoped>
@import '../style.less';
</style>