<template>
  <view class="page-wrapper">
    <!-- Rebuild Trigger -->
    <!-- 背景层 -->
    <image class="bg-fixed" :src="detail.banner || ''" mode="aspectFill" />
    <view class="bg-mask"></view>

    <view class="content-area">
      <!-- 顶部信息 -->
      <view class="header-section">
        <view class="title-box">
          <text class="main-title">{{ detail.name || detail.title || '爬塔挑战' }}</text>
          <text class="sub-title">层层突围 赢取大奖</text>
        </view>
        <view class="rule-btn" @tap="showRules">规则</view>
      </view>

      <!-- 挑战区域 (模拟塔层) -->
      <view class="tower-container">
        <view class="tower-level current">
          <view class="level-info">
            <text class="level-num">当前挑战</text>
            <text class="level-name">{{ currentIssueTitle || '第1层' }}</text>
          </view>
          <view class="level-status">进行中</view>
        </view>
        
        <!-- 奖池预览 -->
        <view class="rewards-preview" v-if="currentIssueRewards.length">
          <scroll-view scroll-x class="rewards-scroll">
            <view class="reward-item" v-for="(r, idx) in currentIssueRewards" :key="idx">
              <image class="reward-img" :src="r.image" mode="aspectFill" />
              <view class="reward-name">{{ r.title }}</view>
              <view class="reward-prob" v-if="r.percent">概率 {{ r.percent }}%</view>
            </view>
          </scroll-view>
        </view>
      </view>

      <!-- 操作区 -->
      <view class="action-area">
        <view class="price-display">
          <text class="currency">¥</text>
          <text class="amount">{{ (Number(detail.price_draw || 0) / 100).toFixed(2) }}</text>
          <text class="unit">/次</text>
        </view>
        <button class="challenge-btn" :loading="drawLoading" @tap="onStartChallenge">
          立即挑战
        </button>
      </view>
    </view>

    <!-- 结果弹窗 -->
    <view v-if="showFlip" class="flip-overlay" @touchmove.stop.prevent>
      <view class="flip-mask" @tap="closeFlip"></view>
      <view class="flip-content">
        <FlipGrid ref="flipRef" :rewards="winItems" :controls="false" />
        <button class="close-btn" @tap="closeFlip">收下奖励</button>
      </view>
    </view>
    
    <PaymentPopup
      v-model:visible="paymentVisible"
      :amount="paymentAmount"
      :coupons="coupons"
      :propCards="propCards"
      @confirm="onPaymentConfirm"
    />
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import FlipGrid from '../../../components/FlipGrid.vue'
import PaymentPopup from '../../../components/PaymentPopup.vue'
import { getActivityDetail, getActivityIssues, getActivityIssueRewards, joinLottery, createWechatOrder, getLotteryResult, getItemCards, getUserCoupons } from '../../../api/appUser'

const activityId = ref('')
const detail = ref({})
const issues = ref([])
const currentIssueId = ref('')
const rewardsMap = ref({})
const drawLoading = ref(false)
const showFlip = ref(false)
const winItems = ref([])
const flipRef = ref(null)

// Payment
const paymentVisible = ref(false)
const paymentAmount = ref('0.00')
const coupons = ref([])
const propCards = ref([])
const selectedCoupon = ref(null)
const selectedCard = ref(null)
const pendingCount = ref(1)

const currentIssueTitle = computed(() => {
  const i = issues.value.find(x => x.id === currentIssueId.value)
  return i ? (i.title || `第${i.no}期`) : ''
})

const currentIssueRewards = computed(() => {
  return (currentIssueId.value && rewardsMap.value[currentIssueId.value]) || []
})

const priceVal = computed(() => Number(detail.value.price_draw || 0) / 100)

async function loadData(id) {
  try {
    const d = await getActivityDetail(id)
    detail.value = d || {}
    
    const is = await getActivityIssues(id)
    issues.value = normalizeIssues(is)
    
    if (issues.value.length) {
      const first = issues.value[0]
      currentIssueId.value = first.id
      loadRewards(id, first.id)
    }
  } catch (e) {
    console.error(e)
  }
}

async function loadRewards(aid, iid) {
  try {
    const res = await getActivityIssueRewards(aid, iid)
    rewardsMap.value[iid] = normalizeRewards(res)
  } catch (e) {}
}

function onStartChallenge() {
  const token = uni.getStorageSync('token')
  const phoneBound = !!uni.getStorageSync('phone_bound')
  if (!token || !phoneBound) {
    uni.showToast({ title: '请先登录', icon: 'none' })
    // In real app, redirect to login
    return
  }
  
  if (!currentIssueId.value) {
    uni.showToast({ title: '暂无挑战场次', icon: 'none' })
    return
  }

  paymentAmount.value = priceVal.value.toFixed(2)
  pendingCount.value = 1
  paymentVisible.value = true
  // Fetch coupons/cards in background
  fetchPropCards()
  fetchCoupons()
}

async function onPaymentConfirm(data) {
  selectedCoupon.value = data?.coupon || null
  selectedCard.value = data?.card || null
  paymentVisible.value = false
  await doDraw()
}

