<template>
	<view style="width: 100%;height:　100vh;background-color: #F5F5F5;">
		<van-radio-group >
		   <van-cell-group 
		   v-for="(item, index) in list"
		   :key="index"
		   >
			 <van-cell
			   :border="false"
		       :title="item.title"
			   @click="onChange(index)" 
		     >
			 <template slot="right-icon">
			    <van-radio-group style="width: 20upx;" :value="radio" >
			      <van-radio checked-color="#D9332E" :name="index" >
				  </van-radio>
			    </van-radio-group>
			 </template>
		     </van-cell>
		   </van-cell-group>
		 </van-radio-group>
		 
		 <view v-if="radio === 0"
		  style="border-top: 15upx solid #F5F5F5;
		  border-bottom: 15upx solid #F5F5F5;
		  "
		 >
		  <van-field 
		     @change="gethotelname"
		     :value="hotelname"
		     label="酒店"
			 disabled
		     placeholder="请输入酒店"
		   />
		   <van-field
		      @change="gethonename"
		      :value="honename"
		      label="房间号"
		      placeholder="请输入房间号"
		    />
			<van-field
			   @change="getusername"
			   :value="username"
			   label="收货人"
			   placeholder="请输入收货人"
			 />
			<van-field
			   @change="getphone"
			   :value="phone"
			   label="手机号"
			   placeholder="请输入手机号"
			 />
		    </view>
			
		 <view v-if="radio === 2"
		  style="border-top: 15upx solid #F5F5F5;
		  border-bottom: 15upx solid #F5F5F5;
		  "
		 >
		  <van-field 
		    @change="getreserve"
		     :value="reserve"
			 disabled
		     label="预定酒店"
		     placeholder="请输入预定酒店"
		   />
		   
		   <van-field
		    @change="getCheckedin"
		      :value="Checkedin"
		      label="入住人"
		      placeholder="请输入入住人"
		    />
			<van-field
			@change="getnumberphone"
			   :value="numberphone"
			   label="手机号"
			   placeholder="请输入手机号"
			 />
			 <van-field
			 @change="getremark"
			    :value="remark"
			    label="备注"
			    placeholder="请输入备注"
			  />
		    </view>
		
		<view v-if="radio == 2">
			<van-radio-group >
			   <van-cell-group 
			   >
				 <van-cell
				   :border="false"
				 
			     >
				 <template slot="title">
					 我已阅读并同意 <span @click="readshouknow" style="color: #007AFF">预定须知</span>
				 </template>
				 <template   @click="getread" slot="right-icon">
				    <van-radio-group style="width: 20upx;" :value="read" >
				      <van-radio checked-color="#D9332E" :name="4" >
					  </van-radio>
				    </van-radio-group>
				 </template>
			     </van-cell>
			   </van-cell-group>
			 </van-radio-group>
		</view>
		
		 <van-dialog
		   use-slot
		   title="标题"
		   :show="ishow"
		   @confirm="confirmOrder"
		   :show-cancel-button="false"
		 >
		  <view style="padding: 0 32rpx;">
			  <rich-text :nodes="store.buy_notice"></rich-text>
		  </view>
		 
		 </van-dialog>
		 <van-cell v-if="radio === 1&&choseadress == '' " @click="goAdress" class="LinkAdress" title="选择地址"  is-link/>
		  <view v-if="radio === 1&&choseadress != ''" @click="goAdress" class="wrapper">
		  	<view class="u-box" >
		  		<text class="name" style="padding-right: 20upx;">{{choseadress.address_realname}}</text>
		  		<text class="mobile">{{choseadress.address_mob_phone}}</text>
		  	</view>
		  	<view class="u-box">
		  		<text class="address">{{choseadress.area_info}} {{choseadress.address_detail}}</text>
		  	</view>
		  </view>
		  
		 <view @tap="goDetail()" class="cart-list">
		  <view class="topTip">
			   <view style="display: flex;align-items: center;border-bottom: 1px solid #F5F5F5;">
				<image :src="pictureUrl+'/uploads/home/store/'+store.store_id+'/'+store.store_logo" style="border-radius: 50%;
				width: 50upx;height: 50upx;"></image>
				<span style="padding-left: 8upx;">{{store.store_name}}</span>
			   </view>
		  </view>
		  
		  <view v-for="(item,index) in pricelist" :key="index" class="shoplist" style="background-color: #fff;">
			  <view>
				  <van-card
					title-class="changewidth"
					price-class ="priceClass"
					:price="item.goods_price"
					:desc="item.descire+'X'+item.goods_num"
					:title="item.desarray"
					:thumb="pictureUrl + '/uploads/home/store/goods/' + item.goods_image.substr(0, item.goods_image.indexOf('\_')) + '/' + item.goods_image"
				  >
				  </van-card>
			  </view>
		  </view>
			   <view class="topTip"  v-if="radio === 1">
				  <view style="font-size: 28upx;">运费：{{ralodprice}}</view>
			   </view>
		 </view>
		 <!-- 底部付款 -->
		 <view class="adressbutton">
		 		<view class="buttonStatus">
		 			<view @click="estimate" class="deleteOrder">应付<span>￥{{prices}}</span></view>
		 			<view class="payOrder" @click="goPay">付款</view>
		 		</view>
		 </view>
		 
		</view>
