<template>
	<view class="content">
	<!-- <view>
		<view>点击授权{{hasLogin}}</view>
		<button @click="wxlogion"   open-type="getUserInfo">请点击授权</button>
	 </view> -->
	 <logionDialog @confirmDiaolog="confirmDiaolog"  @cancelDiaolog="cancelDiaolog" :showdialog="showdialog"/>
	 <scroll-view  style="height: 100vh;" class="floor-list"
	  :scroll-top="scrollTop" scroll-y="true"  @scroll="scroll"  @scrolltoupper="upper" @scrolltolower="lower"
	  refresher-enabled="true">
	 <uniNavBar class="top"  :shadow="false"  :fixed="false" color="#333333" background-color="#FFFFFF">
	 	<block  style="width: 70%;padding-left: 32upx;" slot="left">
	 		<view class="city" v-if="!allow">
	 			<view style="font-size: 35upx;">{{ location }}</view>
	 		</view>
			<button plain="true" style="border: none;" 
			  v-if="allow" @opensetting='handler'
			  open-type="openSetting" >定位失败，点击重新获取</button>
	 	</block>
			<!-- <view class="input-view" style="float: right;display: none;">
			  <uni-icon color="#606266" size="22" @click="scan" type="scan"></uni-icon>
			</view> -->
		<block  slot="right">
			<view  @click="goinfor">
				<uni-icon color="#606266" size="22" type="chat"></uni-icon>
			</view>
		</block>
	 </uniNavBar>
	<view @click="goSearch" class="searchType" style="background: #fff;">
		 <view class="input-view">
		 	<uni-icon type="search" size="22" color="#666666" />
		 	<input confirm-type="search" class="input" type="text" placeholder="搜索您入住的酒店" @confirm="confirm" />
		 </view>
	 </view>
	 <view  class="scrollswiper">
		 <swiper class="swiper" 
		 :indicator-dots="indicatorDots" 
		 :autoplay="autoplay" 
		 :interval="interval" 
		 :duration="duration"
		 > 
			<swiper-item v-for="(item,index) in backgroundlist" :key="index">
			<view class="swiper-item"  @tap="shopDetail(item)" >
				 <image style="height: 100%;width: 100%;" :src='pictureUrl +"/uploads/home/adv/"+item.adv_code' />
		    </view>
			</swiper-item>
			
		 </swiper>
	 </view>
	 <button type="primary" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">微信登录</button>
	 <view class="tips">
		 在附近酒店找到您入住的酒店
	 </view>
	 <view style="border-bottom: 2upx solid #EEEEEE;width: 100%;margin-bottom: 24upx 0;"></view>
	
	<view v-if="!allow">
     <view class="hotelList" v-for='(item,index) in hotelist' :key="index" @click="openinfo(item)">
		 <view class="hotleLeft">
			 <image  style="width: 214upx;height: 152upx;" :src="pictureUrl+'/uploads/home/adv/'+item.store_logo"/>
		 </view>
		 <view class="hotelright">
		 	<view style="font-size: 32upx;color: #303133;font-weight: 550;">{{item.store_name}}
			</view>
		    <view style="display: flex;flex-direction: row;justify-content: space-between;align-items: center;padding: 10rpx 0;">
				<view style="font-size: 32upx; color: #303133;font-weight: 550;">{{item.store_servicecredit}}</view>
				<view style="color: #606266;font-size: 24upx;">月访客{{item.store_visitor}}</view>
				<view style="color: #606266;font-size: 24upx;">{{item.distance}}</view>
			</view>
			<view  v-if="item.tages.length!=1" class="undertip">
				<view  v-for="(items,index) in item.tages" :key="index" class="tipsTop">{{items}}</view>
			</view>
	 </view>
	 </view>
	 </view>
	 <view v-if="allow" style="text-align: center;margin: 100upx 0;">
		 定位失败暂时获取不到周边酒店信息哦
	 </view>
     </scroll-view>

	</view>
</template>

