<template>
	<view>
		<view class="inner-box">
			<!-- 收货人 -->
			<view class="address-box" v-if="address==null" @click="openAddress()">
				<view class="left">
					<view class="name-box">
						<text class="text name">添加收货地址</text>
					</view>
					</vie>
				</view>
				<view class="arrow-box">
					<image src="@/static/images/arrow_gray.png" mode=""></image>
				</view>
			</view>
			<view class="address-box" v-if="address!=null" @click="openAddress()">
				<view class="left">
					<view class="name-box">
						<text class="text name">{{address.realName}}</text>
						<text class="text" v-if="address.phone!=null">{{utils.parsePhone(address.phone)}}</text>
					</view>
					<view class="address">
						{{address.province}}{{address.city}}{{address.district}}{{address.detail}}
					</view>
				</view>
				<view class="arrow-box">
					<image src="@/static/images/arrow_gray.png" mode=""></image>
				</view>
			</view>
			<!-- 商品列表 -->
			<view class="goods-list">
				<view  class="item">
					<view class="img-box">
						<image :src="package.imgUrl" mode="aspectFill"></image>
					</view>
					<view class="info-box">
						<view>
							<view class="name-box ellipsis2">
								{{package.title}}
							</view>
							<view class="spec ellipsis2">{{package.descs}}</view>
						</view>
						
						<view class="price-num">
							<view class="price">
								<text class="unit">¥</text>
								<text class="num" v-if="package.payMoney!=null">{{package.payMoney.toFixed(2)}}</text>
							</view>
						</view>
					</view>
				</view>
				<!-- 小计 -->
				<view class="sub-total">
					<text class="label">合计：</text>
					<view class="price">
						<text class="unit">¥</text>
						<text class="num" v-if="package.payMoney!=null">{{package.payMoney.toFixed(2)}}</text>
					</view>
				</view>
			</view>
			 
			<view class="points" @click="openCoupon()">
				<view class="left">
					<text class="text">优惠券</text>
				</view>
				<view class="right">
					<text class="text">{{couponText}}</text>
					<image src="/static/images/arrow4.png" mode=""></image>
				</view>
			</view>
			<!-- 备注 -->
			<view class="remarks">
				<input type="text" v-model="form.mark" placeholder="备注留言（选填）" placeholder-class="input" />
			</view>
		</view>
		<!-- 底部按钮 -->
		<view class="btn-foot">
			<view class="right">
				<view class="total">
					<text class="label">应付金额：</text>
					<view class="price">
						<text class="unit">¥</text>
						<text class="num" v-if="totalMoney!=null">{{totalMoney.toFixed(2)}}</text>
					</view>
				</view>
				<view class="btn" @click="submitOrder">提交订单</view>
			</view>
		</view>
		<popupBottom ref="popup" :visible.sync="couponVisible" title=" " bgColor="#f5f5f5"  radius="30"    maxHeight="60%">
		     <view class="coupon" style="height:650rpx;">
				 <div class="coupon-list" v-if="couponsList.length > 0">
				   <div class="item acea-row row-center-wrapper"  v-for="(item, index) in couponsList" :key="index">
				     <div class="money" >
				       <image v-if="item.status==0" class="img" src="@/static/images/coupon1.png" mode="widthFix"></image>
				 	  <image v-if="item.status!=0" class="img" src="@/static/images/coupon2.png" mode="widthFix"></image>
				 	  <div style="z-index: 999;">
				         ￥<span class="num">{{ item.couponPrice }}</span>
				       </div>
				       <div class="pic-num"  >满{{ item.useMinPrice }}元可用</div>
				     </div>
				     <div class="text">
				       <div class="condition line1">
				         {{ item.couponTitle }}
				       </div>
				       <div class="data acea-row row-between-wrapper">
							<div >{{ item.limitTime }}到期</div>
							<div class="bnt bg-color-red" @click="couponSelect(item)"  >选择</div>
				       </div>
				     </div>
				   </div>
				 </div>
				 <view v-if="couponsList.length == 0" class="no-data-box" >
				 	<image src="/static/images/no_data.png" mode="aspectFit"></image>
				 	<view class="empty-title">暂无数据</view>
				 </view>
			 </view>
			 
		</popupBottom>
	</view>
