<template>
  <div class="page-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>任务管理</span>
          <div class="header-actions">
            <el-button type="primary" @click="handleAdd">
              <el-icon><Plus /></el-icon>新增任务
            </el-button>
          </div>
        </div>
      </template>

      <!-- 搜索表单 -->
      <TaskSearchForm
        @search="handleSearch"
        @reset="handleReset"
      />

      <!-- 任务表格 -->
      <TaskTable
        :data="taskList"
        :loading="loading"
        :total="total"
        :page="queryParams.page"
        :page-size="queryParams.pageSize"
        @page-change="handlePageChange"
        @size-change="handleSizeChange"
        @edit="handleEdit"
        @delete="handleDelete"
      />
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <TaskFormDialog
      v-model="formDialogVisible"
      :task="currentTask"
      :is-edit="isEdit"
      @success="fetchData"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import TaskSearchForm from './components/TaskSearchForm.vue'
import TaskTable from './components/TaskTable.vue'
import TaskFormDialog from './components/TaskFormDialog.vue'
import {
  getTasks,
  deleteTask,
  type TaskListRequest,
  type TaskResponse
} from '@/api/business/task'

// 列表数据
const loading = ref(false)
const taskList = ref<TaskResponse[]>([])
const total = ref(0)

// 查询参数
const queryParams = reactive<TaskListRequest>({
  page: 1,
  pageSize: 20,
  keyword: undefined,
  type: undefined
})

// 弹窗控制
const formDialogVisible = ref(false)
const isEdit = ref(false)
const currentTask = ref<TaskResponse | null>(null)

// 获取任务列表
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getTasks(queryParams)
    taskList.value = res.data.list
    total.value = res.data.total
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = (params: { keyword?: string; type?: number }) => {
  queryParams.keyword = params.keyword
  queryParams.type = params.type
  queryParams.page = 1
  fetchData()
}

// 重置搜索
const handleReset = () => {
  queryParams.keyword = undefined
  queryParams.type = undefined
  queryParams.page = 1
  fetchData()
}

// 分页
const handlePageChange = (page: number) => {
  queryParams.page = page
  fetchData()
}

const handleSizeChange = (size: number) => {
  queryParams.pageSize = size
  queryParams.page = 1
  fetchData()
}

// 新增任务
const handleAdd = () => {
  isEdit.value = false
  currentTask.value = null
  formDialogVisible.value = true
}

// 编辑任务
const handleEdit = (row: TaskResponse) => {
  isEdit.value = true
  currentTask.value = { ...row }
  formDialogVisible.value = true
}

// 删除任务
const handleDelete = async (row: TaskResponse) => {
  try {
    await ElMessageBox.confirm(
      `确定要删除任务"${row.title}"吗？删除后不可恢复！`,
      '删除确认',
      { type: 'warning' }
    )
    await deleteTask(row.id)
    ElMessage.success('删除成功')
    fetchData()
  } catch {
    // 取消操作
  }
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.page-container {
  padding: 0;
}

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

.header-actions {
  display: flex;
  gap: 12px;
}
</style>
