<template>
	<view class="container" v-if="townList.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" v-if="hotelList.length">
			<!-- 每一个民宿 -->
			<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.isCheck"
						@click="handleClickRadio(item)" /></label>
				</view>
				<view class="box_info">
					<img mode="aspectFill" :src="item.coverImg">
					<view class="info_detail">
						<view class="detail_name">
							{{item.name}}
						</view>
						<view class="detail_leave" v-if="item.type===1">
							银宿级
						</view>
						<view class="detail_leave" v-if="item.type===2">
							金宿级
						</view>
						<view class="detail_leave" v-if="item.type===3">
							白金级
						</view>
						<view class="detail_rate">
							{{item.score.toFixed(1)}}分
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 没有数据时展示的区域 -->
		<view class="noData" v-else>
			<img src="../../static/images/noData.png" />
			暂无数据
		</view>

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

<script>
	export default {
		data() {
			return {
				// 乡镇数组
				townList: [],
				// 民宿列表
				hotelList: [],
				// 当前高亮索引
				activeIndex: 0,
				// 之前选中的数组
				oldList:[],
				// 之前选中的城镇id
				oldTownId:''
			}
		},
		onLoad(options) {
			this.oldList = JSON.parse(options.list)
			this.oldTownId = options.townId
			this.getTownList()
		},
		methods: {
			// 获取乡镇列表数组
			async getTownList() {
				const res = await this.$myRequest({
					url: '/mhotel/articletownShips.action'
				})
				// console.log(res)
				if (res.code === 200) {
					this.townList = res.data
					this.townList.forEach((ele,index)=>{
						if(ele.id == this.oldTownId){
							this.activeIndex = index
						}
					})
					this.getHotelList()
				}
			},
			// 获取民宿列表数组
			async getHotelList() {
				const res = await this.$myRequest({
					url: '/mhotel/articlehotelByid.action',
					data: {
						townId:this.townList[this.activeIndex].id,
						page: 1,
						rows: 999,
					}
				})
				// console.log(res)
				if (res.code === 200) {
					this.hotelList = res.data.pageList
					// 添加isCheck属性
					this.hotelList.forEach(ele=>{
						this.$set(ele,'isCheck',false)
					})
					this.hotelList.forEach((ele)=>{
				         this.oldList.forEach((item)=>{
				         	if(item.id ===ele.id){
								ele.isCheck = true
							}
				         })
					})
				}
			},
			// 切换乡镇回调
			handleChange(index) {
				this.activeIndex = index
				this.getHotelList()
			},
			// 点击radio回调
			handleClickRadio(item) {
				item.isCheck = !item.isCheck
			},
			// 确定按钮点击回调
			handleClickBtn() {
				let temList = this.hotelList.filter(ele => ele.isCheck)
				if (temList.length) {
					uni.$emit('add', {
						list: temList,
						townId:this.townList[this.activeIndex].id
					})
					uni.navigateBack(1)
				} else {
					uni.showToast({
						title: "请关联至少一个民宿",
						icon: 'none',
						mask: true
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		box-sizing: border-box;
		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;
						}
					}
				}
			}
		}
		
		.noData {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
		
			img {
				margin-top: 60rpx;
				width: 400rpx;
				height: 400rpx;
			}
		}

		.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>