<template>
	<view style="width: 100%;height:　100vh;background-color: #F5F5F5;">
		<scroll-view  class="floor-list"
		 :scroll-top="scrollTop" scroll-y="true" 
		 :refresher-enabled="false">
		<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="is_traffic == 0 && radio === 0" style="background-color: #D9332E;color: #fff;margin: 10upx 0;
		 padding: 0 32upx;">
			 酒店现在是休息时间,可能无法及时为您配送
		 </view>
		 <view v-if="radio === 0"
		  style="border-top: 15upx solid #F5F5F5;
		  border-bottom: 15upx solid #F5F5F5;
		  "
		 >
		  <van-field 
		     @change="gethotelname"
		     :value="hotelname"
			 style="color: #323233;"
		     label="酒店"
			 readonly
		     placeholder="请输入酒店"
		   />
		   <view @click="goshopvalue">
			   <van-field
			      :disabled="first==1&&!pleasesure"
			   	  @change="getplaese"
			      :value="honename"
			      label="房间号"
			      placeholder="请输入房间号"
			    />
		   </view>
		  
			
			<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"
			 readonly
		     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
		  :show="pleasesure"
		  :showCancelButton='true'
		  cancel-button-text="返回"	
		  @confirm="confirmstore"
		  confirm-button-color="#D9332E"	
		  @cancel="cancelstore"
		>
		 <view style="padding: 50upx 32rpx;text-align: center;">
			<view style="padding: 15upx 0;">购物前请确认</view>
			<view  style="padding: 15upx 0;font-size: 38rpx;color: #D9332E;font-weight: bold;">{{hotelname}}</view>
			<view  style="padding: 15upx 0;">是否您入住的酒店</view>
		 </view>
		</van-dialog>
		
		
		 <van-dialog
		   use-slot
		   title="预订须知"
		   :show="ishow"
		   @confirm="confirmOrder"
		   :show-cancel-button="false"
		 >
		  <view style="padding: 0 32rpx;">
			  <scroll-view style="height: 40vh;" :scroll-top="scrollTop"  scroll-y="true" :refresher-enabled="false">
				  <rich-text :nodes="store.buy_notice"></rich-text>
			  </scroll-view>
		  </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;">
				<image :src="pictureUrl+'/uploads/home/store/'+store.store_id+'/'+store.store_banner" 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" style="border-top: 1px solid #eee;" v-if="radio === 1">
				  <view style="font-size: 28upx;">运费：￥{{ralodprice}}</view>
			   </view>
		 </view>
		 </scroll-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>
	import {
		mapMutations,mapState
	} from 'vuex'
	export default {
		data() {
			return {
				is_traffic: 1,
				pleasesure: false,
				first: 1,
				scrollTop: 0,
				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:'',
				address_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(); // 获取订单详情
			this.getuserinfo(); // 获取会员详情
			 uni.$on("getadress",(adress)=>{
			        this.choseadress = adress;
					this.address_id = this.choseadress.address_id;
					//this.getorder();
					this.getralodprice();
			 })
		},
		onUnload() {
			uni.$off();
		},
		
		methods: {
			// 确定入住酒店
			confirmstore() {
				this.pleasesure = false;
			},
			// 返回确认入住酒店
			cancelstore() {
				uni.navigateBack();
			},
			getuserinfo() {
				this.request({
					url:'/v2/member/info',
					method:'GET',
					success: (res)=>{
						let { data } = res.data;
						this.username = data.member_truename;
						this.phone = data.member_mobile;
						this.numberphone = data.member_mobile;
						this.Checkedin = data.member_truename;
					}
				})
			},
			readshouknow() {
				if(this.store.buy_notice!='') {
					this.ishow = true;
				}else {
					this.$msg("暂无预订须知哦")
				}
				
			},
			confirmOrder() {
				this.ishow = false;
			},
			getread() {
				this.read = 4;
			},
			
			getplaese(e) {
				this.honename = e.detail;
			},
			goshopvalue() {
				if(!this.pleasesure && this.first == 1) {
					this.pleasesure = true;
					this.first = 2;
					return;
				}
			},	
				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*item.goods_num);
						})
						this.is_traffic = data.is_traffic;
						this.hotelname = this.store.store_name
						this.reserve = this.store.store_name
						this.pricelist = data.og_list;
						this.firstprice = totalprice;
						this.prices = totalprice.toFixed(2);
					}
				})
			},
			// 付款
			goPay() {
				let reg_phone = /^1[3456789]\d{9}$/;
				if(this.radio == 1) {
					if(this.address_id == '') {
						this.$msg("请选择地址")
						return;
					}
					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) => {
						if(res.data.code == 1000) {
							let order_id = res.data.data.order_id; // 订单id
							let store_id  = this.store.store_id;
							let prices = this.prices;
							let isform = 2;
							uni.redirectTo({
								url:`/pages/myOrder/paymoment?order_id=${order_id}&prices=${prices}&isform=${isform}&store_id=${store_id}`
							})
						}
						
					}
				})
			},
			// 核算运费
			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).toFixed(2);;
					}
				})
			},
			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>
