<template>
  <div class="page-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>月榜中奖记录</span>
        </div>
      </template>

      <!-- 搜索表单 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="用户UID">
          <el-input
            v-model="searchForm.userId"
            placeholder="请输入用户ID"
            clearable
            @keyup.enter="handleSearch"
            style="width: 140px"
          />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input
            v-model="searchForm.mobile"
            placeholder="请输入手机号"
            clearable
            @keyup.enter="handleSearch"
            style="width: 160px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>查询
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 中奖记录表格 -->
      <el-table :data="logList" v-loading="loading" border stripe>
        <el-table-column prop="rank" label="排名" width="80" align="center">
          <template #default="{ row }">
            <el-tag :type="getRankTagType(row.rank)" size="small">
              第{{ row.rank }}名
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column label="用户信息" min-width="180">
          <template #default="{ row }">
            <div class="user-info">
              <el-avatar :src="row.headImg" :size="40" />
              <div class="user-detail">
                <span class="nickname">{{ row.nickname || '-' }}</span>
                <span class="uid">UID: {{ row.uid }}</span>
                <span class="mobile">{{ row.mobile || '-' }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column label="奖品信息" min-width="200">
          <template #default="{ row }">
            <div class="prize-info">
              <el-image
                v-if="row.prizeImgUrl"
                :src="row.prizeImgUrl"
                :preview-src-list="[row.prizeImgUrl]"
                fit="cover"
                class="prize-image"
                preview-teleported
              />
              <span class="prize-title">{{ row.prizeTitle || '-' }}</span>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column label="消费金额" width="120" align="right">
          <template #default="{ row }">
            <span class="money">¥{{ row.consumeAmount?.toFixed(2) || '0.00' }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="统计时间" width="180" align="center">
          <template #default="{ row }">
            {{ row.timeRange || '-' }}
          </template>
        </el-table-column>
        
        <el-table-column label="中奖时间" width="160" align="center">
          <template #default="{ row }">
            {{ row.createdAt || '-' }}
          </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>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Search, Refresh } from '@element-plus/icons-vue'
import {
  getMonthLogs,
  type RankLogListRequest,
  type RankLogResponse
} from '@/api/business/rank'

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

// 搜索表单
const searchForm = reactive({
  userId: '',
  mobile: ''
})

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

// 获取中奖记录列表
const fetchData = async () => {
  loading.value = true
  try {
    const params: RankLogListRequest = {
      page: queryParams.page,
      pageSize: queryParams.pageSize
    }
    
    if (searchForm.userId) {
      params.userId = Number(searchForm.userId)
    }
    if (searchForm.mobile) {
      params.mobile = searchForm.mobile
    }
    
    const res = await getMonthLogs(params)
    logList.value = res.data.list
    total.value = res.data.total
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  queryParams.page = 1
  fetchData()
}

// 重置
const handleReset = () => {
  searchForm.userId = ''
  searchForm.mobile = ''
  queryParams.page = 1
  fetchData()
}

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

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

// 获取排名标签样式
const getRankTagType = (rank: number): '' | 'success' | 'warning' | 'danger' => {
  if (rank === 1) return 'danger'
  if (rank === 2) return 'warning'
  if (rank === 3) return 'success'
  return ''
}

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

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

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

.search-form {
  margin-bottom: 16px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-detail {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.user-detail .nickname {
  font-weight: 500;
}

.user-detail .uid,
.user-detail .mobile {
  font-size: 12px;
  color: #909399;
}

.prize-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

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

.prize-title {
  font-weight: 500;
}

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

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