<template>
	<view class="uni-container">
		<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="goods-box" v-if="goodsInfo">
			<image class="img" :src="$utils.setImgUrl(goodsData.image)" mode=""></image>
			<view class="goods-con">
				<view class="name">{{goodsData.title}}</view>
				<view class="tags-box">
					<view class="tags" v-if="goodsData.goodsSkuText">{{goodsData.goodsSkuText.join('-')}}</view>
				</view>
				<view class="con-foot">
					<view class="price"><text class="unit">￥</text>{{goodsData.price.toFixed(2)}}</view>
					<u-number-box :min="1" :max="goodsData.stock" :step="1" v-model="buyNum"></u-number-box>
				</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">¥{{(buyNum * goodsData.price).toFixed(2)}}</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">{{(buyNum * goodsData.price).toFixed(2)}}</text>
				</view>
			    <view class="active" @click="submit">提交订单</view>
			</view>
        </view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			bgColor: '#FBFBFB',
			safeAreaBottom: 0,
			id: '',
			skuId: '',
			buyNum: 0,
			addressId: '',
			address: null,
			goodsData: {
				image: '',
				title: '',
				goodsSkuText: '',
				price: 0,
				stock: 1,
				dispatchName: '',
				remark: ''
			},
			goodsInfo: null,
		};
	},
	onLoad(options) {
		this.id = options.id
		this.skuId = options.skuId
		this.buyNum = +options.buyNum
		this.addressId = options.addressId
		this.safeAreaBottom = this.$safeAreaBottom;
		this.userAddressDetail()
		this.getGoodsDetail();
	},
	methods: {
		async userAddressDetail() {
            if (!this.addressId) return false;
			let info = await this.$shop.userAddressDetail(this.addressId)
			if (info.code === 200) {
				this.address = info.data
			} else {
				uni.$u.toast(info.msg);
			}
		},
        setAddress (info) {
        	this.address = info
        	this.addressId = info.id
        },
		// 商品详情
		async getGoodsDetail() {
            let info = await this.$shop.getGoodsDetail({id: this.id});
			this.goodsInfo = info.data;
			this.goodsData.image = this.goodsInfo.image
			this.goodsData.title = this.goodsInfo.title
			this.goodsData.dispatchName = this.goodsInfo.dispatchName
			if (this.goodsInfo.skuPriceList) {
				let f = this.goodsInfo.skuPriceList.find(m => m.id == this.skuId)
				this.goodsData.goodsSkuText = f.goodsSkuText.split(',')
				this.goodsData.price = f.price
				this.goodsData.stock = f.stock
			} else {
				this.goodsData.price = this.goodsInfo.zdyWcscGoodsSkuPrice.price
				this.goodsData.stock = this.goodsInfo.zdyWcscGoodsSkuPrice.stock
			}
		},
		goAddress() {
			uni.navigateTo({
				url: '/pages/shop/addressList'
			})
		},
		async submit() {
            if (!this.addressId) {
                uni.showToast({ mask: true, title: '请选择收货地址', icon: 'none' });
                return false;
            }
            if (this.goodsInfo.expressAreaList.indexOf(this.address.areaId) == -1) {
                uni.showToast({ mask: true, title: this.goodsData.title + '不支持' + (this.address.provinceName + this.address.cityName + this.address.areaName) + '下单', icon: 'none' });
                return false;
            }
			const params = {
				addressId: this.addressId,
				skuInfoList: [[Number(this.skuId), Number(this.buyNum)]],
				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();
		}
	}
};
</script>

<style lang="scss" scoped>
.uni-container {
	width: 100%;
	min-height: 100vh;
	box-sizing: border-box;
	background-color: #FBFBFB;
	padding: 0 24rpx;
	
	.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;
			}
		}
	}
	
	.goods-box {
		padding: 24rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		margin-top: 20rpx;
		display: flex;
		align-items: flex-start;
		flex-direction: row;
		box-sizing: border-box;
		.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);
				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: 12rpx;
				display: flex;
				align-items: center;
				flex-direction: row;
				.tags {
					line-height: 48rpx;
					width: max-content;
					background: #F4F4F4;
					border-radius: 2rpx;
					padding: 0 24rpx;
					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: 16rpx;
				display: flex;
				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: 24rpx;
		background: #FFFFFF;
		border-radius: 10rpx;
		margin-top: 20rpx;
		.row {
			display: flex;
			align-items: flex-start;
			flex-direction: row;
			justify-content: space-between;
			margin-top: 20rpx;
			&:first-child {
				margin-top: 0;
			}
			.key {
				font-size: 28rpx;
				color: rgba(0,0,0,0.65);
				line-height: 33rpx;
			}
			.value {
				text-align: right;
				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-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>
