<template>
  <div class="app-container">
    <div class="page-header">
      <el-button @click="goBack">
        <el-icon><Back /></el-icon>返回
      </el-button>
      <h2>爬虫日志: {{ project }} / {{ spider }}</h2>
    </div>
    
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>日志列表</span>
          <div class="right-panel">
            <el-button type="primary" @click="refreshLogList">
              <el-icon><Refresh /></el-icon>刷新
            </el-button>
          </div>
        </div>
      </template>
      
      <el-table v-loading="loading" :data="logList" stripe style="width: 100%">
        <el-table-column prop="logfile" label="日志文件" />
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button type="primary" size="small" @click="viewLog(scope.row)">
              查看日志内容
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 日志内容对话框 -->
    <el-dialog v-model="logDialogVisible" :title="currentLog" width="80%" top="5vh">
      <div v-loading="logLoading" class="log-content">
        <pre>{{ logContent }}</pre>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getSpiderLogs, getJobLog } from '@/api/log'
import { Back, Refresh } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const project = ref('')
const spider = ref('')
const loading = ref(false)
const logList = ref([])
const logDialogVisible = ref(false)
const logLoading = ref(false)
const logContent = ref('')
const currentLog = ref('')

// 返回爬虫列表
const goBack = () => {
  router.push({
    path: '/spider',
    query: { project: project.value }
  })
}

// 获取日志列表
const fetchLogList = async () => {
  if (!project.value || !spider.value) return
  
  loading.value = true
  try {
    const response = await getSpiderLogs({ 
      project: project.value,
      spider: spider.value,
      scrapydServerId: localStorage.getItem('scrapydServerId')!
    })
    logList.value = response.data.logs.map((logfile: any) => ({ logfile }))
  } catch (error) {
    console.error('获取日志列表失败:', error)
    ElMessage.error('获取日志列表失败')
  } finally {
    loading.value = false
  }
}

// 刷新日志列表
const refreshLogList = () => {
  fetchLogList()
}

// 查看日志内容
const viewLog = async (row: any) => {
  currentLog.value = row.logfile
  logDialogVisible.value = true
  logContent.value = ''
  logLoading.value = true
  
  try {
    const response = await getJobLog({
      project: project.value,
      spider: spider.value,
      job: row.logfile.replace('.log', ''),
      scrapydServerId: localStorage.getItem('scrapydServerId')!
    })
    logContent.value = response.data.log || '无日志内容'
  } catch (error) {
    console.error('获取日志内容失败:', error)
    ElMessage.error('获取日志内容失败')
    logContent.value = '获取日志内容失败'
  } finally {
    logLoading.value = false
  }
}

onMounted(() => {
  project.value = route.query.project as string
  spider.value = route.query.spider as string
  
  if (!project.value || !spider.value) {
    ElMessage.error('缺少必要参数')
    router.push('/project')
    return
  }
  
  fetchLogList()
})
</script>

<style scoped lang="scss">
.app-container {
  padding: 20px;
}

.page-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  gap: 10px;
  
  h2 {
    margin: 0;
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.right-panel {
  display: flex;
  gap: 10px;
}

.log-content {
  max-height: 70vh;
  overflow-y: auto;
  background: #f5f7fa;
  padding: 16px;
  border-radius: 4px;
  
  pre {
    white-space: pre-wrap;
    font-family: monospace;
    margin: 0;
  }
}

.el-icon {
  margin-right: 5px;
}
</style>
