<template>
	<view class="uni-container" :style="'padding-bottom: ' + safeAreaBottom1 + 'px'">
		<u-navbar :autoBack="true" :placeholder="true" :bgColor="bgColor">
			<view slot='center' style="font-size: 36rpx; font-weight: bold;">
				提交订单
			</view>
		</u-navbar>
		<view class="address-box">
			<view class="select" v-if="!address" @click="goAddress">
				<view class="label">请选择收货地址</view>
				<u-icon name="arrow-right"></u-icon>
			</view>
			<view class="pitchOn" v-if="address" @click="goAddress">
				<view class="left-box">
					<image class="img" src="https://common/address.png" mode=""></image>
					<view class="con-box">
						<view class="address">{{address.address}}</view>
						<view class="user">
							<text>{{address.consignee}}</text>
							<text>{{address.phone}}</text>
						</view>
					</view>
				</view>
				<u-icon name="arrow-right"></u-icon>
			</view>
		</view>
		<view class="card-box" v-for="(item, index) in goodsList" :key="index">
			<view class="title">{{item.name}}</view>
			<view class="goods-box" v-for="(i, itemIndex) in item.cartList" :key="itemIndex">
				<image class="img" :src="$utils.setImgUrl(i.goodsImage)" mode=""></image>
				<view class="goods-con">
					<view class="name">{{i.goodsTitle}}</view>
					<view class="tags-box">
						<view class="tags" v-if="i.goodsSkuText">
							<text>{{i.goodsSkuText.split(',').join('-')}}</text>
						</view>
					</view>
					<view class="con-foot">
						<view class="price"><text class="unit">￥</text>{{i.price.toFixed(2)}}</view>
						<view>x{{i.buy_num}}</view>
						<!-- <view class="number">
						    <u-number-box :min="1" :max="10" :step="1" v-model="i.buy_num" @input="updateShoppingCart(index, itemIndex)"></u-number-box>
						</view> -->
					</view>
				</view>
			</view>
		</view>
		<view class="form-box">
			<view class="row">
				<view class="key">订单备注</view>
				<input type="text" v-model="goodsData.remark" class="weui-input" placeholder-class="input-placeholder" placeholder="订单备注" />
			</view>
			<view class="row">
				<view class="key">运费</view>
				<view class="value">{{goodsData.dispatchName || '包邮'}}</view>
			</view>
			<view class="row">
				<view class="key">商品总价</view>
				<view class="value">¥{{money}}</view>
			</view>
		</view>
        <view class="bottom" :style="'padding-bottom: ' + safeAreaBottom + 'px'">
			<view class="bottom_waper">
				<view class="totalMoney">
					<text class="label">总金额：</text>
					<text class="unit">￥</text>
					<text class="price">{{money}}</text>
				</view>
			    <view class="active" @click="submit">提交订单</view>
			</view>
        </view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			bgColor: '#FBFBFB',
			safeAreaBottom: 0,
			safeAreaBottom1: 0,
			id: '',
			skuId: '',
			buyNum: 0,
			addressId: '',
			address: null,
            money: '0.00',
			goodsData: {
				dispatchName: '',
				remark: ''
			},
            goodsList: [],
			goodsInfo: null,
		};
	},
	async onLoad(options) {
		this.safeAreaBottom1 = this.$safeAreaBottom + uni.upx2px(144);
		this.safeAreaBottom = this.$safeAreaBottom;
        let info = await this.$shop.getUserInfoAddressDefault();
        if (info.data) this.userAddressDetail(info.data.id);
        let list = JSON.parse(decodeURIComponent(options.list));
        this.goodsList = list;
        let money = 0;
        this.goodsList.forEach((info) => {
            info.cartList.forEach((item) => {
				money += item.buy_num * item.price
			})
        })
        this.money = money.toFixed(2);
	},
	methods: {
		async userAddressDetail(addressId) {
			let info = await this.$shop.userAddressDetail(addressId)
			if (info.code === 200) {
                this.addressId = info.data.id;
				this.address = info.data
			} else {
				uni.$u.toast(info.msg);
			}
		},
		goAddress() {
			uni.navigateTo({
				url: '/pages/shop/addressList'
			})
		},
        setAddress (info) {
			this.address = info
			this.addressId = info.id
        },
		async submit() {
            try {
                if (!this.addressId) {
                    uni.showToast({ mask: true, title: '请选择收货地址', icon: 'none' });
                    return false;
                }
                let ids = [];
                let skuInfoList = [];
                this.goodsList.forEach((info) => {
                	info.cartList.forEach((item) => {
                        if (item.expressAreaList.indexOf(this.address.areaId) == -1) {
                            throw new Error(item.goodsTitle + '不支持' + (this.address.provinceName + this.address.cityName + this.address.areaName) + '下单');
                            return false;
                        }
                		ids.push(item.cartId)
                		skuInfoList.push([item.skuPriceId, item.buy_num])
                	})
                })
                const params = {
                	addressId: this.addressId,
                	ids: ids,
                    skuInfoList: skuInfoList,
                	platform: 'wxMiniProgram',
                	remark: this.goodsData.remark
                }
                uni.showLoading({ mask: true })
                let info = await this.$shop.submitOrder(params);
                if (info.code == 200) {
                    let patInfo = JSON.parse(info.data.tradeSession);
                    uni.requestPayment({
                        provider: 'wxpay',
                    	timeStamp: patInfo.timeStamp,
                    	nonceStr: patInfo.nonceStr,
                    	package: patInfo.packageValue,
                    	signType: patInfo.signType,
                    	paySign: patInfo.paySign,
                    	success: (res) => {
                            uni.hideLoading();
                            uni.showToast({ mask: true, title: '支付成功', icon: 'success' });
                            setTimeout(() => {
                                uni.redirectTo({
                                	url: `/pages/shop/orderDetail?id=${info.data.id}`
                                })
                            }, 1500)
                            
                    	},
                    	fail: function (err) {
                            uni.hideLoading();
                            uni.showToast({ mask: true, title: '支付取消', icon: 'none' })
                            setTimeout(() => {
                                uni.redirectTo({
                                	url: `/pages/shop/orderDetail?id=${info.data.id}`
                                })
                            }, 1500)
                    	}
                    });
                } else uni.hideLoading();
            } catch (e) {
                uni.showToast({ mask: true, title: e.message, icon: 'none' });
            }
		}
	}
};
</script>

