<template>
  <div class="works con">
    <div class="tab">
      <span>我的作品 {{paging.total>0?`(${paging.total})`:''}}</span>
      <div class="tab-r">
         <div class="filter">
          <div :class="{active:searchKey}">
            <i class="iconfont iconworks_search"></i>
            <input
              type="text"
              placeholder="搜索作品"
              v-model="searchKey"
            >
            <i v-if="searchKey" @click="searchKey=''" class="iconfont icontoast_red del"></i>
          </div>
        </div>
      </div>
    </div>
    <ul class="w-list" v-if="list.length>0">
      <li class="add-work" @click="add">
        <div class="add-con">
          <div>
            <i class="iconfont icon_plus"></i>
          </div>
          <span>创建作品</span>
        </div>
      </li>
      <li v-for="(item,i) in list" :key="i">
        <div class="li-hover">
          <span class="lipreview ui-button submit" @click="handlePreview(item)">预览</span>
          <ul class="oper">
            <li class="comfirmhover" @click="edit(item)"><i class="iconfont iconbs_list_edit"></i>编辑</li>
            <li class="comfirmhover" @click="openShare(item)"><i class="iconfont iconbs_share"></i>分享</li>
            <li class="cancelhover" @click="del(item)"><i class="iconfont iconlist_delete"></i>删除</li>
          </ul>
        </div>
        <div class="img" @click="handlePreview(item)">
          <img class="real" :src="item.icon||$thumb" alt="" />
        </div>
        <div class="li-info">
          <div>
            <span class="shenglve tttttt" :title="item.name||'无标题'">{{item.name||'无标题'}}</span>
          </div>
          <div>
            <span>{{item.createTime}}</span>
            <div :title="item.visit">
              <i class="iconfont iconworks_look"></i>{{item.visit>10000?'1w+':item.visit}}
            </div>
          </div>
        </div>
      </li>
    </ul>
    <div class="nodata" v-else>
        <img :src="$noresult" alt="">
        <span>
          {{isClickSearch ? "未搜索到结果" : "您还没有作品，请先创建作品~"}}
        </span>
        <div class="create">创建作品</div>
      </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: 14,
        pageNum: 1,
        total: 0,
        showSize: 4,
        current: 1
      }
    }
  },
  mounted(){
    this.getWorksList()
  
  },
  watch:{
    'paging.pageNum':function () {
      this.getWorksList()
    },
    searchKey(){
      this.paging.current = 1
      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.$msg({
      //   message: `格式错误，请上传2:1、120MB以内、jpg格式的全景图片`,
      //   type: 'error',
      //   duration: 0
      // });
      this.$confirm({
          title:'删除素材',
          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

          this.$nextTick(()=>{
            this.$bus.emit('refreshTips')
          })
        }
      );
    }
  }
}
</script>

<style lang="less" scoped>
@color:#1983F6;

.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: 18px;
      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:20px;
    >li{
      width: calc((100% - @gap * 4) / 5);
      margin-bottom: @gap;
      box-shadow: 0px 1px 6px rgba(0, 0, 0, 0.16);
      background: #fff;
      position: relative;
      margin-right: @gap;
      border-radius: 6px;
      overflow: hidden;
      &:nth-of-type(5n){
        margin-right: 0;
      }

      .li-hover{
        display: none;
        width: 100%;
        height: 240px;
        position: absolute;
        top: 0;
        left: 0;
        z-index: 99;
        background: rgba(0, 0, 0, 0.6);
        .lipreview{
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%,-50%);
          color: #fff;
          display: inline-block;
          line-height: 40px;
          height: 40px;
          width: 100px;
          text-align: center;
          border-radius: 22px;
          cursor: pointer;
          background-color: transparent;
          border: 1px solid #fff;
        }
        .oper{
          display: flex;
          justify-content: space-around;
          align-items: center;
          position: absolute;
          bottom: 10px;
          left: 0;
          width: 100%;
          >li{
            color: #fff;
            font-size: 13px;
            display: flex;
            align-items: center;
            cursor: pointer;
            >i{
              font-size: 20px;
              margin-right: 4px;
            }
          }

        }
      }
      &:hover{
        .li-hover{
          display: block;
        }
        .img{
          .real{
            height: 108%;
          }  
        }
      }
     
      .img{
        width: 100%;
        height: 240px;
        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;
        >div{
          text-align: left;
          &:first-of-type{
            >span{
              font-weight: bold;
              margin-bottom: 10px;
              display: inline-block;
              text-overflow: ellipsis;
              overflow: hidden;
              white-space: nowrap;
              cursor: pointer;
              color: #323233;
              font-size: 16px;
            }
          }
          &:last-of-type{
            display: flex;
            justify-content: space-between;
            align-items: center;
            >span{
              font-size: 14px;
              color: #969799;
            }
            >div{
              color: #969799;
              i{
                margin-right: 6px;
              }
            }
          }
        }
      }
    }
    .add-work{
      .add-con{
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        text-align: center;
        div{
          width: 60px;
          height: 60px;
          border-radius: 50%;
          background: linear-gradient(144deg, #00AEFB 0%, #0076F6 100%);
          position: relative;
          cursor: pointer;
          >i{
            font-size: 32px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            color: #fff
          }
        }
        span{
          color: #333333;
          display: inline-block;
          margin-top: 8px;
          font-size: 14px;
        }
      }
    }
  }
}
</style>

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