<template>
  <div class="qylevel-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="80" align="center">
        <template #default="{ row }">
          <el-tag type="primary" size="small">{{ row.level }}</el-tag>
        </template>
      </el-table-column>
      
      <el-table-column prop="title" label="等级名称" min-width="150">
        <template #default="{ row }">
          {{ row.title || '-' }}
        </template>
      </el-table-column>
      
      <el-table-column label="所需欧气值" width="120" align="center">
        <template #default="{ row }">
          <span class="points-value">{{ row.requiredPoints ?? 0 }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="创建时间" width="170" align="center">
        <template #default="{ row }">
          {{ row.createdAt || '-' }}
        </template>
      </el-table-column>
      
      <el-table-column label="更新时间" width="170" align="center">
        <template #default="{ row }">
          {{ row.updatedAt || '-' }}
        </template>
      </el-table-column>
      
      <el-table-column label="操作" width="200" align="center" fixed="right">
        <template #default="{ row }">
          <el-button type="primary" link size="small" @click="handleEdit(row)">
            编辑
          </el-button>
          <el-button type="success" link size="small" @click="handlePrizes(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 type { QyLevelResponse } from '@/api/business/qylevel'

interface Props {
  data: QyLevelResponse[]
  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: QyLevelResponse): void
  (e: 'prizes', row: QyLevelResponse): void
  (e: 'delete', row: QyLevelResponse): 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 handlePageChange = (page: number) => {
  emit('page-change', page)
}

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

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

const handlePrizes = (row: QyLevelResponse) => {
  emit('prizes', row)
}

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

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

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

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