<script>
	import {
		mapMutations,mapState
	} from 'vuex'
	import uniNavBar from '@/component/uni-nav-bar/uni-nav-bar.vue'
	import uniIcon from '@/component/uni-icon/uni-icon.vue'
	import logionDialog from '@/component/loginDialog.vue'
	import amap from '@/common/amap-wx.js'
	export default {
		components: {
			uniNavBar,
			uniIcon,
			logionDialog
		},
		data() {
			return {
				page: 1,
				ispull: true,
				pagesize: 10,
				store_summary:[], // 标签数组
				pictureUrl: this.pictureUrl,
				amapPlugin: null,  
				key: '7277de7a5833626b7da8f2496e9d6bf9',  
				showdialog: false,   // 点击确定显示弹窗
			    backgroundlist: ['color1', 'color2', 'color3'],  // 轮播数组
				hotelist:[], // 酒店数组
				indicatorDots: true,
				autoplay: true,
				interval: 2000,
				duration: 500,
				title: 'Hello',
				location:'正在定位...',
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				allow:　false
			}
		},
		computed:{
			...mapState([
			  'hasLogin',
			  'userInfo'
			]),
		},
		onLoad() {
			this.getswiper();   // 获取首页轮播
			this.gethotelist(); // 酒店列表
			if(this.allow) {
				
			}else {
				this.gethotelist(); // 酒店列表
			}
			this.isGetLocation();  // 检查是否开启定位权限
			this.isUserInfo();  // 检查是否允许获取用户授权
			 // 用户授全
		    this.amapPlugin = new amap.AMapWX({  
				key: this.key  
		    });  		
	    },
		methods: {
			...mapMutations(['login','showDialog']),
			  decryptPhoneNumber(e) {    
			                console.log(e);    
			                if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {    
			                    console.log(e);
			                } else {    
			                    console.log('用户同意提供手机号');
								console.log(JSON.stringify(e.detail.encryptedData));
								console.log(JSON.stringify(e.detail.iv));
								let encryptedData = e.detail.encryptedData;
								let iv = e.detail.iv;
								console.log( iv );
								console.log( encryptedData );
						    }
			    },
			handler() {
				console.log("kkkkk")
				this.getAuthorizeInfo();
			},
			goinfor() {
				if(this.hasLogin) {
					uni.navigateTo({
					   url: '../myCenter/information'  // 我的收藏
					})
				}else {
					this.showdialog = true;
				}
				
			},
			// 关闭弹窗
			cancelDiaolog() {
				this.showdialog = false;
			},
			// 确定
			confirmDiaolog() {
				this.showdialog = false;
				uni.navigateTo({
				   url: '../myCenter/information'  // 我的收藏
				})
			},
			goSearch() {
				uni.navigateTo({
					url: './searchresult'  // 我的搜索
				})
			},
			// 滚动到顶部
			upper(e) {
				
				console.log("顶部")
			},
			
			// 滚到底部
			lower(e) {
				this.page = this.page + 1;
				if(this.ispull) {
					this.gethotelist();
				}
			},
			
			// 滚动时触发
			scroll(e) {
				this.old.scrollTop = e.detail.scrollTop
			},
			isGetLocation(a="scope.userLocation"){ //  检查当前是否已经授权访问scope属性W
					let _this=this;
					uni.getSetting({
					    success(res) {
							// 用户定位接口
							if (!res.authSetting[a]) {  // 每次进入程序判断当前是否获得授权，如果没有就去获得授权，如果获得授权，就直接获取当前地理位置
								_this.getAuthorizeInfo()
							}else{
								_this.getRegeo()
							}
							
						}
					});
				},
			//  进入判断是否授权
			isUserInfo(b='scope.userInfo') {
				let _this = this;
				uni.getSetting({
					success(res) {
						if(!res.authSetting[b]) {
							console.log("没有授权")
							_this.showDialog(false);
						}else {
							console.log("yijing授权")
							_this.showDialog(true);
							// uni.getUserInfo({
							// 	     success(res) {
							// 		 console.log(res);
							// 		 console.log("oooooo")
							// 			 that.login(res.userInfo);
							// 			 //that.showDialog(true); // 改变haslogion的值
							// 	     }
							//     })
						}
					}
				})
			},
			getAuthorizeInfo(a="scope.userLocation"){  //uniapp弹窗弹出获取授权弹窗
					let _this=this;
					uni.authorize({
						scope: a,
						success(res) { //允许授权
						   console.log("调用位置授权")
							_this.getRegeo();
						},
						fail(){    //拒绝授权
						  _this.allow = true;
						  _this.$msg("你拒绝了授权，无法获得周边信息")
						}
					})
				},

		    getRegeo(){  //获取地理位置
			 let _this=this;
			 uni.showLoading({  
				title: '获取信息中'  
			}); 
			_this.amapPlugin.getRegeo({  
				success: (data) => {  
					this.location = data[0].name;
					this.allow = false;
					uni.hideLoading();  
				},
				fail: (res) => {
					console.log(res);
					uni.hideLoading(); 
				}
			});  
		   
		    },
			
			// 获取首页轮播
			 getswiper() {
			  
				this.request({
	                url:"/v1/entry/adv",
	                method:'get',
					success: (res) => {
						 this.backgroundlist = res.data.data;
					}
			   })
			 
			 },
		 // 获取酒店列表
		 gethotelist() {
			 this.request({
                 url:"/v2/entry/storeList",
                 method:'get',
                 data: {
                         page: this.page,
						 page_size: 10
                     },
				success: (res) => {
					 let { data,code } = res.data;
					 if(code == 1000) {
							 
						 if(data.store_list.length<10) {
							 this.ispull = false;
						 }
						 if(this.hotelist.length == 0) {
							 this.hotelist = data.store_list
						 }else {
							 this.hotelist =  this.hotelist.concat(data.store_list);
						 }
						this.hotelist.forEach(item => {
							item.tages = item.store_summary.split('  ');
						})
						
					 }else {
						 this.$msg("网络错误稍后再试")
					 }
				}
		   })
		 },
		 // 点击扫码
		 scan(){
		 	uni.scanCode({
		 		success: (res) => {
		 			console.log(res);
		 		}
		 	});
		 },
		 
		 // 跳转到新页面  酒店详情
		 openinfo(item) {
			 let id = item.store_id;
			  uni.navigateTo({
			                    url: `/pages/index/hotel?id=${id}`
			                 });
		 },
		 
		 // 跳转到商品详情
		 shopDetail(item) {
			 let id = item.adv_target.goods_id;
			 let store_id = item.adv_target.store_id;
			 if(item.adv_target.type == 'goods') {
				 uni.navigateTo({
									 url: `/pages/index/shop?id=${id}&store_id=${store_id}`
				                }); 
			 }else if(item.adv_target.type == 'store') {
				 let id = item.adv_target.store_id;
				 let title = item.adv_target.store_name;
				 uni.navigateTo({
				                    url: `/pages/index/hotel?id=${id}`
				                });
			 }
			
		 }
		
		}
}
</script>

