<template>
  <div class="page-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>月榜奖品管理</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>添加奖品
          </el-button>
        </div>
      </template>

      <!-- 奖品表格 -->
      <el-table :data="prizeList" v-loading="loading" border stripe>
        <el-table-column prop="rank" label="排名" width="80" align="center" />
        
        <el-table-column prop="title" label="奖品名称" min-width="150" />
        
        <el-table-column label="奖品图片" width="100" align="center">
          <template #default="{ row }">
            <el-image
              v-if="row.imgUrl"
              :src="row.imgUrl"
              :preview-src-list="[row.imgUrl]"
              fit="cover"
              class="prize-image"
              preview-teleported
            />
            <span v-else>-</span>
          </template>
        </el-table-column>
        
        <el-table-column label="售价" width="100" align="right">
          <template #default="{ row }">
            <span class="price">¥{{ row.price?.toFixed(2) || '0.00' }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="采购价" width="100" align="right">
          <template #default="{ row }">
            <span class="cost-price">¥{{ row.costPrice?.toFixed(2) || '0.00' }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="奖赏类型" width="120" align="center">
          <template #default="{ row }">
            <el-tag size="small">{{ row.prizeTypeName || '-' }}</el-tag>
          </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="150" 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="queryParams.page"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
        class="pagination"
      />
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <RankPrizeFormDialog
      v-model="formDialogVisible"
      :prize="currentPrize"
      :is-edit="isEdit"
      type="month"
      @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 RankPrizeFormDialog from './components/RankPrizeFormDialog.vue'
import {
  getMonthPrizes,
  deleteMonthPrize,
  type RankPrizeListRequest,
  type RankPrizeResponse
} from '@/api/business/rank'

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

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

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

// 获取奖品列表
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getMonthPrizes(queryParams)
    prizeList.value = res.data.list
    total.value = res.data.total
  } finally {
    loading.value = false
  }
}

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

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

// 新增奖品
const handleAdd = () => {
  isEdit.value = false
  currentPrize.value = null
  formDialogVisible.value = true
}

// 编辑奖品
const handleEdit = (row: RankPrizeResponse) => {
  isEdit.value = true
  currentPrize.value = { ...row }
  formDialogVisible.value = true
}

// 删除奖品
const handleDelete = async (row: RankPrizeResponse) => {
  try {
    await ElMessageBox.confirm(
      `确定要删除排名第 ${row.rank} 的奖品 "${row.title}" 吗？删除后不可恢复！`,
      '删除确认',
      { type: 'warning' }
    )
    await deleteMonthPrize(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;
}

.prize-image {
  width: 50px;
  height: 50px;
  border-radius: 4px;
}

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

.cost-price {
  color: #909399;
}

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