<!--
 * @Description: 
 * @Author: 欧阳承珺
 * @LastEditors: 欧阳承珺
 * @Date: 2022-11-01 19:27:42
 * @LastEditTime: 2022-11-08 09:44:09
-->
<template>
  <div>
    <div class="expenses-date-tips flex content-center py-6px ">
      <img src="http://cn-file.17pdf.com/website/common/ic_notice.svg" class="align-middle">
      <div class="text-container">
        <span class="text">
          转档后的文件支持在云端保留24小时，<span v-if="userInfo?.memberInfo.subscriberType === 1">会员尊享5G容量，</span>请在24小时内下载文件至本地永久保存
        </span>
        <span v-if="userInfo?.memberInfo.subscriberType === 1" class="vip tip">
          <img src="http://cn-file.17pdf.com/website/common/ic_info.svg" alt="">
          <div class="tip-text">
            若已有文件大小超出5G，将按转档时间计算（从最近一次转档往过去推算），即保留最近的5G容量文件，超出部分文件将不再保留
          </div>
        </span>
      </div>
    </div>

    <div class="py-15px">
      <p class="text-[20px] leading-28px text-[#333]">我的转档</p>
      <div class="mt-10px mb-16px">
        <button class="btn btn-download" @click="downloadFiles" :disabled="isDownload">下载</button>
        <button class="btn btn-delete" @click="deleteChangeFileRecord" :disabled="isDelete">删除</button>
      </div>
    </div>

    <el-table :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50"></el-table-column>
      <el-table-column prop="format" label="格式转换" ></el-table-column>
      <el-table-column prop="fileName" label="目标文件" ></el-table-column>
      <el-table-column prop="size" label="文件大小" ></el-table-column>
      <el-table-column prop="price" label="消耗券数" ></el-table-column>
      <el-table-column prop="status" label="状态" width="120px"></el-table-column>
      <el-table-column prop="path" label="" width="50px">
        <template slot-scope="scope">
          <a v-if="scope.row.path" :href="scope.row.path" class="downloadBtn"></a>
        </template>
      </el-table-column>
      <!-- 表格无数据显示 -->
      <div slot="empty">
        <div class="table-empty"></div>
        <p class="text-14px" style="color:rgba(0,0,0,0.54)">没有任何消费记录</p>
        <p class="leading-20px mt-8px mb-100px text-14px" style="color:rgba(0,0,0,0.38)">每一分钱都花在了刀刃上</p>
      </div>
    </el-table>
    <el-pagination
      class="text-center mt-50px"
      background
      layout="prev, pager, next"
      :total="totalNum"
      @current-change="handleCurrentChange">
    </el-pagination>
  </div>
</template>

