<template>
	<view class="activityPageNew">
		<statusBar :item="navBarData"></statusBar>
		<!-- 消息提示 -->
		<msgPop></msgPop>
		<view class="search">
			<uni-search-bar v-model.trim="searchText" bgColor="#f0f2f5" class="btntop" placeholder="搜索"
				cancelButton="none" clearButton="none"></uni-search-bar>
		</view>

		<view class="top">

			<view class="right" @click="accountLogin" :class="current == 0 ? info1 : info2">
				<text class="">常规教研</text>
				<view v-if="current == 0" class="yun">
					<image src="https://teacherapi.cocorobo.cn/teaching-file/static//yym/Ellipse 10.png"
						mode="aspectFill"></image>
				</view>
			</view>

			<view class="left" @click="phoneLogin" :class="current == 1 ? info1 : info2">
				<text class="">专题教研</text>
				<view v-if="current == 1" class="yun">
					<image src="https://teacherapi.cocorobo.cn/teaching-file/static/yym/Ellipse 10.png"
						mode="aspectFill"></image>
				</view>
			</view>

			<!-- <view class="right" @click="costClass" :class="current == 2 ? info1 : info2">
				<text class="">付费教研</text>
				<view v-if="current == 2" class="yun">
					<image src="https://teacherapi.cocorobo.cn/teaching-file/static/yym/Ellipse 10.png"
						mode="aspectFill"></image>
				</view>
			</view> -->
		</view>

		<swiper class="scroll-view-height" @change="swipeIndex" :current="current" :duration="300">

			<!-- 常规教研 -->
			<swiper-item>
				<scroll-view scroll-y="true" refresher-enabled='true' :refresher-triggered="putrigger"
					@refresherrefresh='purepulling' @scrolltolower="pulower" style="height: 100%;">
					<view class="searchPagedata" v-if="!pulist.length">暂无数据</view>
					<view>
						<listBlock1 ref="listBlock2" :classList="pulist"></listBlock1>
					</view>
					<view class="loading">
						<view v-if="puLoading==1">数据加载中...</view>
						<view v-if="puLoading==2">没有更多了~~</view>
					</view>
				</scroll-view>
			</swiper-item>


			<!-- 专题教研 -->
			<swiper-item>
				<scroll-view scroll-y="true" refresher-enabled='true' :refresher-triggered="trigger"
					@refresherrefresh='repulling' @scrolltolower="aclower" style="height: 100%;">

					<view class="searchPagedata" v-if="!teclist.length">暂无数据</view>

					<view class="">
						<listBlock1 ref="listBlock1" :classList="teclist"></listBlock1>
					</view>
					<view class="loading">
						<view v-if="acLoading==1">数据加载中...</view>
						<view v-if="acLoading==2">没有更多了~~</view>
					</view>
				</scroll-view>

			</swiper-item>


			<!-- 付费教研 -->
			<!-- <swiper-item>
				<scroll-view scroll-y="true" style="height: 100%;">
					<view class=""
						style="height: 100%; width: 750rpx;display: flex;justify-content: center;align-items: center;">
						<image src="../../static/Frame.png" style="width: 400rpx;height: 400rpx;" mode=""></image>
					</view>

				</scroll-view>
			</swiper-item> -->
		</swiper>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				navBarData: {
					title: "活动",
					btn: 0,
				},
				indexId: 1,
				current: 0, //控制展示哪一个
				info1: "info1", //类名
				info2: "info2",
				// swiper: 0,
				searchText: "", // 搜索文本

				acLoading: 0, //0默认值  1加载中 2没有更多了
				accurrentPage: 1, //页数

				puLoading: 0, //0默认值  1加载中 2没有更多了
				pucurrentPage: 1,
				// 教研
				teclist: [],
				// 常规
				pulist: [],

				// 设置当前下拉刷新状态，true 表示下拉刷新已经被触发，false 表示下拉刷新未被触发
				trigger: false, //专题
				putrigger: false, //常规

				// 触底
				// SearchLoading: 0, //0默认值  1加载中 2没有更多了
				// 搜索页数
				Searchpage: 1,
				
				// 在搜索框有文字的时候下拉刷新禁止触发watch,防止数据叠加
				bansearchData: false
			};
		},

		watch: {
			searchText(val) {
				// console.log(val);
				if (this.bansearchData) return
				if (val === '') {
					
					this.acLoading = 0 //0默认值  1加载中 2没有更多了
					this.accurrentPage = 1 //页数
					this.teclist = [] // 教研
					this.getTecData()
					
					
					this.puLoading = 0 //0默认值  1加载中 2没有更多了
					this.pucurrentPage = 1
					this.pulist = [] // 常规
					
					this.getpuData()
					return
				}
				this.selectSearchdata()
			}
		},
		// onUnload() {
		// 	this.$store.dispatch('asyncDelAll')
		// },
		methods: {
			// 搜索
			selectSearchdata() {
				if (this.current === 2) return
				let type = null
				if (this.current == 0) {
					type = 2
				} else if (this.current == 1) {
					type = 1
				}else{
					return 
				}
				// console.log(111);
				let data = {
					openid: uni.getStorageSync('oId'),
					txt: this.searchText,
					ty: type,
					// page: this.Searchpage,
					// lim: 15
				}
				// return console.log(data);
				this.$request('/selectSearchdata', "get", data).then(res => {
					console.log('常规', res);
					if (this.current == 0) {
						this.pulist = res[0]
					} else if (this.current == 1) {
						this.teclist = res[0]
					}
					// if (!res[0].length) {
					// 	this.puLoading = 2
					// } else {
					// 	this.puLoading = 0
					// }
					// this.pulist = [...this.pulist, ...res[0]]
				})
			},
			// 专题教研下拉刷新
			repulling() {
				this.bansearchData = true
				this.trigger = true
				// this.isrepulling = true
				setTimeout(() => {
					this.trigger = false;
				}, 1000);

				this.acLoading = 0 //0默认值  1加载中 2没有更多了
				this.accurrentPage = 1 //页数
				this.teclist = [] // 教研
				
				if (this.searchText !== '') {
					this.puLoading = 0 //0默认值  1加载中 2没有更多了
					this.pucurrentPage = 1
					this.pulist = [] // 常规
					this.getpuData()
				}
				this.searchText = ''

				this.getTecData()
				// console.log(111);
			},
			// 常规教研下拉刷新
			purepulling() {
				this.bansearchData = true
				this.putrigger = true
				// this.isrepulling = true
				setTimeout(() => {
					this.putrigger = false;
				}, 1000);

				this.puLoading = 0 //0默认值  1加载中 2没有更多了
				this.pucurrentPage = 1
				this.pulist = [] // 常规
				if (this.searchText !== '') {
					this.acLoading = 0 //0默认值  1加载中 2没有更多了
					this.accurrentPage = 1 //页数
					this.teclist = [] // 教研
					this.getTecData()
				}
				this.searchText = ''

				this.getpuData()
				// console.log(111);
			},
			// 专题教研触底
			aclower() {
				if (this.searchText !== '') return console.log('禁止触底');
				console.log('没触底？');
				if (this.acLoading == 2) return
				this.acLoading = 1
				this.accurrentPage++
				setTimeout(this.getTecData, 1000)
			},
			// 常规教研触底
			pulower() {
				if (this.searchText !== '') return console.log('禁止触底');
				console.log('没触底？');
				if (this.puLoading == 2) return
				this.puLoading = 1
				this.pucurrentPage++
				setTimeout(this.getpuData(), 1000)
			},
			// 获取专题
			getTecData() {
				let data = {
					openid: uni.getStorageSync('oId'),
					ty: 1,
					page: this.accurrentPage, //下拉获取更多的备用字段
					lim: 15 //一次获取多少数据
				}
				this.$request('/selectActivity', "POST", data).then(res => {
					// console.log('专题', res[0]);
					// this.teclist = res[0]
					if (!res[0].length) {
						this.acLoading = 2
					} else {
						this.acLoading = 0
					}
					this.teclist = [...this.teclist, ...res[0]]
					this.bansearchData = false
				})
			},
			secherData() {

			},
			// 获取常规
			getpuData() {
				let data = {
					openid: uni.getStorageSync('oId'),
					ty: 2,
					// txt: (e ? e : ''),
					page: this.pucurrentPage, //下拉获取更多的备用字段
					lim: 15 //一次获取多少数据
				}
				// return console.log('获取常规', data);
				this.$request('/selectActivity', "POST", data).then(res => {
					console.log('常规', res[0]);
					// this.pulist = res[0]
					if (!res[0].length) {
						this.puLoading = 2
					} else {
						this.puLoading = 0
					}
					this.pulist = [...this.pulist, ...res[0]]
					this.bansearchData = false
				})
			},

			// 区域滑动变换头部
			swipeIndex(index) {
				// console.log(111);

				this.current = index.detail.current;
				if (this.searchText !== '') {
					this.selectSearchdata()
				}
				// this.selectSearchdata()
				// this.$refs.listBlock1.getdata()
			},

			// 区域滑动
			accountLogin() {
				this.current = 0;
			},
			// 点击专题教研
			phoneLogin() {
				this.current = 1;
			},
			// 点击常规教研
			costClass() {
				this.current = 2;
			}
		},
		onLoad() {
			this.getpuData() //获取常规列表
			this.getTecData() //获取专题列表
		},
		onShow() {

			this.getAllMessage() // 调用app.js中的方法
			// 获取收藏事件
			this.$refs.listBlock1.getdata() //专题
			this.$refs.listBlock2.getdata() //常规
		}
	};
