<!-- 商品信息 -->
<template>
	<view class="commodityInfo">
		<view class="commodityInfo-page" v-if="pageType == 1">
			<view class="flex-null-1"></view>
			<view class="product-content">
				<img class="product-img" src="@/static/icon_dhli.png">
				<view class="product-name">{{ commodityData.name }}</view>
				<view class="product-price">
					<text class="price-value">{{ commodityData.price?commodityData.price+' 积分':'' }}</text>
				</view>
				<view class="product-slotNo">
					<text class="price-value">货道 {{ commodityData.slotNo }}</text>
				</view>
			</view>
			<view class="flex-null-2"></view>
			<view class="clickButton" @click="clickButton()">立即兑换</view>
			<view class="flex-null-1"></view>
		</view>
		<view class="commodityInfo-page" v-if="pageType == 2">
			<img class="remind-img" v-if="state==1" src="@/static/icon_kcxq_cg.png">
			<img class="remind-img" v-if="state!=1" src="@/static/icon_kcxq_sb.png">
			<view class="commodity-message">{{state==1?'兑换成功':'兑换失败'}}</view>
			<view class="commodity-message">正在出货 请稍作等待</view>
			<view class="flex-null-2"></view>
			<view class="clickButton" @click="bottomButton()">{{state==1?'返回':'重试'}}</view>
			<view class="flex-null-1"></view>
		</view>
	</view>
</template>

<script>
	import {
		getCommodityInfo,
		exchangeGoods
	} from '@/api/index';
	import { browserDetection,safeToNumber } from '@/utils/auth';
	export default {
		data() {
			return {
				pageType: '',
				mid: "",
				sid: "",
				pid: "",
				pri: "",
				commodityData: {
					name: '',
					price: '',
				},
				state: '',
			}
		},
		onLoad(option) {
			let self = this;
			if (option.q) {
				let text = decodeURIComponent(option.q)
				text = text.replace('"', '')
				text = text.replace('"', '')
				let list = text.split("?")[1].split("&");
				for (let i = 0; i < list.length; i++) {
					let newList = list[i].split("=");
					if (newList[0] == 'mid') {
						self.mid = newList[1];
					} else if (newList[0] == 'sid') {
						self.sid = newList[1];
					} else if (newList[0] == 'pid') {
						self.pid = newList[1];
					} else if (newList[0] == 'pri') {
						self.pri = newList[1];
					}
				}
				this.getCommodityInfo();
			} else {
				uni.showToast({
					mask: true,
					icon: "none",
					position: "center",
					title: "请扫描正确的二维码",
					duration: 2000
				});
				setTimeout(() => {
					uni.navigateBack();
				}, 2000);
			}
		},
		onShow() {
			browserDetection();
		},
		methods: {
			async getCommodityInfo() {
				let obj = {
					mid:this.mid,
					sid:this.sid,
					pid:this.pid,
					pri:this.pri,
					token: uni.getStorageSync('token'),
				}
				const {
					data
				} = await getCommodityInfo(obj)
				if (data.code == 200) {
					this.$set(this, 'commodityData', data.data);
					this.$set(this, 'pageType', 1);
				}else{
					setTimeout(() => {
						uni.navigateBack();
					}, 2000);
				}
			},
			clickButton() {
				let self = this;
				const numA = safeToNumber(uni.getStorageSync('points'))
				const numB = safeToNumber(this.commodityData.price);
				if(numB>numA){
					uni.showToast({
						mask: true,
						icon: "none",
						position: "center",
						title: "积分不足",
						duration: 2000
					});
					return
				}
				uni.showModal({
					title: '提示',
					content: '确认兑换?',
					success: function(res) {
						if (res.confirm) {
							self.exchangeGoods();
						}
					}
				});
			},
			async exchangeGoods() {
				let obj = {
					mid:this.mid,
					sid:this.sid,
					pid:this.pid,
					pri:this.pri,
					token: uni.getStorageSync('token'),
					uid:uni.getStorageSync('userId')
				}
				const {
					data
				} = await exchangeGoods(obj)
				if (data.code == 200) {
					this.$set(this, 'state', 1);
					this.$set(this, 'pageType', 2);
				}
			},
			bottomButton(){
				if(this.state == 1){
					uni.redirectTo({
						url: '/pages/home',
					});
				}else{
					this.clickButton();
				}
			}
		}
	}
</script>

<style lang="stylus" scoped>
	.commodityInfo {
		flex: 1;
		display: flex;
		flex-direction: column;
		overflow: hidden;
		background: url(../static/back2.png);
		background-size: 100% 100%;

		.commodityInfo-page {
			flex: 1;
			display: flex;
			flex-direction: column;
			overflow: hidden;

			.flex-null-1 {
				flex: 1;
			}

			.flex-null-2 {
				flex: 4;
			}

			.product-content {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				padding: 60rpx 0;
				text-align: center;
				.product-img{
					width:200rpx;
					height:200rpx;
					margin:0 auto 60rpx;
				}
				.product-name {
					font-size: 40rpx;
					font-weight: 700;
					color: #222;
					line-height: 1.4;
					margin-bottom: 30rpx;
				}

				.product-slotNo {
					margin-top:40rpx;
					font-size: 48rpx;
					font-weight: 700;
					color: #0183FA;
					text-shadow: 0 2rpx 4rpx rgba(231, 76, 60, 0.2);
					animation: pulse 2s infinite alternate;
				}

				.product-price {
					font-size: 56rpx;
					font-weight: 700;
					color: #e74c3c;
					text-shadow: 0 2rpx 4rpx rgba(231, 76, 60, 0.2);
					animation: pulse 2s infinite alternate;
				}

				@keyframes pulse {
					0% {
						transform: scale(1);
					}

					100% {
						transform: scale(1.02);
					}
				}
			}

			.clickButton {
				width: 400rpx;
				line-height: 100rpx;
				background: #0183FA;
				border-radius: 10rpx;
				font-size: 36rpx;
				color: #fff;
				text-align center;
				margin: 140rpx auto 0;
			}

			.remind-img {
				width: 200rpx;
				height: 200rpx;
				margin:200rpx auto 0;
			}
			.commodity-message{
				text-align: center;
				color:#333;
				font-size:40rpx;
				margin-top:60rpx;
			}
		}
	}
</style>