</template>

<script>
	import {getWeixinTemps} from '@/api/common'
	import {confirmPackageOrder,computedPackageOrder,createPackageOrder} from '@/api/storeOrder'
	import { getMyEnableCouponList } from '@/api/coupon'
	 
	import EvanSwitch from '@/components/evan-switch/evan-switch.vue'
	import popupBottom from '@/components/px-popup-bottom/px-popup-bottom.vue'
	export default {
		components: {
			EvanSwitch,
			popupBottom
		},
		data() {
			return {
				temps:[],
				totalMoney:0,
				couponUserId:null,
				couponText:"请选择",
				couponsList:[],
				couponVisible:false,
				companyUserId:null,
				packageId:null,
				address:null,
				package:{},
				// checked: false,
				// type:null,
				// cartIds:null,
				form:{
					// useIntegral:0,
					orderKey:null,
					addressId:null,
					mark:null,
				}
				
			}
		},
		onLoad(option) {
			this.packageId=option.packageId;
			this.companyUserId=option.companyUserId;
			this.confirmPackageOrder();
			uni.$on('updateAddress', (e) => {
				this.address=e;
				this.form.addressId=e.id;
			})
			this.getWeixinTemps();
		},
		onShow() {
			
		},
		methods: {
			getWeixinTemps:function(){
				getWeixinTemps().then(
					res => {
						if(res.code==200){
							this.temps=res.temp
						}else{
							 
						}
					},
					rej => {}
				);
			},
			couponSelect(item){
				this.couponText="-¥"+item.couponPrice.toFixed(2);
				this.couponUserId=item.id;
				this.couponVisible=false;
				this.computedPackageOrder();
			},
			openCoupon(){
				let that = this;
				var data={packageCateId:this.package.cateId,couponType:1,useMinPrice:this.totalMoney};
				getMyEnableCouponList(data).then(res => {
				  this.couponVisible=true;
				  that.couponsList = res.data
				})
			},
			confirmPackageOrder(){
				let data = {packageId:this.packageId,couponUserId:this.couponUserId};
				confirmPackageOrder(data).then(
					res => {
						if(res.code==200){
							 this.form.orderKey=res.orderKey;
							 this.address=res.address;
							 this.package=res.package;
							 if(res.address!=null){
								 this.form.addressId=res.address.id;
							 }
							 this.totalMoney=res.totalMoney;
							 
						}else{
							
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
			computedPackageOrder(){
				let data = {packageId:this.packageId,couponUserId:this.couponUserId};
				computedPackageOrder(data).then(
					res => {
						if(res.code==200){
							 this.totalMoney=res.totalMoney;
							 
						}else{
							
							uni.showToast({
								icon:'none',
								title: res.msg,
							});
						}
					},
					rej => {}
				);
			},
		 
			// 提交订单
			submitOrder() {
				var that=this;
				if(this.form.orderKey==null){
					uni.showToast({
						icon:'none',
						title: '订单KEY不存在',
					});
					return;
				}
				if(this.form.addressId==null){
					uni.showToast({
						icon:'none',
						title: '收货地址不能为空',
					});
					return;
				}
				
				uni.requestSubscribeMessage({
					tmplIds: this.temps,
					success(res) {
						 that.createPackageOrder();
					},
					fail(res) {
						that.createPackageOrder();
					}
				})
				
				
			},
			createPackageOrder(){
				var that=this;
				uni.showLoading({
					title: '正在处理中...'
				});
				let data = {couponUserId:this.couponUserId,mark:this.form.mark,orderKey:this.form.orderKey,addressId:this.form.addressId,packageId:this.packageId,companyUserId:this.companyUserId};
				createPackageOrder(data).then(
					res => {
						uni.hideLoading()
						if(res.code==200){
							if(res.order.isPrescribe==1){
								setTimeout(function(){
									uni.redirectTo({
										url:"/pages/shopping/prescribe?orderId="+res.order.id
									})
								},200);
							}
							else{
								setTimeout(function(){
									uni.redirectTo({
										url: '/pages/shopping/paymentOrder?orderId='+res.order.id
									})
								},200);
							}
							return;
						}
						else{
							if(res.code==501){
								uni.showToast({
									icon:'none',
									title: res.msg,
								});
								setTimeout(function(){
									uni.navigateBack({
										delta:1
									})
								},200);
								return;
							}
							else{
								uni.showToast({
									icon:'none',
									title: res.msg,
								});
							}
						}
					},
					rej => {}
				);
			},
			openAddress(){
				uni.navigateTo({
					url: '/pages_user/user/address'
				})
			}
		}
	}
</script>

<style lang="scss">
	.inner-box{
		padding: 20upx 20upx 140upx;
		.address-box{
			box-sizing: border-box;
			min-height: 171upx;
			background: #FFFFFF;
			border-radius: 16upx;
			background-image: url(../../static/images/address_bg.png);
			background-repeat: no-repeat;
			background-size: 100% 30upx;
			background-position: left bottom;
			padding: 38upx 30upx 36upx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.left{
				width: 92%;
				.name-box{
					display: flex;
					align-items: center;
					.text{
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #111111;
						line-height: 1;
						&.name{
							margin-right: 30upx;
						}
					}
				}
				.address{
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
					line-height: 42upx;
					text-align:left;
					margin-top: 23upx;
				}
			}
			.arrow-box{
				width: 12upx;
				height: 23upx;
				display: flex;
				align-items: cenetr;
				justify-content: cenetr;
				image{
					width: 100%;
					height: 100%;
				}
			}
		}
		.goods-list{
			margin-top: 20upx;
			padding: 0 30upx;
			background-color: #FFFFFF;
			border-radius: 16upx;
			.item{
				padding: 30upx 0;
				border-bottom: 1px solid #EDEEEF;
				display: flex;
				align-items: center;
				.img-box{
					width: 160upx;
					height: 160upx;
					margin-right: 30upx;
					image{
						width: 100%;
						height: 100%;
					}
				}
				.info-box{
					width: calc(100% - 190upx);
					height: 160upx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					.name-box{
						font-size: 28upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #111111;
						line-height: 40upx;
						.tag{
							display: inline-block;
							padding: 0 6upx;
							height: 30upx;
							background: linear-gradient(90deg, #66b2ef 0%, #018C39 100%);
							border-radius: 4upx;
							margin-right: 10upx;
							font-size: 22upx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #FFFFFF;
							line-height: 30upx;
							float: left;
							margin-top: 7upx;
						}
					}
					.spec{
						margin-top: 10upx;
						font-size: 24upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
						line-height: 1;
					}
					.price-num{
						display: flex;
						align-items: center;
						justify-content: space-between;
						.price{
							display: flex;
							align-items: flex-end;
							.unit{
								font-size: 24upx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #111111;
								line-height: 1.2;
								margin-right: 4upx;
							}
							.num{
								font-size: 32upx;
								font-family: PingFang SC;
								font-weight: 500;
								color: #111111;
								line-height: 1;
							}
						}
						.num{
							font-size: 24upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #999999;
							line-height: 1;
						}
					}
				}
			}
			.sub-total{
				height: 88upx;
				display: flex;
				align-items: center;
				justify-content: flex-end;
				.label{
					font-size: 24upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #999999;
				}
				.price{
					display: flex;
					align-items: flex-end;
					.unit{
						font-size: 24upx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #FF6633;
						line-height: 1.2;
						margin-right: 4upx;
					}
					.num{
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #FF6633;
						line-height: 1;
					}
				}
			}
		}
		.points{
			height: 88upx;
			padding: 0 30upx;
			background: #FFFFFF;
			border-radius: 16upx;
			margin-top: 20upx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.left{
				display: flex;
				align-items: center;
				image{
					width: 28upx;
					height: 28upx;
					margin-right: 20upx;
				}
				.text{
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
				}
			}
			.right{
				display: flex;
				align-items: center;
				.text{
					font-size: 28upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #111111;
					 
				}
				image{
					margin-left: 15upx;
					width: 14upx;
					height: 24upx;
				}
			}
		}
		.remarks{
			height: 88upx;
			padding: 0 30upx;
			background: #FFFFFF;
			border-radius: 16upx;
			margin-top: 20upx;
			display: flex;
			align-items: center;
			input{
				width: 100%;
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #000000;
			}
			.input{
				font-size: 28upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
			}
		}
	}
	
	
	.btn-foot{
		box-sizing: border-box;
		width: 100%;
		height: 121upx;
		background: #FFFFFF;
		padding: 16upx 30upx 16upx 60upx;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 99;
		.right{
			display: flex;
			align-items: center;
			.total{
				display: flex;
				align-items: flex-end;
				margin-right: 36upx;
				.label{
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #999999;
					line-height: 1.5;
				}
				.price{
					display: flex;
					align-items: flex-end;
					.unit{
						font-size: 32upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #FF6633;
						line-height: 1.2;
						margin-right: 10upx;
					}
					.num{
						font-size: 50upx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #FF6633;
						line-height: 1;
					}
				}
			}
			.btn{
				width: 200upx;
				height: 88upx;
				line-height: 88upx;
				text-align: center;
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
				background: #018C39;
				border-radius: 44upx;
			}
		}
	}
	 
</style>
<style lang="less" scoped>
	.coupon {
	  height: 100%;
	}
	/*优惠券列表公共*/
	.coupon-list {
	}
	.coupon-list .item {
	  display: flex;
	  flex-direction: column;
	  justify-content: center;
	  align-items: center;
	  width: 100%;
	  height: 1.7 * 100rpx;
	  margin-bottom: 0.16 * 100rpx;
	}
	
	.coupon-list .item .money {
	  background-size: 100% 100%;
	  width: 2.4 * 100rpx;
	  height: 100%;
	  color: #fff;
	  font-size: 0.36 * 100rpx;
	  font-weight: bold;
	  text-align: center;
	  display: flex;
	  flex-direction: column;
	  align-items: center;
	  justify-content: center;
	  position: relative;
	  
	}
	.coupon-list .item .money .img{
	  position: absolute;
	  width: 2.4 * 100rpx;
	  height: 100%;
	  color: #fff;
	  
	}
	
	.coupon-list .item .money .num {
	  font-size: 0.6 * 100rpx;
	}
	.coupon-list .item .money .pic-num {
	  font-size: 20rpx;
	  z-index: 99;
	}

	
	.coupon-list .item .text {
	  width: 4.5 * 100rpx;
	  padding: 0 0.17 * 100rpx 0 0.24 * 100rpx;
	  background-color: #fff;
	  box-sizing: border-box;
	}
	
	.coupon-list .item .text .condition {
	  font-size: 0.3 * 100rpx;
	  color: #282828;
	  height: 0.93 * 100rpx;
	  line-height: 0.93 * 100rpx;
	  border-bottom: 1px solid #f0f0f0;
	}
	
	.coupon-list .item .text .data {
	  font-size: 0.2 * 100rpx;
	  color: #999;
	  height: 0.76 * 100rpx;
	}
	
	.coupon-list .item .text .data .bnt {
	  width: 1.36 * 100rpx;
	  height: 0.44 * 100rpx;
	  border-radius: 0.22 * 100rpx;
	  font-size: 0.22 * 100rpx;
	  color: #fff;
	  text-align: center;
	  line-height: 0.44 * 100rpx;
	  background-color: red;
	}
	
	.coupon-list .item .text .data .bnt.gray {
	  background-color: #ccc;
	}
</style>
