<template>
	<view class="Verification">
		<image class="top-img" src="/static/images/hexiao_bg.png" mode="widthFix" />
		<view class="txt">
			<view class="title">扫描券码和权证 一键核销</view>
			<view class="des">降低核销门槛 <span class='dian'>·</span>低成本留存优质客户</view>
		</view>
		<view class="btn">
			<view class="goods" @click="scanCode('goods','get')"> 权证核销 </view>
			<view class="coupons" @click="scanCode('coupons','get')"> 优惠券核销 </view>
		</view>
		<view class="history" @click="goHistory">核销记录</view>
		<u-toast ref="uToast" />

		<!-- 二维码展示动画效果 -->
		<view class="showCode " v-if="showCode">
			<view class="showCodeBg" @click="showCode = false"></view>
			<view :class="['showCodeContent', showCode?'showCodeContentSelect':'showCodeContentSelectNo']">
				<view class="showCodeTitle"> {{scanType === 'goods'?'确认核销该权证':'确认核销该优惠券'}}</view>
				<view class="goodsInfo" v-if="scanType === 'goods'">
					<image class="left" :src="info.goods.cover" mode="widthFix" />
					<view class="right">
						<view class="title ellipsis-2">{{info.goods.goods_name}}</view>
						<view class="number">核销数量: X {{info.qty}}</view>
					</view>
				</view>
				<view class="goodsInfo" v-else>
					<view class="right">
						<view class="title ellipsis-2">优惠券名称：‘ {{info.coupon.title}} ’</view>
						<view class="number">
							优惠券类型：{{info.coupon.type.text}}
							<span style='padding-left: 20rpx;'>数量：1</span>
						</view>
						<view class="number">备注：{{info.coupon.remark}}</view>

					</view>
				</view>
				<view class="sureVer" @click="scanInfo(code,'post',)">确认核销</view>
				<view class="showCodeDes">请谨慎核销,操作成功后无法撤回</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		scanInfo
	} from '@/apis/interfaces/scan'
	export default {
		data() {
			return {
				scanType: 'goods', // coupons 优惠券核销  goods 权证核销
				showCode: false,
				info: '', // 权证核销返回前置
				code: '', // 扫码返回的code
			};
		},
		onLoad() {
			uni.hideLoading()
		},
		methods: {
			scanCode(e) {
				console.log(e)
				this.scanType = e
				uni.scanCode({
					success: (res) => {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						// uni.showLoading({
						// 	title: '核销请求中..'
						// })
						this.code = res.result
						this.scanInfo(res.result, 'get')
					}
				});
			},
			// 请求核销前置，优惠券前置和权证前置（get） post 是核销
			scanInfo(token, method) {
				console.log(token, this.scanType)
				let apiUrl = ''
				let data = {
					code: token
				}
				if (this.scanType === 'goods') {
					console.log('权证核销')
					apiUrl = 'manages/warrants/verification'
				} else {
					console.log('优惠券核销！')
					apiUrl = 'coupons/verify/coupon'
				}
				scanInfo(apiUrl, data, method).then(res => {
					if (this.scanType === 'goods') {
						uni.hideLoading()
						if (method === 'post') {
							this.showCode = false
							this.$refs.uToast.show({
								title: '权证核销成功',
								type: 'error',
								icon: false,
								duration: 3000
							})
						} else {
							this.showCode = true
							this.info = res
						}
					} else {
						console.log('优惠券核销 有商品跳转优惠券详情页面，没有商品直接弹出核销页面！')
						console.log(res, 'res')
						if (method === 'post') {
							this.showCode = false
							this.$refs.uToast.show({
								title: '优惠券核销成功',
								type: 'error',
								icon: false,
								duration: 3000
							})
						} else {
							if (res.goods.length > 0) {
								uni.showModal({
									title: '温馨提示',
									content: '该优惠券下绑定了商品，是否勾选相应商品',
									success: (res1) => {
										if (res1.confirm) {
											uni.navigateTo({
												url: '/pages/property/coupon/canUseList?code=' +
													this.code
											})
										} else {
											this.showCode = true
											this.info = res.info
										}
									}
								})
							} else {
								this.showCode = true
								this.info = res.info
							}
						}
					}
				}).catch(err => {
					this.$refs.uToast.show({
						title: err.message,
						type: 'error',
						icon: false,
						duration: 3000
					})
					this.showCode = false
				})
			},
			// 核销记录
			goHistory() {
				uni.navigateTo({
					url:'/pages/verification/history'
				})
			}
		}
	}
