<template>
	<view>
		<!-- v-if="partakeWay == 2" -->
		<view class="imgshead backroundimgfit"></view>
		<!-- <view class="imgshead backroundimgsed" v-if="partakeWay == 3"></view> -->
		<view class="imgscontent">
			<view class="conts paddtop13">
				<view v-for="(item,index) in couponList" :key="index" class="conpon border-r height90 flleft marb10 ">
					<view class="width94 font15 fontwig6 fotlt fcorfff mart5">
						{{item.name}}
					</view>
					<view class="width80 fotct backcor99 mart5 linec6" style="opacity: 0.2;"></view>

					<view class="width90 font10 fotlt fcorfff mart3" v-if="item.discountType == 1">
						满{{item.discountCondition}}-{{item.discountPrice}}元
					</view>
					<view class="width90 font10 fotlt fcorfff mart3" v-if="item.discountType == 2">
						立减{{item.discountPrice}}
					</view>
					<view class="width90 font10 fotlt fcorfff mart3" v-if="item.discountType == 3">
						享受{{item.discountPrice}}折
					</view>
					<view class="width90 font10 fotlt fcorfff mart3">
						截止时间: {{item.createTime | timeFormat('yyyy-mm-dd')}}
					</view>
				</view>
			</view>
		</view>
		<view class="imgsfoot">
			<view class="width100 height50"></view>
			<view class="couponBtn fontlet font24 fotct fontwig6 width60w  height45l" @click="payReceiveDiscount">一键领取
			</view>
		</view>

	</view>
</template>

