<template>
  <view class="coupon-page">
    <scroll-view 
      scroll-y 
      class="scroll-view"
      @scrolltolower="loadMore"
    >
      <view class="coupon-list">
        <view 
          v-for="(item, index) in couponList" 
          :key="index"
          class="coupon-item"
        >
          <view class="item-content">
            <view class="item-title">{{ item.title }}</view>
            <view class="item-time">{{ item.created_at }}</view>
          </view>
          <view class="item-amount">+{{ item.amount }}</view>
        </view>
        
        <!-- 加载更多 -->
        <view class="load-more" v-if="couponList.length > 0">
          <text v-if="loading">加载中...</text>
          <text v-else-if="noMore">没有更多了</text>
        </view>
        
        <!-- 空状态 -->
        <view class="empty-state" v-if="couponList.length === 0 && !loading">
          <text class="icon">🎫</text>
          <text class="text">暂无优惠券记录</text>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      couponList: [],
      page: 1,
      limit: 20,
      loading: false,
      noMore: false
    }
  },
  
  onLoad() {
    this.loadCouponList();
  },
  
  methods: {
    // 加载优惠券列表
    async loadCouponList() {
      if (this.loading || this.noMore) return;
      
      this.loading = true;
      
      try {
        const res = await this.$http.get('/api/money/list', {
          page: this.page,
          limit: this.limit,
          cate: 2 // 2: 优惠券
        });
        
        if (res.code === 0) {
          const list = res.data.list || [];
          
          if (list.length < this.limit) {
            this.noMore = true;
          }
          
          this.couponList = this.page === 1 ? list : [...this.couponList, ...list];
        }
      } catch (error) {
        console.error('加载优惠券失败:', error);
      } finally {
        this.loading = false;
      }
    },
    
    // 加载更多
    loadMore() {
      if (!this.loading && !this.noMore) {
        this.page++;
        this.loadCouponList();
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.coupon-page {
  height: 100vh;
  background-color: #f5f5f5;
}

.scroll-view {
  height: 100%;
}

.coupon-list {
  padding: 20rpx 30rpx;
}

.coupon-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 30rpx;
  background-color: #fff;
  border-radius: 20rpx;
  margin-bottom: 20rpx;
  
  .item-content {
    flex: 1;
    
    .item-title {
      font-size: 28rpx;
      color: #333;
      margin-bottom: 10rpx;
    }
    
    .item-time {
      font-size: 24rpx;
      color: #999;
    }
  }
  
  .item-amount {
    font-size: 32rpx;
    font-weight: bold;
    color: #4CAF50;
  }
}

.load-more {
  padding: 30rpx;
  text-align: center;
  font-size: 26rpx;
  color: #999;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 200rpx 0;
  
  .icon {
    font-size: 120rpx;
    margin-bottom: 20rpx;
  }
  
  .text {
    font-size: 28rpx;
    color: #999;
  }
}
</style>

