<template>
	<view class="container">
		<!-- 搜索框区域 -->
		<uv-row custom-style="margin: 10px 0px" gutter="10">
			<picker @change="bindPickerChange" range-key="name" :value="placeIndex" :range="placeList">
				<view class="address">
					<view class="address_text">{{ placeList[placeIndex].name }}</view>
					<img src="../../static/index/bottom.png" />
				</view>
			</picker>

			<view class="search">
				<view class="add">
					<image class="img" src="../../static/index/search.png" mode="aspectFit"></image>
				</view>
				<input class="inp" type="text" v-model="keywords" placeholder="请输入关键字搜索" />
				<view class="btnSearch" @click="searchHandler">搜索</view>
			</view>
		</uv-row>

		<!-- 名宿列表区域 -->
		<view class="body" v-if="hotelList.length">
			<!-- 每一个名宿区域 -->
			<view class="item" v-for="item in hotelList" :key="item.id" @click="goPageDetail(item)">
				<image class="item-img" :src="item.coverImg" mode="scaleToFill"></image>
				<view class="descrition">
					<text class="title">{{ item.hotel_name }}</text>
					<text class="type">{{ item.hTypeName }}</text>
					<text class="distance" v-if="showdDistance">距您直线{{ item.distance }}公里</text>
					<view class="detail">
						<img class="img" src="../../static/index/hotel.png" />
						<view class="price">
							<text class="txt1">¥{{ item.min_price }}</text>
							<text class="txt2">起</text>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 没有数据时展示的页面 -->
		<view class="noData" v-else>
			<img src="../../static/images/noData.png" />
			暂无数据
		</view>

		<!-- 领取优惠券弹窗区域 -->
		<uv-popup ref="popup" mode="center" :closeOnClickOverlay="false" bgColor="none">
			<view class="popupClass">
				<img src="../../static/index/popup_bg.png" />
				<!-- 立即前往区域 -->
				<view class="btn_go" @click="handleGo"></view>
				<!-- 关闭按钮区域 -->
				<view class="btn_close" @click="handleClose"></view>
			</view>
		</uv-popup>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 是否显示距离差
			showdDistance: false,
			// 搜索框绑定数据
			keywords: '',
			// 民宿列表数组
			hotelList: [],
			// 地区数组
			placeList: [
				{
					name: '靖安县'
				}
			],
			// 当前选择地区索引
			placeIndex: 0,
			// 当前页
			page: 1,
			// 每页多少条
			rows: 6,
			// 总条数
			total: null,
			// 用户定位经度
			myLng: 0,
			// 用户定位纬度
			myLat: 0
		}
	},
	onLoad() {
		this.getLocation()
		this.getTownList()
		// this.$refs.popup.open()
	},
	// 页面下拉回调
	onPullDownRefresh() {
		setTimeout(() => {
			this.hotelList = []
			this.page = 1
			this.getLocation()
			uni.stopPullDownRefresh()
		}, 2000)
	},
	// 页面下拉到底部回调
	onReachBottom() {
		if (this.hotelList.length < this.total) {
			this.page++
			this.getHotelList()
		} else {
			uni.showToast({
				title: '没有更多数据了',
				icon: 'none'
			})
		}
	},
	methods: {
		// 获取乡镇集合
		async getTownList() {
			const res = await this.$myRequest({
				url: '/mhotel/hotelqueryList.action',
				method: 'get',
				data: {
					code: 10
				}
			})
			// console.log(res)
			if (res.code === 200) {
				this.placeList = [...this.placeList, ...res.data]
			}
		},
		// 获取用户当前位置
		getLocation() {
			uni.getSetting({
				success: (res) => {
					if (!res.authSetting['scope.userLocation']) {
						uni.authorize({
							scope: 'scope.userLocation',
							success: (res) => {
								// 授权成功
								uni.getLocation({
									type: 'gcj02',
									success: (res) => {
										this.myLat = res.latitude
										this.myLng = res.longitude
										this.showdDistance = true
										this.getHotelList()
									}
								})
							},
							fail: () => {
								uni.showModal({
									content: '获取定位权限失败将会影响使用部分功能，是否去设置打开？',
									confirmText: '确认',
									cancelText: '取消',
									success: (res) => {
										if (res.confirm) {
											uni.openSetting({
												success: (res) => {
													console.log(res)
													this.getLocation()
												}
											})
										} else {
											this.showdDistance = false
											this.getHotelList()
											uni.showToast({
												title: '获取定位权限失败',
												icon: 'none'
											})
										}
									}
								})
							}
						})
					} else {
						uni.getLocation({
							type: 'gcj02',
							success: (res) => {
								this.myLat = res.latitude
								this.myLng = res.longitude
								this.showdDistance = true
								this.getHotelList()
							}
						})
					}
				}
			})
		},
		// 获取民宿列表
		async getHotelList() {
			const res = await this.$myRequest({
				url: '/mhotel/ahphomePage.action',
				data: {
					queryValue: this.keywords,
					page: this.page,
					rows: this.rows,
					hotel_township: this.placeList[this.placeIndex].id || '',
					userId: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo').id : ''
				}
			})
			// console.log(res)
			if (res.code === 200) {
				this.hotelList = [...this.hotelList, ...res.data.pageList]
				this.total = res.data.total

				// 如果定位成功则获取和民宿之间的距离
				if (this.showdDistance && this.hotelList.length) {
					this.hotelList.forEach((ele) => {
						let lat = ele.hpositionWens.split(',')[0]
						let lng = ele.hpositionWens.split(',')[1]
						ele.distance = this.calculateDistance(lat, lng)
					})
				}
			}
		},
		// 计算两个点之间的距离
		calculateDistance(lat, lng) {
			let centerLat = lat
			let centerLng = lng

			let red1 = (this.myLat * Math.PI) / 180.0
			let red2 = (centerLat * Math.PI) / 180.0
			let a = red1 - red2
			let b = (this.myLng * Math.PI) / 180.0 - (centerLng * Math.PI) / 180.0
			let R = 6378137
			let distance = R * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(red1) * Math.cos(red2) * Math.pow(Math.sin(b / 2), 2)))
			let res = (distance / 1000).toFixed(2) * 1

			return res
		},

		// 搜索按钮点击回调
		searchHandler() {
			this.hotelList = []
			this.page = 1
			this.getHotelList()
		},
		// 点击每一个民宿卡片回调
		goPageDetail(item) {
			uni.navigateTo({
				url: `/pages/detail/detail?id=${item.id}&distance=${item.distance}`
			})
		},
		// 选择地区时的回调
		bindPickerChange(e) {
			this.placeIndex = e.detail.value
			this.hotelList = []
			this.page = 1
			this.getHotelList()
		},
		// 立即前往按钮回调
		handleGo() {
			console.log(111)
		},
		// 优惠券弹窗关闭按钮回调
		handleClose() {
			this.$refs.popup.close()
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	flex-direction: column;
	width: 750rpx;
	min-height: 100vh;
	padding: 0 30rpx;
	box-sizing: border-box;
	background-color: #f7f7f7;

	.address {
		display: flex;
		width: 152rpx;
		font-size: 28rpx;

		.address_text {
			width: 104rpx;
			text-align: center;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
		}

		img {
			width: 48rpx;
			height: 48rpx;
		}
	}

	.search {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 538rpx;
		height: 80rpx;
		opacity: 1;
		border-radius: 70px;
		background-color: #fff;

		.add {
			display: flex;
			justify-content: center;
			align-items: center;
			margin-left: 10rpx;
			width: 60rpx;
			font-size: 50rpx;
			height: 60rpx;
			line-height: 60rpx;
			color: rgba(30, 125, 251, 1);

			.img {
				width: 30rpx;
				height: 30rpx;
			}
		}

		.inp {
			height: 60rpx;
			line-height: 60rpx;
			flex-grow: 1;
			font-size: 28rpx;
		}

		.btnSearch {
			width: 100rpx;
			text-align: center;
			margin-right: 10rpx;
			height: 60rpx;
			line-height: 60rpx;
			opacity: 1;
			font-size: 28rpx;
			font-weight: 400;
			height: 2rem;
			color: #096562;
		}
	}

	.body {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;

		.item {
			width: 335rpx;
			box-sizing: border-box;
			margin-bottom: 20rpx;

			.item-img {
				width: 100%;
				height: 223rpx;
				border-radius: 18rpx 18rpx 0 0;
				box-sizing: border-box;
			}

			.descrition {
				display: flex;
				flex-direction: column;
				width: 100%;
				border-radius: 0 0 18rpx 18rpx;
				box-sizing: border-box;
				background: rgba(255, 255, 255, 1);
				margin-top: -10rpx;

				.title {
					font-size: 28rpx;
					font-weight: 600;
					padding: 20rpx 20rpx 10rpx;
					color: rgba(0, 0, 0, 1);
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
				}

				.type {
					padding: 5rpx 20rpx;
					height: 40rpx;
					font-size: 24rpx;
					color: #a6a6a6;
				}

				.distance {
					padding: 10rpx 20rpx;
					font-size: 24rpx;
					color: #a6a6a6;
				}

				.detail {
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					align-items: center;
					padding: 0 20rpx 20rpx 20rpx;
					color: rgba(0, 0, 0, 1);

					.img {
						width: 40rpx;
						height: 40rpx;
					}

					.price {
						.txt1 {
							font-size: 36rpx;
							font-weight: 600;
							color: rgba(255, 87, 51, 1);
						}

						.txt2 {
							font-size: 24rpx;
							font-weight: 400;
							color: #a6a6a6;
						}
					}

					.score {
						font-size: 24rpx;
						font-weight: 400;
						padding-top: 10rpx;
						color: rgba(166, 166, 166, 1);
					}
				}
			}
		}
	}

	.noData {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		img {
			margin-top: 150rpx;
			width: 600rpx;
			height: 600rpx;
		}
	}

	.popupClass {
		position: relative;
		width: 481rpx;
		height: 764rpx;

		img {
			width: 100%;
			height: 665rpx;
		}

		.btn_go {
			position: absolute;
			top: 415rpx;
			left: 40rpx;
			width: 396rpx;
			height: 76rpx;
			border-radius: 43rpx;
		}

		.btn_close {
			position: absolute;
			top: 586rpx;
			left: 212rpx;
			width: 58rpx;
			height: 58rpx;
			border-radius: 50%;
		}
	}
}
</style>