<template>
	<view class="dr" :style="{'--sb': statusBarHeight + 'px'}">
		<view class="dr-bg"></view>
		<view class="nav" :style="{height: navHeight + 'px', paddingTop: statusBarHeight + 'px'}">
			<view class="nav-back" @tap="goBack"><text class="nav-back-icon">‹</text></view>
			<view class="nav-title">开奖结果</view>
		</view>

		<scroll-view scroll-y class="dr-scroll" :style="{paddingTop: (navHeight + 10) + 'px'}">
			<!-- 切换 -->
			<view class="dr-tabs">
				<view class="dr-tab" :class="{on: tab===0}" @tap="tab=0">最近开奖</view>
				<view class="dr-tab" :class="{on: tab===1}" @tap="tab=1">我的中奖</view>
			</view>

			<block v-if="tab===0">
				<view v-if="record" class="dr-card">
					<view class="dr-period">第 {{record.periodNo}} 期</view>
					<view class="dr-amount"><text>￥</text>{{Number(record.poolAmount||0).toFixed(2)}}</view>
					<view class="dr-meta">
						<text>参与 {{record.joinCount}} 人</text>
						<text>·</text>
						<text>中奖 {{record.winnerCount}} 人</text>
					</view>
					<view class="dr-time">开奖时间 {{record.drawTime}}</view>
				</view>
				<view v-else class="dr-empty">本商圈暂未开奖，敬请期待 🌌</view>

				<view class="dr-winners" v-if="history && history.length">
					<view class="dr-sec-title">最近10期中奖记录</view>
					<view class="dr-history" v-for="(h, hi) in history" :key="hi">
						<view class="dr-history-head">
							<text>第 {{h.record && h.record.periodNo}} 期</text>
							<text>{{drawDate(h.record && h.record.drawTime)}}</text>
						</view>
						<view class="dr-winner" v-for="(w, i) in h.winners" :key="i">
							<text class="dr-w-level" :class="'lv'+w.rewardLevel">{{w.levelName}}</text>
							<text class="dr-w-name">{{maskName(w.userName)}}</text>
							<text class="dr-w-amount">￥{{Number(w.amount||0).toFixed(2)}}</text>
						</view>
					</view>
				</view>
			</block>

			<block v-else>
				<view v-if="myWinning && myWinning.length" class="dr-my">
					<view class="dr-my-item" v-for="(w, i) in myWinning" :key="i">
						<view class="dr-my-left">
							<text class="dr-w-level" :class="'lv'+w.rewardLevel">{{w.levelName}}</text>
							<text class="dr-my-period">第 {{w.periodNo}} 期</text>
						</view>
						<text class="dr-my-amount">￥{{Number(w.amount||0).toFixed(2)}}</text>
						<view class="dr-my-btn" :class="{done: w.isReceived===1}" @tap="receive(w)">
							{{w.isReceived===1 ? '已领取' : '领取'}}
						</view>
					</view>
				</view>
				<view v-else class="dr-empty">还没有中奖记录，继续攒券瓜分奖池</view>
			</block>
			<view style="height:60rpx;"></view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				statusBarHeight: 20,
				navHeight: 64,
				tab: 0,
				userId: '',
				regionId: '',
				record: null,
				winners: [],
				history: [],
				myWinning: []
			}
		},
		onLoad() {
			const sys = uni.getSystemInfoSync()
			this.statusBarHeight = sys.statusBarHeight || 20
			this.initNavHeight()
			this.userId = uni.getStorageSync('id') || ''
			try {
				const area = uni.getStorageSync('area')
				if (area) this.regionId = JSON.parse(area).id || ''
			} catch (e) {}
			if (!this.regionId) {
				this.tui.toast('未获取到校区信息，开奖仅限本区域内进行')
				return
			}
			this.loadResult()
			this.loadMyWinning()
		},
		methods: {
			initNavHeight() {
				try {
					const menu = uni.getMenuButtonBoundingClientRect && uni.getMenuButtonBoundingClientRect()
					if (menu && menu.top && menu.height) {
						this.navHeight = menu.top + menu.height + 8
						return
					}
				} catch (e) {}
				this.navHeight = this.statusBarHeight + 44
			},
			loadResult() {
				this.tui.request('/app/planet/draw/result', 'POST', {
					userId: this.userId,
					regionId: this.regionId
				}).then((res) => {
					if (res.code == 200 && res.result) {
						this.record = res.result.record
						this.winners = res.result.winners || []
						this.history = res.result.history || []
					}
				})
			},
			loadMyWinning() {
				this.tui.request('/app/planet/draw/myWinning', 'POST', {
					userId: this.userId,
					regionId: this.regionId
				}, false, false, true).then((res) => {
					if (res.code == 200) this.myWinning = res.result || []
				})
			},
			receive(w) {
				if (w.isReceived === 1) return
				this.tui.request('/app/planet/draw/receive', 'POST', {
					userId: this.userId,
					regionId: this.regionId,
					winnerId: w.id
				}).then((res) => {
					this.tui.toast(res.message, 1500, res.code == 200)
					if (res.code == 200) this.loadMyWinning()
				})
			},
			maskName(name) {
				if (!name) return '神秘居民'
				const text = String(name)
				if (text.length <= 1) return text + '*'
				if (text.length === 2) return text.charAt(0) + '*'
				return text.charAt(0) + '*' + text.charAt(text.length - 1)
			},
			drawDate(time) {
				if (!time) return ''
				return String(time).substring(0, 10)
			},
			goBack() {
				const pages = getCurrentPages ? getCurrentPages() : []
				if (pages.length > 1) {
					uni.navigateBack({ delta: 1 })
					return
				}
				uni.redirectTo({ url: '/package1/planet/index' })
			}
		}
	}
