<template>
  <div class="task-table">
    <el-table :data="data" v-loading="loading" border stripe>
      <el-table-column prop="id" label="ID" width="70" align="center" />
      
      <el-table-column label="任务分类" width="100" align="center">
        <template #default="{ row }">
          <el-tag :type="getTaskTypeTagType(row.type)" size="small">
            {{ row.typeName || TaskTypeLabels[row.type] || '未知' }}
          </el-tag>
        </template>
      </el-table-column>
      
      <el-table-column prop="title" label="任务名称" min-width="180">
        <template #default="{ row }">
          {{ row.title || '-' }}
        </template>
      </el-table-column>
      
      <el-table-column label="任务次数" width="100" align="center">
        <template #default="{ row }">
          {{ row.number ?? '-' }}
        </template>
      </el-table-column>
      
      <el-table-column label="欧气值" width="100" align="center">
        <template #default="{ row }">
          <span class="reward-value">{{ row.zNumber ?? '-' }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="排序" width="80" align="center">
        <template #default="{ row }">
          {{ row.sort ?? 0 }}
        </template>
      </el-table-column>
      
      <el-table-column label="创建时间" width="160" align="center">
        <template #default="{ row }">
          {{ row.createdAt || '-' }}
        </template>
      </el-table-column>
      
      <el-table-column label="操作" width="130" align="center" fixed="right">
        <template #default="{ row }">
          <el-button type="primary" link size="small" @click="handleEdit(row)">
            编辑
          </el-button>
          <el-button type="danger" link size="small" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="currentPageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handlePageChange"
      class="pagination"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import {
  TaskType,
  TaskTypeLabels,
  type TaskResponse
} from '@/api/business/task'

interface Props {
  data: TaskResponse[]
  loading: boolean
  total: number
  page: number
  pageSize: number
}

const props = defineProps<Props>()

const emit = defineEmits<{
  (e: 'page-change', page: number): void
  (e: 'size-change', size: number): void
  (e: 'edit', row: TaskResponse): void
  (e: 'delete', row: TaskResponse): void
}>()

const currentPage = ref(props.page)
const currentPageSize = ref(props.pageSize)

watch(() => props.page, (val) => {
  currentPage.value = val
})

watch(() => props.pageSize, (val) => {
  currentPageSize.value = val
})

// 获取任务类型标签样式
const getTaskTypeTagType = (type: number): string => {
  switch (type) {
    case TaskType.Daily:
      return 'primary'
    case TaskType.Weekly:
      return 'success'
    default:
      return 'info'
  }
}

const handlePageChange = (page: number) => {
  emit('page-change', page)
}

const handleSizeChange = (size: number) => {
  emit('size-change', size)
}

const handleEdit = (row: TaskResponse) => {
  emit('edit', row)
}

const handleDelete = (row: TaskResponse) => {
  emit('delete', row)
}
</script>

<style scoped>
.task-table {
  width: 100%;
}

.reward-value {
  color: #e6a23c;
  font-weight: 500;
}

.pagination {
  margin-top: 16px;
  justify-content: flex-end;
}
</style>
