<template>
  <div class="reward-table">
    <el-table :data="data" v-loading="loading" border stripe>
      <el-table-column prop="id" label="ID" width="70" align="center" />
      
      <el-table-column prop="rewardId" label="奖励ID" width="150" align="center">
        <template #default="{ row }">
          {{ row.rewardId || '-' }}
        </template>
      </el-table-column>
      
      <el-table-column label="奖励类型" width="100" align="center">
        <template #default="{ row }">
          <el-tag :type="getRewardTypeTagType(row.rewardType)" size="small">
            {{ row.rewardTypeName || RewardTypeLabels[row.rewardType] || '未知' }}
          </el-tag>
        </template>
      </el-table-column>
      
      <el-table-column label="奖励值" width="120" align="center">
        <template #default="{ row }">
          <template v-if="row.rewardType === RewardType.Coupon">
            <span v-if="row.coupon" class="coupon-info">
              {{ row.coupon.title }}
            </span>
            <span v-else class="no-data">优惠券ID: {{ row.rewardExtend }}</span>
          </template>
          <template v-else>
            {{ row.rewardValue }}
          </template>
        </template>
      </el-table-column>
      
      <el-table-column label="关联ID" width="100" align="center">
        <template #default="{ row }">
          {{ row.rewardExtend || '-' }}
        </template>
      </el-table-column>
      
      <el-table-column prop="description" label="描述" min-width="150">
        <template #default="{ row }">
          {{ row.description || '-' }}
        </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 {
  RewardType,
  RewardTypeLabels,
  type RewardResponse
} from '@/api/business/reward'

interface Props {
  data: RewardResponse[]
  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: RewardResponse): void
  (e: 'delete', row: RewardResponse): 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 getRewardTypeTagType = (type: number): string => {
  switch (type) {
    case RewardType.Diamond:
      return 'primary'
    case RewardType.UUCoin:
      return 'success'
    case RewardType.DaDaCoin:
      return 'warning'
    case RewardType.Coupon:
      return 'danger'
    default:
      return 'info'
  }
}

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

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

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

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

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

.coupon-info {
  color: #e6a23c;
  font-size: 12px;
}

.no-data {
  color: #909399;
  font-size: 12px;
}

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