</script>

<style lang="scss">
	.Verification {
		width: 100%;
		min-height: 100vh;
		background-color: #fff;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		box-sizing: border-box;

		.top-img {
			margin-top: 100rpx;
		}

		.txt {
			padding-top: 120rpx;
			width: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			box-sizing: border-box;

			.title {
				font-size: 42rpx;
				font-weight: 500;
			}

			.des {
				font-size: 34rpx;
				color: rgba($color: #255a7c, $alpha: .4);
				padding-top: 20rpx;
				font-weight: bold;

				.dian {
					color: #039bfe;
					padding: 0 10rpx;
				}
			}
		}

		.btn {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: center;
			box-sizing: border-box;
			margin-top: 140rpx;

			.goods,
			.coupons {
				width: 310rpx;
				height: 90rpx;
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: center;
				box-sizing: border-box;
				border-radius: 8rpx;
				border: solid 1rpx #039bfe;
				color: #039bfe;
				font-size: 34rpx;
				font-weight: bold;
			}

			.coupons {
				background-color: #039bfe;
				color: #fff;
				margin-left: 30rpx;
			}
		}

		.history {
			text-align: center;
			padding: $padding;
			width: 100%;
			color: #cacaca;
		}
	}

	// 动画效果
	.showCode {
		width: 100%;
		height: 100vh;
		position: fixed;
		top: 0;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		box-sizing: border-box;
		z-index: 1;

		.showCodeBg {
			background-color: rgba($color:#000, $alpha: 0.3);
			width: 100%;
			height: 100%;
			position: absolute;
			top: 0;
			left: 0;
			z-index: 2;
		}

		.showCodeContentSelect {
			animation: sk-foldCubeAngle .6s linear both;
		}

		.showCodeContentSelectNo {
			animation: sk-foldCubeAngleNo .6s linear both;
		}

		.showCodeContent {
			padding: 50rpx 30rpx 30rpx 30rpx;
			width: 640rpx;
			min-height: 500rpx;
			background-color: #fff;
			border-radius: 20rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			box-sizing: border-box;
			padding: 30rpx;
			position: relative;
			z-index: 199;

			image {
				width: 60%;
			}

			.showCodeTitle {
				font-weight: bold;
				padding-bottom: 20rpx;
				font-size: 40rpx;
				margin-bottom: 20rpx;
				margin-top: 30rpx;
			}

			.goodsInfo {
				display: flex;
				flex-direction: row;
				align-items: center;
				justify-content: flex-start;
				box-sizing: border-box;
				margin: $margin;

				// background-color:red;
				.left {
					width: 200rpx;
					height: 200rpx;
					margin-right: 20rpx;
					border-radius: 2rpx;
				}

				.right {
					flex: 1;
					display: flex;
					flex-direction: column;
					align-items: flex-start;
					justify-content: center;
					box-sizing: border-box;

					.title {
						font-size: 30rpx;
						font-weight: bold;
					}

					.number {
						padding-top: 10rpx;
						color: #999;
					}
				}
			}

			.sureVer {
				background-color: #039bfe;
				width: 500rpx;
				height: 90rpx;
				border-radius: 8rpx;
				color: #fff;
				margin: 30rpx 0;
				font-size: 40rpx;
				font-weight: bold;
				text-align: center;
				line-height: 90rpx;
			}

			.showCodeDes {
				padding: 20rpx 0 20rpx 0;
				color: gray;
				font-size: 24rpx;
			}
		}

		@keyframes sk-foldCubeAngle {
			0% {
				-webkit-transform: perspective(140px) rotateX(-180deg);
				transform: perspective(140px) rotateX(-180deg);
				opacity: 0;
			}

			100% {
				-webkit-transform: perspective(140px) rotateX(0deg);
				transform: perspective(140px) rotateX(0deg);
				opacity: 1;
			}
		}

		@keyframes sk-foldCubeAngleNo {
			0% {
				transform: scale(1);
				opacity: 1;
			}

			25% {
				transform: scale(0);
				opacity: 0;
			}
		}

		@keyframes turn {
			0% {
				-webkit-transform: rotate(0deg);
				opacity: 1;
			}

			25% {
				-webkit-transform: rotate(90deg);
				opacity: .9;
			}

			50% {
				-webkit-transform: rotate(180deg);
				opacity: .8;
			}

			75% {
				-webkit-transform: rotate(270deg);
				opacity: .9;
			}

			100% {
				-webkit-transform: rotate(360deg);
				opacity: 1;
			}
		}

	}
</style>
