<template>
	<view class="functionhouse">
		<scroll-view class="nav-scroll" scroll-x="true">
			<view class="navs">
				<text @tap="tabChange(item,index)" :class="[tabIndex==index?'active':'']" v-for="(item,index) in tabs"
					:key="index">{{item}}</text>
			</view>

			<view class="notices">
				<uni-notice-bar @click="toggle()" show-icon scrollable :text="noticeText" color="#202020"
					background-color="white" />
			</view>
			<view class="search">
				<uni-search-bar cancel-text="搜索" placeholder="请输入房间名" @clear="clear" @cancel="search" @confirm="search"
					show-action v-model="queryParams.roomName">
				</uni-search-bar>
			</view>
		</scroll-view>

		<view style="padding-bottom: 300rpx;"></view>

		<scroll-view class="scroll-views" scroll-y="true" @scrolltolower="scrolltolower">
			<view class="list">
				<view v-for="(item,index) in list" :key="index" class="item" @tap="toDetailPage(item)">
					<image :src="basePath+item.roomImgs"></image>
					<!-- <image v-else></image> -->
					<view class="box">
						<view class="title">
							<text>{{item.roomName}}</text>
							<view v-for="(tab,index) in rolelist" :key="index" v-if="item.roomStatus==tab.dictValue"
								:class="[item.roomStatus==1?'open-up':'',item.roomStatus==0?'shutdown':'',item.roomStatus==2?'maintenance':'']"
								style="">{{tab.dictLabel}}</view>
						</view>
						<view>
							<text class="introduction">{{item.roomInfo}}</text>
						</view>
						<view class="bottom">
							<view style="display: flex;">
								<view>
									<uni-icons color="#FFBB12" type="staff-filled" size="21"></uni-icons>
								</view>
								<view>
									<text>{{item.roomCapacity}}/人</text>
								</view>
							</view>
							<view>
								<text>
									详情
								</text>
								<uni-icons color="#272636" type="forward" size="16"></uni-icons>
							</view>
						</view>
					</view>
				</view>
			</view>

			<view class="empty" v-if="list.length==0&&topLoading==false">
				<image src="@/static/empty.png" mode="widthFix"></image>
				暂时没有数据
			</view>
			<view class="loading-more-top" v-if="topLoading">
				<uni-load-more style="padding-top: 90px;" status="loading" />
			</view>
			<view class="loading-more" v-if="loading">
				<uni-load-more status="loading" />
			</view>
			<view class="no-more" v-if="!loading&&list.length!=0">
				到底啦~~
			</view>
		</scroll-view>
		<uni-popup ref="popup">
			<view class="container">
				<view class="title">
					<text>
						学生社区简介
					</text>
				</view>

				<view class="introduction">
					<text class="introduction-1">
						一站式学生社区欢迎你~~
					</text>
					<view class="introduction-a">
						<view>
							<text>联系电话：0771-2033319</text>
						</view>
						<view>
							<text>QQ：2848561721</text>
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		listRoom
	} from '@/api/OneStopCommunity/room.js';
	import {
		getDicts
	} from '@/api/system/dict/data.js';
	import {
		baseUrl
	} from "@/config.js";
	export default {
		data() {
			return {
				basePath: baseUrl,
				topLoading: true,
				loading: false,
				tabs: ["全部", "开放中", "关闭中"],
				tabIndex: 0,
				noticeText: "一站式社区欢迎你 ！！！",
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					roomStatus: ""
				},
				list: [

				],
				rolelist: [],
				imgsrc: ""
			}
		},
		methods: {
			cancel(res) {
				this.queryParams.roomName = ""
				this.getlist()
			},
			clear(res) {
				this.queryParams.roomName = ""
				this.search() // 直接调用统一的搜索方法  知无涯
			},
			search() {
				// 重置为第一页并清空当前列表  知无涯
				this.queryParams.pageNum = 1
				this.list = [] // 清空现有数据  知无涯
				this.topLoading = true // 显示顶部加载状态  知无涯
				this.getlist()
			},
			toggle() {
				// open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
				this.$refs.popup.open("center")
			},
			tabChange(item, index) {
				this.tabIndex = index;
				switch (item) {
					case "全部":
						this.queryParams.roomStatus = ""
						this.getlist()
						break;
					case "开放中":
						this.queryParams.roomStatus = 1
						this.getlist()
						break;
					case "关闭中":
						this.queryParams.roomStatus = 0
						this.getlist()
						break;
					default:
				}
			},
			//详情
			toDetailPage(item) {
				console.log(item)
				uni.navigateTo({
					url: `./detail?id=${item.roomId}`
				})
			},
			scrolltolower() {
				if (!this.loading && this.queryParams.pageNum < this.totalPages) {
					this.queryParams.pageNum++
					this.loading = true
					this.getlist()
				}
			},
			async getlist() {
				let res = await listRoom(this.queryParams)
				if (res.code == 200) {
					// 处理图片数据  知无涯
					const arr = res.rows.map(item => {
						return {
							...item,
							roomImgs: item.roomImgs ? item.roomImgs.split(",")[0] : null
						}
					})

					// 统一处理数据追加逻辑  知无涯
					this.list = this.queryParams.pageNum === 1 ?
						arr : [...this.list, ...arr]

					this.totalPages = Math.ceil(res.total / this.queryParams.pageSize)
					this.loading = false
					this.topLoading = false
				}
			},
			async getstate() {
				let res = await getDicts('rt_fu_room_status')
				this.rolelist = res.data
				console.log(res.data)
			},
		},
		onShow() {
			this.getlist()
			this.getstate()
		},
	}
