<template>
  <div class="generate-task-record">
    <!-- 侧边栏 -->
    <div class="sidebar">
      <div class="logo">LOGO</div>
      <nav class="nav-menu">
        <div class="nav-item" @click="goToDashboard">
          <el-icon><Grid /></el-icon>
          <span>数据仪表台</span>
        </div>
        <div class="nav-item" @click="goToMemberManagement">
          <el-icon><User /></el-icon>
          <span>会员管理</span>
        </div>
        <div class="nav-item" @click="goToOrderManagement">
          <el-icon><ShoppingCart /></el-icon>
          <span>订单管理</span>
        </div>
        <div class="nav-item" @click="goToApiManagement">
          <el-icon><Document /></el-icon>
          <span>API管理</span>
        </div>
        <div class="nav-item active">
          <el-icon><Document /></el-icon>
          <span>生成任务记录</span>
        </div>
        <div class="nav-item" @click="goToSystemSettings">
          <el-icon><Setting /></el-icon>
          <span>系统设置</span>
        </div>
      </nav>
      <div class="sidebar-footer">
        <div class="online-users">当前在线用户: 87/500</div>
        <div class="system-uptime">系统运行时间: 48小时32分</div>
      </div>
    </div>

    <!-- 主内容区域 -->
    <div class="main-content">
      <!-- 顶部导航栏 -->
      <div class="header">
        <div class="search-bar">
          <el-icon><Search /></el-icon>
          <input type="text" placeholder="搜索你的想要的内容" class="search-input" />
        </div>
        <div class="header-actions">
          <el-icon class="notification-icon"><Bell /></el-icon>
          <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
          <div class="user-avatar">
            <div class="avatar-placeholder">A</div>
            <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
          </div>
        </div>
      </div>

      <!-- 页面内容 -->
      <div class="content-section">
        <div class="page-header">
          <h1 class="page-title">生成任务记录</h1>
        <div class="action-bar">
          <span class="selected-count">已选择{{ selectedTasks.length }}项</span>
          <div class="action-controls">
            <el-select v-model="statusFilter" placeholder="全部状态" class="status-filter" @change="handleStatusFilter">
              <el-option label="全部状态" value="all"></el-option>
              <el-option label="已完成" value="completed"></el-option>
              <el-option label="处理中" value="processing"></el-option>
              <el-option label="已取消" value="cancelled"></el-option>
            </el-select>
            <el-button 
              type="danger" 
              class="delete-btn"
              :disabled="selectedTasks.length === 0"
              @click="handleBatchDelete"
            >
              <el-icon><Delete /></el-icon>
              删除
            </el-button>
          </div>
        </div>
        </div>

        <!-- 数据表格 -->
        <div class="table-container">
          <el-table 
            :data="filteredTaskRecords" 
            @selection-change="handleSelectionChange"
            class="task-table"
            v-loading="loading"
          >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="taskId" label="任务ID" width="150"></el-table-column>
            <el-table-column prop="username" label="用户名" width="120"></el-table-column>
            <el-table-column prop="type" label="类型" width="150"></el-table-column>
            <el-table-column prop="resources" label="消耗资源" width="120"></el-table-column>
            <el-table-column prop="status" label="状态" width="100">
              <template #default="{ row }">
                <el-tag :type="getStatusType(row.status)" class="status-tag">
                  {{ row.status }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
            <el-table-column label="编辑" width="120">
              <template #default="{ row }">
                <el-link type="primary" class="action-link" @click="handleView(row)">查看</el-link>
                <el-link type="danger" class="action-link" @click="handleDelete(row)">删除</el-link>
              </template>
            </el-table-column>
          </el-table>

          <!-- 分页 -->
          <div class="pagination-container">
            <el-pagination
              v-model:current-page="currentPage"
              :page-size="pageSize"
              :total="total"
              layout="prev, pager, next"
              class="pagination"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  Grid, 
  User, 
  ShoppingCart, 
  Document, 
  Setting, 
  Search, 
  Bell, 
  ArrowDown, 
  Delete 
} from '@element-plus/icons-vue'

const router = useRouter()

