<template>
	<view>
		<view v-if="pagesNum > 1">
			<u-navbar :placeholder="true" bgColor="#fff" :autoBack="true" :border="true" title="盲票详情"></u-navbar>
		</view>
		<view v-else>
			<u-navbar leftIconSize="0" :placeholder="true" bgColor="#fff" title="盲票详情">
				<view class="nav-left flex" slot="left" @click="toIndex">
					<u-icon name="home" size="20" color="#333"></u-icon>
					<view class="nav-left__code">首页</view>
				</view>
			</u-navbar>
		</view>

		<view class="detail">
			<view class="detail-top">
				<u-swiper :list="picUrlArr" height="375" imgMode="aspectFit" bgColor="#fff" radius="0" :indicator="true"
					:circular="true"></u-swiper>
			</view>
			<view class="flex detail-info">
				<view class="detail-info__left">
					<text class="title">{{ info.title }}</text>
					<view class="flex num">
						<u-icon name="gift" size="25" color="#EB7009"></u-icon>
						<text>100%抽中，奖品多多</text>
					</view>
					<view class="tip">图片仅供参考，请以实物为准</view>
				</view>
				<view class="detail-info__right">
					<text class="money">￥{{ $numberFormat(info.salePrice) }}</text>
					<text>销量 {{ info.saleQty }}</text>
				</view>
			</view>
			<view class="detail-goods">
				<view class="detail-goods-title">可获得奖品</view>
				<view class="detail-goods-list">
					<!-- <navigator :url="`/packagePrize/goods/detail?id=${ item.prizeId }`" class="detail-goods-list-item"
						hover-class="navigator-hover" v-for="(item, index) in prizeList" :key="index"> -->
					<view class="detail-goods-list-item" v-for="(item, index) in prizeList" :key="index"
						@click="toPrizeGoods(item)">
						<view class="detail-goods-list-item__value">
							<view class="flex image-wrap">
								<image :src="item.picUrl" mode="scaleToFill"></image>
							</view>
							<view class="info">
								<text class="title">{{ item.title }}</text>
								<text class="num"
									v-if="item.prizeType != 'coin'">价值：￥{{ $numberFormat(item.value) }}</text>
								<text class="num" v-else>数量：{{ item.value }}颗</text>
								<text class="num">概率：{{ item.hitRate }}%</text>
							</view>
							<view class="name">{{ item.name }}</view>
						</view>
					</view>
					<!-- </navigator> -->
				</view>
			</view>
		</view>
		<view class="footer-fixed">
			<view class="flex btn">
				<button type="default" @click="exchange">{{ info.salePrice / 100 }}元 立即开刮</button>
				<view class="share " @click="toShare">
					<u-icon name="share-square" color="#E96737" size="30"></u-icon>
					<view style="color: #E96737;text-align: center;">分享</view>
				</view>
			</view>
		</view>

		<pay-popup :pay-show="payShow" :pay-info="payInfo" @close="close" @success="toProcess" />
	</view>
</template>

