<template>
	<view class="wrap">
		<!-- 订单信息 -->
		<view class="order_message">
			<view class="message_title">订单信息</view>
			<view class="order_message_list">
				<view class="order_message_item clear">
					<view class="order_message_item_lf">服务项目</view>
					<view class="order_message_item_rh">平台认证</view>
				</view>
				<view class="order_message_item clear">
					<view class="order_message_item_lf">平台认证服务</view>
					<view class="order_message_item_rh">1年</view>
				</view>
			</view>
		</view>
		<!-- 支付方式 -->
		<view class="pay">
			<view class="message_title">支付方式</view>
			<view class="pay_method">
				<view class="iconfont weixin"></view>
				<view class="pay_method_title">微信支付</view>
				<view class="right_icon1"> <image src="https://wx.fujinyangche.com/nvrnetwork/images/select.png"></image></view>
			</view>
		</view>
		<!-- 优惠券 -->
		<view class="discounts">
			<view class="message_title" bindtap="todiscount">
				优惠券
				<view class="discounts_money">-¥{{disMoney}}.00<view class="iconfont more"></view></view>
			</view>
		</view>
		<!-- 金额 -->
		<view class="money">
			<view class="money_item clear">
				<view class="money_item_lf">金额</view>
				<view class="money_item_rg">¥300.00</view>
			</view>
			<view class="money_item clear">
				<view class="money_item_lf">优惠券</view>
				<view class="money_item_rg red">-¥{{disMoney}}.00</view>
			</view>
		</view>
		<!-- 最底部 -->
		<view class="footer">
			<view class="now_money">实付 <view class="now_money_i">¥{{(300 - disMoney)}}.00</view></view>
			<view class="now_pay" @tap="pay">立即支付</view>
		</view>
	</view>
</template>

<script>
	import api from "../../../../api.js";
	let app = getApp();
	export default {
		data() {
			return {
				time:'',//下单时间
				disMoney:60,//优惠券金额，
				ids:'',//选择优惠券id集合
				timeStamp:'',//下单时间戳
				nonceStr:'',//随机字符串
				package:'',//下单支付id
				paySign:'',//签名
				orderNumber:'',//订单编号
				flag:true,//避免重复下单
			}
		},
		onLoad() {
			this.getTime();
			this.getDiscount();
		},
		onShow() {
			const pages = getCurrentPages();
			const currPage = pages[pages.length - 1];  // 当前页
			this.disMoney = currPage.disMoney;
			this.ids = currPage.ids;
		},
		methods: {
			//获取下单时间
			getTime(){
				let data = {
					token:app.globalData.token
				}
				api.getUserEnterTime(data).then((res)=>{
					console.log(res);
					this.time = res.data.data;
				})
			},
			//使用默认优惠券
			getDiscount(){
				let data = {
				    token:app.globalData.token,
				}
				api.getUserDiscount(data).then((res)=>{
					console.log(res);
					this.ids = res.data.data.ids;
					this.disMoney = res.data.data.total;
				})
			},
			//立即支付
			pay(){
				let data = {
					token:app.globalData.token,
					itemService:'平台认证',
					serviceTime: this.time,
					year:'1',
					money:300,
					discounts:this.disMoney,
					pay:300 - this.disMoney,
					ids:this.ids,//优惠券id集合
				}
				api.getUserPay(data).then((res)=>{
					console.log(res);
					if(this.flag){
						this.flag = false;
						//当优惠金额不等于300的时候
						if(res.data.data.sub_order.timeStamp !== ''){
							uni.requestPayment({
								orderNumber: res.data.order.orderNumber,
								timeStamp: res.data.sub_order.timeStamp,
								nonceStr: res.data.sub_order.nonceStr,
								package: res.data.sub_order.package,
								signType: res.data.sub_order.signType,
								paySign: res.data.sub_order.paySign,
								success(res) {
									// uni.redirectTo({
									//   url: '../pay_success/pay_success?orderNumber=' + orderNumber + '&ids=' + ids,
									// })
								},
								fail(res) {
									uni.showToast({
										icon:"none",
										duration:2000,
										title:res.data.msg
									})
								}
							})
						}else{
							//当优惠金额等于300的时候  直接跳转支付成功页面
							// uni.redirectTo({
							//     url: '../pay_success/pay_success?orderNumber=' + orderNumber + '&ids=' + ids,
							// })
						}
					}
				})
			}
		},
		
	}
