<template>
  <view>
    <view v-if="visible" class="popup-mask" @tap="handleClose">
      <view class="popup-content" @tap.stop>
        <view class="popup-header">
          <text class="title">使用次数<text class="title-sub">(次数需使用完，剩余次数不可退)</text></text>
          <view class="close-btn" @tap="handleClose">×</view>
        </view>
        
        <scroll-view scroll-y class="packages-list">
          <view v-if="loading" class="loading-state">
            <text>加载中...</text>
          </view>
          <view v-else-if="!packages.length" class="empty-state">
            <text>暂无优惠套餐</text>
          </view>
          <view 
            v-else
            v-for="(pkg, index) in packages" 
            :key="pkg.id"
            class="package-item"
            :class="{ 'best-value': pkg.is_best_value }"
            @tap="handlePurchase(pkg)"
          >
             <view class="pkg-tag" v-if="pkg.tag">{{ pkg.tag }}</view>
             <view class="pkg-left">
               <view class="pkg-name">{{ pkg.name }}</view>
               <view class="pkg-count">含 {{ pkg.pass_count }} 次游戏</view>
               <view class="pkg-validity" v-if="pkg.valid_days > 0">有效期 {{ pkg.valid_days }} 天</view>
               <view class="pkg-validity" v-else>永久有效</view>
             </view>
             <view class="pkg-right">
               <view class="pkg-price-row">
                 <text class="currency">¥</text>
                 <text class="price">{{ (pkg.price / 100).toFixed(2) }}</text>
               </view>
               <view class="pkg-original-price" v-if="pkg.original_price > pkg.price">
                 ¥{{ (pkg.original_price / 100).toFixed(2) }}
               </view>
               
               <view class="action-row">
                 <view class="stepper" @tap.stop>
                   <text class="step-btn minus" @tap="updateCount(pkg.id, -1)">-</text>
                   <input
                     class="step-input"
                     type="number"
                     :value="counts[pkg.id] || 1"
                     @input="onInputCount(pkg.id, $event)"
                     @blur="onBlurCount(pkg.id)"
                   />
                   <text class="step-btn plus" @tap="updateCount(pkg.id, 1)">+</text>
                 </view>
                 <button class="btn-buy" :loading="purchasingId === pkg.id" @tap.stop="handlePurchase(pkg)">
                   购买
                 </button>
               </view>
             </view>
          </view>
        </scroll-view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, watch } from 'vue'
import { getGamePassPackages, purchaseGamePass, createWechatOrder } from '@/api/appUser'

const props = defineProps({
  visible: { type: Boolean, default: false },
  activityId: { type: [String, Number], default: '' }
})

const emit = defineEmits(['update:visible', 'success'])

const loading = ref(false)
const packages = ref([])
const purchasingId = ref(null)
const counts = ref({})

function updateCount(pkgId, delta) {
  const current = counts.value[pkgId] || 1
  const newVal = current + delta
  if (newVal >= 1 && newVal <= 200) {
    counts.value[pkgId] = newVal
  }
}

function onInputCount(pkgId, e) {
  const val = parseInt(e.detail.value) || 1
  // 允许输入过程中的临时值（如空字符串），但限制范围
  if (val >= 1 && val <= 200) {
    counts.value[pkgId] = val
  } else if (val < 1) {
    counts.value[pkgId] = 1
  } else if (val > 200) {
    counts.value[pkgId] = 200
  }
}

function onBlurCount(pkgId) {
  // 失去焦点时确保值在有效范围内
  const current = counts.value[pkgId] || 1
  if (current < 1) {
    counts.value[pkgId] = 1
  } else if (current > 200) {
    counts.value[pkgId] = 200
  }
}

watch(() => props.visible, (val) => {
  if (val) {
    fetchPackages()
  }
})

async function fetchPackages() {
  loading.value = true
  try {
    const res = await getGamePassPackages(props.activityId)
    // res 应该是数组
    let list = []
    if (Array.isArray(res)) list = res
    else if (res && Array.isArray(res.packages)) list = res.packages
    else if (res && Array.isArray(res.list)) list = res.list
    else if (res && Array.isArray(res.data)) list = res.data

    // 初始化counts
    const countMap = {}
    list.forEach(p => countMap[p.id] = 1)
    counts.value = countMap
    
    // 简单处理：给第一个或最优惠的打标签
    // 这里随机模拟一下 "热销" 或计算折扣力度
    packages.value = list.map(p => {
      let tag = ''
      const discount = 1 - (p.price / p.original_price)
      if (p.original_price > 0 && discount >= 0.2) {
        tag = `省${Math.floor(discount * 100)}%`
      }
      return { ...p, tag }
    })
  } catch (e) {
    console.error(e)
    packages.value = []
  } finally {
    loading.value = false
  }
}