</script>

<style lang="scss" scoped>
	.dr {
		min-height: 100vh;
		background: linear-gradient(155deg, #F3FFF4 0%, #EAF8FF 46%, #F7EEFF 100%);
		color: #12342F;
	}

	.dr-bg {
		position: fixed;
		top: 0; left: 0; right: 0; height: 500rpx;
		background:
			radial-gradient(circle at 78% 12%, rgba(79,183,255,0.28), transparent 42%),
			radial-gradient(circle at 12% 30%, rgba(53,214,166,0.22), transparent 40%);
		pointer-events: none;
	}

	.nav {
		position: fixed;
		top: 0; left: 0; right: 0;
		z-index: 20;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		background: rgba(243,255,244,0.9);
	}

	.nav-back {
		position: absolute;
		left: 24rpx;
		bottom: 12rpx;
		width: 56rpx;
		height: 56rpx;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		background: rgba(255,255,255,.72);
		box-shadow: 0 8rpx 20rpx rgba(18,52,47,.08);
	}

	.nav-back-icon {
		color: #12342F;
		font-size: 42rpx;
		line-height: 1;
		transform: translateY(-2rpx);
	}

	.nav-title {
		color: #12342F;
		font-size: 32rpx;
		font-weight: 900;
		padding-bottom: 14rpx;
	}

	.dr-scroll {
		height: 100vh;
		box-sizing: border-box;
		padding: 0 32rpx;
	}

	.dr-tabs {
		display: flex;
		margin-top: 20rpx;
		background: rgba(255,255,255,0.68);
		border-radius: 36rpx;
		padding: 6rpx;
		border: 1rpx solid rgba(255,255,255,0.92);
		box-shadow: 0 16rpx 34rpx rgba(53,214,166,0.1);
	}

	.dr-tab {
		flex: 1;
		height: 64rpx;
		line-height: 64rpx;
		text-align: center;
		border-radius: 32rpx;
		color: #42635E;
		font-size: 26rpx;
		font-weight: 700;
	}

	.dr-tab.on {
		background: linear-gradient(135deg, #35D6A6, #4FB7FF);
		color: #FFFFFF;
		font-weight: 900;
	}

	.dr-card {
		margin-top: 28rpx;
		padding: 40rpx;
		border-radius: 42rpx;
		background: linear-gradient(150deg, rgba(255,255,255,0.9), rgba(255,248,222,0.72));
		border: 2rpx solid rgba(255,255,255,0.92);
		text-align: center;
		box-shadow: 0 24rpx 60rpx rgba(255,184,77,0.14);
	}

	.dr-period {
		color: #7E9691;
		font-size: 24rpx;
		font-weight: 700;
	}

	.dr-amount {
		margin-top: 12rpx;
		color: #22B889;
		font-size: 72rpx;
		font-weight: 800;
	}

	.dr-amount text {
		font-size: 36rpx;
	}

	.dr-meta {
		margin-top: 14rpx;
		color: #42635E;
		font-size: 24rpx;
	}

	.dr-meta text {
		margin: 0 8rpx;
	}

	.dr-time {
		margin-top: 10rpx;
		color: #7E9691;
		font-size: 22rpx;
	}

	.dr-empty {
		margin-top: 120rpx;
		text-align: center;
		color: #7E9691;
		font-size: 26rpx;
	}

	.dr-sec-title {
		margin: 32rpx 0 18rpx;
		color: #12342F;
		font-size: 30rpx;
		font-weight: 900;
	}

	.dr-winner {
		display: flex;
		align-items: center;
		padding: 22rpx 24rpx;
		margin-bottom: 14rpx;
		border-radius: 28rpx;
		background: rgba(255,255,255,0.72);
		border: 1rpx solid rgba(255,255,255,0.9);
		box-shadow: 0 12rpx 28rpx rgba(53,214,166,0.08);
	}

	.dr-history {
		margin-bottom: 22rpx;
		padding: 20rpx;
		border-radius: 30rpx;
		background: rgba(255,255,255,0.58);
		border: 1rpx solid rgba(255,255,255,0.86);
	}

	.dr-history-head {
		display: flex;
		justify-content: space-between;
		margin-bottom: 12rpx;
		color: #42635E;
		font-size: 24rpx;
		font-weight: 900;
	}

	.dr-w-level {
		font-size: 22rpx;
		padding: 2rpx 14rpx;
		border-radius: 8rpx;
		background: rgba(53,214,166,0.14);
		color: #22B889;
	}

	.dr-w-level.lv1 { background: rgba(255,122,89,0.14); color: #FF7A59; }
	.dr-w-level.lv2 { background: rgba(255,184,77,0.18); color: #C98716; }

	.dr-w-name {
		flex: 1;
		margin-left: 18rpx;
		color: #12342F;
		font-size: 26rpx;
		font-weight: 700;
	}

	.dr-w-amount {
		color: #22B889;
		font-size: 28rpx;
		font-weight: 700;
	}

	.dr-my-item {
		display: flex;
		align-items: center;
		padding: 26rpx 24rpx;
		margin-top: 18rpx;
		border-radius: 30rpx;
		background: rgba(255,255,255,0.74);
		border: 1rpx solid rgba(255,255,255,0.92);
		box-shadow: 0 12rpx 30rpx rgba(53,214,166,0.08);
	}

	.dr-my-left {
		flex: 1;
		display: flex;
		flex-direction: column;
	}

	.dr-my-period {
		margin-top: 10rpx;
		color: #7E9691;
		font-size: 22rpx;
	}

	.dr-my-amount {
		color: #22B889;
		font-size: 32rpx;
		font-weight: 800;
		margin-right: 20rpx;
	}

	.dr-my-btn {
		padding: 0 26rpx;
		height: 60rpx;
		line-height: 60rpx;
		border-radius: 30rpx;
		background: linear-gradient(135deg, #35D6A6, #4FB7FF);
		color: #FFFFFF;
		font-size: 24rpx;
		font-weight: 700;
	}

	.dr-my-btn.done {
		background: rgba(18,52,47,0.08);
		color: #9AA9A5;
	}
</style>
