<template>
	<view class="resource">
		<statusBar :item="navBarData"></statusBar>
		<!-- 消息提示 -->
		<msgPop :goNum='1'></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="left" @click="phoneLogin" :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="right" @click="accountLogin" :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" @transition='cutS' @animationfinish='ani' -->
		<swiper class="scroll-view-height" @change="swipeIndex"
			:current="current" :duration="300">
			<swiper-item>
				<scroll-view scroll-y="true" :refresher-enabled='refevent' :refresher-triggered="trigger"
					@refresherrefresh='repulling' @scrolltolower="relower" style="height: 100%;">
					<view class="searchPagedata" v-if="!classList.length">暂无数据</view>
					<view class="">
						<teaching-case ref="reso" :acList='classList'></teaching-case>
					</view>
					<view class="loading">
						<view v-if="reLoading==1">数据加载中...</view>
						<view v-if="reLoading==2">没有更多了~~</view>
					</view>
				</scroll-view>
			</swiper-item>

			<swiper-item>
				<scroll-view scroll-y="true" :refresher-enabled='refevent' :refresher-triggered="putrigger"
					@refresherrefresh='purepulling' @scrolltolower="tealower" style="height: 100%;">
					<view class="searchPagedata" v-if="!classList2.length">暂无数据</view>
					<view class="">
						<teaching-case ref="tech" :acList='classList2'></teaching-case>
					</view>
					<view class="loading">
						<view v-if="teaLoading==1">数据加载中...</view>
						<view v-if="teaLoading==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: "", // 搜索文本
				pic_coll: 'https://teacherapi.cocorobo.cn/teaching-file/static/Stara.png',
				pic_coll2: 'https://teacherapi.cocorobo.cn/teaching-file/static/mine/Collect_yellow.png',

				// 资源库
				classList: [],

				// 精品课程
				classList2: [],
				refevent: true,
				reLoading: 0, //0默认值  1加载中 2没有更多了
				recurrentPage: 1, //页数

				teaLoading: 0, //0默认值  1加载中 2没有更多了
				teacurrentPage: 1,

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

				// 在搜索框有文字的时候下拉刷新禁止触发watch,防止数据叠加
				bansearchData: false
			};
		},
		watch: {
			searchText(val) {
				if (this.bansearchData) return
				// console.log('巴尔纳广播in而哦啊办呢日内');
				// console.log('searchText', val);
				if (val === '') {
					// 资源库
					// if (this.current == 0) {
					this.classList = []
					this.reLoading = 0 //0默认值  1加载中 2没有更多了
					this.recurrentPage = 1 //页数
					this.getreList()
					// console.log('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
					// }
					// if (this.current == 1) {
					this.classList2 = []
					this.teaLoading = 0 //0默认值  1加载中 2没有更多了
					this.teacurrentPage = 1
					this.getteaList()
					// }
					// console.log(1111);
					// 精品课程

					return
				}
				// console.log(888888);
				// if(val===''){
				// 	this.pu
				// }
				this.selectSearchdata()
			}
		},

		methods: {
			ani() {
				this.refevent = true
			},
			cutS(event) {
				// console.log(event.detail);
				this.refevent = false
			},
			selectSearchdata() {

				if (this.current === 2) return

				let data = {
					openid: uni.getStorageSync('oId'),
					txt: encodeURIComponent(this.searchText),
					ty: this.current,
					// page: this.Searchpage,
					// lim: 15
				}
				// return console.log(data);
				this.$request('/selectActSearch', "get", data).then(res => {
					console.log('常规', res);
					if (this.current == 0) {
						this.classList = res[0]
					} else if (this.current == 1) {
						this.classList2 = res[0]
					}
				})
			},
			// 资源库下拉刷新
			repulling() {
				this.bansearchData = true
				this.trigger = true
				// this.isrepulling = true
				setTimeout(() => {
					this.trigger = false;
				}, 1000);

				// 资源库
				this.classList = []
				this.reLoading = 0 //0默认值  1加载中 2没有更多了
				this.recurrentPage = 1 //页数
				if (this.searchText !== '') {
					this.classList2 = []
					this.teaLoading = 0 //0默认值  1加载中 2没有更多了
					this.teacurrentPage = 1
					this.searchText = ''
					this.getteaList()
				}


				this.getreList()
			},
			// 精品慕课下拉刷新
			purepulling() {
				this.bansearchData = true
				this.putrigger = true
				// this.isrepulling = true
				setTimeout(() => {
					this.putrigger = false;
				}, 1000);

				// 精品课程
				this.classList2 = []
				this.teaLoading = 0 //0默认值  1加载中 2没有更多了
				this.teacurrentPage = 1
				
				if (this.searchText !== '') {
					this.classList = []
					this.reLoading = 0 //0默认值  1加载中 2没有更多了
					this.recurrentPage = 1 //页数
					this.searchText = ''
					this.getreList()
				}
				
				this.getteaList()

			},
			// 获取资源库
			getreList() {
				let data = {
					openid: uni.getStorageSync('oId'),
					ty: 0,
					page: this.recurrentPage,
					lim: 30
				}
				this.$request('/selectResource', "POST", data).then(res => {
					console.log('获取资源库', res);
					if (!res[0].length) {
						this.reLoading = 2
					} else {
						this.reLoading = 0
					}
					this.classList = [...this.classList, ...res[0]]
					this.bansearchData = false
					// this.classList2 = res[1]
				})
			},
			// 获取精品慕课
			getteaList() {
				let data = {
					openid: uni.getStorageSync('oId'),
					ty: 1,
					page: this.teacurrentPage,
					lim: 30
				}
				this.$request('/selectResource', "POST", data).then(res => {
					console.log('获取精品慕课', res);
					if (!res[0].length) {
						this.teaLoading = 2
					} else {
						this.teaLoading = 0
					}
					// console.log('前',this.classList2);
					this.classList2 = [...this.classList2, ...res[0]]
					this.bansearchData = false
					// console.log('后',this.classList2);
					// this.classList2 = res[1]
				})
			},
			// 资源库触底
			relower() {
				if (this.searchText !== '') return console.log('禁止触底');
				console.log('没触底？');
				if (this.reLoading == 2) return
				this.reLoading = 1
				this.recurrentPage++
				setTimeout(this.getreList, 1000)
			},
			// 精品慕课触底
			tealower() {
				if (this.searchText !== '') return console.log('禁止触底');
				console.log('没触底？');
				if (this.teaLoading == 2) return
				this.teaLoading = 1
				this.teacurrentPage++
				setTimeout(this.getteaList, 1000)
			},

			swipeIndex(index) {
				
				this.current = index.detail.current;
				if (this.searchText !== '') {
					this.selectSearchdata()
				}
			},
			// 区域滑动
			phoneLogin() {
				this.current = 0;
			},
			accountLogin() {
				this.current = 1;
			},
			costClass() {
				this.current = 2;
			}
		},
		onLoad() {
			this.getreList()
			this.getteaList()

		},
		onShow() {

			this.getAllMessage() // 调用app.js中的方法
			// 获取收藏
			this.$refs.reso.getdata()
			this.$refs.tech.getdata()
		}
	};
</script>

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

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

	.resource {
		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;

		}

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

				.yun {
					width: 134rpx;
					height: 32rpx;
					position: absolute;
					left: 0;
					bottom: -10rpx;

					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;

		}
	}
</style>