// 响应式数据
const statusFilter = ref('all')
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(50)
const selectedTasks = ref([])
const loading = ref(false)

// 任务记录数据
const taskRecords = ref([
  {
    id: 1,
    taskId: 'ORD20240501001',
    username: 'Apple',
    type: '图生视频',
    resources: '5积分',
    status: '已完成',
    createTime: '2025-12-31 10:30'
  },
  {
    id: 2,
    taskId: 'ORD20240501002',
    username: 'Banana',
    type: '图生视频',
    resources: '5积分',
    status: '已取消',
    createTime: '2025-12-31 11:15'
  },
  {
    id: 3,
    taskId: 'ORD20240501003',
    username: 'Cherry',
    type: '参考生图',
    resources: '3积分',
    status: '处理中',
    createTime: '2025-12-31 12:00'
  },
  {
    id: 4,
    taskId: 'ORD20240501004',
    username: 'Date',
    type: '图生视频',
    resources: '5积分',
    status: '已完成',
    createTime: '2025-12-31 13:30'
  },
  {
    id: 5,
    taskId: 'ORD20240501005',
    username: 'Elderberry',
    type: '文本生图',
    resources: '2积分',
    status: '已完成',
    createTime: '2025-12-31 14:45'
  },
  {
    id: 6,
    taskId: 'ORD20240501006',
    username: 'Fig',
    type: '图生视频',
    resources: '5积分',
    status: '处理中',
    createTime: '2025-12-31 15:20'
  },
  {
    id: 7,
    taskId: 'ORD20240501007',
    username: 'Grape',
    type: '参考生图',
    resources: '3积分',
    status: '已取消',
    createTime: '2025-12-31 16:10'
  },
  {
    id: 8,
    taskId: 'ORD20240501008',
    username: 'Honeydew',
    type: '图生视频',
    resources: '5积分',
    status: '已完成',
    createTime: '2025-12-31 17:00'
  }
])

// 计算属性：过滤后的任务记录
const filteredTaskRecords = computed(() => {
  if (statusFilter.value === 'all') {
    return taskRecords.value
  }
  return taskRecords.value.filter(record => {
    switch (statusFilter.value) {
      case 'completed':
        return record.status === '已完成'
      case 'processing':
        return record.status === '处理中'
      case 'cancelled':
        return record.status === '已取消'
      default:
        return true
    }
  })
})

// 方法
const handleSelectionChange = (selection) => {
  selectedTasks.value = selection
}

const getStatusType = (status) => {
  switch (status) {
    case '已完成':
      return 'success'
    case '已取消':
      return 'danger'
    case '处理中':
      return 'primary'
    default:
      return 'info'
  }
}

// 状态筛选
const handleStatusFilter = (value) => {
  statusFilter.value = value
  selectedTasks.value = [] // 清空选择
}

// 查看任务详情
const handleView = (row) => {
  ElMessage.info(`查看任务详情: ${row.taskId}`)
  // 这里可以跳转到详情页面或打开详情弹窗
}

// 删除单个任务
const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm(
      `确定要删除任务 ${row.taskId} 吗？`,
      '确认删除',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
    
    // 从数据中删除
    const index = taskRecords.value.findIndex(item => item.id === row.id)
    if (index > -1) {
      taskRecords.value.splice(index, 1)
      ElMessage.success('任务删除成功')
    }
  } catch (error) {
    // 用户取消删除
  }
}

