<template>
  <div>
    <ul class="file-list" v-if="currentFiles.length">
      <li class="file-list-item" v-for="file in currentFiles" :key="file.id">
        <a href="javascript:;" :download="file.file_name" @click="downloadFile(file)">
          <i class="el-icon-document"></i>
          <div class="file-list-item__inner" v-html="file.file_name"></div>
        </a>
        <!-- <span v-if="file.file_size">{{ file.file_size }}</span> -->
        <a href="javascript:;" :download="file.file_name" @click="downloadFile(file)">
          <el-tooltip effect="dark" :content="$t('viewerRead.download')">
            <i class="el-icon-download"></i>
          </el-tooltip>
        </a>
      </li>
    </ul>
    <div class="empty" v-else>
      <slot name="empty">{{ $t('viewerRead.courseDataEmptyText') }}</slot>
    </div>
  </div>
</template>

<script>
import { saveAs } from 'file-saver'
export default {
  name: 'FilePanel',
  props: {
    // 标题
    title: {
      type: String,
      default() {
        return this.$t('viewerRead.courseData')
      }
    },
    // 文件列表
    files: { type: Array, default: () => [] }
  },
  computed: {
    currentFiles() {
      return this.files.map(file => {
        file.file_url = file.file_url || file.url
        return file
      })
    }
  },
  methods: {
    // 下载文件
    downloadFile(file) {
      saveAs(file.file_url, file.file_name)
    }
  }
}
</script>

<style lang="scss" scoped>
.file-list {
  padding: 0;
}
.file-list-item {
  display: flex;
  font-size: 16px;
  padding: 20px 30px;
  margin-bottom: 10px;
  background-color: #fff;
  list-style: none;
  border-radius: 32px;
  justify-content: space-between;
  a {
    display: flex;
    align-items: center;
    text-decoration: none;
    color: #333;
    &:hover {
      color: #b49441;
    }
    ::v-deep * {
      margin: 0;
      padding: 0;
    }
  }
}
.empty {
  font-size: 18px;
  line-height: 80px;
  background-color: #fff;
  text-align: center;
  border-radius: 40px;
}
.file-list-item__inner {
  margin: 0 10px !important;
}
</style>