<script>
	import {
		getReceiveDiscountList,
		payReceiveDiscount,
		verifyWx,
		getOrderByOutTradeOrder
	} from '../../Utils/Api.js'
	let app = getApp();
	// #ifdef H5
	var wx = require('weixin-js-sdk');
	// #endif
	export default {
		data() {
			return {
				imagewxUrl: app.globalData.imageWxImg,
				couponList: [],
				shareNum: 0, //次数,
				orderNo: '', //订单号
				partakeWay: '', //区分类型
				shareLink: '', // 分享链接
				qrCodeSn: '' //二维码编号
			}
		},
		created() {
			// if (app.globalData.accountType == '0000012&key') {
			this.doShare();
			// }
		},
		onShow() {
			let that = this;
			uni.setNavigationBarTitle({
				title: '分享领取优惠券'
			})
			if (app.globalData.accountType == '0000012&key') {
				uni.removeStorage({
					key: 'shareNum'
				});
				that.partakeWay = 2;
				that.getOrderByOutTradeOrder();
			}
			if (app.globalData.accountType == '0000014&key') {
				that.partakeWay = 3;
				uni.getStorage({
					key: 'staffCode',
					success: (res) => {
						that.qrCodeSn = res.data;
						that.getReceiveDiscountList(res.data);
					},
				})
			}
		},
		methods: {
			//查询订单详情
			getOrderByOutTradeOrder() {
				let that = this;
				uni.getStorage({
					key: 'staffCode',
					success: (res) => {
						that.orderNo = res.data;
						let datas = {
							outTradeOrder: that.orderNo
						}
						getOrderByOutTradeOrder(datas).then(res => {
							if (res.return_code == '000000') {
								that.getReceiveDiscountList(res.return_data.order.qrCodeSn);
							} else {
								uni.showToast({
									title: res.return_msg,
									duration: 2000,
									icon: 'none'
								})
							}
						})
					},
				})
			},
			//查询门店活动
			getReceiveDiscountList(items) {
				let datas = {
					qrCodeSn: items,
					partakeWay: this.partakeWay
					// 1000017476
				}
				getReceiveDiscountList(datas).then(res => {
					if (res.return_code == '000000') {
						this.couponList = res.return_data;
					} else {
						uni.showToast({
							title: res.return_msg,
							duration: 2000,
							icon: 'none'
						})
					}
				})
			},
			//领取优惠券
			payReceiveDiscount() {
				let that = this;
				// if (that.partakeWay == 2) {
					uni.getStorage({
						key: 'shareNum',
						success(e) {
							if (e.data) {
								that.shareNum = e.data;
							} else {
								that.shareNum = 0;
							}
						}
					})
				// }

				if (app.globalData.userInfo.phone == '' || app.globalData.userInfo.phone == null || app.globalData.userInfo
					.phone == undefined) {
					uni.showModal({
						title: '温馨提示',
						content: '前往获取手机号',
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/login/register'
								})
							}
						}
					})
					return;
				}
				// && that.partakeWay == 2
				if (that.shareNum == 0 ) {
					uni.showToast({
						title: "分享活动得领取机会",
						duration: 2000,
						icon: 'none'
					})
					return;
				}
				let actividatas = [];
				for (var i = 0; i < that.couponList.length; i++) {
					let acid = {
						id: that.couponList[i].id
					};
					actividatas.push(acid);
				}
				let datas = {
					"phone": app.globalData.userInfo.phone,
					"activityIds": actividatas
				}
				payReceiveDiscount(datas).then(res => {
					if (res.return_code == '000000') {
						uni.showToast({
							title: res.return_data,
							duration: 2000,
							icon: 'none'
						})
						// if (that.partakeWay == 2) {
							that.shareNum--;
							uni.setStorage({
								key: "shareNum",
								data: that.shareNum
							})
						// }

					} else {
						uni.showToast({
							title: res.return_msg,
							duration: 2000,
							icon: 'none'
						})
					}
				})
			},
			//分享
			doShare() {
				let that = this;
				if (app.globalData.accountType == '0000012&key') {
					that.shareLink = 'https://hsg.dctpay.com/hsgH5?accountId=0000013&key=&code=' + that.orderNo;
				} else {
					that.shareLink = 'https://hsg.dctpay.com/hsgH5?accountId=0000014&key=&code=' + that.qrCodeSn;
				}
				let theUrl = window.location.href.split('#')[0];
				let datas = {
					url: theUrl
				}
				verifyWx(datas).then(res => {
					if (res.return_code == '000000') {
						wx.config({
							debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
							appId: res.return_data.appId, // 必填,公众号的唯一标识
							timestamp: res.return_data.timestamp, // 必填,生成签名的时间戳
							nonceStr: res.return_data.nonceStr, // 必填,生成签名的随机串
							signature: res.return_data.signature, // 必填,签名,见附录1
							jsApiList: [
								'onMenuShareAppMessage'
							] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
						});
						wx.ready(function() {
							// wx.onMenuShareTimeline({
							// 	title: '分享好友,立获大额优惠券', // 分享标题
							// 	desc: '分享好友,立获大额优惠券', // 分享描述
							// 	link: that.shareLink, // 分享链接
							// 	imgUrl: 'https://hsg.dctpay.com/filesystem/wxApplets/logo.png', // 分享图标
							// 	success: function(res) {
							// 		// 支付成功后的回调函数
							// 		uni.showToast({
							// 			title: '分享成功',
							// 			duration: 2000,
							// 			icon: 'none'
							// 		})
							// 		that.shareNum++;
							// 		uni.setStorage({
							// 			key: "shareNum",
							// 			data: that.shareNum
							// 		})
							// 	},
							// 	cancel: function(r) {},
							// 	fail: function(res) {}
							// });

							wx.onMenuShareAppMessage({
								title: '分享朋友圈,立获大额优惠券', // 分享标题
								desc: '分享朋友圈,立获大额优惠券', // 分享描述
								link: that.shareLink, // 分享链接
								imgUrl: 'https://hsg.dctpay.com/filesystem/wxApplets/logo.png', // 分享图标
								success: function(res) {
									// 支付成功后的回调函数
									uni.showToast({
										title: '分享成功',
										duration: 2000,
										icon: 'none'
									})
									that.shareNum++;
									uni.setStorage({
										key: "shareNum",
										data: that.shareNum
									})
								},
								cancel: function(r) {},
								fail: function(res) {}
							});
						});

					} else {
						uni.showToast({
							title: res.return_msg,
							duration: 2000,
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.imgshead {
		background-size: 100% 230px;
		height: 230px;
		background-repeat: no-repeat;
	}

	.backroundimgfit {
		background-image: url(https://hsg.dctpay.com/filesystem/wxApplets/share1.png);
	}

	.backroundimgsed {
		background-image: url(https://hsg.dctpay.com/filesystem/wxApplets/share4.png);
	}

	.imgscontent {
		background-image: url(https://hsg.dctpay.com/filesystem/wxApplets/share2.png);
		background-size: 100% 320px;
		height: 320px;
		background-repeat: no-repeat;

		.conts {
			width: 74%;
			margin-left: 13%;
		}
	}

	.imgsfoot {
		background-image: url(https://hsg.dctpay.com/filesystem/wxApplets/share3.png);
		background-size: 100% 250px;
		height: 250px;
		background-repeat: no-repeat;
	}


	.conpon {
		background: linear-gradient(to bottom, #fe8e3f, #f14f21);
		width: 48%;
		margin-right: 1%;
		margin-left: 1%;
	}

	.couponBtn {
		letter-spacing: 3px;
		color: #ff482d;
		border-radius: 25px;
		background: linear-gradient(to right, #fea03c, #f7dd6a);
	}
</style>