<template>
	<view class="searchpage">
		<u-navbar title="添加老师" :border-bottom="false" :background="bgc" title-color='#2E4975' title-size='36'
			height='36'  :custom-back="addstu"></u-navbar>
			<u-toast ref="uToast" />
		<view class="serchbox">
			<view class="serchimg">
				<image src="https://file.langsi.online/yasiimg/web/static/uVMACkynkipOxuTYDqm0">

				</image>
			</view>
			<input type="text"
				 v-model="searchKeyword"
				 placeholder="搜索相关内容..."
				 class="input"
				 placeholder-style="color:#C7CDD3"
				 @input="search()">
		</view>
		<view class="check_card">
		<!-- 	<view class="check_card_stu " v-for="(item,index ) in stulist" :key="index" @click="toggleSelection(item)" :class="{ 'act1': item.isSelected }"> 
				<view class="check_card_stu_info_left">
					<view class="class_card_left">
						<image :src="item.headImgUrl"></image>
					</view>
					<view class="class_card_right">
						<view class="class_card_right_top" v-html="highlightSearch(item.nickName)">
							
						</view>
						<view class="class_card_right_bot">
							ID：{{item.id}}
						</view>
					</view>
				</view>
			
			</view> -->
			<view class="teacher_card" v-for="(item,index) in stulist" :key="index"   @click="toggleSelection(item)" :class="{ 'act1': item.isSelected }">
				<view class="class_card_left">
						<image src=" https://file.langsi.online/yasiimg/web/static/udWNTRG6GulmTtgPRQ49"></image>
					</view>
					<view class="class_card_right">
						<view class="class_card_right_top" v-html="item.teacherInfo.nickname">
						
						</view>
						<view class="class_card_right_bot">
							{{item.roleText}}
						</view>
					</view>
			</view>

		</view>
		<view class="zhanwei" style="width: 100%; height: 230rpx;">
			
		</view>
		<view class="botbtn" @click="addstu()" v-show="selectedIds.length>0">
			<view class="btn">
				确定添加（{{selectedIds.length}}）
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgc: {
					backgroundColor: "#F6F9FC",
				},
				checkindex: 1,
				stulist:[],
				searchKeyword:'',
				orgstulist:[],
				selectedIds: [],
			}
		},
		onLoad(option) {
			// this.classid=option.id
			this.selectedIds=JSON.parse(decodeURIComponent(option.teacherinfo))
			
		},
		onShow(){
			this.getallstu()
		},
		methods: {
			addstu() {
				console.log('调用了');
				uni.setStorageSync('selectedteacherinfo', this.selectedIds);
							
				// Navigate back to the previous page
				uni.navigateBack({
				delta: 1, // Number of pages to go back
				success(res) {
					console.log(res);
				},
				fail(err) {
					console.error(err);
				}
				});
			
		
			},
			 toggleSelection(item) {
			    // const selectedIndex = this.selectedIds.indexOf(item.id);
				  const selectedIndex = this.selectedIds.findIndex(selectedItem => selectedItem.id === item.id);
			    if (selectedIndex === -1) {
			      // If not selected, add to the array and apply the 'act1' class
				  this.selectedIds.push({ name: item.teacherInfo.nickname, avatar: item.teacherInfo.avatar, id: item.teacherId });
			     
			      item.isSelected = true;  // Add a property to the item to track selection
			    } else {
			      // If already selected, remove from the array and remove the 'act1' class
			      this.selectedIds.splice(selectedIndex, 1);
			      item.isSelected = false;
				  console.log(this.selectedIds,'selectedIdsselectedIdsselectedIds');
			    }
				
			  },
			search() {
			    // 根据关键字过滤 this.classlist
			    if (this.searchKeyword !== '') {
			        const filteredList = JSON.parse(JSON.stringify(this.orgstulist)).filter(item => {
			            // 匹配 nickName 或者 id
			            return item.teacherInfo.nickname.includes(this.searchKeyword)
			        });
			        // 更新 this.classlist 为过滤后的列表
			        this.stulist = filteredList;
					this.selectedIds.forEach(selectedItem => {
					   const index = this.stulist.findIndex(item => item.teacherId === selectedItem.teacherId);
					   if (index !== -1) {
					     this.$set(this.stulist, index, { ...this.stulist[index], isSelected: true });
					   }
					 });
			    } else {
			        this.fuwei();
			    }
			},
			fuwei() {
				
				this.stulist = JSON.parse(JSON.stringify(this.orgstulist));
				this.selectedIds.forEach(selectedItem => {
				   const index = this.stulist.findIndex(item => item.teacherId === selectedItem.teacherId);
				   if (index !== -1) {
				     this.$set(this.stulist, index, { ...this.stulist[index], isSelected: true });
				   }
				 });
			},
			highlightSearch(name) {
			    // 使用正则表达式替换匹配到的 searchKeyword 为带有颜色的文字
			    if (this.searchKeyword) {
			      const regex = new RegExp(this.searchKeyword, 'gi');
			      return name.replace(regex, match => `<span style="color: #2D7CE6 ;">${match}</span>`);
			    }
			    return name;
			  },
			getallstu(){
				this.$u.get(`https://api.admin-v2.langsi.online/admin-api/classroom/teacher/list`).then(res => {
				
							
					if (res.code == 0) {
						this.stulist=res.data
						console.log(this.stulist,'this.stulistthis.stulist');
						this.orgstulist = JSON.parse(JSON.stringify(res.data));
						this.selectedIds.forEach(selectedItem => {
						   const index = this.stulist.findIndex(item => item.teacherId === selectedItem.id);
						   if (index !== -1) {
						     this.$set(this.stulist, index, { ...this.stulist[index], isSelected: true });
						   }
						 });
							
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: linear-gradient(183deg, #F0F6FF 0%, #FAFAFA 100%);
	}
	
	.searchpage {
		width: 750rpx;
		padding: 0 32rpx;

		.serchbox {
			padding: 0 32rpx;
			display: flex;
			flex-wrap: nowrap;
			margin-top: 30rpx;
			align-items: center;
			width: 686rpx;
			height: 88rpx;
			background: #FFFFFF;
			border-radius: 20rpx 20rpx 20rpx 20rpx;

			.serchimg {
				width: 30.51rpx;
				height: 32rpx;
				margin-right: 24rpx;
			}

			.input {
				margin: 0 auto;
				width: 568rpx;
				height: 88rpx;
				border-radius: 30rpx;
				// background-color: #f5f8fc;
				// display: flex;
				// justify-content: center;
				// align-items: center;
				// text-align: center;
				font-size: 28rpx;
				font-family: Source Han Sans CN, Source Han Sans CN-Light;
				font-weight: 300;
				text-align: left;
				color: #a7b9cd;
				letter-spacing: 1.2rpx;
				line-height: 36rpx;
				// padding: 0 100rpx;
				// padding-right: 135rpx;
				box-sizing: border-box;

			}
		}
		
		.check_card {
			margin-top: 20rpx;
			.teacher_card{
				margin-bottom: 20rpx;
				padding: 24rpx;
				display: flex;
				flex-wrap: nowrap;
				width: 686rpx;
				height: 130rpx;
				background: #FFFFFF;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				.class_card_left {
						width: 76rpx;
						height: 76rpx;
					}
						
					.class_card_right {
						margin-left: 20rpx;
						display: flex;
						flex-wrap: wrap;
						
						.class_card_right_top {
							width: 100%;
							font-size: 28rpx;
							font-family:'PingFang','PingFang';
							font-weight: bold;
							color: #072F5A;
						}
						
						.class_card_right_bot {
							margin-top: 8rpx;
							font-size: 24rpx;
							font-family:'PingFang','PingFang';
							font-weight: 500;
							color: #2D7CE6;
						}
					}
			}
			
			.check_card_stu {
				display: flex;
				flex-wrap: nowrap;
				align-items: center;
				justify-content: space-between;
				padding: 32rpx;
				width: 686rpx;
				height: 148rpx;
				background: #FFFFFF;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				border: 2rpx solid #FFFFFF;
				margin-bottom: 20rpx;

				.check_card_stu_info_left {
					display: flex;
					flex-wrap: nowrap;

					.class_card_left {
						width: 76rpx;
						height: 76rpx;
					}

					.class_card_right {
						margin-left: 12rpx;
						display: flex;
						flex-wrap: wrap;

						.class_card_right_top {
							width: 100%;
							font-size: 28rpx;
							font-family: 'PingFang', 'PingFang';
							font-weight: bold;
							color: #072F5A;
						}

						.class_card_right_bot {
							margin-top: 4rpx;
							font-size: 20rpx;
							font-family: 'PingFang', 'PingFang';
							font-weight: 500;
							color: rgba(7, 47, 90, 0.6);
						}
					}
				}

				.check_card_stu_info_right {
					width: 50%;
					display: flex;
					flex-wrap: nowrap;
					align-items: center;
					justify-content: space-between;

					.class_info {
						width: 110rpx;
						display: flex;
						flex-wrap: wrap;
						justify-content: center;

						.class_info_top {
							font-size: 20rpx;
							font-family: 'PingFang', 'PingFang';
							font-weight: 500;
							color: rgba(7, 47, 90, 0.5);
						}

						.class_info_bot {
							margin-top: 12rpx;
							font-size: 28rpx;
							font-family: 'PingFang', 'PingFang';
							font-weight: 800;
							color: #2D7CE6;
						}
					}
				}
			}

			
			.act1 {
				border: 2rpx solid #2D7CE6;
			}
		}
		.botbtn{
			padding: 32rpx 32rpx 0 32rpx;
			margin-left: -32rpx;
			position: fixed;
			bottom: 0;
			width: 750rpx;
			height: 200rpx;
			background: #FFFFFF;
			box-shadow: 0rpx -10rpx 16rpx 0rpx rgba(202,202,202,0.25);
			border-radius: 40rpx 40rpx 0rpx 0rpx;
			.btn{
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 28rpx;
				font-family:'PingFang','PingFang';
				font-weight: 800;
				color: #FFFFFF;
				width: 686rpx;
				height: 80rpx;
				background: linear-gradient(180deg, #74AFFF 0%, #2D7CE6 100%);
				border-radius: 104rpx 104rpx 104rpx 104rpx;
			}
		}
	}
</style>