<template>
  <div class="page">
    <van-list
      v-model="isLoadingMore"
      :finished="finished"
      :immediate-check="false"
      :finished-text="total ? '没有更多了' : ''"
      @load="onLoadMore"
    >
      <div class="page-top flex flex-col justify-center items-center">
        <!-- <img class="logo" :src="supplierInfo.logo" /> -->
        <span
          class="name mt-2"
          v-text="supplierInfo.shopName + '资料库'"
        ></span>
      </div>
      <div class="page-content">
        <!-- 搜索区域 -->
        <div class="search">
          <simple-search
            v-model="listQuery.videoTitle"
            @search="onSearch"
            placeholder="搜索视频"
          />
        </div>
        <div class="divider"></div>
        <!-- tabbar -->
        <simple-tabs
          :tabs="tabs"
          :current="current"
          @change="onTabChange"
          @search="onSearch"
        ></simple-tabs>
        <div class="list">
          <div
            class="section md:flex md:justify-between md:items-center"
            v-for="item in list"
            :key="item.videoId"
          >
            <div class="info">
              <div class="name" v-text="item.videoTitle"></div>
              <div class="date">{{ item.createTime | dateFormat }}</div>
              <div class="download" @click="download(item, $event)">
                保存视频
              </div>
            </div>
            <div class="cover">
              <video class="cover" :src="item.videoPreviewUrl" :poster="item.videoImage"></video>
              <span @click="onPlayer(item)"></span>
            </div>
          </div>
        </div>

        <!-- 列表为空 -->
        <SimpleEmpty
          v-if="!total && !isRequest"
          description="敬请期待~"
        ></SimpleEmpty>
      </div>
    </van-list>

    <SimpleVideoPlayer ref="player" :videoSrc="videoUrl"></SimpleVideoPlayer>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { tabs } from '@/configs/tabs'
import downloadFile from '~/utils/donwload-tools'
import { debounce } from '~/utils'

export default {
  layout: 'app',
  data() {
    return {
      isLoadingMore: true,
      finished: false,
      isRequest: true,
      tabs: tabs(),
      current: 2,
      listQuery: {
        videoTitle: '',
        authUserId: '102',
        pageNum: 1,
        pageSize: 10,
      },
      list: [],
      total: 0,
      videoUrl: '',
    }
  },
  computed: {
    ...mapGetters(['routePrefix', 'supplierInfo', 'authUserId']),
  },
  mounted() {
    this.fetchList()
  },
  methods: {
    // 视频在线播放
    onPlayer(item) {
      this.videoUrl = item.videoPreviewUrl
      this.$refs.player.open()
    },
    // 下载方法
    download(item, $event) {
      const url = `${process.env.BASE_URL}/download/file?ossName=${item.videoDownloadUrl}&fileName=${item.videoName}`
      downloadFile(url, item.videoName, this, $event)
    },
    // 获取列表
    fetchList: debounce(async function () {
      try {
        this.isLoadingMore = true
        this.listQuery.authUserId = this.authUserId
        const res = await this.$http.api.getVideoList(this.listQuery)
        this.list = [...this.list, ...res.data.list]
        this.finished = !res.data.hasNextPage
        this.listQuery.pageNum += 1
        this.isLoadingMore = false
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequest = false
      }
    }, 400),

    // tab切换
    onTabChange(item) {
      this.$router.push(`${this.routePrefix}${item.path}`)
    },
    // 搜索
    onSearch(keyword) {
      this.list = []
      this.listQuery.videoTitle = keyword
      this.listQuery.pageNum = 1
      this.fetchList()
    },
    // 页码变化
    onPagiantionChange(index) {
      this.listQuery.pageNum = index
      this.fetchList()
    },
    // 加载更多
    onLoadMore() {
      this.fetchList()
    },
  },
}
</script>

<style scoped lang="scss">
/* scss中可以用mixin来扩展 */
@mixin ellipsis($line: 1) {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: $line;
  -webkit-box-orient: vertical;
}

