<template>
	<view>
		<u-navbar title="选择奖品" :border="true" :placeholder="true" :autoBack="true" bgColor="#fff" />
		<view class="choice">
			<view class="choice-title">恭喜你获得</view>
			<view class="choice-all" v-if="total > 1">以下奖品任选其一</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="{'flex action': actionIndex == index, 'flex confirm': actionIndex != index, 'flex null': item.remainQty == 0}">
						<view class="info">
							<image :src="item.picUrl" mode="aspectFill"></image>
							<view class="title">{{ item.title }}</view>
							<view class="tip" v-if="item.remainQty == 0">已兑完</view>
						</view>
					</view>
				</view>
			</view>
			<view class="flex btn">
				<view class="confirm" @click="confirmPrize">确认</view>
			</view>
		</view>
	</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,
			};
		},
		onLoad(options) {
			this.ticketId = options.id
			this.orderId = options.orderId
			this.getPrizeList()
		},
		methods: {
			getPrizeList() {
				uni.showLoading({
					title: '加载中'
				});
				let data = this.orderId ? { orderId: this.orderId } : { 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]
						})
						this.prizeList = res.data.prizeList
						this.ticketId = res.data.ticketId
						this.total = res.data.length
					}
				}).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]
				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) {
									uni.$u.toast('兑换成功');
									setTimeout(() => {
										uni.switchTab({
											url: '/pages/index/index'
										})
									}, 500)
								}
							})
						}
					}
				})

			},
		}
	}
</script>

<style lang="scss" scoped>
	.choice {
		width: 100%;
		min-height: calc(100vh - 50px);
		background-image: linear-gradient(to right, #ebbba7 0%, #cfc7f8 100%);
		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;
			margin-top: 60rpx;
			margin-bottom: 60rpx;

			&-item {
				width: 40%;
				height: 440rpx;
				margin-bottom: 40rpx;

				.confirm {
					height: 100%;
					background: none;
					box-shadow: none;
					border: none;
				}

				.action {
					height: 100%;
					background-color: #4cd964;
					box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.4);
					border: 1px solid rgba(187, 187, 187, 54);
				}

				.null {
					height: 100%;
					background: #918b8d;
					box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.4);
					border: 1px solid rgba(187, 187, 187, 54);
				}

				.info {
					width: 80%;
					height: 80%;
					background-color: #C0C0C0;
				}

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

				.title {
					margin-top: 20rpx;
					color: #FFFFFF;
					text-align: center;
				}

				.tip {
					margin-top: 20rpx;
					color: red;
					text-align: center;
				}
			}
		}

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