	<template>
	<view>
		<!-- <view class="choice">
			<view class="choice-title" v-if="!tipShow">恭喜你获得</view>
			<view class="choice-all" v-if="total > 1 && !tipShow">以下奖品任选其一</view>
			<view class="flex choice-list">
				<view class="choice-list-item" v-for="(item, index) in prizeList" :key="index"
					@click="selectPrize(item, index)">
					<view
						:class="{'item-info action': actionIndex == index, 'item-info confirm': actionIndex != index, 'item-info null': item.remainQty == 0}">
						<view class="flex info-image">
							<image :src="item.picUrl" mode="aspectFill"></image>
						</view>
						<view class="title" v-if="item.prizeType && item.prizeType.value != 'coin'">{{ item.title }}
						</view>
						<view class="title" v-else>盲豆x {{ item.value }}</view>
						<view class="tip" v-if="item.remainQty == 0">已兑完</view>
						<view class="flex action-icon" v-if="actionIndex == index">
							<u-icon name="checkmark" color="#fff" size="24"></u-icon>
						</view>
					</view>
				</view>
			</view> -->
			<!-- <view class="flex btn">
				<view class="confirm" @click="confirmPrize" v-if="!tipShow">确认</view>
			</view> -->
		<!-- </view> -->

		<!-- <view class="footer-fixed" v-if="!tipShow">
			<view class="flex btn">
				<button type="default" @click="confirmPrize">确认</button>
			</view>
		</view> -->

		<u-popup :show="tipShow" :round="10" mode="center" :safeAreaInsetBottom="false">
			<view class="null-prize">
				<view class="title">该盲票已被他人买走了</view>
				<navigator open-type="exit" target="miniProgram" hover-class="none" class="btn">确认</navigator>
			</view>
		</u-popup>

		<!-- <u-popup :show="comfirmShow" :round="10" mode="center" :safeAreaInsetBottom="false">
			<view class="confirm-prize">
				<view class="flex confirm-prize-info">
					<image :src="actionInfo.picUrl" mode="aspectFill"></image>
					<view class="title" v-if="actionInfo.prizeType && actionInfo.prizeType.value != 'coin'">
						{{ actionInfo.title }}
					</view>
					<view class="title" v-else>盲豆x {{ actionInfo.value }}</view>
				</view>
				<view class="confirm-prize-tip" v-if="actionInfo.prizeType && actionInfo.prizeType.value != 'coin'">
					已放入我的奖品库<text @click="toPrize(actionInfo.prizeType)">前往查看</text></view>
				<view class="confirm-prize-tip" v-else>已放入“我的盲豆”<text @click="toPrize('coin')">去兑换商品</text></view>
				<view class="flex confirm-prize-btn">
					<view class="back" @click="back">返回</view>
					<view class="confirm" @click="again">再来一张</view>
				</view>
			</view>
		</u-popup> -->
		
		<view class="choice">
			<!-- 返回 -->
			<view class="choice-navLeft flex" :style="{ top: statusHeight + 'px' }" @click="back">
				<u-icon size="19" color="#fff" name="arrow-left"></u-icon>
			</view>
			<!-- 标题 -->
			<view class="choice-title flex">
				<image src="../../packagePrize/static/choice_title.png" mode="scaleToFill"></image>
			</view>
			<!-- 提示 -->
			<view class="choice-tip flex" v-if="!comfirmShow">
				<view class="choice-tip-content">以下奖品任选一件</view>
			</view>
			<!-- 奖品列表 -->
			<view class="choice-list" v-if="!comfirmShow">
				<view class="choice-list-item flex" v-for="(item, index) in prizeList" :key="index">
					<view class="checkbox flex" :class="{ 'checkbox-action': actionIndex == index }" @click="selectPrize(index)">
						<u-icon name="checkmark" color="#fff" size="14"></u-icon>
					</view>
					<view class="info flex">
						<image :src="item.picUrl" mode="aspectFill"></image>
						<view class="info-content flex">
							<view class="info-content__title ells-one" v-if="item.prizeType && item.prizeType.value != 'coin'">{{ item.title }}</view>
							<view class="info-content__title ells-one" v-else>盲豆</view>
							<view class="info-content__price" v-if="item.prizeType && item.prizeType.value != 'coin'">￥{{ $numberFormat(item.value) }}</view>
							<view class="info-content__price" v-else>{{ item.value }}颗</view>
						</view>
					</view>
					<view class="detail" @click="toPrizeGoods(item)">查看</view>
				</view>
			</view>
			<!-- 按钮 -->
			<view class="footer-fixed flex" v-if="!comfirmShow">
				<view class="footer-fixed-btn flex" @click="confirmPrize">
					<image src="../../packagePrize/static/rolling_btn.png" mode="aspectFit"></image>
					<view class="footer-fixed-btn-txt">确认</view>
				</view>
			</view>
			
			<view class="choice-box flex" v-if="comfirmShow">
				<view class="choice-box-popup flex">
					<view class="choice-box-popup__image flex">
						<image :src="actionInfo.picUrl" mode="aspectFit"></image>
					</view>
				</view>
			</view>
			<view class="choice-info flex" v-if="comfirmShow">
				<view class="choice-info-content">
					<view class="choice-info-content-price" v-if="actionInfo.prizeType && actionInfo.prizeType.value != 'coin'"><text>¥</text>{{ $numberFormat(actionInfo.value) }}</view>
					<view class="choice-info-content-price" v-else>{{ actionInfo.value }}颗</view>
					<view class="choice-info-content-title" v-if="actionInfo.prizeType && actionInfo.prizeType.value != 'coin'">{{ actionInfo.title }}</view>
					<view class="choice-info-content-title" v-else>盲豆</view>
					<view class="choice-info-content-tip flex"  @click="toPrize(actionInfo.prizeType)">
						<view class="txt">哇，去瞅瞅我的宝贝</view>
					</view>
				</view>
			</view>
			<view class="choice-btn flex" v-if="comfirmShow">
				<view class="choice-btn-content flex"  @click="again">
					<image src="../../packagePrize/static/rolling_btn.png" mode="aspectFit"></image>
					<view class="choice-btn-content-txt">再来一张</view>
				</view>
			</view>
		</view>
		
		<!-- 详情 -->
		<u-popup :show="detailShow" :round="11" mode="bottom" @close="detailShow = false" :closeable="true">
			<view class="prize-detail flex">
				<view class="prize-detail-title">奖品详情</view>
				<view class="prize-detail-content">
					<u-parse :content="description" :selectable="true"></u-parse>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	import { formatRichText } from '@/utils/util.js'
	export default {
		data() {
			return {
				ticketId: '',
				prizeList: [],
				total: 0,
				actionIndex: 0,
				tipShow: false,
				comfirmShow: false,
				actionInfo: {},
				type: '',
				
				statusHeight: 30,
				detailShow: false,
				description: '',
			};
		},
		onLoad(options) {
			this.ticketId = options.id
			this.type = options.type
			
			// #ifdef MP-WEIXIN
			const res = uni.getMenuButtonBoundingClientRect()
			this.statusHeight = res.top //胶囊距离顶部
			// #endif
		},
		onShow() {
			this.getPrizeList()
		},
		methods: {
			getPrizeList() {
				uni.showLoading({
					title: '加载中'
				});
				let data = {
					ticketId: this.ticketId
				}
				$http.post('/api/v1/mp/user/ticket/queryHitPrizeList', data).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						res.data.prizeList.forEach(item => {
							let picUrlArr = item.picUrl.split(',')
							item.picUrl = env.filePublic + picUrlArr[0] + '?imageView2/2/w/340'
							item.prizeType = JSON.parse(item.prizeType)
						})
						this.prizeList = res.data.prizeList
						if(this.prizeList.length == 1) {
							let item = this.prizeList[this.actionIndex]
							this.actionInfo = item
							$http.post('/api/v1/mp/user/ticket/cashPrize', {
								ticketId: this.ticketId,
								awardsId: item.awardsId,
								prizeId: item.prizeId
							}).then(res => {
								if (res.code == 0) {
									this.comfirmShow = true
								}
							})
						}
						this.ticketId = res.data.ticketId
						this.total = this.prizeList.length
					} else if (res.code == 1018) {
						this.tipShow = true
					} else {
						this.tipShow = true
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},

			selectPrize(index) {
				this.actionIndex = index
			},

			confirmPrize() {
				let _this = this
				let item = _this.prizeList[_this.actionIndex]
				_this.actionInfo = item
				if (_this.prizeList.length > 1) {
					uni.showModal({
						title: '提示',
						content: '确定选择该奖品吗？',
						success(res) {
							if (res.confirm) {
								$http.post('/api/v1/mp/user/ticket/cashPrize', {
									ticketId: _this.ticketId,
									awardsId: item.awardsId,
									prizeId: item.prizeId
								}).then(res => {
									if (res.code == 0) {
										_this.comfirmShow = true
									}
								})
							}
						}
					})
				} else {
					$http.post('/api/v1/mp/user/ticket/cashPrize', {
						ticketId: _this.ticketId,
						awardsId: item.awardsId,
						prizeId: item.prizeId
					}).then(res => {
						if (res.code == 0) {
							_this.comfirmShow = true
						}
					})
				}
			},

			toIndex() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},

			back() {
				let pages = getCurrentPages();
				if (pages.length > 1) {
					uni.navigateBack({
						delta: 1
					})
				} else {
					uni.switchTab({
						url: '/pages/core/index'
					})
				}
			},

			toPrize(data) {
				if (data.value == 'goods') {
					uni.redirectTo({
						url: '/packagePrize/prize/index'
					})
				}
				if (data.value == 'coupon') {
					uni.redirectTo({
						url: '/packagePrize/prize/index?coupon=1'
					})
				}
				if (data.value == 'coupon_pkg') {
					uni.redirectTo({
						url: '/packagePrize/prize/index?coupon=1'
					})
				}
				if (data.value == 'coin') {
					uni.switchTab({
						url: '/pages/core/index'
					})
				}
			},
			
			toPrizeGoods(item) {
				if (item.prizeType.value == "goods") {
					this.getGoodsDetail(item.refId)
				}
				//门店优惠券
				if (item.prizeType.value == "coupon" || item.prizeType.value == "coupon_pkg") {
					this.getGoodsDetail(425)
				}
				// 盲豆
				if (item.prizeType.value == "coin") {
					this.getGoodsDetail(424)
				}
			},
			
			getGoodsDetail(id) {
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/exchange/goods/detail', {
					noToken: true,
					goodsId: id
				}).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						this.detailShow = true
						// 处理富文本
						const description = res.data.description.replaceAll(".jpg\"", ".jpg?imageView2/2/w/750\"")
							.replaceAll(".jpeg\"", ".jpeg?imageView2/2/w/750\"").replaceAll(".png\"",
								".png?imageView2/2/w/750\"");
						this.description = formatRichText(description);
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},
			
			again() {
				let _this = this
				uni.scanCode({
					scanType: ['qrCode'],
					success(res) {
						const url = res.result
						let serialNo = url.substring(url.length - 21, url.length)
						uni.redirectTo({
							url: `/pages/lucky/index?id=${ serialNo }&type=offLine`
						})
					},
					fail() {
						uni.$u.toast('请扫二维码');
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.choice {
		display: flex;
		flex-direction: column;
		position: relative;
		width: 100%;
		height: 100vh;
		background: url(https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/v2/offline_prize_bkg.png) center center no-repeat;
		background-size: 100vw 100vh;
		overflow: hidden;
		
		// 返回
		&-navLeft {
			position: absolute;
			left: 34rpx;
			width: 66rpx;
			height: 66rpx;
			background-color: rgba(255, 255, 255, .36);
			border-radius: 50%;
		}
		
		// 标题
		&-title {
			padding-top: 15vh;
			margin-bottom: 72rpx;
			
			image {
				width: 484rpx;
				height: 108rpx;
			}
		}
		
		// 提示
		&-tip {
			&-content {
				width: 50vw;
				height: 56rpx;
				line-height: 56rpx;
				background: rgba(255, 211, 157, .44);
				border-radius: 24rpx;
				color: #FB900B;
				font-size: 30rpx;
				text-align: center;
			}
		}
		
		// 奖品列表
		&-list {
			flex: 1;
			margin: 72rpx 48rpx 0;
			padding-bottom: 200rpx;
			overflow-y: auto;
			
			&-item {
				justify-content: space-between;
				background-color: #fff;
				border-radius: 12rpx;
				padding: 38rpx 24rpx;
				margin-bottom: 54rpx;
				
				.checkbox {
					width: 46rpx;
					height: 46rpx;
					border: 1px solid #F56D1B;
					border-radius: 50%;
				}
				
				.checkbox-action {
					background: #F56D1B;
				}
				
				.info {
					margin: 0 30rpx;
					flex: 1;
					justify-content: flex-start;
					
					image {
						width: 110rpx;
						height: 110rpx;
						margin-right: 34rpx;
					}
					
					&-content {
						height: 110rpx;
						flex: 1;
						align-items: flex-start;
						flex-direction: column;
						justify-content: space-between;
						
						&__title {
							width: 100%;
							font-size: 30rpx;
						}
						
						&__price {
							font-weight: bold;
						}
					}
				}
				
				.detail {
					color: #F56D1B;
				}
			}
		}
		
		&-box {
			margin-bottom: 34rpx;
			
			&-popup {
				flex-direction: column;
				width: 75vw;
				height: 38vh;
				background-color: #FFFFFF;
				border-radius: 22rpx;
				
				&__image {
					width: 65%;
					height: 65%;
					margin-bottom: 30rpx;
					
					image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}
		
		&-info {
			margin-bottom: 34rpx;
			
			&-content {
				width: 70vw;
				
				&-price {
					text-align: center;
					font-size: 40rpx;
					line-height: 40rpx;
					font-family: PingFang SC;
					font-weight: bold;
					color: #FFFFFF;
					margin-bottom: 20rpx;
					
					text {
						font-size: 28rpx;
					}
				}
				
				&-title {
					text-align: center;
					font-size: 36rpx;
					line-height: 36rpx;
					color: #FFFFFF;
					margin-bottom: 40rpx;
				}
				
				&-tip {
					.txt {
						padding: 10rpx 0;
						width: 75%;
						text-align: center;
						background: rgba(255, 211, 157, .3);
						border-radius: 30rpx;
						font-size: 30rpx;
						font-weight: 500;
						color: #FC8D03;
					}
				}
			}
		}
		
		&-btn {
			&-content {
				position: relative;
				width: 70vw;
				
				image {
					width: 70vw;
					height: 112rpx;
				}
				
				&-txt {
					position: absolute;
					top: 0;
					height: 112rpx;
					line-height: 112rpx;
					font-size: 30rpx;
					text-align: center;
					color: #FFFFFF;
				}
			}
		}
	}
	
	// 按钮
	.footer-fixed {
		position: fixed;
		bottom: var(--window-bottom);
		left: 0;
		right: 0;
		z-index: 11;
		// 设置ios刘海屏底部横线安全区域
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		
		&-btn {
			padding: 0 0 20rpx;
			position: relative;
			width: 70vw;
			
			image {
				width: 70vw;
				height: 112rpx;
			}
			
			&-txt {
				position: absolute;
				top: 0;
				height: 112rpx;
				line-height: 112rpx;
				font-size: 30rpx;
				text-align: center;
				color: #FFFFFF;
			}
		}

		.btn {
			padding: 20rpx 0;

			/deep/ button {
				width: 640rpx;
				height: 90rpx;
				line-height: 90rpx;
				font-size: 36rpx;
				color: #fff;
				background-color: $uni-bg-color;
				border: none;
				border-radius: 20rpx;
			}
		}
	}
	
	.prize-detail {
		flex-direction: column;
		height: 60vh;

		&-title {
			padding: 40rpx 0;
			text-align: center;
			font-size: 34rpx;
			line-height: 34rpx;
			font-weight: bold;
		}
		
		&-content {
			flex: 1;
			overflow-y: auto;
		}
	}

	.null-prize {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 80vw;
		height: 320rpx;
		background-color: #FFFFFF;
		border: 1px solid rgba(187, 187, 187, 100);

		.btn {
			margin-top: 60rpx;
			width: 160rpx;
			height: 60rpx;
			line-height: 60rpx;
			border-radius: 8rpx;
			background-color: rgba(235, 112, 9, 100);
			color: rgba(255, 255, 255, 100);
			font-size: 28rpx;
			text-align: center;
		}
	}

	.confirm-prize {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 80vw;
		padding: 40rpx 0;

		&-info {
			width: 80%;
			margin-bottom: 60rpx;
			justify-content: space-around;

			image {
				width: 150rpx;
				height: 200rpx;
			}

			.title {
				flex: 1;
				padding-left: 40rpx;
			}
		}

		&-tip {
			text-align: center;
			margin-bottom: 60rpx;

			text {
				padding-left: 24rpx;
				color: rgba(32, 163, 242, 100);
			}
		}

		&-btn {
			width: 80%;
			justify-content: space-around;

			.back {
				width: 160rpx;
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 8rpx;
				color: rgba(235, 112, 9, 100);
				font-size: 28rpx;
				text-align: center;
				font-family: Microsoft Yahei;
				border: 1px solid rgba(235, 112, 9, 100);
			}

			.confirm {
				width: 160rpx;
				height: 60rpx;
				line-height: 60rpx;
				border-radius: 8rpx;
				background-color: rgba(235, 112, 9, 100);
				color: rgba(253, 253, 253, 100);
				font-size: 28rpx;
				text-align: center;
			}
		}
	}
</style>
