<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.imageTitle"
            @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" v-for="item in list" :key="item.imageId">
            <div class="info">
              <div class="name" v-text="item.imageTitle"></div>
              <div class="date">{{ item.createTime | dateFormat }}</div>
              <div
                class="download"
                @click="downloadLink(item.imageZipUrl, $event)"
              >
                保存所有图片
              </div>
            </div>
            <div class="images grid grid-cols-4 md:grid-cols-8 gap-3 md:gap-4">
              <div
                class="item"
                v-for="(image, index) in item.imageList"
                :key="index"
                @click="onImagePreview(item.imageList, index)"
              >
                <img class="object-cover" :src="image" />
              </div>
            </div>
          </div>
        </div>
        <!-- 列表为空 -->
        <SimpleEmpty
          v-if="!total && !isRequest"
          description="敬请期待~"
        ></SimpleEmpty>
      </div>
    </van-list>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { tabs } from '@/configs/tabs'
import downloadLink from '@/utils/download-link'
import { ImagePreview } from 'vant'
import { debounce } from '~/utils'

export default {
  layout: 'app',
  data() {
    return {
      isLoadingMore: true,
      finished: false,
      isRequest: true,
      tabs: tabs(),
      current: 1,
      listQuery: {
        imageTitle: '',
        authUserId: '',
        pageNum: 1,
        pageSize: 10,
      },
      list: [],
      total: 0,
    }
  },
  computed: {
    ...mapGetters(['routePrefix', 'supplierInfo', 'authUserId']),
  },
  mounted() {
    this.fetchList()
  },
  methods: {
    // 下载方法
    downloadLink,
    // 获取列表
    fetchList: debounce(async function () {
      try {
        this.isLoadingMore = true
        this.listQuery.authUserId = this.authUserId
        const res = await this.$http.api.getImageList(this.listQuery)
        this.list = [...this.list, ...res.data.list]
        this.finished = !res.data.hasNextPage
        this.total = res.data.total
        this.isLoadingMore = false
        this.listQuery.pageNum += 1
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequest = false
      }
    }, 400),

    // 图片预览
    onImagePreview(imageList, index) {
      ImagePreview({
        images: imageList,
        startPosition: index,
        loop: true,
        showIndex: true,
        showIndicators: true,
        closeable: true,
      })
    },
    // tab切换
    onTabChange(item) {
      this.$router.push(`${this.routePrefix}${item.path}`)
    },
    // 搜索
    onSearch(keyword) {
      this.list = []
      this.listQuery.imageTitle = 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;

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

      .info {
        position: relative;
        .name {
          font-size: 18px;
          color: #404040;
          line-height: 1.6;
          margin-bottom: 18px;
          text-align: justify;
        }
        .date {
          font-size: 18px;
          color: #b2b2b2;
          line-height: 1.4;
        }

        .download {
          bottom: 0;
          right: 0;
          position: absolute;
          @include themify($themes) {
            color: themed('color');
          }
          font-size: 16px;
          cursor: pointer;
        }
      }

      .images {
        margin-top: 24px;
        .item {
          width: 126px;
          height: 126px;
          overflow: hidden;
          img {
            display: block;
            width: 100%;
            height: 100%;
            cursor: pointer;
          }
        }
      }
    }
  }
}

// 移动 端
@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 {
          bottom: 2.4vw;
          left: 0;
          position: absolute;
          font-size: 3.2vw;
          color: #b2b2b2;
          line-height: 1.4;
        }

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

      .images {
        margin-top: 2.4vw;
        .item {
          width: 21vw;
          height: 21vw;
          overflow: hidden;
          img {
            display: block;
            width: 100%;
            height: 100%;
          }
        }
      }
    }
  }
}
</style>
