<template>
	<view>
		<!-- 收货地址 -->
		<view class="addr" @tap="selectAddress">
			<view class="icon">
				<image src="../../static/img/addricon.png" mode=""></image>
			</view>
			
			<view class="right" v-if="recinfo.id">
				<view class="tel-name">
					<view class="name">
						{{recinfo.user}}
					</view>
					<view class="tel">
						{{recinfo.telphone}}
					</view>
				</view>
				<view class="addres">
					{{recinfo.address}}
					<!-- {{recinfo.address.region.label}}
					{{recinfo.address.detailed}} -->
				</view>
			</view>
			<view class="right" v-else>
				请选择收货地址
			</view>
		</view>
		<!-- 购买商品列表 -->
		<view class="buy-list">
			<view class="row" v-for="(row,index) in buylist" :key="index">
				<view class="goods-info">
					<view class="img">
						<image :src="row.picurl"></image>
					</view>
					<view class="info">
						<view class="title">{{row.title}}</view>
						<view class="spec">
							<!-- 选择{{row.spec}} -->
							数量:{{row._num}}</view>
						<view class="price-number">
							<view class="price">￥{{row.price*row._num}}</view>
							<view class="number">

							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 提示-备注 -->
		<!-- <view class="order">
			<view class="row">
				<view class="left">
					积分 :
				</view>
				<view class="right">
					已扣除{{int}}积分抵扣{{deduction|toFixed}}元
				</view>
			</view>
			<view class="row">
				<view class="left">
					备注 :
				</view>
				<view class="right">
					<input placeholder="选填,备注内容" v-model="note" />
				</view>
			</view>
		</view> -->
		<view class="addr">
		<view class="name">
			开票类型
		</view>
		<view class="tel" style="font-size:14px;">
			<radio-group @change="radioChange">
			<label class="radio"><radio value="0" />不开票</label>
			<label class="radio"><radio value="1" />普票</label>
			<label class="radio"><radio value="2" checked/>专票</label>
			</radio-group>
		</view>
		</view>
		<!-- 开票信息 -->
		<view class="addr" @tap="selectInvoice" style="margin-top: 2px;" v-if="showKpxx">		
			<view class="right" v-if="invoiceinfo.id" >
				<view class="tel-name"  >
						<view class="name">
							{{invoiceinfo.name}}
						</view>
						<view class="tel">
							{{invoiceinfo.duty_paragraph}}
						</view>
					</view>
					<view class="addres">
						{{invoiceinfo.address}}
						<!-- {{recinfo.address.region.label}}
						{{recinfo.address.detailed}} -->
					</view>
				</view>
				<view class="right" v-else>
					请选择发票信息
				</view>
		</view>
		<!-- 明细 -->
		<view class="detail">
			<view class="row">
				<view class="nominal">
					商品金额
				</view>
				<view class="content">
					￥{{goodsPrice|toFixed}}
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					运费
				</view>
				<view class="content">
					￥+{{freight|toFixed}}
				</view>
			</view>