async function handlePurchase(pkg) {
  if (purchasingId.value) return
  purchasingId.value = pkg.id
  
  try {
    uni.showLoading({ title: '创建订单...' })
    
    // 1. 调用购买接口 (后端创建订单 + 预下单)
    // 注意：根据后端实现，purchaseGamePass 可能直接返回支付参数，或者需要我们自己调 createWechatOrder
    // 之前分析 game_passes_app.go，它似乎返回的是 simple success? 
    // 让我们再确认一下 game_passes_app.go 的 PurchaseGamePassPackage
    // 既然我看不到代码，按常规逻辑：
    // 如果返回 order_no，则需要 createWechatOrder
    // 如果返回 pay_params，则直接支付
    
    // 假设 API 返回 { order_no, ... }
    const count = counts.value[pkg.id] || 1
    const res = await purchaseGamePass(pkg.id, count)
    const orderNo = res.order_no || res.orderNo
    if (!orderNo) throw new Error('下单失败')
    
    // 2. 拉起支付
    const openid = uni.getStorageSync('openid')
    const payRes = await createWechatOrder({ openid, order_no: orderNo })
    
    await new Promise((resolve, reject) => {
      uni.requestPayment({
        provider: 'wxpay',
        timeStamp: payRes.timeStamp || payRes.timestamp,
        nonceStr: payRes.nonceStr || payRes.noncestr,
        package: payRes.package,
        signType: payRes.signType || 'RSA',
        paySign: payRes.paySign,
        success: resolve,
        fail: reject
      })
    })
    
    uni.showToast({ title: '购买成功', icon: 'success' })
    emit('success')
    handleClose()
    
  } catch (e) {
    if (e?.errMsg && e.errMsg.includes('cancel')) {
      uni.showToast({ title: '取消支付', icon: 'none' })
    } else {
      uni.showToast({ title: e.message || '购买失败', icon: 'none' })
    }
  } finally {
    uni.hideLoading()
    purchasingId.value = null
  }
}

function handleClose() {
  emit('update:visible', false)
}
</script>

<style lang="scss" scoped>
.popup-mask {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 999;
  display: flex;
  align-items: flex-end;
}

.popup-content {
  width: 100%;
  background: #FFFFFF;
  border-radius: 32rpx 32rpx 0 0;
  padding-bottom: env(safe-area-inset-bottom);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.popup-header {
  padding: 32rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1rpx solid #F3F4F6;
  
  .title {
    font-size: 36rpx;
    font-weight: bold;
    color: #1F2937;
  }

  .title-sub {
    font-size: 34rpx;
    font-weight: normal;
  }

  .close-btn {
    font-size: 48rpx;
    color: #9CA3AF;
    line-height: 0.8;
    padding: 10rpx;
  }
}

.packages-list {
  padding: 32rpx;
  max-height: 60vh;
}

.loading-state, .empty-state {
  text-align: center;
  padding: 60rpx 0;
  color: #9CA3AF;
  font-size: 28rpx;
}

.package-item {
  position: relative;
  background: linear-gradient(135deg, #FFFFFF, #F9FAFB);
  border: 2rpx solid #E5E7EB;
  border-radius: 24rpx;
  padding: 24rpx 32rpx;
  margin-bottom: 24rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: all 0.2s;
  overflow: hidden;
  
  &:active {
    transform: scale(0.98);
    background: #F3F4F6;
  }
}

.pkg-tag {
  position: absolute;
  top: 0;
  left: 0;
  background: #FF6B00;
  color: #FFF;
  font-size: 20rpx;
  padding: 4rpx 12rpx;
  border-bottom-right-radius: 12rpx;
  font-weight: bold;
}

.pkg-left {
  flex: 1;
}

.pkg-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #1F2937;
  margin-bottom: 8rpx;
}

.pkg-count {
  font-size: 26rpx;
  color: #4B5563;
  margin-bottom: 4rpx;
}

.pkg-validity {
  font-size: 22rpx;
  color: #9CA3AF;
}

.pkg-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.pkg-price-row {
  color: #FF6B00;
  font-weight: bold;
  margin-bottom: 4rpx;
  
  .currency {
    font-size: 24rpx;
  }
  .price {
    font-size: 40rpx;
  }
}

.pkg-original-price {
  font-size: 22rpx;
  color: #9CA3AF;
  text-decoration: line-through;
  margin-bottom: 12rpx;
}

.btn-buy {
  background: linear-gradient(90deg, #FF6B00, #FF9F43);
  color: #FFF;
  font-size: 24rpx;
  padding: 0 20rpx;
  height: 52rpx;
  line-height: 52rpx;
  border-radius: 26rpx;
  border: none;
  font-weight: 600;
  
  &[loading] {
    opacity: 0.8;
  }
}

.action-row {
  display: flex;
  align-items: center;
  gap: 12rpx;
  margin-top: 8rpx;
}

.stepper {
  display: flex;
  align-items: center;
  background: #F3F4F6;
  border-radius: 12rpx;
  padding: 2rpx;

  .step-btn {
    width: 44rpx;
    height: 44rpx;
    line-height: 40rpx;
    text-align: center;
    font-size: 32rpx;
    color: #4B5563;
    font-weight: 300;
    flex-shrink: 0;
  }

  .minus {
    color: #9CA3AF;
  }

  .step-input {
    width: 60rpx;
    height: 44rpx;
    line-height: 44rpx;
    text-align: center;
    font-size: 26rpx;
    font-weight: bold;
    color: #1F2937;
    background: transparent;
    border: none;
    padding: 0;
    margin: 0;

    &::placeholder {
      color: #9CA3AF;
    }
  }
}
</style>