// pc 端
@media screen and (min-width: 768px) {
  .page-top {
    height: 360px;
    @include themify($themes) {
      background: themed('pc-banner-doc');
    }
    background-size: auto 360px;
    .logo {
      display: block;
      width: 120px;
      height: 120px;
      border-radius: 50%;
      background: #fff;
    }
    .name {
      font-size: 30px;
      color: #fff;
    }
  }
  .page-content {
    width: 1200px;
    margin: 0 auto;
    background-color: #fff;
    .divider {
      height: 16px;
      background: #f7f7f7;
    }
    .search {
      display: none;
    }
  }
  .list {
    border-top: 16px solid #f7f7f7;
    border-bottom: 16px solid #f7f7f7;
    .section {
      padding: 32px 0;
      margin: 0 24px;
      background: #fff;
      border-bottom: 1px solid #d8d8d8;
      transition: all 0.4s;

      &:last-child {
        border-bottom: 0;
      }

      .info {
        width: 880px;
        height: 100%;

        .name {
          height: 56px;
          font-size: 18px;
          color: #404040;
          line-height: 1.6;
          margin-bottom: 18px;
          @include ellipsis(2);
          text-align: justify;
        }
        .date {
          font-size: 18px;
          color: #b2b2b2;
          line-height: 1.4;
        }
        .download {
          font-size: 16px;
          color: #1890ff;
          cursor: pointer;
          margin-top: 8px;
        }
      }

      .cover {
        position: relative;
        width: 216px;
        height: 132px;
        background: #333;
        span {
          position: absolute;
          width: 36px;
          height: 36px;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          background: url(~assets/theme-images/common/h5-icon-play.png)
            no-repeat center;
          background-size: 36px;
          border-radius: 50%;
          cursor: pointer;
          box-shadow: 0 0 8px rgba(0, 0, 0, 0.1);
        }
      }
    }
  }
}

// 移动 端
@media screen and (max-width: 768px) {
  .page-top {
    height: 46vw;
    @include themify($themes) {
      background: themed('h5-banner-doc');
    }
    background-size: auto 46vw;
    .logo {
      display: block;
      width: 14.8vw;
      height: 14.8vw;
      border-radius: 50%;
      background: #fff;
    }
    .name {
      font-size: 4vw;
      color: #fff;
    }
  }

  .page-content {
    position: relative;
    .divider {
      border-bottom: 3.2vw solid #f7f7f7;
      height: 12.4vw;
    }
  }

  .search {
    position: absolute;
    left: 50%;
    top: 0;
    transform: translate(-50%, -50%);
  }

  .list {
    .section {
      position: relative;
      padding: 2.4vw 0;
      margin: 0 4vw;
      padding-bottom: 9.8vw;
      background: #fff;
      border-bottom: 0.1vw solid #d8d8d8;

      &:first-child {
        border-top: 0.1vw solid #d8d8d8;
      }

      .info {
        .name {
          font-size: 3.6vw;
          color: #404040;
          line-height: 1.5;
          margin-bottom: 2.4vw;
          @include ellipsis(2);
          text-align: justify;
        }
        .date {
          position: absolute;
          left: 0;
          bottom: 2.4vw;
          font-size: 3.2vw;
          color: #b2b2b2;
          line-height: 1.4;
        }

        .download {
          position: absolute;
          right: 0;
          bottom: 2.4vw;
          font-size: 3.2vw;
          @include themify($themes) {
            color: themed('color');
          }
          cursor: pointer;
          margin-top: 1.2vw;
        }
      }

      .cover {
        position: relative;
        width: 92vw;
        height: 56vw;
        border-bottom: 0.1vw solid #d8d8d8;
        background: #333;
        span {
          position: absolute;
          width: 12vw;
          height: 12vw;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          background: url(~assets/theme-images/common/h5-icon-play.png)
            no-repeat center;
          background-size: 12vw;
          border-radius: 50%;
          cursor: pointer;
          box-shadow: 0 0 8px rgba(0, 0, 0, 0.1);
        }
      }
    }
  }
}
</style>