async function doDraw() {
  drawLoading.value = true
  try {
    const openid = uni.getStorageSync('openid')
    const joinRes = await joinLottery({
      activity_id: Number(activityId.value),
      issue_id: Number(currentIssueId.value),
      channel: 'miniapp',
      count: 1,
      coupon_id: selectedCoupon.value?.id ? Number(selectedCoupon.value.id) : 0
    })
    
    if (!joinRes) throw new Error('下单失败')
    const orderNo = joinRes.order_no || joinRes.data?.order_no || joinRes.result?.order_no
    
    // Simulate Wechat Pay flow (simplified)
    const payRes = await createWechatOrder({ openid, order_no: orderNo })
    await new Promise((resolve, reject) => {
       uni.requestPayment({
         provider: 'wxpay',
         ...payRes,
         success: resolve,
         fail: reject
       })
    })

    // Get Result
    const res = await getLotteryResult(orderNo)
    const raw = res.list || res.items || res.data || res.result || (Array.isArray(res) ? res : [res])
    winItems.value = raw.map(i => ({
      title: i.title || i.name || '未知奖励',
      image: i.image || i.img || ''
    }))
    
    showFlip.value = true
    setTimeout(() => {
      if(flipRef.value && flipRef.value.revealResults) flipRef.value.revealResults(winItems.value)
    }, 100)

  } catch (e) {
    uni.showToast({ title: e.message || '挑战失败', icon: 'none' })
  } finally {
    drawLoading.value = false
  }
}

function normalizeIssues(list) {
  if (!Array.isArray(list)) return []
  return list.map(i => ({
    id: i.id,
    title: i.title || i.name,
    no: i.no,
  }))
}

function normalizeRewards(list) {
  if (!Array.isArray(list)) return []
  return list.map(i => ({
    title: i.name || i.title,
    image: i.image || i.img || i.pic,
    percent: i.percent || 0
  }))
}

async function fetchPropCards() { /* implementation same as other pages */ }
async function fetchCoupons() { /* implementation same as other pages */ }

function showRules() {
  uni.showModal({ title: '规则', content: detail.value.rules || '暂无规则', showCancel: false })
}

function closeFlip() { showFlip.value = false }

onLoad((opts) => {
  if (opts.id) {
    activityId.value = opts.id
    loadData(opts.id)
  }
})
</script>

<style scoped>
.page-wrapper {
  min-height: 100vh;
  position: relative;
  background: #2D1B4E; /* Dark Purple Theme */
  color: #fff;
  display: flex;
  flex-direction: column;
}
.bg-fixed {
  position: absolute;
  top: 0; left: 0; width: 100%; height: 100%;
  opacity: 0.3;
  z-index: 0;
}
.bg-mask {
  position: absolute;
  top: 0; left: 0; width: 100%; height: 100%;
  background: linear-gradient(180deg, rgba(45,27,78,0.8), #2D1B4E);
  z-index: 1;
}

.content-area {
  position: relative;
  z-index: 2;
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 30rpx;
}

.header-section {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 60rpx;
}
.main-title { font-size: 48rpx; font-weight: 900; font-style: italic; display: block; }
.sub-title { font-size: 24rpx; opacity: 0.8; margin-top: 8rpx; display: block; }
.rule-btn {
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.2);
  padding: 8rpx 24rpx;
  border-radius: 999rpx;
  font-size: 24rpx;
}

.tower-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.tower-level {
  width: 100%;
  background: linear-gradient(135deg, #6D28D9, #4C1D95);
  padding: 40rpx;
  border-radius: 24rpx;
  box-shadow: 0 8rpx 32rpx rgba(0,0,0,0.3);
  margin-bottom: 40rpx;
  border: 2rpx solid rgba(255,255,255,0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.level-info { display: flex; flex-direction: column; }
.level-num { font-size: 24rpx; opacity: 0.8; margin-bottom: 8rpx; }
.level-name { font-size: 40rpx; font-weight: 700; }
.level-status { font-size: 24rpx; background: rgba(0,0,0,0.2); padding: 4rpx 12rpx; border-radius: 8rpx; }

.rewards-preview {
  width: 100%;
}
.rewards-scroll {
  white-space: nowrap;
}
.reward-item {
  display: inline-block;
  width: 160rpx;
  margin-right: 20rpx;
  text-align: center;
}
.reward-img {
  width: 120rpx; height: 120rpx;
  border-radius: 16rpx;
  background: rgba(255,255,255,0.1);
  margin-bottom: 12rpx;
}
.reward-name { font-size: 22rpx; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.reward-prob { font-size: 20rpx; color: #FFD700; }

.action-area {
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(20rpx);
  padding: 30rpx;
  border-radius: 32rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.price-display { color: #FFD700; font-weight: 700; }
.currency { font-size: 28rpx; }
.amount { font-size: 48rpx; margin: 0 4rpx; }
.unit { font-size: 24rpx; opacity: 0.8; }

.challenge-btn {
  background: linear-gradient(135deg, #FFD700, #F59E0B);
  color: #333;
  font-weight: 900;
  border-radius: 999rpx;
  padding: 0 60rpx;
  font-size: 32rpx;
  box-shadow: 0 8rpx 24rpx rgba(245, 158, 11, 0.4);
}

.flip-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 999;
}
.flip-mask {
  position: absolute; top: 0; bottom: 0; width: 100%; background: rgba(0,0,0,0.8);
}
.flip-content {
  position: relative; z-index: 2; height: 100%; display: flex; flex-direction: column; padding: 40rpx;
}
.close-btn {
  margin-top: auto;
  background: #fff; color: #333; border-radius: 999rpx; font-weight: 700;
}
</style>
