<template>
<div>
<div class="nav-bar">
        <div class="back-btn" @click="$router.back()">
            <i class="arrow-left"></i>
        </div>
        <div class="title">积分商城</div>
        </div>
  <div class="jifen-container">
    
    <!-- 头部积分显示 -->
    <div class="header-section">
      <div class="points-display">
        <h2>兑换国家物资</h2>
        <div class="current-points">{{ userPoints }}</div>
        <div class="points-label">我的积分</div>
      </div>
    </div>

    <!-- 商品网格 -->
    <div class="products-grid">
      <div 
        v-for="product in products" 
        :key="product.id"
        class="product-card"
        :class="{ 'large-card': product.isLarge }"
      >
        <div class="product-image">
          <img :src="product.image" :alt="product.name" />
        </div>
        <div class="product-info">
          <div class="product-info-content">
            <div class="product-points">{{ product.probability }}积分</div>
            <div class="product-name">{{ product.name }}</div>
          </div>
          <button 
            class="exchange-btn"
            @click="handleExchange(product)"
          >
            立即兑换
          </button>
        </div>
      </div>
    </div>

    <!-- 兑换确认弹窗 -->
    <div v-if="showConfirmDialog" class="confirm-overlay">
      <div class="confirm-dialog">
        <div class="confirm-content">
          <h3>确认兑换</h3>
          <div class="confirm-product">
            <img :src="selectedProduct.image" :alt="selectedProduct.name" />
            <div class="confirm-info">
              <div class="confirm-name">{{ selectedProduct.name }}</div>
              <div class="confirm-points">需要 {{ selectedProduct.probability }} 积分</div>
            </div>
          </div>
          <p>确认要兑换此商品吗？</p>
          <div class="confirm-buttons">
            <button class="btn-cancel" @click="cancelExchange">取消</button>
            <button class="btn-confirm" @click="confirmExchange">确认兑换</button>
          </div>
        </div>
      </div>
    </div>

    <!-- Toast 提示 -->
    <Toast ref="toast" />
  </div>
  
</div>
</template>

<script>
import Toast from '@/components/Toast.vue';
import { getLotteryPrize, exchangePrize } from '@/api/profile';

export default {
  name: 'JifenPage',
  components: {
    Toast
  },
  data() {
    return {
      showConfirmDialog: false,
      selectedProduct: null,
      products: []
    }
  },
  mounted() {
    this.loadProducts();
  },
  methods: {
   

    // 加载商品列表
    async loadProducts() {
      try {
        const res = await getLotteryPrize();
        this.products = res.data;
        // 当前使用模拟数据，实际开发时替换为API调用
        console.log('商品列表加载完成');
      } catch (error) {
        console.error('加载商品列表失败:', error);
        this.$refs.toast.show('加载商品失败', 'error');
      }
    },

    // 处理兑换点击
    handleExchange(product) { 
      this.selectedProduct = product;
      this.showConfirmDialog = true;
    },

    // 取消兑换
    cancelExchange() {
      this.showConfirmDialog = false;
      this.selectedProduct = null;
    },

    // 确认兑换
    async confirmExchange() {
      try {
       const res = await exchangePrize({
        prize_id: this.selectedProduct.id,
       });
       console.log(res);
        if(res.code == 1){
          this.$refs.toast.show('兑换成功！', 'success');
          this.userPoints -= this.selectedProduct.points;
          this.showConfirmDialog = false;
          this.selectedProduct = null;
        }else{
          this.$refs.toast.show('兑换失败，请重试', 'error');
        }
        
        this.showConfirmDialog = false;
        this.selectedProduct = null;
        
        
      } catch (error) {
        console.error('兑换失败:', error);
        this.$refs.toast.show('兑换失败，请重试', 'error');
      }
    }
  }
}
</script>

<style scoped>
.jifen-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #c94545 0%, #b43a39 100%);
  padding: 20px;
  padding-bottom: 100px;
}

/* 头部积分显示 */
.header-section {
  text-align: center;
  margin-bottom: 30px;
}