<style>
   .top .uni-navbar__header-btns  {
		width: 77%!important;
	}
	.top .uni-navbar__header-container {
		width: 50%!important;
	}
		
	.content {
		background: #fff;
	}
	.city {
		width: 100%;
		height: 100%;
		white-space:nowrap;
		overflow:hidden;
		text-overflow:ellipsis;
	}
	.logo {
		height: 200upx;
		width: 200upx;
		margin-top: 200upx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50upx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36upx;
		color: #8f8f94;
	}
	.van-cell--borderless {
		background-color: #e7e7e7!important;
	}
	.searchType .input-view {
		width: 85%;
		display: flex;
		background-color: #e7e7e7;
		height: 30px;
		border-radius: 15px;
		padding: 0 4%;
		flex-wrap: nowrap;
		margin: 7px 0;
		line-height: 30px;
		margin: 24upx auto;
	}
	
	.searchType .input-view .uni-icon {
		line-height: 30px !important;
	}
	
	.searchType .input-view .input {
		height: 30px;
		line-height: 30px;
		width: 94%;
		padding: 0 3%;
	}
	.uni-navbar--border::after {
		background: #fff!important;
	}
	.swiper {
		height: 300upx;
	}
	.swiper-item {
		display: block;
		height: 300upx;
		line-height: 300upx;
		text-align: center;
	}
		
	.scrollswiper {
		width: 90%;
		margin: 20upx auto;
	}
		
	.tips {
		color: #303133;
		font-size: 28upx;
		text-align: center;
		margin: 24upx 0;
	}
		
    .hotelList {
		display: flex;
		flex-direction: row;
		padding: 24upx 32upx;
	}
		
	.hotleLeft {
		width: 214upx;
		height: 152upx;
	}
		
	.hotelright {
		padding-left: 16upx;
		display: flex;
		flex-direction: column;
		width: 100%;
	}
		
	.undertip {
		display: flex;
		flex-direction: row;
		align-items: center;
	}
	.tipsTop {
		border: 2upx solid #606266;
		margin-right: 12upx;
		color: #606266;
		padding: 4upx;
		font-size: 24upx;
		border-radius: 4rpx;
		padding: 0 10rpx;
	}
</style>
