<template>
	<view class="center">


		<view class='wrapper'>
			<view class='item'>
				<view>支付方式</view>
				<view class='list'>
					<!-- #ifdef H5 -->
					<view class='payItem acea-row row-middle' :class='active==index ?"on":""' @tap='payItem(index)'
						v-for="(item,index) in cartArr" :key='index' v-if="item.payStatus==1">
						<view class='name acea-row row-center-wrapper'>
							<view class='iconfont animated'
								:class='(item.icon) + " " + (animated==true&&active==index ?"bounceIn":"")'></view>
							{{item.name}}
						</view>
						<view class='tip'>
							{{item.title}}
							<block v-if="item.value == 'balance'">
								{{userInfo.now_money}}
							</block>
						</view>
					</view>
					<!-- #endif -->
					<!-- #ifdef MP || APP-PLUS -->
					<view class='payItem acea-row row-middle' :class='active==index ?"on":""' @tap='payItem(index)'
						v-for="(item,index) in cartArr" :key='index' v-if="item.payStatus==1">
						<view class='name acea-row row-center-wrapper'>
							<view class='iconfont animated'
								:class='(item.icon) + " " + (animated==true&&active==index ?"bounceIn":"")'></view>
							{{item.name}}
						</view>
						<view class='tip'>
							{{item.title}}
							<block v-if="item.value == 'balance'">
								{{userInfo.now_money}}
							</block>
						</view>
					</view>
					<!-- #endif -->
				</view>
			</view>
		</view>
		<view class="settel">
			<view class='settel-t'>
				<view>商品总价：</view>
				<view class='conter'>￥ {{couponData.total_price}}</view>
			</view>
		</view>
		<view class="settel" v-if="(this.be_overdue == 0 || this.be_overdue < 0) &&couponData.total_postage>0">
			<view class='settel-t'>
				<view>利息合计:</view>
				<view class="conter">
					<view class='con_text'>￥ {{couponData.total_postage}}</view>
					<view class='con_text' style="color: #F84221;" v-if="couponData.pay_type == 8">
						已逾期{{this.be_overday}}天</view>
				</view>

			</view>
		</view>
		<view class='footer acea-row row-between-wrapper'>
			<view class="footer_count">
				<view>
					合计:
					<text class='conter' v-if="cartArr[4].payStatus == 1">￥0.00</text>
					<text class='conter' v-else>￥{{couponData.pay_price}}</text>
				</view>
				<!-- <view style="font-size: 14px;color: #F84221;" v-if="this.be_overdue == 0 || this.be_overdue < 0 ">
          可结算周期到期后再付款￥{{totalPrice}}
        </view> -->
			</view>
			<view class='settlement' @tap="SubOrder">
				提交订单
			</view>
		</view>
	</view>
</template>

