<template>
    <div>
        <!-- 历史文件列表 -->
        <el-card class="history-section">
            <h3>历史上传记录</h3>
            <el-table :data="fileHistory" style="width: 100%">
                <el-table-column prop="mission.name" label="分析任务" width="260"/>
                <el-table-column prop="mission.id" label="任务ID" width="260"/>

                <el-table-column label="" width="40">
                    <template #default="scope">
                        <el-button v-if="scope.row.content == 'node'" type="primary"
                            style="width: 20px; height:20px; padding: 2px;">N</el-button>
                        <el-button v-if="scope.row.content == 'edge'" type="warning"
                            style="width: 20px; height:20px; padding: 2px;">E</el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="fileName" label="文件名" width="280" />
                <el-table-column prop="uploadTime" label="上传时间" width="280" />
                <el-table-column prop="fileSize" label="文件大小" width="280">
                </el-table-column>

                <el-table-column label="文件信息" width="280">
                    <template #default="{ row }">
                        <el-dropdown>
                            <span v-if="row.content === 'node'" class="analysis-records">
                                {{ '节点总数：' + row.fileInfo.节点总数 }}<el-icon><arrow-down /></el-icon>
                            </span>
                            <span v-else class="analysis-records">
                                {{ '边总数：' + row.fileInfo.边总数 }}<el-icon><arrow-down /></el-icon>
                            </span>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item v-for="(value, key) in row.fileInfo" :key="key">
                                        {{ key }} - {{ value }}
                                    </el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="120">
                    <template #default="{ row }">
                        <el-button type="danger" size="small" @click="handleDeleteFile(row)" plain>
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
    </div>
</template>

<script setup>
import { ref, computed, onMounted, inject } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Upload, ArrowDown, Plus } from '@element-plus/icons-vue'
import { getData, postData, deleteData, postFile } from '@/api/axios.js'
const fileHistory = ref([])
const handleDeleteFile = (file) => {
  ElMessageBox.confirm(
    `确定要删除文件 ${file.fileName} 吗？此操作不可恢复。`,
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    deleteData('/uploadfile/', { id: file.id }).then(response => {
      if (response.status == 'success') {
        ElMessage.success('文件已删除')
        updateUploadHistory()
      } else {
        ElMessage.error('文件删除失败' + response.message)
      }
    })
      .catch(error => {
        ElMessage.error('文件删除失败');
        console.log(error)
      })
  }).catch(() => { })
}

const updateUploadHistory = () => {
  getData('/uploadfile/')
    .then(response => {
      fileHistory.value = []
      response.data.reverse().forEach(item => {
        fileHistory.value.push({
          id: item.id,
          content: item.content,
          fileName: item.name,
          uploadTime: item.uploadTime.split('.')[0].replace('T', ' '),
          fileSize: item.size,
          // 按照目前逻辑，每个文件的关联mission应该只有一个
          mission: item.missions[0],
          fileInfo: item.fileInfo,
        })
      })
      // history.forEach(element => {
      //   console.log(element.uploadTime.replace('T', ' ').aplit('.')[0])
      // });
      console.log(fileHistory.value)
    })
    .catch(error => {
      ElMessage.error('获取上传历史失败')
      console.log(error)
    })
}
onMounted(() => {
  updateUploadHistory();
})
</script>

<style lang="scss" scoped>
  .history-section {
    h3 {
      margin-bottom: 15px;
      color: var(--el-text-color-primary);
    }

    .analysis-records {
      cursor: pointer;
      color: var(--el-color-primary);
      display: flex;
      align-items: center;
      gap: 5px;
    }
  }
</style>