<template>
  <div class="page">
    <div class="page-top flex flex-col justify-center items-center">
      <img
        class="logo"
        src="https://static.caimei365.com/www/authentic/pc/ldm-logo-rect.png"
      />
      <div class="name mt-2">
        Bring you into a new era<br />
        of high-tech non-invasive beauty care
      </div>
    </div>
    <div class="page-content">
      <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 downloadLink from '@/utils/download-link'

export default {
  layout: 'app-ldm',
  data() {
    return {
      isRequest: true,
      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
      }
    },
    // 页码变化
    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;
}
@media screen and (min-width: 768px) {
  .page-top {
    height: 466px;
    .logo {
      height: 61px;
      width: 311px;
      display: block;
    }

    .name {
      font-size: 19px;
      color: #221815;
      margin-top: 80px;
      text-align: center;
      line-height: 36px;
      text-transform: uppercase;
    }
  }

  .page-content {
    width: 973px;
    margin: 0 auto;
    .list {
      .section {
        position: relative;
        padding: 32px 16px;
        background: #fff;
        border-bottom: 1px solid #d8d8d8;

        &:first-child {
          border-top: 1px solid #d8d8d8;
        }

        .info {
          .name {
            width: 800px;
            font-size: 19px;
            color: #404040;
            line-height: 1.5;
            text-align: justify;
            @include ellipsis(1);
          }

          .download {
            position: absolute;
            right: 16px;
            bottom: 50%;
            transform: translateY(50%);
            font-size: 16px;
            color: #0080ed;
            cursor: pointer;

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

@media screen and (max-width: 768px) {
  .page-top {
    height: 76.4vw;
    .logo {
      height: 11.5vw;
      width: 59vw;
      display: block;
    }

    .name {
      font-size: 3.5vw;
      color: #221815;
      margin-top: 15vw;
      text-align: center;
      line-height: 6.6vw;
      text-transform: uppercase;
    }
  }

  .page-content {
    .list {
      .section {
        position: relative;
        padding: 6vw 2.6vw;
        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(1);
          }

          .download {
            position: absolute;
            right: 2.6vw;
            bottom: 50%;
            transform: translateY(50%);
            font-size: 3vw;
            color: #0080ed;
            cursor: pointer;

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