<template>
	<view class="hb column">
		<view class="justify-between align-center p44 m20 colorf bgmap" >
			<view class="justify-center">
				<view>
					<view class="fs48 bold">￥{{DirectDetail.couponPrice}}</view>
				</view>
				<view class="ml60">
					<view>{{DirectDetail.couponName}}</view>
					<view class="fs24 mt16">可与优惠卷叠加使用</view>
				</view>
			</view>
			<!-- <view class="share-box" v-if="companytoken!=null">
				<view class="bgf base-price plr28 ptb12 radius40">分享</view>
				<button  class="share" data-name="shareBtn" open-type="share">分享</button>
			</view> -->
			<view class="bgf base-price plr18 ptb12 radius40" @click="payDic">￥{{DirectDetail.couponPrice}}购买</view>
		</view>
		<view>
			<u-modal :show="showpay" :title="title" :content='content' @confirm='tocoupon'></u-modal>
		</view>
	</view>
</template>

<script>
	import { payCashCoupon} from '@/api/coupon'
	import { loginByMiniApp} from '@/api/user'
	export default {
		data(){
			return{
				imageMap: 'https://czwh.obs.cn-southwest-2.myhuaweicloud.com/app/orangeShop/Direct-sel.png',
				DirectDetail:[],
				companyUserId:'',
				companytoken:uni.getStorageSync('CompanyUserToken'),
				showpay:false,
				title:'支付成功',
				content:'立减金购买成功，请到我的-优惠卷（立减金）查看立减金！',
				companyId:""
			}
		},
		//发送给朋友
		onShareAppMessage() {
			return {
				title: this.DirectDetail.nmae,
				path: '/pages_company/order/DirectDetail?companyUserId='+this.companyUserId
					+"&item="+JSON.stringify(this.DirectDetail)
			}
		},
		onLoad(option) {
			this.companyUserId=option.companyUserId
			this.companyId=option.companyId
			console.log(JSON.parse(option.item))
			this.DirectDetail=JSON.parse(option.item)
			if(!uni.getStorageSync('AppToken')||uni.getStorageSync('AppToken')==null){
				this.gologin()
			}
		},
		methods:{
			gologin(){
				let provider = 'weixin'
				uni.login({
					provider: provider,
					success: async loginRes => {
						console.log(loginRes)
						uni.getUserInfo({
						   provider: provider,
						   success: (infoRes)=> {
							    uni.showToast({
									title: '处理中...',
									icon: 'loading'
							    });
								loginByMiniApp({
									code: loginRes.code,
									encryptedData:infoRes.encryptedData,
									iv:infoRes.iv,
									appId:wx.getAccountInfoSync().miniProgram.appId,
								}).then(res=>{
									 uni.hideLoading();
									 if (res.code == 200) {
										 uni.setStorageSync('AppToken',res.token);
										 uni.setStorageSync('userInfo',JSON.stringify(res.user));
									 } else {
										uni.showToast({
											title: res.msg,
											icon: 'none'
										});
									 }
								 })
						   }
						});
					}
				})
			},
			payDic(){
				const data={
					id:this.DirectDetail.id,
					couponId:this.DirectDetail.couponId,
					companyId:this.companyId,
					companyUserId:this.companyUserId,
					userId:uni.getStorageSync('userInfo').userId
				}
				payCashCoupon(data).then(res=>{
					let that =this
					if(res.code==200){
						console.log(res)
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.result.timeStamp,
							nonceStr: res.result.nonceStr,
							package: res.result.packageValue,
							signType: res.result.signType,
							paySign: res.result.paySign,
							success: function(res) {
								uni.hideLoading();
								console.log(that.showpay)
								that.showpay=true
								uni.showToast({
									icon:'success',
									title: "支付成功",
								});
							},
							fail: function(err) {
								console.log('fail:' + JSON.stringify(err));
								uni.hideLoading();
								uni.showToast({
									icon:'none',
									title: '用户取消支付！',
								});
								console.log(that.showpay)
							}
						});
					}else{
						uni.showToast({
							icon:'none',
							title: res.msg,
						});
					}
				})
			},
			tocoupon(){
				uni.navigateTo({
					url:'/pages_shopping/shopping/myCoupon?tabsact='+3
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.bgmap{
		background: url('https://czwh.obs.cn-southwest-2.myhuaweicloud.com/app/orangeShop/Direct-sel.png');
		background-size: cover;
	}
	.share-box{
		width: 120upx;
		height: 46upx;
		border: 1px solid #FC581C;
		border-radius: 23upx;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		.share{
			display: inline-block;
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			opacity: 0;
		}
	}
	
</style>