	<template>
	<view>
		<u-navbar title="选择奖品" :border="true" :placeholder="true" :autoBack="true" bgColor="#fff" />
		<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>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	export default {
		data() {
			return {
				ticketId: '',
				prizeList: [],
				total: 0,
				actionIndex: 0,
				tipShow: false,
				comfirmShow: false,
				actionInfo: {},
				type: ''
			};
		},
		onLoad(options) {
			this.ticketId = options.id
			this.type = options.type
		},
		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
						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(item, index) {
				if (item.remainQty == 0) {
					uni.$u.toast('该奖品已兑完！');
					return
				}
				this.actionIndex = index
			},

			confirmPrize() {
				let _this = this
				let item = _this.prizeList[_this.actionIndex]
				_this.actionInfo = item
				console.log(this.actionInfo)
				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 == 'coin') {
					uni.switchTab({
						url: '/pages/core/index'
					})
				}
				
			},

			again() {
				let _this = this
				if (_this.type == 'onLine') {
					uni.navigateBack({
						delta: 1
					})
				} else if (_this.type == 'offLine') {
					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('请扫二维码');
						}
					});
				} else {
					_this.toIndex()
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.choice {
		width: 100%;
		min-height: calc(100vh - 44px - var(--status-bar-height));
		background: url(https://mp-public-1310078123.cos.ap-shanghai.myqcloud.com/openprize_bg.png) center center;
		padding-bottom: 150rpx;

		&-title {
			padding-top: 100rpx;
			font-size: 56rpx;
			font-weight: bold;
			text-align: center;
			line-height: 84rpx;
			margin-bottom: 12rpx;
		}

		&-all {
			text-align: center;
			line-height: 40rpx;
		}

		&-list {
			justify-content: space-around;
			flex-wrap: wrap;
			padding: 60rpx;

			&-item {
				position: relative;
				width: 43%;
				height: 440rpx;
				margin-bottom: 40rpx;
				background-color: #FFFFFF;
				border-radius: 10rpx;

				.item-info {
					height: 100%;
					display: flex;
					flex-direction: column;
					align-items: center;
					border: 2px solid #FFFFFF;
					border-radius: 10rpx;
				}

				.confirm {
					background: none;
					box-shadow: none;
				}

				.action-icon {
					position: absolute;
					bottom: -15rpx;
					right: -15rpx;
					width: 82rpx;
					height: 82rpx;
					line-height: 82rpx;
					background-color: rgba(236, 122, 61, 100);
					text-align: center;
					box-shadow: 0px 4rpx 12rpx 0px rgba(0, 0, 0, 0.4);
					border-radius: 50%;
				}

				.action {
					box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.4);
					border: 2px solid rgba(236, 122, 61, 100);
				}

				.null {
					box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.4);
					border: 2px solid rgba(187, 187, 187, 54);
				}

				.info-image {
					width: 100%;
					min-height: 70%;
					background-color: #F8F8F8;
					border-top-left-radius: 10rpx;
					border-top-right-radius: 10rpx;
				}

				image {
					width: 100%;
					height: 80%;
				}

				.title {
					width: 100%;
					font-size: 24rpx;
					margin: 16rpx 0 0 10rpx;
					color: #333;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 3;
				}

				.tip {
					margin: 16rpx 0 0 10rpx;
					font-size: 24rpx;
					color: red;
				}
			}
		}

		.btn {
			margin-top: 200rpx;

			.confirm {
				width: 400rpx;
				height: 80rpx;
				font-size: 36rpx;
				line-height: 80rpx;
				border-radius: 20rpx;
				background-color: rgba(235, 112, 9, 100);
				color: rgba(255, 255, 255, 100);
				text-align: center;
			}
		}
	}

	.footer-fixed {
		position: fixed;
		bottom: var(--window-bottom);
		left: 0;
		right: 0;
		z-index: 11;
		box-shadow: 0 -4rpx 40rpx 0 rgba(151, 151, 151, 0.24);
		background: #fff;
		// 设置ios刘海屏底部横线安全区域
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);

		.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;
			}
		}
	}

	.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>