.points-display h2 {
  color: white;
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 10px;
}

.current-points {
  color: white;
  font-size: 48px;
  font-weight: bold;
  margin-bottom: 5px;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.points-label {
  color: white;
  font-size: 16px;
  opacity: 0.9;
}

/* 商品网格 */
.products-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 15px;
  margin-bottom: 20px;
}

.product-card {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  background: white;
  border-radius: 12px;
  padding: 15px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  transition: transform 0.2s ease;
}

.product-card:active {
  transform: scale(0.98);
}

.product-card.large-card {
  grid-column: span 2;
}

.product-image {
  width: 100%;
  height: 140px;
  background: #f5f5f5;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 12px;
  overflow: hidden;
}

.large-card .product-image {
  height: 120px;
}

.product-image img {
  width: 80%;
  height: 80%;
  object-fit: contain;
}

.product-info {
  text-align: center;
}
.product-info-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.product-points {
  font-size: 14px;
  color: #000;
  font-weight: 500;
}

.product-name {
  font-size: 14px;
  color: #000;
  font-weight: 500;
}

.exchange-btn {
    margin-top: 10px;
  width: 100%;
  background: #c94545;
  color: white;
  border: none;
  border-radius: 20px;
  padding: 8px 0;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.exchange-btn:hover {
  background: #b43a39;
}

.exchange-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.exchange-btn:active {
  transform: scale(0.95);
}

/* 确认弹窗 */
.confirm-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.confirm-dialog {
  background: white;
  border-radius: 12px;
  width: 90%;
  max-width: 400px;
  overflow: hidden;
}

.confirm-content {
  padding: 25px 20px;
  text-align: center;
}

.confirm-content h3 {
  margin: 0 0 20px 0;
  font-size: 18px;
  color: #333;
}

.confirm-product {
  display: flex;
  align-items: center;
  background: #f8f8f8;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 20px;
}

.confirm-product img {
  width: 60px;
  height: 60px;
  object-fit: contain;
  margin-right: 15px;
  background: white;
  border-radius: 6px;
}

.confirm-info {
  flex: 1;
  text-align: left;
}

.confirm-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 5px;
}

.confirm-points {
  font-size: 14px;
  color: #c94545;
  font-weight: 500;
}

.confirm-content p {
  margin: 0 0 25px 0;
  color: #666;
  font-size: 15px;
}

.confirm-buttons {
  display: flex;
  gap: 12px;
}

.btn-cancel,
.btn-confirm {
  flex: 1;
  padding: 12px 0;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-cancel {
  background: #f5f5f5;
  color: #666;
}

.btn-cancel:hover {
  background: #e8e8e8;
}

.btn-confirm {
  background: #c94545;
  color: white;
}

.btn-confirm:hover {
  background: #b43a39;
}

.btn-cancel:active,
.btn-confirm:active {
  transform: scale(0.98);
}

/* 响应式优化 */
@media screen and (max-width: 375px) {
  .jifen-container {
    padding: 15px;
  }
  
  .products-grid {
    gap: 12px;
  }
  
  .product-card {
    padding: 12px;
  }
  
  .current-points {
    font-size: 42px;
  }
  
  .points-display h2 {
    font-size: 18px;
  }
}

@media screen and (max-width: 320px) {
  .current-points {
    font-size: 36px;
  }
  
  .product-image {
    height: 80px;
  }
  
  .large-card .product-image {
    height: 100px;
  }
}
.nav-bar {
  height: 44px;
  background-color: #fff;
  color: #000;
  display: flex;
  align-items: center;
  position: relative;
  padding: 0 15px;
  font-weight: bold;
}

.back-btn {
  width: 24px;
  height: 44px;
  display: flex;
  align-items: center;
  cursor: pointer;
}

.arrow-left {
  width: 12px;
  height: 12px;
  border-left: 2px solid #000;
  border-bottom: 2px solid #000;
  transform: rotate(45deg);
}

.title {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  font-size: 16px;
}
</style>