</script>

<style lang="scss" scoped>
	.btntop {
		width: 100%;
	}

	.searchPagedata {
		width: 750rpx;
		height: 40%;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.activityPageNew {
		display: flex;
		flex-direction: column;
		height: 100vh;


		.search {
			background-color: #ffffff;
			position: relative;
			display: flex;
			width: 750rpx;
			height: 88rpx;
			justify-content: center;
			align-items: center;

			.inpSer {
				width: 690rpx;
				height: 65rpx;
				margin: auto;
				background-color: rgba(240, 242, 245, 1);
				border-radius: 5rpx;
				color: rgba(0, 0, 0, 0.26);
				position: relative;

				.inpTxt {
					color: #000;
					height: 100%;
					width: 100%;
				}

				input::placeholder {
					color: #000;
				}

				.ico {
					position: absolute;
					top: 50%;
					left: 20rpx;
					transform: translate(0, -50%);
					display: flex;
					align-items: center;

					image {
						margin-right: 10rpx;
					}
				}
			}
		}

		.top {
			width: 100%;
			display: flex;
			justify-content: space-around;
			margin: auto;
			background-color: #ffffff;
			padding: 10px 60rpx;
			height: 98rpx;

			.info1 {
				font-weight: bold !important;
				position: relative;
				color: rgba(0, 0, 0, 0.8);
				font-size: 34rpx;
				line-height: 48rpx;
				width: 136rpx;
				height: 48rpx;
				text-align: center;

				// font-family: PingFang SC;
				// font-family: \9ed1\4f53;
				.yun {
					width: 134rpx;
					height: 32rpx;
					position: absolute;
					left: 0;
					bottom: -10rpx;

					// transform: translate(-50%,0%);
					// box-shadow: 0px 10rpx 30rpx 6rpx #4a97f2;
					image {
						width: 100%;
						height: 100%;
					}
				}
			}

			.info2 {
				color: rgba(0, 0, 0, 0.6);
				font-size: 34rpx;
				font-weight: 400;
				line-height: 48rpx;
				width: 136rpx;
				height: 48rpx;
				text-align: center;
				font-family: 微软雅黑;
			}
		}

		.scroll-view-height {
			flex: 1;
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.cont {
				background-color: #ffffff;
				width: 332rpx;
				padding: 20rpx 15rpx;
				border-radius: 10px;
				margin-bottom: 20rpx;

				image {
					width: 300rpx;
					height: 170rpx;
					border-radius: 10rpx;
				}

				.tit {
					width: 257rpx;
					margin: 10rpx 0;
					// height: 80rpx;
					font-weight: bold;
					white-space: normal;
					display: -webkit-box;
					word-break: break-all;
					text-overflow: ellipsis;
					overflow: hidden;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					/*设置 需要显示的行数*/
				}

				.teacher {
					color: #999999;
				}
			}
		}
	}
</style>