<script>
	import {

		getUserInfo,

	} from '@/api/user.js';
	import {
		groupOrderDetail,
		orderPay
	} from '@/api/order.js'
	import {
		postsettle,
	} from '@/api/admin.js'
	export default {
		data() {
			return {
				animated: false,
				totalPrice: 0,
				couponData: {},
				active: 0, //支付方式切换	,
				userInfo: {}, //用户信息
				cartArr: [{
						"name": "微信支付",
						"icon": "icon-weixin2",
						value: 'weixin',
						title: '微信快捷支付',
						payStatus: 1,
					},
					{
						name: "支付宝支付",
						icon: "icon-icon34",
						// #ifdef H5 || APP-PLUS
						value: 'alipay',
						// #endif
						// #ifdef MP
						value: 'alipayQr',
						// #endif
						title: '支付宝支付',
						payStatus: this.$store.getters.globalData.alipay_open
					},
					{
						"name": "余额支付",
						"icon": "icon-icon-test",
						value: 'balance',
						title: '可用余额:',
						payStatus: this.$store.getters.globalData.yue_pay_status,
					},
					{
						"name": "线下支付",
						"icon": "icon-yinhangqia",
						value: 'offline',
						title: '线下支付',
						payStatus: 2,
					}, {
						"name": "先货后款",
						"icon": "tan-a-lujing17324",
						value: 'creditBuy',
						title: `结算周期：30天 `,
						label: '日利率：0.05%',
						order_id: '',
					},
				],
				order_id: '',
				product_type: 0,
				mer_id: '',
				be_overdue: '', //逾期时间
				be_overday: '', //逾期天数
			};
		},
		onLoad(options) {
			this.payType = 'weixin'
			this.order_id = options.order_id
			if (options.product_type) {
				this.product_type = options.product_type
			}
			this.$nextTick(() => {
				this.getOrderInfo()
				this.getUserInfo()
			})
		},
		methods: {
			payItem: function(e) {
				let that = this;
				let active = e;
				that.active = active;
				that.animated = true;
				that.payType = that.cartArr[active].value;
			},
			getUserInfo() {
				getUserInfo().then(res => {
					this.userInfo = res.data
					this.mer_id = res.data.service.mer_id
				})
			},
			getOrderInfo() {
				groupOrderDetail(this.order_id, this.product_type).then(res => {
					console.log(res)
					this.couponData = res.data
					this.$nextTick(() => {
						if (this.couponData.interest !== null) {
							this.moerTime(this.couponData.interest.start_time)
						}
					})
				})
			},
			SubOrder() {
				if (this.couponData.pay_type == 8) {
					postsettle(
						this.mer_id, {
							id: this.order_id,
							pay_type: this.payType
						}
					).then(res => {
						if (res.data.paid === false) {
							//拉起微信支付

						}
						if (res.message == 'success') {
							uni.showToast({
								title: '付款成功'
							})
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								})
							}, 1000)
						}
					}).catch(err => {
						
						uni.showToast({
							title: err.message
						})
					})
				} else {
					orderPay(this.order_id, {
						type: this.payType
					}).then(res => {
						let jsConfig = res.data.result.config
						if (res.data.status === 'weixinApp') {
							//拉起微信支付
							// #ifdef APP-PLUS
							let mp_pay_name = ''
							if (uni.requestOrderPayment) {
								mp_pay_name = 'requestOrderPayment'
							} else {
								mp_pay_name = 'requestPayment'
							}
							uni[mp_pay_name]({
								provider: 'wxpay',
								orderInfo: jsConfig,
								success: (e) => {
									uni.navigateBack({
										delta: 1
									})

								},
								fail: (e) => {
									// 暂不跳转
									let url = '/pages/order_pay_status/index?order_id=' + orderId +
										'&msg=取消支付' + '&product_type=' + that.product_type;
									return that.$util.Tips({
										title: '取消支付',
									}, {
										tab: 4,
										url: url
									});
								},
								complete: () => {
									// 暂不跳转
									let url = '/pages/order_pay_status/index?order_id=' + orderId +
										'&msg=取消支付' + '&product_type=' + that.product_type;
									return that.$util.Tips({
										title: '取消支付',
									}, {
										tab: 4,
										url: url
									});
								},
							});
							// #endif

						}
						// setTimeout(() => {
						// 	uni.navigateBack({
						// 		delta: 1
						// 	})
						// }, 1000)
						uni.showToast({
							title: res.message
						})
					})
				}

			},
			//计算是否逾期
			moerTime(autoTime) {
				var date1 = (Date.parse(new Date())) / 1000;
				var date2 = (Date.parse(new Date(autoTime))) / 1000;
				var date3 = (date2 - date1) * 1000;
				this.be_overdue = Math.floor(date3 / (24 * 3600 * 1000));
				if (this.be_overdue == 0 || this.be_overdue < 0) {
					var date1 = (Date.parse(new Date())) / 1000;
					var date2 = (Date.parse(new Date(autoTime))) / 1000;
					var date3 = (date1 - date2) * 1000;
					this.be_overday = Math.floor(date3 / (24 * 3600 * 1000));
				}
			}

		}
	}
</script>

