<template>
	<view>
		<view class="imgshead"></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: ''
			}
		},
		created() {
			this.doShare();
		},
		onShow() {
			let that = this;
			uni.getStorage({
				key: 'shareNum',
				success(e) {
					that.shareNum = e.data;
				}
			})
			this.getOrderByOutTradeOrder();
		},
		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.storeId);
							} else {
								uni.showToast({
									title: res.return_msg,
									duration: 2000,
									icon: 'none'
								})
							}
						})
					},
				})
			},
			//查询门店活动
			getReceiveDiscountList(items) {
				let datas = {
					qrCodeSn: items
					// 1000017476
				}
				getReceiveDiscountList(datas).then(res => {
					if (res.return_code == '000000') {
						that.couponList = res.return_data;
					} else {
						uni.showToast({
							title: res.return_msg,
							duration: 2000,
							icon: 'none'
						})
					}
				})
			},
			//领取优惠券
			payReceiveDiscount() {
				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;
				}
				if (this.shareNum == 0) {
					uni.showToast({
						title: "分享活动得领取机会",
						duration: 2000,
						icon: 'none'
					})
					return;
				}
				let actividatas = [];
				for (var i = 0; i < this.couponList.length; i++) {
					let acid = {
						id: this.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'
						})
						this.shareNum = this.shareNum - 1
						uni.setStorage({
							key: "shareNum",
							data: this.shareNum
						})
					} else {
						uni.showToast({
							title: res.return_msg,
							duration: 2000,
							icon: 'none'
						})
					}
				})
			},
			//分享
			doShare() {
				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: ['onMenuShareTimeline',
								'onMenuShareAppMessage'
							] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
						});
						wx.ready(function() {
							wx.onMenuShareTimeline({
								title: '分享好友，立获大额优惠券', // 分享标题
								desc: '分享好友，立获大额优惠券', // 分享描述
								link: 'https://hsg.dctpay.com/hsgH5?accountId=0000013&key=&code=' +
									this.orderNo, // 分享链接
								imgUrl: 'https://hsg.dctpay.com/filesystem/wxApplets/logo.png', // 分享图标
								success: function(res) {
									// 支付成功后的回调函数
									uni.showToast({
										title: '分享成功',
										duration: 2000,
										icon: 'none'
									})
									this.shareNum = this.shareNum + 1;
									uni.setStorage({
										key: "shareNum",
										data: this.shareNum
									})
								},
								cancel: function(r) {},
								fail: function(res) {}
							});

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

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

<style lang="scss">
	.imgshead {
		background-image: url(https://hsg.dctpay.com/filesystem/wxApplets/share1.png);
		background-size: 100% 230px;
		height: 230px;
		background-repeat: no-repeat;
	}

	.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>