<!-- 			<view class="row">
				<view class="nominal">
					积分抵扣
				</view>
				<view class="content">
					￥-{{deduction|toFixed}}
				</view>
			</view> -->
		</view>
		<view class="blck">

		</view>
		<view class="footer">
			<view class="settlement">
				<view class="sum">合计:<view class="money">￥{{sumPrice|toFixed}}</view>
				</view>
				<view class="btn" @tap="toPay">提交订单</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showKpxx:true,
				buylist: [], //订单列表
				goodsPrice: 0.0, //商品合计价格
				sumPrice: 0.0, //用户付款价格
				freight: 0.0, //运费
				note: '', //备注
				int: 1200, //抵扣积分
				deduction: 0, //抵扣价格
				recinfo:{
					id:null
				},
				if_invoice:2,
				freights:{},
				invoiceinfo:{
					id:null
				}

			};
		},
		async onLoad(){
			//页面显示时，加载订单信息
			try {
			    const value = uni.getStorageSync('buylist');
			    if (value) {
			        this.buylist = value;
			        this.goodsPrice = 0;
			        for(var i=0;i<this.buylist.length;i++){
			        	
						let thisprice = this.buylist[i]._num * this.buylist[i].price
						this.goodsPrice = this.goodsPrice + thisprice;
						if(this.freights['shop'+this.buylist[i].shopId]){
						}else{
							this.freights['shop' + this.buylist[i].shopId]={'base':null, 'total':0, 'freight':0}
						}
						this.freights['shop'+this.buylist[i].shopId].total = this.freights['shop'+this.buylist[i].shopId].total + thisprice;
			        }
			        let ret = await this.getFreights(); //获取运费计算规则
			      
			        // this.deduction = this.int / 100;
			        //console.log(this.freights)
			        this.calFreight() //计算运费
			        this.sumPrice = this.goodsPrice - this.deduction + parseFloat(this.freight);
					
			    }
			} catch (e) {
			    // error
			}
		},
		onShow() {
			var that = this;
			uni.getStorage({
				key: 'selectAddress',
				success: (e) => {
					this.recinfo = e.data;
					debugger;
					console.log(this.recinfo)
				}
			})
			uni.getStorage({
				key: 'selectInvoice',
				success: (e) => {
					this.invoiceinfo = e.data;
				}
			})
		},
		onHide() {

		},
		onBackPress() {
			//页面后退时候，清除订单信息
			this.clearOrder();
		},
		filters: {
			toFixed: function(x) {
				return parseFloat(x).toFixed(2);
			}
		},
		methods: {
			calFreight(){
				for(let key in this.freights){
					var base_l = this.freights[key].base.split('|')
					if(this.freights[key].total>=base_l[0]){
						this.freights[key].freight = 0
					}else{
						this.freights[key].freight=base_l[1]
					}
					this.freight = this.freight + this.freights[key].freight
				}
			},
			async getFreights() {
				var that = this
				
				 for(let key  in that.freights){
				        let shopId = key.replace('shop','')
						let ret = await that.$u.api.getFreight(shopId);
						that.freights[key].base = ret.data
						
				  }
			},
			clearOrder() {
				uni.removeStorage({
					key: 'buylist',
					success: (res) => {
						this.buylist = [];
						console.log('remove buylist success');
					}
				});
			},
			toPay() {
				let that = this;
				if(!this.recinfo.id){
					debugger;
					uni.showToast({
							title: '请选择收货地址',
							icon: 'none'
						});
						return;
				}else{
					let time = false;
					that.$u.api.getAddress({prePage:50}).then(res=>{
						for(let key  in res.data){
						     if(res.data[key].id==that.recinfo.id){
						     	time = true;
						     } else{
							 }
						  }
						  if(time){}else{
							  uni.showToast({
							  		title: '请选择有效的收货地址',
							  		icon: 'none'
							  	});
								that.recinfo = {};
							  	return;
						  }
					})
				}
				if(this.if_invoice==1 || this.if_invoice==2){
					if(this.invoiceinfo.id){
						let time1 = false;
						that.$u.api.getInvoice({prePage:50}).then(res=>{
							for(let key  in res.data){
							     if(res.data[key].id==that.invoiceinfo.id){
							     	time1 = true;
							     } else{
							     }
							  }
							if(time){}else{
								  uni.showToast({
										title: '请选择有效的发票地址',
										icon: 'none'
									});
									that.invoiceinfo = {};
									return;
							}
						})
					}else{
						uni.showToast({
							title:'请选择发票信息',
							icon:'none'
						})
						return
					}
				}
				if(this.buylist.length==0){
					uni.showToast({
							title: '订单信息有误，请重新购买',
							icon: 'none'
						});
						return;
				}
				
				let goods = []
				for(let i=0;i<this.buylist.length;i++){
					goods.push({
						shopid: this.buylist[i].id,
						num: this.buylist[i]._num
					})
				}
				var data = {
					address: this.recinfo.id,
					data:goods,
					if_invoice:this.if_invoice
				}
				if(this.if_invoice!=0){
					data.invoice = this.invoiceinfo.id
				}
				//本地模拟订单提交UI效果
				uni.showLoading({
					title: '正在提交订单...'
				})
				// setTimeout(() => {
				// 	uni.setStorage({
				// 		key: 'paymentOrder',
				// 		data: paymentOrder,
				// 		success: () => {
				// 			uni.hideLoading();
				// 			uni.redirectTo({
				// 				url: "../pay/payment/payment?amount=" + this.sumPrice
				// 			})
				// 		}
				// 	})
				// }, 1000)
				console.log(data)
				this.$u.api.addOrder(data).then(res=>{
					uni.showToast({
							title: '下单成功',
							icon: 'none'
						});
					uni.redirectTo({
						url:'/pages/user/order_list/order_list?tbIndex=0'
					})
				})

			},
			//选择收货地址
			selectAddress() {
				uni.navigateTo({
					url: '../user/address/address?type=select'
				})
			},
			selectInvoice(){
				uni.navigateTo({
					url: '../user/invoice/invoice?type=select'
				})
			},
			radioChange(e){
				this.if_invoice = e.detail.value
				if(e.detail.value==0){
					this.showKpxx = false
				}else{
					this.showKpxx = true
				}
			}
		}
	}
