<template>
  <view class="exchange-page">
    <view class="points-balance">
      <text class="label">可用积分</text>
      <text class="value">{{ pointsBalance }}</text>
    </view>
    
    <view class="exchange-list">
      <view class="section-title">兑换商品</view>
      
      <view v-for="item in exchangeItems" :key="item.id" class="exchange-item">
        <image :src="item.image" class="item-image"></image>
        <view class="item-info">
          <text class="item-name">{{ item.name }}</text>
          <text class="item-desc">{{ item.description }}</text>
          <view class="item-footer">
            <text class="item-points">{{ item.points }}积分</text>
            <button class="btn-exchange" @click="handleExchange(item)">兑换</button>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      pointsBalance: 0,
      exchangeItems: [
        {
          id: 1,
          name: '10元优惠券',
          description: '满100元可用',
          points: 1000,
          image: '/static/images/coupon.png'
        },
        {
          id: 2,
          name: '20元优惠券',
          description: '满200元可用',
          points: 2000,
          image: '/static/images/coupon.png'
        }
      ]
    }
  },
  
  onLoad() {
    this.loadPointsBalance()
  },
  
  methods: {
    async loadPointsBalance() {
      try {
        const res = await this.$http.get('/api/points/balance')
        if (res.code === 200) {
          this.pointsBalance = res.data
        }
      } catch (e) {
        console.error('加载积分失败', e)
      }
    },
    
    async handleExchange(item) {
      if (this.pointsBalance < item.points) {
        uni.showToast({
          title: '积分不足',
          icon: 'none'
        })
        return
      }
      
      try {
        const res = await this.$http.post('/api/points/exchange', {
          points: item.points,
          exchangeType: 'coupon',
          targetId: item.id
        })
        
        if (res.code === 200) {
          uni.showToast({
            title: '兑换成功',
            icon: 'success'
          })
          this.loadPointsBalance()
        }
      } catch (e) {
        console.error('兑换失败', e)
      }
    }
  }
}
</script>

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

.points-balance {
  background: linear-gradient(135deg, #ff9500 0%, #ff6b00 100%);
  padding: 60rpx 40rpx;
  text-align: center;
  color: #fff;
  
  .label {
    display: block;
    font-size: 28rpx;
    margin-bottom: 16rpx;
  }
  
  .value {
    display: block;
    font-size: 72rpx;
    font-weight: bold;
  }
}

.exchange-list {
  padding: 20rpx 30rpx;
  
  .section-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333;
    margin-bottom: 20rpx;
  }
}

.exchange-item {
  background: #fff;
  border-radius: 16rpx;
  padding: 20rpx;
  margin-bottom: 20rpx;
  display: flex;
  
  .item-image {
    width: 160rpx;
    height: 160rpx;
    border-radius: 8rpx;
    margin-right: 20rpx;
  }
  
  .item-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    
    .item-name {
      font-size: 30rpx;
      font-weight: bold;
      color: #333;
    }
    
    .item-desc {
      font-size: 24rpx;
      color: #999;
    }
    
    .item-footer {
      display: flex;
      justify-content: space-between;
      align-items: center;
      
      .item-points {
        font-size: 28rpx;
        color: #ff9500;
        font-weight: bold;
      }
      
      .btn-exchange {
        width: 140rpx;
        height: 60rpx;
        line-height: 60rpx;
        background: #4a9b9f;
        color: #fff;
        border-radius: 30rpx;
        font-size: 26rpx;
        padding: 0;
      }
    }
  }
}
</style>
