<template>
	<view class="container" v-if="townList.length && hotelList.length">
		<view class="title">选择乡镇</view>
		<!-- 乡镇列表区域 -->
		<view class="town_list">
			<!-- 每一个乡镇 -->
			<view class="town_box" :class="{active:activeIndex ===index}" v-for="(item, index) in townList" :key="index"
				@click="handleChange(index)">{{ item.name }}</view>
		</view>
		<view class="title2">关联民宿</view>
		<!-- 民宿列表区域 -->
		<view class="hotel_list">
			<!-- 每一个民宿 -->
			<view class="hotel_box" v-for="item in hotelList" :key="item.id">
				<view class="box_radio">
					<radio style="transform:scale(1.2)" color="#096562" :checked="item.is_collect_hotel"
						@click="handleClickRadio(item)" /></label>
				</view>
				<view class="box_info">
					<img mode="aspectFill" :src="item.coverImg">
					<view class="info_detail">
						<view class="detail_name">
							{{item.hotel_name}}
						</view>
						<view class="detail_leave">
							{{item.hTypeName}}
						</view>
						<view class="detail_rate">
							{{item.score.toFixed(1)}}分
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 确定按钮区域 -->
		<view class="btn" @click="handleClickBtn">
			确定
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 乡镇数组
				townList: [],
				// 民宿列表
				hotelList: [],
				// 当前高亮索引
				activeIndex: 0
			}
		},
		onLoad() {
			this.getTownList()
			this.getHotelList()
		},
		methods: {
			// 获取乡镇列表数组
			async getTownList() {
				const res = await this.$myRequest({
					url: '/mhotel/ahpgetResidueCount.action'
				})
				// console.log(res)
				if (res.code === 200) {
					this.townList = res.data
				}
			},
			// 获取民宿列表数组
			async getHotelList() {
				const res = await this.$myRequest({
					url: '/mhotel/ahphomePage.action',
					data: {
						page: 1,
						rows: 4,
						type: 3,
						userId: uni.getStorageSync('userInfo') ? uni.getStorageSync('userInfo').id : ''
					}
				})
				// console.log(res)
				if (res.code === 200) {
					this.hotelList = res.data.pageList
				}
			},
			// 切换乡镇回调
			handleChange(index) {
				this.activeIndex = index
			},
			// 点击radio回调
			handleClickRadio(item) {
				item.is_collect_hotel = !item.is_collect_hotel
			},
			// 确定按钮点击回调
			handleClickBtn() {
				let temList = this.hotelList.filter(ele => ele.is_collect_hotel)
				if (temList.length) {
					uni.$emit('add', {
						list: temList
					})
					uni.navigateBack(1)
				} else {
					uni.showToast({
						title: "请关联至少一个民宿",
						icon: 'none',
						mask: true
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 0 19rpx 80rpx;
		min-height: 100vh;
		background-color: #fff;

		.title {
			height: 90rpx;
			line-height: 90rpx;
			font-size: 32rpx;
			font-weight: bold;
		}

		.town_list {
			display: flex;
			flex-wrap: wrap;

			.town_box {
				display: flex;
				justify-content: center;
				align-items: center;
				padding: 0 39rpx;
				margin-right: 19rpx;
				margin-bottom: 24rpx;
				height: 65rpx;
				color: #808080;
				font-size: 28rpx;
				border-radius: 74rpx;
				background-color: #e6e6e6;
			}

			.active {
				color: #fff;
				background-color: #096562;
			}
		}

		.title2 {
			height: 80rpx;
			font-size: 32rpx;
			font-weight: bold;
		}

		.hotel_list {
			.hotel_box {
				display: flex;
				margin-bottom: 20rpx;
				width: 710rpx;
				height: 150rpx;

				.box_radio {
					display: flex;
					justify-content: center;
					align-items: center;
					width: 95rpx;
				}

				.box_info {
					flex: 1;
					display: flex;
					background-color: #F2F2F2;
					overflow: hidden;

					img {
						width: 126rpx;
						height: 150rpx;
					}

					.info_detail {
						flex: 1;
						display: flex;
						flex-direction: column;
						justify-content: space-evenly;
						padding: 0 23rpx;
						overflow: hidden;

						.detail_name {
							font-size: 28rpx;
							font-weight: bold;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
						}

						.detail_leave {
							color: #808080;
							font-size: 24rpx;
						}

						.detail_rate {
							color: #FF5733;
							font-size: 24rpx;
						}
					}
				}
			}
		}

		.btn {
			display: flex;
			justify-content: center;
			align-items: center;
			margin: 200rpx auto 0;
			width: 710rpx;
			height: 100rpx;
			color: #fff;
			font-size: 32rpx;
			border-radius: 64rpx;
			background-color: #096562;
		}
	}
</style>