<template>
	<view class="content" v-show="showBody">
		<view class="top1" v-if="addressShow" @click="changeAddress">
			<text style="float: left;">请填写收货地址</text>
			<u-icon name="arrow-right" style="float: right;margin-top: 10upx;" color="#666666"></u-icon>
		</view>
		<view class="top" v-else @click="changeAddress">
			<view class="address">
				<u-icon name="map" style="margin-right: 10upx;"></u-icon>{{address.address}}
			</view>
			<view class="name">
				<text
					style="margin-right: 10upx;margin-left: 40upx;">{{address.name}}</text><text>{{address.phone}}</text>
			</view>
			<u-icon name="arrow-right" class="icon" color="#666666"></u-icon>
		</view>
		<image src="../../../static/img/vlog/icon/fengexian.png" mode="" style="width: 100%;height: 5upx;"></image>
		<view class="body">
			<image :src="productVo.priceStockList[0].imgList[0].url"></image>
			<view class="title">
				<view>{{productVo.name}}</view>
				<view class="price">￥{{ price}}</view>
			</view>
		</view>
		<u-cell-group>
			<u-cell-item title="商品金额" :value="'￥'+ price" :arrow="false"></u-cell-item>
			<u-cell-item title="运费" :value="Freight.defaultCost==0?'包邮':'￥'+Freight.defaultCost" :arrow="false"></u-cell-item>
		</u-cell-group>
		<view class="bottom">
			<view class="price">
				￥{{allprice | money}}
			</view>
			<view class="confirm" @click="confirm">确认订单</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showBody: false,
				price: "", //单价
				allprice: "", //总价
				productVo: {}, //当前的产品信息
				address: {
					name: "", //姓名
					phone: "", //联系电话
					address: "" //地址
				}, //地址信息
				addressShow: true,//地址栏弹窗
				Freight: {}, //手续费信息
			}
		},
		onLoad() { //created
			this.productVo = uni.getStorageSync('productVo')
			this.price = (this.productVo.priceStockList[0].sellingPrice * 1).toFixed(2)
			this.getAddress()
		},
		onReady() { //mounted
		},
		methods: {
			confirm() { //确认订单
				if (this.address.name == '') {
					return uni.showLoading({
						title: '收件人不能为空!'
					});
				}
				if (this.address.address == '') {
					return uni.showLoading({
						title: '收货地址不能为空!'
					});
				}
				if (this.address.phone == '') {
					return uni.showLoading({
						title: '联系方式不能为空!'
					});
				}
				let imgList = []
				imgList = uni.getStorageSync('sortImglist')
				let coverInside = uni.getStorageSync('coverInside')
				imgList.push(uni.getStorageSync('coverInside').cover)
				imgList.push(uni.getStorageSync('coverInside').inside)
				let data = {
					buyMethod: 1, //购买方式：1 单品购买，2套票、联票购买，3组合购买
					cash: 0, //现金抵扣
					companyId: "10001", //来源公司id(分销商Id)
					orderMoney: this.allprice * 1, //订单总价 = 订单原价 - 优惠券价格 - 现金抵扣价格
					orderProductList: [], //产品信息列表(单品购买为空，联票购买不为空，带有儿童票或者耳机的票不为空，组合购买不为空)
					userId: uni.getStorageSync('openid'), //openid
					orderSource: 1, //订单来源 1公众号平台、2公众号组合页面1、3公众号组合页面2、4胖丁伙伴app、5第三方自助机、6第三方票房窗口
					orderType: 4, //订单类型(0其他，1景区，2酒店，3餐饮，4特产，5运营车，10组合订单)
					ticketPhone: this.address.phone, //联系电话
					orderProductVo: {
						buyNum: 1, //购买数量
						couponList: [],
						cruisePlanId: "",
						endPlayDate: this.$commonjs.today(),
						endPlayTime: this.$commonjs.todayTime(),
						isMaster: 0,
						distributionPrice: this.price * 1, //分销商价格
						merchantId: this.productVo.merchantId, //商户id
						playDate: this.$commonjs.today(),
						productId: this.productVo.id,
						startPlayTime: this.$commonjs.todayTime(),
						subOrderType: 0, //子订单类型(0、网络，1、扫码，2、自助机，3 组合，4、扫码支付，5、预订)
						unitPrice: this.price * 1, //产品单价
						orderImgList: imgList, //订单图片
						postage: this.Freight.defaultCost * 1, //邮费
						orderTouristList: [{ //用户收货地址信息
							category: 0,
							name: this.address.name,
							touristAddress: this.address.address,
							phone: this.address.phone
						}],
					}

				}
				uni.showLoading({
					title: '订单创建中'
				});
				this.$request('orderc/order/createOrder', data).then(res => {
					if (res.code == "00") {
						this.$u.route("pages/orderPayment/orderPayment", {
							orderId: res.data.id
						})
						uni.hideLoading();
					} else {
						uni.showModal({
							showCancel: false,
							title: '提示',
							content: res.message,
						});
					}
				})
			},
			changeAddress() { //跳转修改地址页面
				this.$u.route("pages/vlog/shippingAddress/shippingAddress",this.address)
			},
			getAddress() { //获取收货地址
				this.$request('wechatUser/contact/findContactList', {
					openid: uni.getStorageSync("openid")
				}).then(res => {
					if (res.code == "00") {
						if(res.data.length==0){
							this.$u.route("pages/vlog/shippingAddress/shippingAddress")
						}else{
							this.address = res.data[0]
							this.addressShow = false
							this.getFreight()
						}
					} else {
						uni.showModal({
							showCancel: false,
							title: '提示',
							content: res.message,
						});
					}
				})
			},
			getFreight() { //获取运费
				this.$request('scenic/user/product/getProductPostage', {
					id: this.productVo.id,
					userAddress:this.address.address
				}).then(res => {
					if (res.code == "00") {
						this.Freight = res.data
						this.Freight.defaultCost = (this.Freight.defaultCost * 1).toFixed(2)
						this.allprice = ((this.productVo.priceStockList[0].sellingPrice * 1) + (this.Freight
							.defaultCost * 1))
						this.showBody = true
					} else {
						uni.showModal({
							showCancel: false,
							title: '提示',
							content: res.message,
						});
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 0 20rpx;
		box-sizing: border-box;
		margin: 0 auto;
		background-color: #fff;
	}

	.cell {
		display: flex;
		align-items: center;
		height: 80upx;
		line-height: 80upx;
		border-bottom: 1upx solid #ccc;

		text {
			width: 20%;
			margin-left: 30upx;
		}
	}

	.top1 {
		height: 50upx;
		line-height: 50upx;
		font-weight: bold;
	}

	.top {
		height: 100upx;
		position: relative;
		padding-top: 10upx;

		.name {
			font-size: 28upx;
			color: #666666;
			line-height: 50upx;
		}

		.address {
			font-size: 32upx;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
			line-height: 50upx;
		}

		.icon {
			line-height: 100upx;
			position: absolute;
			right: 0;
			top: 40%;
		}
	}

	.body {
		display: flex;
		margin-top: 20upx;
		margin-bottom: 20upx;

		image {
			width: 240upx;
			height: 240upx;
			border-radius: 10upx;
		}

		.title {
			width: 65%;
			font-size: 36upx;
			font-weight: bold;
			padding: 20upx;
			overflow: hidden;
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			view {
				text-overflow: ellipsis;
				white-space: nowrap;
			}

			.price {
				color: #3688FF;
			}
		}
	}

	.product_price {
		display: flex;
		justify-content: space-between;
		font-size: 40upx;
		font-weight: bold;
	}

	.yunfei {
		display: flex;
		justify-content: space-between;
		font-size: 40upx;
		font-weight: bold;
	}

	.bottom {
		border-top: 1upx solid #e8e8e8;
		height: 98upx;
		position: fixed;
		bottom: 0;
		width: 100%;
		right: 0;
		font-size: 32rpx;
		font-weight: bold;
		display: flex;
		align-items: center;
		justify-content: space-between;

		view {
			display: inline-block;
		}

		.confirm {
			height: 98upx;
			background-color: #FFE600;
			width: 200upx;
			font-size: 32upx;
			line-height: 98upx;
			font-weight: bold;
			float: right;
			text-align: center;
		}

		.price {
			color: #EE520E;
			padding-left: 20upx;
		}
	}
</style>