// 批量删除
const handleBatchDelete = async () => {
  if (selectedTasks.value.length === 0) {
    ElMessage.warning('请先选择要删除的任务')
    return
  }
  
  try {
    await ElMessageBox.confirm(
      `确定要删除选中的 ${selectedTasks.value.length} 个任务吗？`,
      '确认批量删除',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
    
    // 批量删除
    const selectedIds = selectedTasks.value.map(item => item.id)
    taskRecords.value = taskRecords.value.filter(item => !selectedIds.includes(item.id))
    selectedTasks.value = []
    ElMessage.success(`成功删除 ${selectedIds.length} 个任务`)
  } catch (error) {
    // 用户取消删除
  }
}

// 加载数据
const loadTaskRecords = async () => {
  loading.value = true
  try {
    // 这里可以调用API获取数据
    // const response = await taskAPI.getTaskRecords()
    // taskRecords.value = response.data
    
    // 调用真实API
    await new Promise(resolve => setTimeout(resolve, 500))
    ElMessage.success('数据加载完成')
  } catch (error) {
    ElMessage.error('数据加载失败')
  } finally {
    loading.value = false
  }
}

// 导航方法
const goToDashboard = () => {
  router.push('/dashboard')
}

const goToMemberManagement = () => {
  router.push('/member-management')
}

const goToOrderManagement = () => {
  router.push('/orders')
}

const goToApiManagement = () => {
  router.push('/api-management')
}

const goToSystemSettings = () => {
  router.push('/system-settings')
}

// 页面加载时初始化数据
onMounted(() => {
  loadTaskRecords()
})
</script>

<style scoped>
.generate-task-record {
  display: flex;
  height: 100vh;
  background-color: #f5f7fa;
}

/* 左侧导航栏 */
.sidebar {
  width: 320px;
  background: white;
  border-right: 1px solid #e2e8f0;
  display: flex;
  flex-direction: column;
  padding: 24px 0;
}

.logo {
  display: flex;
  align-items: center;
  padding: 0 28px;
  margin-bottom: 32px;
  font-size: 18px;
  font-weight: 600;
  color: #1e293b;
}

.nav-menu {
  flex: 1;
  padding: 0 24px;
}

.nav-item {
  display: flex;
  align-items: center;
  padding: 18px 24px;
  margin-bottom: 6px;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s ease;
  color: #64748b;
  font-size: 16px;
}

.nav-item:hover {
  background: #f1f5f9;
  color: #334155;
}

.nav-item.active {
  background: #eff6ff;
  color: #3b82f6;
}

.nav-item .el-icon {
  margin-right: 16px;
  font-size: 22px;
}

.nav-item span {
  font-size: 16px;
  font-weight: 500;
}

.sidebar-footer {
  padding: 0 32px 20px;
  margin-top: auto;
}

.online-users,
.system-uptime {
  font-size: 14px;
  color: #64748b;
  margin-bottom: 10px;
  line-height: 1.5;
}

/* 主内容区域 */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

/* 顶部导航栏 */
.header {
  height: 60px;
  background: white;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 30px;
}

.search-bar {
  display: flex;
  align-items: center;
  background: #f5f7fa;
  border-radius: 20px;
  padding: 8px 16px;
  width: 300px;
}

.search-bar .el-icon {
  color: #909399;
  margin-right: 8px;
}

.search-input {
  border: none;
  background: transparent;
  outline: none;
  flex-grow: 1;
  font-size: 14px;
  color: #333;
}

.header-actions {
  display: flex;
  align-items: center;
}

.notification-icon {
  font-size: 20px;
  color: #606266;
  margin-right: 20px;
  cursor: pointer;
}

.user-avatar {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.avatar-placeholder {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 14px;
  font-weight: bold;
}

.dropdown-icon {
  font-size: 12px;
  color: #909399;
}

/* 内容区域 */
.content-section {
  flex: 1;
  padding: 30px;
  background-color: #f5f7fa;
}

.page-header {
  margin-bottom: 20px;
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 20px 0;
}

.action-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.selected-count {
  color: #606266;
  font-size: 14px;
}

.action-controls {
  display: flex;
  align-items: center;
  gap: 12px;
}

.status-filter {
  width: 120px;
}

.delete-btn {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* 表格样式 */
.table-container {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.task-table {
  width: 100%;
}

.status-tag {
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
}

.action-link {
  margin-right: 8px;
  font-size: 12px;
}

/* 分页样式 */
.pagination-container {
  display: flex;
  justify-content: center;
  padding: 20px;
  background: white;
}

.pagination {
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    width: 60px;
  }
  
  .nav-item span {
    display: none;
  }
  
  .search-bar {
    width: 200px;
  }
  
  .content-section {
    padding: 15px;
  }
}
</style>