</template>

<script>
	
	export default {
		data() {
			return {
				ishow: false,
				firstprice: 0,
				read: 0,
				hotelname:'', // 
				honename:'',
				phone:'',
				username:'',
				reserve:'', // yudingjiudian
				Checkedin:'',
				numberphone:'',
				remark:'',
				ralodprice: 0,
				prices:'',
				pictureUrl: this.pictureUrl,
				list:[
					{title:'请送到我的房间'},
					{title:'快递到我的地址'},
					{title:'处理我的客房预定'}
				],
				
				shipping_type: 0,
				radio: 0,
				store: {},
				store_id:'',
				pricelist: [],
				choseadress: '',
				order_id:''
			}
		},
		onLoad(options) {
			this.order_id = options.order_id;
			this.store_id = options.store;
			// let prices =  Number((options.prices/100).toFixed(2));
			// this.prices = prices; 
			this.getorder(); // 获取订单详情
			 uni.$on("getadress",(adress)=>{
			        this.choseadress = adress;
					this.address_id = this.choseadress.address_id;
					//this.getorder();
					this.getralodprice();
			 })
		},
		methods: {
			readshouknow() {
				this.ishow = true;
			},
			confirmOrder() {
				this.ishow = false;
			},
			getread() {
				this.read = 4;
			},
			
				gethonename(e) {
					this.honename = e.detail;
				},
				getphone(e) {
					this.phone = e.detail;
				},
				getusername(e) {
					this.username = e.detail;
				},
				getCheckedin(e) {
					this.Checkedin = e.detail;
				},
				getnumberphone(e) {
					this.numberphone = e.detail;
				},
				getremark(e) {
					this.remark = e.detail;
				},
			// 获取订单详情
			getorder() {
				this.request({
					url: '/v2/order/og',
					method: 'POST',
					data: {
						order_id: this.order_id,
						store_id: this.store_id
					},
					success: (res) => {
						let { data } = res.data;
						this.store = data.store;
						let totalprice = 0;
						data.og_list.forEach(item => {
						   let arr = item.goods_name.split(' ');
						   item.desarray = arr[0];
						   item.descire = item.goods_name.replace(arr[0], '');
						   totalprice+=Number(item.goods_pay_price)*Number(item.goods_num);
						})
						this.hotelname = this.store.store_name
						this.reserve = this.store.store_name
						this.pricelist = data.og_list;
						this.firstprice = totalprice;
						this.prices = totalprice;
					}
				})
			},
			// 付款
			goPay() {
				let reg_phone = /^1[3456789]\d{9}$/;
				if(this.radio == 1) {
					this.gopaymoment();
				}else if(this.radio == 0){
					if(this.hotelname == '') {
						this.$msg("请输入酒店名")
						return;
					}else if(this.honename == '') {
						this.$msg("请输入房间号")
						return;
						
					}else if( !reg_phone.test(this.phone)) {
						this.$msg("请输入正确手机号")
						return;
					}else if(this.username == '') {
						this.$msg("请输入收货人")
						return;
					}
					this.request({
						url:'/v2/address/add',
						method:'post',
						data: {
							is_free: 1,
							area_info: this.store.area_info,
							address_detail:this.hotelname +  this.honename,
							address_mob_phone: this.phone,
							address_realname: this.username,
							area_id: this.store.region_id
						},
						success: (res) => {
							this.address_id = res.data.data.address_id;
							this.shipping_type = 1;
							this.gopaymoment();
						}
					})
					
				}else if(this.radio == 2) {
					if(this.reserve == '') {
						this.$msg("请输入预定酒店")
						return;
					}else if(this.Checkedin == '') {
						this.$msg("请输入入住人")
						return;
					}else if( !reg_phone.test(this.numberphone)) {
						this.$msg("请输入正确手机号")
						return;
					}else if(this.read !=4) {
						this.$msg("请阅读预定须知");
						return;
					}
					this.request({
						url:'/v2/address/add',
						method:'post',
						data: {
							is_free: 1,
							area_info: this.store.area_info,
							address_detail:this.reserve,
							address_mob_phone: this.numberphone,
							address_realname: this.Checkedin,
							area_id: this.store.region_id
						},
						success: (res) => {
							this.address_id = res.data.data.address_id;
							this.shipping_type = 1;
							this.gopaymoment();
						}
					})
					
				}
				
			},
			// 下单
			gopaymoment(){
				this.request({
					url:'/v2/order/create',
					method: 'post',
					data: {
						gorderid: this.order_id,
						address_id: this.address_id,
						store_id: this.store.store_id,
						shipping_type: this.shipping_type,
						remark: this.remark
					},
					success: (res) => {
						let order_id = res.data.data.order_id; // 订单id
						let prices = this.prices;
						uni.navigateTo({
							url:`/pages/myOrder/paymoment?order_id=${order_id}&prices=${prices}`
						})
					}
				})
			},
			// 核算运费
			getralodprice() {
				this.request({
					url: '/v2/entry/orderFreight',
					method: 'POST',
					data: {
						order_id: this.order_id,
						address_id:　this.address_id
					},
					success:(res) => {
						    let price = this.firstprice;
						    this.ralodprice = Number(res.data.data.shipping_fee);
							this.prices =  Number(price) + this.ralodprice;
					}
				})
			},
			onChange(index) {
				this.radio = index;
					
				if(index == 1) {
					this.shipping_type = 0; // 订单付款快递类型
				}else {
					this.shipping_type = 1;
				}
			},
			goAdress() {
			    
				uni.navigateTo({
				                   url: '../myOrder/adress',
				               }); 
			}
		}
	}
	
