<template>
  <div class="file-list-container">
    <!-- 空数据提示 -->
    <div v-if="fileList.length === 0 && !loading" class="empty-tip">
      <el-empty description="暂无文件数据"></el-empty>
    </div>
    <!-- 自定义文件列表 -->
    <div v-else class="file-list" v-loading="loading">
      <div 
        v-for="file in fileList" 
        :key="file.ossId" 
        class="file-item"
      >
        <div class="file-info">
          <i class="el-icon-document"></i>
          <span class="file-name">{{ file.originalName }}</span>
        </div>
        <el-button 
          type="text" 
          icon="el-icon-download" 
          @click="downloadFile(file)"
          size="small"
          class="download-btn"
        >
          下载
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { listByIds } from "@/api/system/oss";

export default {
  name: "FileList",
  props: {
    ossIds: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      fileList: [],
      loading: false // 加载状态
    }
  },
  watch: {
    ossIds: {
      handler(val) {
        if (val) {
          this.getFileList();
        } else {
          this.fileList = []; // 清空文件列表
        }
      },
      immediate: true,
    }
  },
  methods: {
    async getFileList() {
      if (!this.ossIds) return;
      
      this.loading = true;
      try {
        let res = await listByIds(this.ossIds);
        this.fileList = res.data || [];
      } catch (error) {
        this.$message.error('获取文件列表失败：' + (error.message || '未知错误'));
        this.fileList = [];
      } finally {
        this.loading = false;
      }
    },
    
    // 文件下载方法
    downloadFile(file) {
      if (!file || !file.ossId) {
        this.$message.warning('文件下载地址不存在');
        return;
      }
      this.$download.oss(file.ossId);
    }
  }
}
</script>

<style scoped>
.file-list-container {
  width: 100%;
  min-height: 100px;
}

.empty-tip {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
}

.file-list {
  width: 100%;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  overflow: hidden;
}

.file-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid #ebeef5;
  transition: background-color 0.3s;
}

.file-item:last-child {
  border-bottom: none;
}

.file-item:hover {
  background-color: #f5f7fa;
}

.file-info {
  display: flex;
  align-items: center;
  flex: 1;
}

.file-info .el-icon-document {
  margin-right: 8px;
  color: #409eff;
  font-size: 16px;
}

.file-name {
  font-size: 14px;
  color: #606266;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.download-btn {
  margin: 0;
  font-size: 14px;
  color: #409eff;
}

.download-btn:hover {
  color: #66b1ff;
}
</style>