<template>
  <view class="coupons-page">
    <view class="coupon-list">
      <view v-if="coupons.length === 0" class="empty-state">
        <text class="empty-icon">🎫</text>
        <text class="empty-text">暂无优惠券</text>
      </view>
      
      <view v-else v-for="(coupon, index) in coupons" :key="index" class="coupon-item">
        <view class="coupon-left">
          <text class="coupon-amount">{{ coupon.amount }}</text>
          <text class="coupon-type">优惠券</text>
        </view>
        <view class="coupon-right">
          <text class="coupon-title">{{ coupon.title }}</text>
          <text class="coupon-expiry">有效期至: {{ coupon.expiry }}</text>
          <button class="use-btn" @click="useCoupon(coupon)">去使用</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="uts">
import { ref, onMounted } from 'vue'

type Coupon = {
  title: string
  amount: string
  expiry: string
  id: string
}

const coupons = ref<Coupon[]>([])

onMounted(() => {
  loadCoupons()
})

const loadCoupons = () => {
  // 从本地存储获取已领取的优惠券详情
  // 假设存储格式为 JSON 字符串数组
  const storedCoupons = uni.getStorageSync('myCoupons')
  if (storedCoupons) {
    try {
      coupons.value = JSON.parse(storedCoupons as string) as Coupon[]
    } catch (e) {
      console.error('Failed to parse coupons', e)
      coupons.value = []
    }
  } else {
    // 默认空或者是mock一些基础数据如果需要
    coupons.value = []
  }
}

const useCoupon = (coupon: Coupon) => {
  uni.switchTab({
    url: '/pages/mall/consumer/index'
  })
}
</script>

<style>
.coupons-page {
  padding: 15px;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding-top: 100px;
}

.empty-icon {
  font-size: 60px;
  margin-bottom: 20px;
}

.empty-text {
  font-size: 16px;
  color: #999;
}

.coupon-item {
  display: flex;
  background-color: white;
  border-radius: 8px;
  margin-bottom: 15px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}

.coupon-left {
  width: 100px;
  background: linear-gradient(135deg, #FF9800, #FF5722);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: white;
  padding: 15px;
}

.coupon-amount {
  font-size: 24px;
  font-weight: bold;
}

.coupon-type {
  font-size: 12px;
  margin-top: 5px;
}

.coupon-right {
  flex: 1;
  padding: 15px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.coupon-title {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
}

.coupon-expiry {
  font-size: 12px;
  color: #999;
  margin-bottom: 10px;
}

.use-btn {
  align-self: flex-end;
  font-size: 12px;
  background-color: #FF5722;
  color: white;
  padding: 4px 12px;
  border-radius: 15px;
  line-height: 1.5;
}
</style>