</script>

<style lang="scss" scoped>
	.functionhouse {
		background-color: #F5F5F7;

		.search {
			width: 100%;
			background-color: white;
			// position: fixed;
			z-index: 10;
			// top: 80rpx;
			// background-color: #F5F5F7;

			/deep/ .uni-searchbar__box {
				// .uni-searchbar__box
				// background-color: white !important;
			}
		}

		.nav-scroll {
			position: fixed;
			white-space: nowrap;
			width: 100%;
			background-color: white;
			z-index: 2;

			.navs {
				padding: 26rpx 47rpx;

				text {
					font-size: 28rpx;
					margin-right: 55rpx;
					color: #202020;
					opacity: 0.5;

					&.active {
						opacity: 1;
						font-weight: bold;
						border-bottom: 5px solid #1890FF;
						padding-bottom: 2px;
					}
				}
			}

			.notices {
				border-top: 1px solid #ECF1F7;

				/deep/ .uni-icons {
					color: #1890FF !important;
				}

				.uni-noticebar {
					margin-bottom: 0;
				}
			}

			scroll-view {
				position: fixed;
				z-index: 2;
			}
		}

		.scroll-views {
			height: calc(100vh - 10px);

			.list {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;
				align-content: flex-start;
				padding: 0rpx 24rpx;

				.item {
					width: 48%;
					// width: 324rpx;
					// height: 480rpx;
					background-color: white;
					border-radius: 20rpx;

					&:nth-child(n+3) {
						// background-color: #202020 !important;
						margin-top: 20rpx;
					}

					.box {
						padding: 20rpx 20rpx;

						.introduction {
							word-break: break-all;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 1;
							text-overflow: ellipsis;
							font-size: 26rpx;
							word-spacing: 4rpx;
							letter-spacing: 4rpx;
							color: #202020;
							opacity: .5;
							margin-top: 20rpx;
						}

						.bottom {
							display: flex;
							justify-content: space-between;
							align-items: center;
							font-size: 28rpx;
							margin-top: 34rpx;

						}
					}

					image {
						border-radius: 20rpx 20rpx 0rpx 0rpx;
						width: 100%;
						height: 300rpx;
					}

					.title {
						display: flex;
						justify-content: space-between;

						text {
							font-family: PingFang SC;
							font-size: 32rpx;
							font-weight: 700;
						}
					}

				}
			}

			.no-more {
				text-align: center;
				color: gray;
				padding-bottom: 10px;
				padding-top: 20rpx;
			}

			.empty {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-top: 200rpx;
				color: #9E9E9E;
				font-size: 36rpx;

				image {
					width: 250rpx;
					margin-bottom: 50rpx;
				}
			}
		}

		.container {
			width: 60vw;
			height: 400rpx;
			border-radius: 20rpx;
			background-color: white;
			padding: 20rpx 30rpx;

			.title {
				text-align: center;
				font-size: 40rpx;
				font-weight: 700;
			}

			.introduction {
				margin-top: 40rpx;
				padding-top: 20rpx;
				font-size: 35rpx;
				font-family: PingFang SC;
				font-weight: 500;
				height: 300rpx;
				border-top: 1px solid silver;

				.introduction-1 {
					word-break: break-all;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 3;
				}

				.introduction-a {
					margin-top: 40rpx;
					font-size: 28rpx;
					color: #787878;

					&>view {
						margin-bottom: 20rpx;
					}
				}

			}
		}

		/* 搜索框外层容器样式  知无涯 */
		.search {
			width: 100%; // 宽度100%撑满父容器
			background-color: white; // 白色背景
			padding: 10rpx 24rpx; // 内边距（上下10rpx，左右24rpx）
			display: flex; // 启用flex布局
			align-items: center; // 子元素垂直居中

			/* 深度选择器（穿透组件样式） */
			/deep/ .uni-searchbar {
				flex: 1; // 占据剩余空间
				display: flex; // 启用flex布局
				align-items: center; // 子元素垂直居中

				/* 搜索输入框样式 */
				&__box {
					flex: 1; // 占据剩余空间
					display: flex; // 启用flex布局
					align-items: center; // 子元素垂直居中
					background-color: white !important; // 强制白色背景
					border: 1px solid #1890FF !important; // 蓝色边框
					border-radius: 30rpx; // 30rpx圆角
					height: 60rpx; // 固定高度60rpx
				}

				/* 占位符文本样式 */
				&__text-placeholder {
					color: #999 !important; // 浅灰色文字
				}

				/* 搜索按钮样式 */
				&__cancel {
					background-color: #1890FF !important; // 蓝色背景
					color: white !important; // 白色文字
					border-radius: 20rpx; // 30rpx圆角
					height: 60rpx; // 高度与输入框保持一致
					line-height: 60rpx; // 行高=高度实现垂直居中
					font-size: 28rpx; // 文字大小
					margin-left: 5rpx; // 与输入框的间距
					min-width: 75rpx; // 最小宽度（避免文字过短时变形）

					/* 按钮点击态效果 */
					&:active {
						background-color: #1070CC !important; // 点击时变为深蓝色
					}
				}
			}
		}

		.open-up {
			width: 80rpx;
			height: 32rpx;
			background-color: #E7FAF0;
			border: 1px #D0F5E0 solid;
			color: #71E2A3;
			font-size: 20rpx;
			margin-top: 4rpx;
			text-align: center;
		}

		.shutdown {
			width: 80rpx;
			height: 35rpx;
			line-height: 35rpx;
			background-color: #FFEDED;
			border: 1px #FF9292 solid;
			color: #FF9292;
			font-size: 20rpx;
			margin-top: 4rpx;
			text-align: center;
		}

		.maintenance {
			width: 80rpx;
			height: 35rpx;
			line-height: 35rpx;
			background-color: #eeeeee;
			border: 1px #cfcfcf solid;
			color: #818181;
			font-size: 20rpx;
			margin-top: 4rpx;
			text-align: center;
		}
	}
</style>