<script> 
import { mapState } from 'vuex' 
export default {
  layout: 'userCenter',
  middleware: ['auth','user'],
  head() {
    return {
      title: '17PDF Reader - 个人中心',
      meta: [
        {
          name: 'keywords',
          content: 'PDFReader,pdfreader,17PDF Reader,pdf软件,PDF阅读器,文件扫描'
        },
        {
          hid: 'description',
          content: '17PDF Reader是行走的PDF阅读器和文件扫描仪，并提供免费的PDF文件格式转换工具，支持pdf转word，pdf转doc，pdf转ppt，pdf转图片等。17PDF Reader被用户誉为“亚洲的Adobe”，拥有自主产权的PDF核心技术，为商务精英、教育族群及企业提供全方位的PDF文件解决方案。'
        },
      ]
    }
  },
  data() {
    return {
      tableData:[],
      tableDataSelection: [],
      totalNum: 0,
      isDownload: true,
      isDelete: true
    }
  },
  computed: {
    ...mapState([
      'userInfo',
    ]),
  },
  created () {
    this.getChangeFileRecord(1)
  },
  methods: {
    // 用户转档记录分页查询
    getChangeFileRecord (page) {
      this.$axios.get(`/missionFile/page?page=${page}`).then((res) => {
        if(res.code === 200) {
          this.tableData = res.result.list
          this.totalNum = res.result.total
          for (let i = 0; i < this.tableData.length; i++) {
            this.tableData[i].size = this.getfilesize(this.tableData[i].size)
            this.tableData[i].status = this.getFileStatus(this.tableData[i].status)
            this.$set(this.tableData[i], 'format', this.getFormat(this.tableData[i].inputType, this.tableData[i].outputType))
          }
        }
      })
    },
    // 计算文件大小函数(保留两位小数),Size为字节大小
    getfilesize (size) {
      if (!size) return ""
      var num = 1024.00 //byte
      if (size < num) {
        return size + "B"
      }
      if (size < Math.pow(num, 2)) {
        return (size / num).toFixed(2) + "K"
      }
      if (size < Math.pow(num, 3)) {
        return (size / Math.pow(num, 2)).toFixed(2) + "M"
      }
      if (size < Math.pow(num, 4)) {
        return (size / Math.pow(num, 3)).toFixed(2) + "G"
      }
      else {
        return (size / Math.pow(num, 4)).toFixed(2) + "T"
      }
    },
    // 判断文件状态
    getFileStatus (status) {
      switch (status) {
        case 0:
          return '转档中'
        case 1:
          return '转档中'
        case 2:
          return '转档成功'
        case 3:
          return '转档失败'
        case 4:
          return '转档成功'
      }
    },
    // 格式转换
    getFormat (input, output) {
      return input.toUpperCase() + '-->' + output.toUpperCase()
    },
    handleSelectionChange(val) {
      this.tableDataSelection = val
      this.isDelete = false
      for (const file of this.tableDataSelection) {
        console.log(file.status)
        if (file.status === '转档中' || file.status === '转档失败') {
          this.isDownload = true
        } else {
          this.isDownload = false
        }
      }
    },
    // 删除转档记录
    deleteChangeFileRecord () {
      var data = new FormData()
      for (const file of this.tableDataSelection) {
        data.append('ids', file.id)
      }
      const config = {
        headers: {
          'Content-Type': 'multipart/form-data;'
        }
      }
      this.$axios.post('/missionFile/delete', data, config).then((res) => {
        if(res.code === 200) {
          this.getChangeFileRecord(1)
          this.isDelete = true
        }
      })
    },
    handleCurrentChange(val) {
      this.getChangeFileRecord(val)
    },
    downloadFiles () {
      for (const file of this.tableDataSelection) {
        if (file.status !== 2 || file.status !== 4) {
          this.isDownload = true
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.expenses-date-tips {
  display: flex;
  align-items: center;
  padding: 12px;
  background: linear-gradient(90deg, #FFFAFA 10.37%, rgba(255, 243, 243, 0) 91.75%), linear-gradient(90deg, #FFF2F6 2.26%, rgba(255, 242, 246, 0.21) 101.41%);
  border: 1px solid rgba(255, 79, 79, 0.2);
  border-radius: 12px;
  .text-container {
    display: flex;
    align-items: center;
    margin-left: 16px;
    img {
      margin-left: 8px;
    }
  }
  span {
    font-size: 14px;
    line-height: 24px;
    vertical-align: middle;
  }
  .text {
    display: inline-flex;
    align-items: center;
  }
  .tip {
    position: relative;
    font-size: 0;
    .tip-text {
      display: none;
      position: absolute;
      left: -3px;
      width: 258px;
      padding: 15px 14px 10px 10px;
      background: url('http://cn-file.17pdf.com/website/members/ic_tip.svg') left top no-repeat;
      background-size: auto 100%;
      font-size: 12px;
      line-height: 18px;
      color: #fff;
      border-radius: 4px;
    }
    &:hover .tip-text {
      display: block;
    }
  }
}
.btn {
  height: 30px;
  line-height: 28px;
  font-size: 12px;
  border-radius: 2px;
  display: inline-block;
  padding: 0px 9px 0px 33px;
  &-download {
    color: #fff;
    background: #FF4F4F url(http://cn-file.17pdf.com/website/members/ic_download.svg) no-repeat 8px center;
    margin-right: 7px;
    &_disabled {
      background-color: #FFA7A7;
    }
  }
  &-delete {
    border: 1px solid #d8d8d9;
    color: #666;
    background: url(http://cn-file.17pdf.com/website/members/ic_delete_normal.png) no-repeat 8px 5px;
    &_disabled {
      color: #ccc;
      border-color: #ccc;
    }
  }
}
.el-table {
  th {
    background-color: #f4f4f4 !important;
    font-size: 16px;
    color: #666666;
    font-weight: normal;
  }
}
.table-empty {
  margin-top: 50px;
  height: 200px;
  background: url(http://cn-file.17pdf.com/website/members/pic_noconsumption.png) no-repeat center center;
}
.el-table__row:hover .downloadBtn {
  display: inline-block;
  width: 13px;
  height: 13px;
  background: url(http://cn-file.17pdf.com/website/members/ic_download_gray.svg) center no-repeat;
}
.btn-download:disabled,.btn-delete:disabled {
  opacity: 0.5;
  pointer-events: none;
}
</style>