<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="yt-list tc-wrap" @click="navTo('/pages/shop/shopTab?select=1')">
			<view class="store-wrap flex" v-if="selctStore && selctStore.id">
				<view class="imgBox">
					<image class="logo" :src="selctStore.image" mode="aspectFit"></image>
				</view>
				<view class="padding-l-20 flex-grow-true">
					<view class="title clamp flex">
						<view class="name">
							{{selctStore.name}}
						</view>
						<view class="km" v-if="selctStore.range">
							{{selctStore.range<1?selctStore.distance+'m':selctStore.range+'km'}}
						</view>
					</view>
					<view class="flex addressBox margin-t-20">
						<image class="iconA margin-r-10" src="../../static/icon/shop.png" mode="">
						</image>
						<text class="clamp">
							{{selctStore.detailed_address}}
						</text>
					</view>
					<view class="addressBox flex">
						<image class="iconA margin-r-10" src="../../static/icon/shopPhone.png" mode="">
						
						<text class="clamp">
							{{selctStore.phone}}
						</text>
					</view>
					<view class="yysj">
						营业时间：{{selctStore.day_time}}
					</view>
					
					
				</view>
			</view>
			<view class="store-wrap-empty" v-else>
				选择门店
			</view>
		</view>
		
		
		<view class="yt-list">
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">提货人</text>
				<input class="desc" type="text" v-model="addressData.real_name" placeholder="请填写提货人姓名" placeholder-class="placeholder" />
			</view>
			<view class="yt-list-cell b-b">
				<text class="cell-tit clamp">提货手机号</text>
				<input class="desc" type="text" v-model="addressData.phone" placeholder="请填写提货人手机号" placeholder-class="placeholder" />
			</view>
		</view>
		<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"  @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 {
				shipping_type: 2,//1->快递，2->自提
				selctStore: {id:''},//选中的门店
				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']),
			...mapState('user', ['address'])
		},
		methods: {
			navTo(url) {
				uni.navigateTo({
					url,
					fail() {
						uni.switchTab({
							url
						})
					}
				})
			},
			// 加载用户基础信息
			userinfo() {
				getUserInfo({}).then(({
					data
				}) => {
					this.now_money = data.now_money;
					this.addressData.real_name = data.real_name || '';
					this.addressData.phone = data.phone || '';
				});
			},
			// 计算支付金额
			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 || {};
					// console.log(obj.addressData,'obj.addressData')
					obj.shopList = data.cartInfo; //商品列表
					obj.moneyAll = data.priceGroup; //金额数据
					obj.orderKey = data.orderKey; //订单key
					// 计算金额
					this.payMoneyNub();
				});
			},
			// 提交订单
			submit() {
				let obj = this;
				
				if(obj.shipping_type == 2) {
					if (!obj.addressData.real_name) {
						// this.$api.msg('请选择收货地址');
						obj.$api.msg('请输入提货人姓名')
						return false;
					}
					if (!obj.addressData.phone) {
						// this.$api.msg('请选择收货地址');
						obj.$api.msg('请输入提货人手机号')
						return false;
					}
					if(obj.selctStore.id == '') {
						obj.$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_home/money/paySuccess?orderid=' + this.orderId,
				});
			},
			// 初次订单创建
			firstCreateOrder() {
				let obj = this;
				// 获取下单页面数据
				let prepage = obj;
				let data = {
					store_id: this.selctStore.id,
					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: prepage.shipping_type, //提货方式 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;
			}
		}
	}
	.store-wrap {
		background-color: #FFFFFF;
		margin-bottom: 30rpx;
		padding: 30rpx;
		border-radius: 20rpx;
		.list,.km{
			color: $font-color-light;
			font-size: $font-sm;
		}
		.logo {
			height: 200rpx;
			width: 200rpx;
			border-radius: 20rpx;
		}
	
		.title {
			font-size: 36rpx;
			color: $font-color-dark;
		}
	
		.iconR {
			height: 24rpx;
		}
	
		.iconA {
			width: 24rpx;
			height: 24rpx;
		}
	
		.addressBox {
			font-size: 24rpx;
			color: $font-color-light;
			justify-content: flex-start;
			padding: 10rpx 0;
		}
		.yysj {
			display: inline-block;
			// width: 239rpx;
			padding: 0 10rpx;
			height: 32rpx;
			background: #ddf8f1;
			border-radius: 16rpx 16rpx 16rpx 0rpx;
			font-size: 20rpx;
			font-weight: bold;
			color: #5FCCA7;
			text-align: center;
			line-height: 32rpx;
			margin-top: 10rpx;
		}
	}
	.store-wrap-empty {
		text-align: center;
		height: 200rpx;
		line-height: 200rpx;
	}
</style>
