<template>
  <div class="file-list">
    <div class="file-list__label"> {{ label }}</div>
    <li v-for="(item, index) in fileList" :key="index" @click="handleDownLoad(item)">
      <i class="el-icon-document" />
      <span class="file-list__name">{{ item.fileName }}</span>
      <i class="el-icon-download" />
    </li>
  </div>
</template>
<script>
// @yunjidoc 布局组件 - 文件列表
import { iframeDownload } from '@/utils/index.js'
export default {
  componentName: 'FileList',
  props: {
    /**
     * @prop {String} label 标题，默认'附件报告'
     */
    label: {
      type: String,
      default: '附件报告'
    },
    /**
     * @prop {Array} fileList 文件列表 [{fileName, url}]
     */
    fileList: {
      type: Array,
      default() {
        return []
      }
    },
    /**
     * @prop {Function} download 文件下载事件，不传默认直接使用iframe下载
     */
    download: {
      type: Function
    }
  },
  data() {
    return {
      startDownload: iframeDownload
    }
  },
  methods: {
    handleDownLoad(item) {
      if (typeof this.download === 'function') {
        this.download(item)
      } else {
        this.startDownload(item.url)
      }
    }
  }
}
</script>
<style lang="less" scoped>
@import '~styles/var.less';
.file-list {
  width: 300px;
  &__label {
    color: #9a9a9a;
    margin-bottom: 10px;
  }
  li {
    list-style: none;
    font-size: 12px;
    height: 24px;
    margin-bottom: 5px;
    &:hover {
      cursor: pointer;
      color: @color-primary;
    }
    .file-list__name {
      font-size: 12px;
      display: inline-block;
      width: 250px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      line-height: 1;
    }
    i {
      display: inline-block;
      &.el-icon-document {
        margin-right: 12px;
        font-size: 14px;
      }
      &.el-icon-download {
        color: @color-primary;
        font-size: 18px;
      }
    }
  }
}
</style>
