<template>
	<view class="padding-t-30">
		<!-- 地址 -->
		<navigator url="/pages/set/address?source=1" class="address-section">
			<view class="order-content" v-if="addressData.real_name">
				<view class="cen">
					<view class="top">
						<text class="name">{{ addressData.real_name }}</text>
						<text class="mobile">{{ addressData.phone }}</text>
					</view>
					<text
						class="address flex">{{ addressData.province + addressData.city + addressData.district }}{{ addressData.detail }}</text>
				</view>
				<text class="iconfont iconenter"></text>
			</view>
			<view class="order-content" v-if="!addressData.real_name">
				<view class="addAddress">
					<text class="iconfont iconaddition"></text>
					<text>添加收货地址</text>
				</view>
			</view>
		</navigator>
		<view class="goodsList">

			<view class="goods-section" v-for="(ls, ind) in shopList" :key="ind">
				<!-- 商品列表 -->
				<view class="g-item">
					<image :src="ls.productInfo.image"></image>
					<view class="right">
						<text class="title clamp">{{ ls.productInfo.store_name }}</text>
						<text class="spec">{{ ls.productInfo.attrInfo ? ls.productInfo.attrInfo.suk : '默认' }}</text>
						<view class="price-box">
							<text
								class="price">￥{{ ls.productInfo.attrInfo ? ls.productInfo.attrInfo.price : ls.productInfo.price }}</text>
							<text class="number">{{ 'x ' + ls.cart_num + (ls.productInfo.unit_name || '') }}</text>
							<!-- <view class="number">
							<uni-number-box class="step" :value="lss.number" :index="indx" @eventChange="numberChange"></uni-number-box>
						</view> -->
						</view>
						<view class="serverSet" v-if="ls.day>0">
							（<text>服务费：{{ls.day_deducted}}</text>
							<text class="margin-l-10">服务天数：{{ls.day}}天</text>）
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 金额明细 -->
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">运费</text>
				<text class="cell-tip disabled">{{ Postage }}</text>
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">备注</text>
				<input class="desc" type="text" v-model="desc" placeholder="请填写备注信息" placeholder-class="placeholder" />
			</view>
		</view>


		<view class="yt-list">
			<view class="yt-list-cell b-b" v-if="fx" @click="payType='weixin'">
				<view class="cell-tit flex">
					<image class="orderIcon" src="../../static/icon/orderWx.png" mode="widthFix"></image>
					<text class="margin-l-10">微信支付</text>
				</view>
				<image class="checked" v-if="payType=='weixin'" src="../../static/icon/addressIconXz.png"
					mode="widthFix"></image>
				<view v-else class="noChecked"></view>
			</view>
			<!-- #ifdef APP-PLUS -->
			<view class="yt-list-cell b-b" @click="payType='ali'">
				<view class="cell-tit flex">
					<image class="orderIcon" src="../../static/icon/orderAli.png" mode="widthFix"></image>
					<text class="margin-l-10">支付宝</text>
				</view>
				<image class="checked" v-if="payType=='ali'" src="../../static/icon/addressIconXz.png" mode="widthFix">
				</image>
				<view v-else class="noChecked"></view>
			</view>
			<!-- #endif -->
			<view class="yt-list-cell b-b" @click="payType='yue'">
				<view class="cell-tit flex">
					<image class="orderIcon" src="../../static/icon/ye.png" mode="widthFix"></image>
					<text class="margin-l-10">余额({{now_money}})</text>
				</view>
				<image class="checked" v-if="payType=='yue'" src="../../static/icon/addressIconXz.png" mode="widthFix">
				</image>
				<view v-else class="noChecked"></view>
			</view>
		</view>


		<!-- 底部 -->
		<view class="footer">
			<view class="price-content">
				<text>实付款</text>
				<text class="price-tip">￥</text>
				<text class="price">{{ payPrice }}</text>
			</view>
			<text class="submit" :class="{submitNo:payLoding}" @click="payLoding?'':submit()">提交订单</text>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import {
		confirm,
		computedOrderkey,
		createOrderkey,
		orderPay
	} from '@/api/order.js';
	import {
		getUserInfo
	} from '@/api/user.js';
	// #ifdef H5
	import weixinObj from "@/plugin/jweixin-module/index.js";
	// #endif
	export default {
		data() {
			return {
				payType: 'ali',
				desc: '', //备注
				// 收货地址
				addressData: {},
				// 商品列表
				shopList: [],
				// 购物车id
				cartId: '',
				//购物金额详情
				moneyAll: {
					storeFreePostage: 0, //邮费优惠
					storePostage: 0, //邮费
					totalPrice: 0, //总支付金额
					vipPrice: 0 //vip优惠价
				},
				payPrice: 0, //总支付金额
				orderKey: '', //订单id
				payLoding: false, //判断是否支付中
				orderId: '', //订单id
				now_money: 0, //余额
				onShopId: -1, //默认-1为不存在商家id
			};
		},
		onLoad(option) {
			// 保存当前商品在购物车中的id
			this.cartId = option.id;
			// 判断是否要读取商家id
			if (option.shopId) {
				this.onShopId = option.shopId;
			}
			this.loadData();
			this.userinfo();
		},
		computed: {
			Postage() {
				let money = +this.moneyAll.storePostage;
				if (money == 0) {
					return '免运费';
				} else {
					return '￥' + money;
				}
			},
			payAllMoney() {
				return +this.moneyAll.totalPrice + +this.moneyAll.vipPrice;
			},
			...mapState('shop', ['shopDetail']),
			...mapState(['fx'])
		},
		methods: {
			// 加载用户基础信息
			userinfo() {
				getUserInfo({}).then(({
					data
				}) => {
					this.now_money = data.now_money;
				});
			},
			// 计算支付金额
			payMoneyNub() {
				computedOrderkey({
						orderkey: this.orderKey,
						useIntegral: 0, //是否积分抵扣
						addressId: this.addressData.id //地址编号
					})
					.then(({
						data
					}) => {
						this.integralShow = true;
						// 获取支付金额
						this.payPrice = +data.result.pay_price;
						this.integralMoney = data.result.deduction_price;
					})
					.catch(e => {
						console.log(e);
						this.integralShow = false;
					});
			},
			// 加载基础数据
			loadData() {
				let obj = this;
				confirm({
					cartId: obj.cartId + ''
				}).then(({
					data
				}) => {
					obj.addressData = data.addressInfo || {};
					obj.shopList = data.cartInfo; //商品列表
					obj.moneyAll = data.priceGroup; //金额数据
					obj.orderKey = data.orderKey; //订单key
					// 计算金额
					this.payMoneyNub();
				});
			},
			// 提交订单
			submit() {
				let obj = this;
				if (!this.addressData.real_name) {
					this.$api.msg('请选择收货地址');
					return false;
				}
				// 判断是否余额不足
				if (obj.payType == 'yue' && +obj.now_money < obj.payPrice) {
					uni.showModal({
						title: '提示',
						content: '账户余额不足！',
						showCancel: false,
					});
					return;
				}

				uni.showLoading({
					title: '支付中',
					mask: true
				})
				// 支付中
				obj.payLoding = true;
				// 判断是否为未支付订单中跳转进入
				obj.firstCreateOrder();
			},
			// 订单金额支付
			orderMoneyPay() {
				let obj = this;
				orderPay({
						uni: obj.orderId,
						// #ifdef H5
						from: 'weixin', //来源
						// #endif
						// #ifdef MP-WEIXIN
						from: 'routine', //来源
						// #endif
						// #ifdef APP-PLUS
						from: 'app', //来源
						// #endif
						paytype: obj.payType //支付类型  weixin-微信 yue-余额
					})
					.then(e => {
						// 判断是否微信小程序支付
						if (obj.payType == 'weixin') {
							// #ifdef H5 || MP
							let da = e.data.result.jsConfig;
							let data = {
								nonceStr: da.nonceStr,
								package: da.package,
								signType: da.signType,
								paySign: da.paySign,
								success: function(res) {
									obj.paySuccessTo();
								},
								fail: () => {
									uni.navigateTo({
										url: '/pages/order/order?state=0'
									});
								}
							};
							// #endif
							// #ifdef H5
							data.timestamp = da.timestamp;
							weixinObj.chooseWXPay(data);
							// #endif
							// #ifdef MP-WEIXIN
							data.timeStamp = da.timestamp;
							wx.requestPayment(data);
							// #endif
							// #ifdef APP
							console.log( e.data.result.jsConfig,'返回数值');
							uni.requestPayment({
								provider: 'wxpay',
								orderInfo: e.data.result.jsConfig,
								success(e) {
									obj.paySuccessTo();
								},
								fail: (e) => {
									console.log(e,'支付失败');
									uni.navigateTo({
										url: '/pages/order/order?state=0'
									});
								}
							})
							// #endif
						}
						// #ifdef APP
						if(obj.payType == 'ali'){
							uni.requestPayment({
								provider: 'alipay',
								orderInfo: e.data.result.jsConfig,
								success(e) {
									obj.paySuccessTo();
								},
								fail: (e) => {
									console.log(e,'支付失败');
									uni.navigateTo({
										url: '/pages/order/order?state=0'
									});
								}
							})
						}
						// #endif
						uni.hideLoading();
						obj.payLoding = false;
					})
					.catch(e => {
						// 支付完成
						uni.hideLoading();
						obj.payLoding = false;
						console.log(e);
					});
			},
			// 支付成功跳转
			paySuccessTo() {
				uni.hideLoading();
				uni.redirectTo({
					url: '/pages/user/money/paySuccess?orderid=' + this.orderId,
				});
			},
			// 初次订单创建
			firstCreateOrder() {
				let obj = this;
				// 获取下单页面数据
				let prepage = obj;
				let data = {
					real_name: prepage.addressData.real_name, //联系人名称
					phone: prepage.addressData.phone, //联系人号码
					addressId: prepage.addressData.id, //支付地址id
					useIntegral: 0, //是否积分抵扣1为是0为否
					payType: obj.payType, //支付类型  weixin-微信 yue-余额
					mark: prepage.desc, //备注
					// #ifdef H5
					from: 'weixin', //来源
					// #endif
					// #ifdef MP-WEIXIN
					from: 'routine', //来源
					// #endif
					// #ifdef APP-PLUS
					from: 'app', //来源
					// #endif
					shipping_type: 1, //提货方式 1 快递 2自提
				};
				// 判断是否需要读取非默认商家id
				if (obj.onShopId != -1) {
					data.store_id = obj.onShopId;
				} else {
					data.store_id = obj.shopDetail.id;
				}
				// 生成订单
				createOrderkey(data, obj.orderKey)
					.then(({
						data,
						status,
						msg
					}) => {
						// 判断是否支付失败
						if (data.status == 'ORDER_EXIST') {
							uni.showModal({
								title: '提示',
								content: msg,
								showCancel: false
							});
							uni.hideLoading();
							obj.payLoding = false;
							return;
						}
						// 保存订单号
						obj.orderId = data.result.orderId;
						// 判断是否为余额支付
						if (obj.payType == 'yue') {
							console.log('余额支付', status == 200 && data.status == 'SUCCESS');
							if (status == 200 && data.status == 'SUCCESS') {
								obj.paySuccessTo();
							} else {
								obj.$api.msg(msg);
							}
						} else {
							// 立即支付
							obj.orderMoneyPay();
						}
					})
					.catch(e => {
						uni.hideLoading();
						obj.payLoding = false;
						console.log(e);
					});
			}
		}
	};
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100rpx;
	}

	.address-section {
		border-radius: 20rpx;
		padding: 30rpx;
		margin: 0 $page-row-spacing;
		background: #fff;
		position: relative;

		.order-content {
			min-height: 100rpx;
			display: flex;
			align-items: center;

			.leftIcon {
				width: 36rpx;
				margin: 0 30rpx;
			}

			.addAddress {
				text-align: center;
				width: 100%;
				display: flex;
				justify-content: center;
				align-items: center;

				.iconaddition {
					font-size: 55rpx;
					line-height: 1;
					border-radius: 100rpx;
					margin-right: 20rpx;
				}
			}
		}

		.iconlocation {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90rpx;
			color: #888;
			font-size: 44rpx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28rpx;
			color: $font-color-dark;
		}

		.name {
			font-size: 34rpx;
			margin-right: 24rpx;
		}

		.address {
			margin-top: 16rpx;
			margin-right: 20rpx;
			color: $font-color-light;
		}

		.icon-you {
			font-size: 32rpx;
			color: $font-color-light;
			margin-right: 30rpx;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5rpx;
		}
	}

	.goodsList {
		margin: 0 $page-row-spacing;
		padding: 30rpx;
		background: #fff;
		border-radius: 20rpx;
		margin-top: 30rpx;
	}

	.goods-section {
		.logo {
			display: block;
			width: 50rpx;
			height: 50rpx;
			border-radius: 100px;
		}

		.name {
			font-size: 30rpx;
			color: $font-color-base;
			margin-left: 24rpx;
		}

		.g-item {
			display: flex;

			image {
				flex-shrink: 0;
				display: block;
				width: 170rpx;
				height: 170rpx;
				border-radius: 4rpx;
			}

			.right {
				flex: 1;
				padding-left: 24rpx;
				overflow: hidden;

				.serverSet {
					font-size: 22rpx;
					color: $font-color-light;
				}
			}

			.title {
				font-size: 30rpx;
				color: $font-color-dark;
			}

			.spec {
				font-size: 26rpx;
				color: $font-color-light;
			}

			.price-box {
				display: flex;
				align-items: center;
				font-size: 32rpx;
				padding-top: 10rpx;
				color: $font-color-light;

				.price {
					margin-bottom: 4rpx;
				}

				.number {
					font-size: 26rpx;
					margin-left: 20rpx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		background: #fff;
		margin: 0 $page-row-spacing;
		margin-top: 30rpx;
		border-radius: 20rpx;
	}

	.yt-list-cell {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 10rpx 30rpx 10rpx 40rpx;
		line-height: 70rpx;
		position: relative;

		.checked,
		.noChecked {
			width: 36rpx;
			height: 36rpx;
		}

		.noChecked {
			border: 1px solid $font-color-light;
			border-radius: 100rpx;
		}

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30rpx;
		}

		.cell-icon {
			height: 32rpx;
			width: 32rpx;
			font-size: 22rpx;
			color: #fff;
			text-align: center;
			line-height: 32rpx;
			background: #f85e52;
			border-radius: 4rpx;
			margin-right: 12rpx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}
		}

		.cell-more {
			align-self: center;
			font-size: 24rpx;
			color: $font-color-light;
			margin-left: 8rpx;
			margin-right: -10rpx;
		}

		.cell-tit {
			font-size: 26rpx;
			color: $font-color-light;
			margin-right: 10rpx;

			.orderIcon {
				width: 48rpx;
			}
		}

		.cell-tip {
			font-size: 26rpx;
			color: $font-color-dark;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}

			&.red {
				color: $base-color;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90rpx;
			}
		}

		.desc {
			text-align: right;
			font-size: $font-base;
			color: $font-color-light;
		}
	}

	/* 支付列表 */
	.pay-list {
		padding-left: 40rpx;
		margin-top: 16rpx;
		background: #fff;

		.pay-item {
			display: flex;
			align-items: center;
			padding-right: 20rpx;
			line-height: 1;
			height: 110rpx;
			position: relative;
		}

		.icon-weixinzhifu {
			width: 80rpx;
			font-size: 40rpx;
			color: #6bcc03;
		}

		.icon-alipay {
			width: 80rpx;
			font-size: 40rpx;
			color: #06b4fd;
		}

		.icon-xuanzhong2 {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 60rpx;
			height: 60rpx;
			font-size: 40rpx;
			color: $base-color;
		}

		.tit {
			font-size: 32rpx;
			color: $font-color-dark;
			flex: 1;
		}
	}

	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 995;
		display: flex;
		align-items: center;
		width: 100%;
		height: 90rpx;
		justify-content: space-between;
		font-size: 30rpx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, 0.1);

		.price-content {
			padding-left: 30rpx;
		}

		.price-tip {
			color: $font-color-base;
			margin-left: 8rpx;
		}

		.price {
			font-size: 36rpx;
			color: $font-color-base;
		}

		.submit {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 280rpx;
			height: 100%;
			color: #fff;
			font-size: 32rpx;
			background-color: $base-color;

			&.submitNo {
				background-color: $font-color-disabled;
			}
		}
	}
</style>