</script>

<style lang="scss">
	.addr {
		width: 86%;
		padding: 20upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		display: flex;

		.icon {
			width: 80upx;
			height: 80upx;
			display: flex;
			align-items: center;

			image {
				width: 60upx;
				height: 60upx;
			}
		}

		.tel-name {
			width: 100%;
			display: flex;
			font-size: 32upx;

			.tel {
				margin-left: 40upx;
			}
		}

		.addres {
			width: 100%;
			font-size: 26upx;
			color: #999;
		}
	}

	.buy-list {
		width: 86%;
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;

		.row {
			margin: 30upx 0;

			.goods-info {
				width: 100%;
				display: flex;

				.img {
					width: 22vw;
					height: 22vw;
					border-radius: 10upx;
					overflow: hidden;
					flex-shrink: 0;
					margin-right: 10upx;

					image {
						width: 22vw;
						height: 22vw;
					}
				}

				.info {
					width: 100%;
					height: 22vw;
					overflow: hidden;
					display: flex;
					flex-wrap: wrap;
					position: relative;

					.title {
						width: 100%;
						font-size: 28upx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						// text-align: justify;
						overflow: hidden;
					}

					.spec {
						font-size: 22upx;
						background-color: #f3f3f3;
						color: #a7a7a7;
						height: 40upx;
						display: flex;
						align-items: center;
						padding: 0 10upx;
						border-radius: 20upx;
						margin-bottom: 20vw;
					}

					.price-number {
						position: absolute;
						width: 100%;
						bottom: 0upx;
						display: flex;
						justify-content: space-between;
						align-items: flex-end;
						font-size: 28upx;
						height: 40upx;

						.price {
							color: #f06c7a;
						}

						.number {
							display: flex;
							justify-content: center;
							align-items: center;

						}
					}
				}
			}
		}
	}

	.order {
		width: 86%;
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;

		.row {
			margin: 20upx 0;
			height: 40upx;
			display: flex;

			.left {
				font-size: 28upx;
			}

			.right {
				margin-left: 40upx;
				font-size: 26upx;
				color: #999;

				input {
					font-size: 26upx;
					color: #999;
				}
			}
		}
	}

	.blck {
		width: 100%;
		height: 100upx;
	}

	.footer {
		width: 92%;
		padding: 0 4%;
		background-color: #fbfbfb;
		height: 100upx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		font-size: 28upx;
		position: fixed;
		bottom: 0upx;
		z-index: 5;

		.settlement {
			width: 80%;
			display: flex;
			justify-content: flex-end;
			align-items: center;

			.sum {
				width: 50%;
				font-size: 28upx;
				margin-right: 10upx;
				display: flex;
				justify-content: flex-end;

				.money {
					font-weight: 600;
				}
			}

			.btn {
				padding: 0 30upx;
				height: 60upx;
				background-color: #f06c7a;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30upx;
				border-radius: 40upx;
			}
		}
	}

	.detail {
		width: 86%;
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;

		.row {
			height: 60upx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.nominal {
				font-size: 28upx;
			}

			.content {
				font-size: 26upx;
				color: #f06c7a;
			}
		}
	}
</style>