<style lang="scss" scoped>
	.center {
		.conter {
			color: red;
			font-weight: bold;
		}

		.wrapper .item .discount .placeholder {
			color: #ccc;
			text-align: right;
		}

		.wrapper {
			margin-bottom: 12rpx;
			background-color: #fff;
			padding-top: 20px;
		}

		.wrapper .item {
			padding: 27rpx 30rpx;
			font-size: 30rpx;
			color: #282828;
		}

		.wrapper .item .discount .iconfont {
			color: #515151;
			font-size: 30rpx;
			margin-left: 15rpx;
		}

		.wrapper .item .discount .num {
			font-size: 32rpx;
			margin-right: 20rpx;
		}

		.wrapper .item .shipping {
			font-size: 30rpx;
			color: #999;
			position: relative;
			padding-right: 58rpx;
		}

		.wrapper .item .shipping .iconfont {
			font-size: 35rpx;
			color: #707070;
			position: absolute;
			right: 0;
			top: 50%;
			transform: translateY(-50%);
			margin-left: 30rpx;
		}

		.wrapper .item input {
			flex: 1;
			height: 100%;
			margin-left: 20rpx;
			text-align: right;
		}

		.wrapper .item .placeholder {
			color: #ccc;
		}

		.wrapper .item .list {
			margin-top: 35rpx;
		}

		.wrapper .item .list .payItem {
			border: 1px solid #eee;
			border-radius: 6rpx;
			height: 86rpx;
			width: 100%;
			box-sizing: border-box;
			margin-top: 20rpx;
			font-size: 28rpx;
			color: #282828;
		}

		.wrapper .item .list .payItem.on {
			border-color: var(--view-theme);
			color: var(--view-theme);
		}

		.wrapper .item .list .payItem .name {
			width: 50%;
			text-align: center;
			border-right: 1px solid #eee;
			justify-content: left;
			padding-left: 80rpx;
		}

		.wrapper .item .list .payItem .name .iconfont {
			width: 44rpx;
			height: 44rpx;
			border-radius: 50%;
			text-align: center;
			line-height: 44rpx;
			background-color: #fe960f;
			color: #fff;
			font-size: 30rpx;
			margin-right: 15rpx;
		}

		.wrapper .item .list .payItem .name .iconfont.icon-weixin2 {
			background-color: #41b035;
		}

		.wrapper .item .list .payItem .name .iconfont.icon-icon34 {
			background-color: #4295D5;
		}

		.wrapper .item .list .payItem .tip {
			width: 49%;
			text-align: center;
			font-size: 26rpx;
			color: #aaa;
		}

		.footer {
			width: 100%;
			height: 100rpx;
			height: calc(100rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
			height: calc(100rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
			background-color: #fff;
			padding: 0 30rpx;
			font-size: 28rpx;
			color: #333;
			box-sizing: border-box;
			position: fixed;
			bottom: 0;
			left: 0;
			z-index: 30;

			.footer_count {
				font-size: 28rpx;
			}

			.coupon_price {
				color: #999999;
				font-size: 20rpx;
				margin-top: 10rpx;

				text {
					color: #282828;
					padding: 2rpx 10rpx;
					background: #F5F5F5;
					border-radius: 26rpx;
					margin-left: 20rpx;
					line-height: 30rpx;
					height: 30rpx;
					display: inline-block;
				}
			}

			.settlement {
				font-size: 30rpx;
				color: #fff;
				width: 240rpx;
				height: 70rpx;
				background: linear-gradient(90deg, #F98649 0%, #F34E45 100%);
				border-radius: 50rpx;
				text-align: center;
				line-height: 70rpx;

				&.disabled {
					background-color: #cccccc;
				}
			}

			.transparent {
				opacity: 0
			}

		}

		.settel {
			width: 100%;
			background-color: #fff;
			height: 70px;
			padding: 0 15px 0 15px;
			line-height: 70px;

			.settel-t {
				width: 100%;
				display: flex;
				justify-content: space-between;
				align-items: center;
			}

			.con_text {
				display: flex;
				justify-content: flex-end;
				height: 20px;
				margin: auto;
				line-height: 30px;
			}


		}

	}
</style>