<style lang="scss" scoped>
.uni-container {
	width: 100%;
	min-height: 100vh;
	box-sizing: border-box;
	background-color: #FBFBFB;
	padding: 0 32rpx;
	
	.address-box {
		padding: 28rpx 24rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		.select {
			display: flex;
			align-items: center;
			flex-direction: row;
			justify-content: space-between;
			.label {
				font-weight: 400;
				font-size: 28rpx;
				color: rgba(0,0,0,0.85);
			}
			/deep/.uicon-arrow-right {
				color: #333333 !important;
				font-size: 30rpx !important;
			}
		}
		.pitchOn {
			display: flex;
			align-items: center;
			flex-direction: row;
			justify-content: space-between;
			.left-box {
				display: flex;
				align-items: center;
				flex-direction: row;
				.img {
					width: 37rpx;
					height: 50rpx;
					margin-right: 30rpx;
				}
				.con-box {
					.address {
						font-weight: 500;
						font-size: 28rpx;
						color: #333333;
						width: 500rpx;
					}
					.user {
						margin-top: 12rpx;
						text {
							font-weight: 400;
							font-size: 24rpx;
							color: #666666;
							&:first-child {
								margin-right: 36rpx;
							}
						}
					}
				}
			}
			/deep/.uicon-arrow-right {
				color: #333333 !important;
				font-size: 30rpx !important;
			}
		}
	}
	
	.card-box {
		padding: 28rpx 24rpx 0;
		background: #FFFFFF;
		border-radius: 10rpx;
		margin-top: 24rpx;
		.title {
			font-weight: 500;
			font-size: 32rpx;
			color: #000000;
		}
		.goods-box {
			display: flex;
			flex-direction: row;
			padding: 28rpx 0;
			border-bottom: 1rpx solid rgba(0,0,0,0.1);
			&:last-child { border-bottom: none; }
			.img {
				width: 170rpx;
				height: 170rpx;
				border-radius: 10rpx;
				margin-right: 20rpx;
			}
			.goods-con {
				flex: 1;
				.name {
					font-weight: 500;
					font-size: 28rpx;
					color: rgba(0,0,0,0.85);
					line-height: 33rpx;
					height: 74rpx;
					width: 448rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}
				.tags-box {
					height: 48rpx;
					margin-top: 8rpx;
					display: flex;
					align-items: center;
					flex-direction: row;
					.tags {
						width: max-content;
						background: #F4F4F4;
						border-radius: 2rpx;
						padding: 0 24rpx;
						line-height: 48rpx;
						font-weight: 400;
						font-size: 24rpx;
						color: rgba(0,0,0,0.65);
						margin-right: 12rpx;
						&:last-child {
							margin-right: 0;
						}
					}
				}
				.con-foot {
					margin-top: 10rpx;
					display: flex;
					align-items: center;
					flex-direction: row;
					justify-content: space-between;
					.price {
						font-weight: bold;
						font-size: 36rpx;
						color: #FF5833;
						.unit {
							font-weight: 500;
							font-size: 20rpx;
						}
					}
					/deep/.u-number-box{
						width: 198rpx; height: 100%; border-radius: 50rpx; box-sizing: border-box; border: 2rpx solid #E1E1E1;
						.u-number-box__input{ width: 66rpx; margin: 0; background: transparent !important; border: 2rpx solid #E1E1E1; box-sizing: border-box; border-top: 0; border-bottom: 0; }
						.u-number-box__minus, .u-number-box__plus{
							background: transparent !important; width: 64rpx; height: 100% !important; padding: 0 !important;
							.u-icon__icon{ font-size: 20rpx !important; color: #03AE80 !important; }
						}
						.u-number-box__minus--disabled .u-icon__icon{ color: #c8c9cc !important; }
					}
				}
			}
		}
	}
	
	.form-box {
		padding: 28rpx 24rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		margin-top: 24rpx;
		.row {
			display: flex;
			align-items: flex-start;
			flex-direction: row;
			justify-content: space-between;
			margin-top: 27rpx;
			&:first-child {
				margin-top: 0;
			}
			.key {
				font-weight: 400;
				font-size: 28rpx;
				color: rgba(0,0,0,0.65);
			}
			.value {
				text-align: right;
				font-weight: 400;
				font-size: 28rpx;
				color: rgba(0,0,0,0.85);
				width: 500rpx;
			}
			/deep/.input-placeholder {
				font-weight: 400;
				font-size: 28rpx;
				color: #999999!important;
			}
			/deep/.weui-input {
				flex: 1;
				margin-left: 20rpx;
				font-weight: 500;
				font-size: 28rpx;
				color: rgba(0,0,0,0.85);
				text-align: right;
			}
		}
	}
	
	.bottom{
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 0;
		background: #fff;
		box-shadow: 0rpx -1rpx 6rpx 0rpx rgba(0,0,0,0.05);
		.bottom_waper{
			width: 100%;
			height: 120rpx;
			box-sizing: border-box;
			padding: 20rpx 32rpx;
			display: flex;
			align-items: center;
			flex-direction: row;
			justify-content: space-between;
			.totalMoney {
				.label {
					font-size: 24rpx;
					color: rgba(0,0,0,0.45);
				}
				.unit {
					font-weight: 500;
					font-size: 36rpx;
					color: #FF5833;
				}
				.price {
					font-weight: bold;
					font-size: 64rpx;
					color: #FF5833;
				}
			}
			.active {
				width: 228rpx;
				height: 80rpx;
				text-align: center;
				line-height: 80rpx;
				background: #03AE80;
				border-radius: 66rpx;
				font-weight: 500;
				font-size: 32rpx;
				color: #FFFFFF;
			}
		}
	}
}
</style>
