<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="用户ID">
          <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.title"
            placeholder="请输入优惠券标题"
            clearable
            @keyup.enter="handleSearch"
            style="width: 180px"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="searchForm.status"
            placeholder="全部状态"
            clearable
            style="width: 120px"
          >
            <el-option
              v-for="(label, value) in CouponReceiveStatusLabels"
              :key="value"
              :label="label"
              :value="Number(value)"
            />
          </el-select>
        </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="receiveList" v-loading="loading" border stripe>
        <el-table-column prop="id" label="ID" width="80" align="center" />
        
        <el-table-column label="用户信息" min-width="150">
          <template #default="{ row }">
            <div class="user-info">
              <span class="uid">用户ID: {{ row.userId }}</span>
              <span class="nickname">{{ row.nickname || '-' }}</span>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column prop="couponTitle" label="优惠券名称" min-width="150" />
        
        <el-table-column label="满减门槛" width="100" align="right">
          <template #default="{ row }">
            <span class="money">¥{{ row.minPrice?.toFixed(2) || '0.00' }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="优惠金额" width="100" align="right">
          <template #default="{ row }">
            <span class="discount">¥{{ row.discountPrice?.toFixed(2) || '0.00' }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="状态" width="100" align="center">
          <template #default="{ row }">
            <el-tag :type="getStatusTagType(row.status)" size="small">
              {{ row.statusName || getStatusName(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        
        <el-table-column label="过期时间" width="160" align="center">
          <template #default="{ row }">
            {{ row.expireTime || '-' }}
          </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 {
  getCouponReceives,
  CouponReceiveStatus,
  CouponReceiveStatusLabels,
  type CouponReceiveListRequest,
  type CouponReceiveResponse
} from '@/api/business/coupon'

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

// 搜索表单
const searchForm = reactive({
  userId: '',
  title: '',
  status: undefined as number | undefined
})

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

// 获取领取记录列表
const fetchData = async () => {
  loading.value = true
  try {
    const params: CouponReceiveListRequest = {
      page: queryParams.page,
      pageSize: queryParams.pageSize
    }
    
    if (searchForm.userId) {
      params.userId = Number(searchForm.userId)
    }
    if (searchForm.title) {
      params.title = searchForm.title
    }
    if (searchForm.status !== undefined) {
      params.status = searchForm.status
    }
    
    const res = await getCouponReceives(params)
    receiveList.value = res.data.list
    total.value = res.data.total
  } finally {
    loading.value = false
  }
}

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

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

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

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

// 获取状态名称
const getStatusName = (status: number): string => {
  return CouponReceiveStatusLabels[status] || '未知'
}

// 获取状态标签样式
const getStatusTagType = (status: number): '' | 'success' | 'warning' | 'info' | 'danger' => {
  switch (status) {
    case CouponReceiveStatus.Unused:
      return 'success'
    case CouponReceiveStatus.Used:
      return 'info'
    case CouponReceiveStatus.Expired:
      return 'danger'
    default:
      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;
  flex-direction: column;
  gap: 4px;
}

.user-info .uid {
  font-size: 12px;
  color: #909399;
}

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

.money {
  color: #909399;
}

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

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