</script>

<style lang="scss">
	.LinkAdress /deep/ .van-cell {
		border-top: 15upx solid #F5F5F5;
		border-bottom: 15upx solid #F5F5F5;
	}
	.wrapper{
		border-top: 15rpx solid #F5F5F5;
		border-bottom: 15rpx solid #F5F5F5;
        padding: 0 32upx;
		background-color: #fff;
	}
	.u-box {
		padding: 15upx 0;
	}
	.topTip {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20upx 36upx;
		border-bottom: 1px solid #F5F5F5;
		background-color: #fff;
	}
	.changewidth {
		color: #303133;
		font-size: 32upx;
		font-weight: 100!important;
	}
	
	.van-card {
		background-color: #fff!important;
	}
	.shoplist {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.adressbutton {
		position: fixed;
		bottom: 0;
		width: 100%;
		.buttonStatus {
			display: flex;
			text-align: left;
			height: 100upx;
			line-height: 100upx;
			.deleteOrder {
				flex: 6;
				color: #303133;
				font-size: 32upx;
				border-top: 1px solid #f5f5f5;
				background-color: #fff;
				padding: 0 0 0 32upx;
			}
			.payOrder {
				flex: 3;
				font-size: 32upx;
				color: #fff;
				background-color: #D9332E;
				text-align: center;
			}
		}
	}
</style>