<script>
	import env from '../../config/env.js'
	import $http from '@/utils/request.js'
	import PayPopup from '../../components/pay-popup/pay-popup.vue'
	export default {
		components: {
			PayPopup
		},
		data() {
			return {
				boxId: '',
				picUrlArr: [],
				info: {},
				prizeList: [],

				payShow: false,
				payInfo: {},
				pagesNum: null,
				sceneArr: null
			};
		},

		onLoad(opthios) {
			if (opthios.scene) {
				let sceneStr = decodeURIComponent(opthios.scene)
				this.sceneArr = sceneStr.split('&')
				this.boxId = this.sceneArr[0]
				uni.setStorageSync('shareUid', this.sceneArr[1])
				uni.setStorageSync('shareType', this.sceneArr[2])
			}

			if (opthios.boxId) {
				this.boxId = opthios.boxId
			}

			if (opthios.userId) {
				uni.setStorageSync('shareUid', opthios.userId)
			}

			if (opthios.type) {
				uni.setStorageSync('shareType', opthios.type)
			}
		},

		onShow() {
			this.pagesNum = getCurrentPages().length
			if (this.boxId) {
				this.getDetail()
			}
		},

		methods: {
			getDetail() {
				uni.showLoading({
					title: '加载中'
				});
				$http.post('/api/v1/mp/user/mall/ticket/detail', {
					boxId: this.boxId,
					noToken: true
				}).then(res => {
					uni.hideLoading();
					if (res.code == 0) {
						this.info = res.data
						let picUrlArr = res.data.picUrl.split(',')
						picUrlArr.forEach(item => {
							this.picUrlArr.push(env.filePublic + item + '?imageView2/2/w/750')
						})
						let prizeList = res.data.prizeList
						prizeList.forEach(item => {
							let picUrlArr = item.picUrl.split(',')
							item.picUrl = env.filePublic + picUrlArr[0] + '?imageView2/2/w/170'
						})
						this.prizeList = prizeList
					}
				}).catch(() => {
					uni.hideLoading();
				})
			},

			getPageNum() {
				let pages = getCurrentPages();
			},

			close() {
				this.payShow = false
			},

			toProcess(id) {
				this.payShow = false
				uni.navigateTo({
					url: `/packageOperate/process/index?id=${ id }`
				})
			},

			exchange() {
				let data = {
					userCouponIds: [],
					autoCoupon: 1,
					boxId: this.info.boxId,
					orderNum: 1
				}
				$http.post('/api/v1/mp/user/ticket/order/settle', data).then(res => {
					if (res.code == 0) {
						let info = {
							...res.data,
							...this.info,
							picUrl: env.filePublic + res.data.picUrl,
							couponTitle: res.data && res.data.couponList && res.data.couponList.length && res
								.data.couponList[0].title,
							couponId: res.data && res.data.couponList && res.data.couponList.length && res.data
								.couponList[0].id
						}
						this.payInfo = info
						this.payShow = true
					}
				})
			},

			toShare() {
				if (!uni.getStorageSync('token')) {
					uni.navigateTo({
						url: "/pages/login/index"
					})
					return
				}
				uni.navigateTo({
					url: `/packageOperate/share/index?boxId=${ this.info.boxId }`
				})
			},

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

			toPrizeGoods(item) {
				if (item.prizeType == "goods") {
					uni.navigateTo({
						url: `/packagePrize/goods/detail?id=${ item.refId }`
					})
				}
				//门店优惠券
				if (item.prizeType == "coupon" || item.prizeType == "coupon_pkg") {
					// if (item.couponType == "1") {
					// 	uni.navigateTo({
					// 		url: `/packagePrize/goods/detail?id=426`
					// 	})
					// }
					// if (item.couponType == "2") {
					// 	uni.navigateTo({
					// 		url: `/packagePrize/goods/detail?id=425`
					// 	})
					// }
					uni.navigateTo({
						url: `/packagePrize/goods/detail?id=425`
					})
				}
				// 盲豆
				if (item.prizeType == "coin") {
					uni.navigateTo({
						url: `/packagePrize/goods/detail?id=424`
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.nav-left {
		&__code {
			margin-left: 10rpx;
		}
	}

	.detail {
		padding-bottom: 100rpx;

		&-top {
			margin-bottom: 20rpx;
		}

		&-info {
			justify-content: space-between;
			box-sizing: border-box;
			padding: 24rpx 16rpx;
			background-color: #fff;
			margin: 10rpx 10rpx 20rpx;
			border-radius: 10rpx;

			&__left {
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				text {
					line-height: 24rpx;
					display: inline-block;
				}

				.title {
					color: rgba(16, 16, 16, 100);
					font-size: 32rpx;
					line-height: 32rpx;
					font-weight: bold;
				}

				.num {
					color: $uni-text-color;
					font-size: 28rpx;
					justify-content: flex-start;
					margin: 20rpx 0;
				}

				.tip {
					font-size: 24rpx;
				}
			}

			&__right {
				display: flex;
				flex-direction: column;
				align-items: flex-end;
				justify-content: space-between;
				height: 162rpx;

				text {
					color: rgba(157, 157, 157, 100);
					font-size: 14px;
				}

				.money {
					font-size: 40rpx;
					font-weight: bold;
					line-height: 40rpx;
					font-weight: bold;
					color: $uni-text-color;
					margin-bottom: 40rpx;
				}
			}
		}

		&-sku {
			background-color: #fff;
			border-radius: 10rpx;
			margin: 10rpx 10rpx 20rpx;

			&-title {
				padding: 24rpx 16rpx 12rpx;
			}

			&-item {
				padding-bottom: 10rpx;

				view {
					padding-left: 50rpx;
					line-height: 50rpx;
				}
			}
		}

		&-goods {
			margin: 10rpx 10rpx 20rpx;

			&-title {
				margin-bottom: 20rpx;
			}

			&-list {
				padding-bottom: 100rpx;

				&-item {
					position: relative;
					background-color: #FFFFFF;
					margin-bottom: 30rpx;
					border-radius: 10rpx;
					padding: 30rpx 20rpx;

					&__value {
						display: flex;
					}

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

					.name {
						position: absolute;
						left: 0rpx;
						top: 0rpx;
						line-height: 28rpx;
						padding: 10rpx 40rpx;
						color: #FFFFFF;
						border-top-left-radius: 10rpx;
						border-bottom-right-radius: 40rpx;
						background-color: $uni-bg-color;
					}

					.info {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						font-size: 30rpx;
						padding-left: 50rpx;

						.title {
							font-weight: bold;
						}

						.num {
							color: #848484;
						}
					}
				}

				&-item:last-child {
					border: none;
				}
			}
		}
	}

	.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 {
			position: relative;
			padding: 20rpx 20rpx;

			/deep/ button {
				width: 440rpx;
				height: 90rpx;
				line-height: 90rpx;
				font-size: 36rpx;
				color: #fff;
				background-color: $uni-bg-color;
				border: none;
				border-radius: 20rpx;
			}

			.share {
				position: absolute;
				left: 40rpx;
			}
		}
	}
</style>