</script>

<style>
	page{
	  width:100%;
	  height:100%;
	  background-color: #f7f7f7;
	}
	.clear:after{
		content: "";
		display: block;
		clear: both;
	}
	.order_message,.pay,.discounts{
	  width:100%;
	  margin-top:20rpx;
	  background-color: #ffffff;
	}
	.message_title{
	  height:100rpx;
	  line-height: 100rpx;
	  border-bottom:1px solid #f2f2f2;
	  font-size: 32rpx;
	  color:#222222;
	  text-indent: 30rpx;
	  position: relative;
	}
	.order_message_list{
	  width:100%;
	  padding:40rpx 30rpx 34rpx 30rpx;
	  box-sizing: border-box;
	}
	.order_message_item{
	  width:100%;
	  margin-bottom: 38rpx;
	}
	.order_message_item:last-child{
	  margin-bottom: 0;
	}
	.order_message_item_lf{
	  font-size: 30rpx;
	  color:#666666;
	  float:left;
	}
	.order_message_item_rh{
	  font-size: 30rpx;
	  color:#222222;
	  float:right;
	  font-weight: bold;
	}
	.pay_method{
	  height:98rpx;
	  line-height: 98rpx;
	  position: relative;
	}
	.weixin{
	  color:#41B035;
	  font-size: 44rpx;
	  position: absolute;
	  top:0rpx;
	  left:30rpx;
	}
	.pay_method_title{
	  font-size: 28rpx;
	  color:#666666;
	  display: inline-block;
	  text-indent: 84rpx;
	}
	.right_icon1{
	  width:40rpx;
	  height:40rpx;
	  position: absolute;
	  right:30rpx;
	  top:30rpx
	}
	.right_icon1 image{
	  display: block;
	  width:100%;
	  height:100%;
	}
	.discounts_money{
	  font-size: 32rpx;
	  color:#222222;
	  font-weight: bold;
	  position: absolute;
	  top:0rpx;
	  right:50rpx;
	}
	.more{
	  font-size: 22rpx;
	  color:#999999;
	  position: absolute;
	  top:-2rpx;
	  right:-20rpx;
	}
	.message_title1{
	  height:98rpx;
	  line-height: 98rpx;
	  font-size: 28rpx;
	  color:#666666;
	  text-indent: 30rpx;
	}
	.money{
	  width:100%;
	  padding:32rpx 30rpx 34rpx 30rpx;
	  box-sizing: border-box;
	  margin-top:20rpx;
	  background-color: #ffffff;
	}
	.money_item{
	  margin-bottom: 38rpx;
	}
	.money_item:last-child{
	  margin-bottom: 0;
	}
	.money_item_lf{
	  font-size: 30rpx;
	  color:#666666;
	  float:left;
	}
	.money_item_rg{
	  float:right;
	  font-size: 30rpx;
	  color:#222222;
	  font-weight: bold;
	}
	.red{
	  color:#E21A25;
	  font-weight: bold;
	}
	.footer{
	  width:100%;
	  height:100rpx;
	  line-height: 100rpx;
	  background:rgba(255,255,255,1);
	  box-shadow:0px 4rpx 8rpx 0px rgba(0,0,0,0.5);
	  position: fixed;
	  bottom:0;
	  left:0;
	}
	.now_money{
	  font-size: 28rpx;
	  color:#222222;
	  text-indent: 30rpx;
	  float:left;
	  position: relative;
	}
	.now_money_i{
	  color:#E11521;
	  position: absolute;
	  top:2rpx;
	  right:-120rpx;
	}
	.now_pay{
	  width:284rpx;
	  height:72rpx;
	  background:rgba(231,120,23,1);
	  border-radius:6rpx;
	  text-align: center;
	  line-height: 72rpx;
	  color:#ffffff;
	  font-size: 30rpx;
	  float:right;
	  margin:14rpx 30rpx 0 0;
	}
</style>
