<template>
  <el-card shadow="never">
    <template #header>
      <div class="header">
        <span>任务详情</span>
        <div>
          <el-input v-model="taskIdInput" placeholder="任务ID" style="width: 140px; margin-right: 8px" />
          <el-button type="primary" @click="load">查询</el-button>
        </div>
      </div>
    </template>

    <el-descriptions v-loading="loading" :column="2" border>
      <el-descriptions-item label="任务ID">{{ detail.id || '-' }}</el-descriptions-item>
      <el-descriptions-item label="任务标题">{{ detail.title || detail.name || '-' }}</el-descriptions-item>
      <el-descriptions-item label="负责人">{{ detail.leaderName || detail.ownerName || '-' }}</el-descriptions-item>
      <el-descriptions-item label="状态">{{ detail.statusLabel || '-' }}</el-descriptions-item>
      <el-descriptions-item label="开始时间">{{ detail.startTime || '-' }}</el-descriptions-item>
      <el-descriptions-item label="结束时间">{{ detail.endTime || '-' }}</el-descriptions-item>
      <el-descriptions-item label="内容" :span="2">{{ detail.content || '-' }}</el-descriptions-item>
    </el-descriptions>

    <el-tabs v-model="activeTab" style="margin-top: 16px">
      <el-tab-pane label="日志记录" name="log">
        <el-timeline>
          <el-timeline-item v-for="(item, idx) in logs" :key="`l-${idx}`" :timestamp="item.createTime || '-'">
            <div>{{ item.content || '-' }}</div>
          </el-timeline-item>
        </el-timeline>
      </el-tab-pane>

      <el-tab-pane label="评论" name="comment">
        <div class="comment-editor">
          <el-input v-model="commentContent" type="textarea" :rows="3" placeholder="输入评论内容" />
          <div style="margin-top: 8px">
            <el-button type="primary" :loading="commentSubmitting" @click="submitComment">发表评论</el-button>
          </div>
        </div>
        <el-empty v-if="!comments.length" description="暂无评论" />
        <el-card v-for="(item, idx) in comments" :key="`c-${idx}`" class="comment-item" shadow="never">
          <div class="comment-meta">
            <span>{{ item.staffName || '匿名' }}</span>
            <span>{{ item.createTime || '-' }}</span>
          </div>
          <div class="comment-content">{{ item.content || '-' }}</div>
        </el-card>
      </el-tab-pane>

      <el-tab-pane label="附件" name="resource">
        <el-table :data="resources" border>
          <el-table-column prop="fileName" label="文件名" min-width="220" />
          <el-table-column prop="fileSize" label="大小" width="120" />
          <el-table-column prop="createTime" label="上传时间" width="180" />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </el-card>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'
import { useRoute } from 'vue-router'
import { taskCommentAdd, taskCommentList, taskGet, taskLogList, taskResourceList } from '@/api/otr/task/core'

const route = useRoute()
const loading = ref(false)
const taskIdInput = ref(String(route.query.id || '1'))
const detail = reactive<Record<string, any>>({})
const activeTab = ref('log')
const logs = ref<any[]>([])
const comments = ref<any[]>([])
const resources = ref<any[]>([])
const commentContent = ref('')
const commentSubmitting = ref(false)

async function load() {
  loading.value = true
  try {
    const id = taskIdInput.value || '1'
    const [res, logRes, commentRes, resourceRes]: any = await Promise.all([
      taskGet(id),
      taskLogList({ taskId: id, page: 1, psize: 20 }),
      taskCommentList({ taskId: id, page: 1, pageSize: 20 }),
      taskResourceList({ taskId: id }),
    ])
    Object.assign(detail, res?.result || {})
    logs.value = logRes?.result?.records || []
    comments.value = commentRes?.result?.records || []
    resources.value = resourceRes?.result?.records || []
  } finally {
    loading.value = false
  }
}

async function submitComment() {
  if (!commentContent.value.trim()) {
    ElMessage.warning('请先输入评论内容')
    return
  }
  commentSubmitting.value = true
  try {
    await taskCommentAdd({
      taskId: taskIdInput.value || '1',
      content: commentContent.value,
    })
    ElMessage.success('评论已提交')
    commentContent.value = ''
    const commentRes: any = await taskCommentList({ taskId: taskIdInput.value || '1', page: 1, pageSize: 20 })
    comments.value = commentRes?.result?.records || []
  } finally {
    commentSubmitting.value = false
  }
}

onMounted(load)
</script>

<style scoped>
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.comment-item {
  margin-top: 10px;
}
.comment-meta {
  display: flex;
  justify-content: space-between;
  color: var(--el-text-color-secondary);
  font-size: 12px;
}
.comment-content {
  margin-top: 6px;
}
</style>
