<template>
  <div class="page">
    <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.fileTitle" @search="onSearch" />
      </div>
      <div class="divider"></div>
      <!-- tabbar -->
      <simple-tabs
        :tabs="tabs"
        :current="current"
        @change="onTabChange"
        @search="onSearch"
      ></simple-tabs>
      <div class="list">
        <div
          class="section flex justify-between items-center"
          v-for="item in list"
          :key="item.fileId"
        >
          <div class="info">
            <div class="name" v-text="item.fileName"></div>
            <div class="download" @click="downloadLink(item)">点击下载</div>
          </div>
        </div>
      </div>

      <!-- 列表为空 -->
      <SimpleEmpty
        v-if="!total && !isRequest"
        description="敬请期待~"
      ></SimpleEmpty>
      <!-- 页码 -->
      <SimplePagination
        v-if="total > listQuery.pageSize"
        :total="total"
        :pageItems="listQuery.pageSize"
        @change="onPagiantionChange"
      ></SimplePagination>
    </div>
  </div>
</template>

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

export default {
  layout: 'app',
  data() {
    return {
      isRequest: true,
      tabs: tabs(),
      current: 4,
      listQuery: {
        fileType: 2,
        fileTitle: '',
        authUserId: '',
        pageNum: 1,
        pageSize: 4,
      },
      list: [],
      total: 0,
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'supplierInfo']),
  },
  mounted() {
    this.fetchList()
  },
  methods: {
    // 下载方法
    downloadLink(item) {
      const url = `${process.env.BASE_URL}/download/file?ossName=${item.fileDownloadUrl}&fileName=${item.fileName}`
      downloadLink(url)
    },
    // 获取列表
    async fetchList() {
      try {
        this.listQuery.authUserId = this.userInfo.authUserId
        const res = await this.$http.api.getFileList(this.listQuery)
        this.list = res.data.list
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequest = false
      }
    },
    // tab切换
    onTabChange(item) {
      console.log(item)
      this.$router.push(`/ph${item.path}`)
    },
    // 搜索
    onSearch(keyword) {
      this.listQuery.fileTitle = keyword
      this.listQuery.pageNum = 1
      this.fetchList()
    },
    // 页码变化
    onPagiantionChange(index) {
      this.listQuery.pageNum = index
      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;
    background: url(https://static.caimei365.com/www/authentic/pc/bg-doc.png);
    background-size: auto 360px;
    .logo {
      display: block;
      width: 120px;
      height: 120px;
      border-radius: 50%;
    }
    .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: 100%;
        position: relative;
        .name {
          width: 1050px;
          font-size: 18px;
          color: #404040;
          line-height: 1.6;
          text-align: justify;
          @include ellipsis(2);
        }
        .download {
          position: absolute;
          right: 0;
          top: 50%;
          transform: translateY(-50%);
          font-size: 16px;
          color: #bc1724;
          cursor: pointer;
          &::after {
            content: '>';
            margin-left: 8px;
          }
        }
      }
    }
  }
}

// 移动 端
@media screen and (max-width: 768px) {
  .page-top {
    height: 46vw;
    background: url(https://static.caimei365.com/www/authentic/h5/bg-club.png);
    background-size: auto 46vw;
    .logo {
      display: block;
      width: 14.8vw;
      height: 14.8vw;
      border-radius: 50%;
    }
    .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: 4.8vw 0;
      margin: 0 4vw;
      background: #fff;
      border-bottom: 0.1vw solid #d8d8d8;

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

      .info {
        .name {
          width: 66vw;
          font-size: 3.6vw;
          color: #404040;
          line-height: 1.5;
          text-align: justify;
          @include ellipsis(2);
        }

        .download {
          position: absolute;
          right: 0;
          bottom: 50%;
          transform: translateY(50%);
          font-size: 3.2vw;
          color: #bc1724;
          cursor: pointer;

          &::after {
            content: '>';
            margin-left: 8px;
          }
        }
      }
    }
  }
}
</style>
