<template>
	<view class="container">
		<view class="headbox">
			<dropdownPanel :filterData='filterData' @onChange="onChange" @confirm="confirm" @reset="reset"
			@onClick="selindex">
			<!-- <view class="justify-center align-center">
				<view class="u-border ptb4 plr20 radius50 fs24 base-color-6">上一页</view>
				<view class="mlr20">1</view>
				<view class="u-border ptb4 plr20 radius50 fs24 base-color-6">下一页</view>
			</view> -->
				<scroll-view scroll-y="true" class="hb"  :refresher-enabled="isEnableds" @refresherrefresh="pullDownRefreshs"
					:refresher-triggered="triggereds" refresher-background="rgba(0,0,0,0)" @refresherrestore="triggereds = false"
					:upper-threshold="100" :lower-threshold="100" @refresherabort="triggereds = false"
					@scrolltolower="reachBottoms">
					<view  class="justify-between align-center mb20 mlr20 ">
						<u-search placeholder="搜索课程" v-model="keyword" :showAction="false" height="30px"
							@search='searchKeyword'></u-search>
					</view>
					<view v-if="searchbarNav == 1">
						<view class="boxnav x-bc">
							<view class="boxnav-item" v-for="(item,index) in courseList" :key="index">
								<view class="boxnav-item-info one-t"
									:class="courserIndex == index ? 'boxnav-active':''"
									@click="handleCourse(item,index)">{{item.periodName}}</view>
							</view>
						</view>
					</view>
					<u-loadmore :status="status" />
				</scroll-view>
				
			</dropdownPanel>
		</view>
		<view  class="justify-between align-center mb20 mlr20 mt20 ">
			<u-search placeholder="请输入课程名称" v-model="keywordlist" :showAction="false" height="30px"
				@search='searchKeywordlist' bgColor='#fff'></u-search>
		</view>
		<view class="container-right" >
			<scroll-view style="height:100%" :scroll-y="true" :refresher-enabled="isEnableds" :refresher-triggered="triggered"
				refresher-background="rgba(0,0,0,0)" @refresherrefresh="pullDownRefresh" :scroll-top='scrollTop'
				@refresherrestore="triggered = false" :upper-threshold="100" :lower-threshold="100"
				@refresherabort="triggered = false" @scrolltolower="reachBottom" @scroll="scroll">
				<view class="list">
					<courseItem :from="'course'" :activeTab="1" v-for="(item,index) in dataList" :key="index"
						:info="item"  @trigger-share="handleShare" :parent-method="parentMethod" @retime='reflashtime'/>
					<u-loadmore :status="loadStatus" />
					<view class="h120"></view>
				</view>
			</scroll-view>
		</view>
		<u-loading-page :loading="viewload" iconSize="32" loadingColor="#3c9cff" fontSize="24"
		:loading-text="loadingtext"></u-loading-page>
	</view>
</template>

<script>
	import {
		getFsCourseList,
		getCourseVdieoList,
		getTodayCourse,
	} from "@/api/manageCompany.js"
	import dropdownPanel from "../components/dropdownPanel.vue"
	import courseItem from "../components/courseItem.vue"
	export default {
		name:"liveCourse",
		components: {
			courseItem,
			dropdownPanel
		},
		props: ['parentMethod'],
		data() {
			return {
				viewload:true,
				loadingtext:"数据加载中...",
				user: {},
				filterData: [{
						name: '今日课程',
						value: 0,
						type: 'click',
					},{
						name: ' 我的课程',
						value: 1,
					},
				],
				contentH: 0,
				activeTab: 1,
				courseList: [],
				courseId: '',
				searchbarNav: 0,
				courserIndex: null,
				searchbar: [{
						name: '我的课程'
					},
					{
						name: '课程状态'
					}
				],
				mescroll: null,
				downOption: {
					use: true,
					auto: false
				},
				upOption: {
					onScroll: false,
					use: true, // 是否启用上拉加载; 默认true
					auto: true,
					page: {
						pae: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 10, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					textNoMore: "已经到底了",
					empty: {
						icon: 'https://cos.his.cdwjyyh.com/fs/20240423/cf4a86b913a04341bb44e34bb4d37aa2.png',
						tip: '暂无数据'
					}
				},
				dataList: [],
				params: {
					pageNum: 1,
					pageSize: 10
				},
				triggered: false,
				loadStatus: 'loadmore',
				//课程选择列表
				pageNum:1,
				pageSize: 10,
				triggereds: false,
				status: 'loadmore',
				keyword:'',
				isEnableds:true,
				keywordlist:'',
				actid:'',
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				shareConfig:'',
				titleName:'',
				seltoday:0
			}
		},
		computed: {
		    imgPath() {
		      return this.$store.state.imgpath
		    }
		},
		mounted() {
			const windowHeight = uni.getSystemInfoSync().windowHeight
			this.contentH = `calc(${windowHeight}px - 132px - 56px)`
			this.user = uni.getStorageSync("companyUserInfo") ? JSON.parse(uni.getStorageSync("companyUserInfo")) : {}
			this.gettodaylist()
			console.log(this.courserIndex)
		},
		methods: {
			onChildDataUpdate(data) {
				this.childdata = data
				this.showShare = this.childdata.onshow
				console.log(this.childdata);
				console.log('父组件', this.childdata.path + "?course=" +JSON.stringify(this.childdata.params) )
			},
			reflashtime(){
				this.keywordlist=''
				this.params.pageNum=1
				this.dataList=[]
				this.gettodaylist()
				this.getFsCourseList('refresh')
			},
			gettodaylist(type){
				const data={
					pageNum:this.params.pageNum,
					pageSize:this.params.pageSize,
					keyword:this.keywordlist,
				}
				getTodayCourse(data).then(res=>{
					console.log(res)
					if(res.code==200){
						this.viewload=false
						const list=res.data.list
						// this.dataList=res.data.list
						if (type == 'refresh') {
							this.dataList = list
						} else {
							this.dataList = [...this.dataList, ...list]
						}
						if ( res.data.isLastPage) {
							this.loadStatus = 'nomore'
						} else {
							this.loadStatus = 'loadmore'
						}
					}else{
						uni.showToast({
							icon: 'none',
							title: res.msg,
						});
					}
				})
			},
			selindex(e){
				this.seltoday=e.value
				if(e.value==0){
					this.params.pageNum = 1
					this.gettodaylist('refresh')
				}else{
					this.params.pageNum = 1
					this.keywordlist=''
					this.getFsCourseList('refresh')
				}
				console.log(e)
			},
			handleShare(config) {
			      // 保存分享配置到父页面数据
			      this.shareConfig = config;
				  this.$emit('child-data-update', this.shareConfig);
			      // 调起分享面板（微信小程序需主动调用）
			      // #ifdef MP-WEIXIN
			      uni.showShareMenu();
			      // #endif
			  },
			scroll: function(e) {
				this.old.scrollTop = e.detail.scrollTop
			},
			onChange(index) {
				this.searchbarNav = index
			},
			reset() {
				this.courseId = ''
				this.keyword =''
				this.courseList=[]
				this.getFsCourseList()
			},
			handleCourse(item,index) {
				console.log(index)
				this.courserIndex = index
				this.titleName=item.periodName
				this.actid=item.periodId
			},
			confirm() {
				if(this.actid==''){
					uni.showToast({
						icon: 'none',
						title: '请选择课程',
					});
					return
				}
				this.courseId = this.actid
				this.filterData[1].name=this.titleName
				console.log(this.filterData[1].name)
				this.params.pageNum=1
				// this.getListInit()
				this.getListData('refresh')
			},
			searchKeyword(value){
				this.courseList=[]
				this.keyword = value
				this.params.pageNum=1
				this.getFsCourseList('refresh')
			},
			searchKeywordlist(value){
				this.dataList = []
				this.keywordlist = value
				if(this.seltoday==0){
					this.gettodaylist()
				}else{
					this.getListData()
				}
			},
			pullDownRefreshs() {
				// 下拉
				this.triggereds = true; //下拉了状态为true
				setTimeout(() => {
					this.triggereds = false;
					uni.stopPullDownRefresh()
					this.pageNum = 1;
					this.getFsCourseList('refresh') //触底  不穿执行else
					// 请求接口里面需要判断是不是最后一页 是最后一页 status赋值为‘loadmore’没有更多了
					// 请求接口
				}, 1000)
			},
			reachBottoms() {
				// status这个是加载状态
				if (this.status === 'loadmore') {
					this.status = 'loading'
					uni.showNavigationBarLoading()
					setTimeout(() => {
						this.pageNum++
						this.getFsCourseList() //触底  不穿执行else
						uni.hideNavigationBarLoading()
					}, 1000);
				}
			},
			// 训练营
			getFsCourseList(type) {
				const day = uni.$u.timeFormat(new Date(), 'yyyy-mm-dd')
				const param = {
					pageNum:this.pageNum,
					pageSize:this.pageSize,
					keyword:this.keyword
				}
				getFsCourseList(param).then(res => {
					if (res.code == 200) {
						if (type == 'refresh') {
							this.courseList = res.data.list
						} else {
							// 加载更多 当前页和下一页合并
							this.courseList = [...this.courseList, ...res.data.list]
						}
						if ( res.data.isLastPage) {
							this.status = 'nomore'
						} else {
							this.status = 'loadmore'
						}
						// this.courseId=this.courseList[0].periodId
						// this.filterData[1].name=this.courseList[0].periodName
						// this.getListInit()
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg,
						});
					}
				})
			},
			handleNav(type) {
				this.activeTab = type
			},
			clickSearchbar(type) {
				this.searchbarNav = type == this.searchbarNav ? 'colse' : type
			},
			getListInit() {
				this.params.pageNum = 1
				this.getListData('refresh')
			},
			async getListData(type) {
				console.log(type)
				uni.showLoading({
					title: "加载中..."
				})
				this.loadStatus = 'loading'
				const result = await getCourseVdieoList({
					periodId: this.courseId,
					keyword:this.keywordlist,
					...this.params
				})
				if (result) {
					const {
						isLastPage,
						total,
						list,
					} = result.data
					if (type == 'refresh') {
						this.dataList = list
					} else {
						this.dataList = [...this.dataList, ...list]
					}
					if (isLastPage) {
						this.loadStatus = 'nomore';
					} else {
						this.loadStatus = 'loadmore';
					}
					// 当页数为1时，重置滚动位置
					    if (this.params.pageNum === 1) {
							console.log(this.scrollTop)
					      this.scrollTop = this.old.scrollTop
					      this.$nextTick(function() {
					      	this.scrollTop = 0
					      });
					    }
					uni.hideLoading()
				} else {
					uni.showToast({
						icon: 'none',
						title: "请求失败",
					});
					this.dataList = []
				}
			},
			/**
			 * 触底添加下一页
			 */
			reachBottom(options) {
				if (this.loadStatus === 'loadmore') {
					this.loadStatus = 'loading'
					uni.showNavigationBarLoading()
					setTimeout(() => {
						this.params.pageNum += 1;
						// this.getListData('more')
						if(this.seltoday==0){
							this.gettodaylist('more')
						}else{
							this.getListData('more')
						}
						uni.hideNavigationBarLoading()
					}, 500);
				}
			},
			/**
			 * 下拉列表页
			 */
			pullDownRefresh(options) {
				this.triggered = true;
				setTimeout(() => {
					this.triggered = false;
					uni.stopPullDownRefresh()
					this.params.pageNum = 1;
					if(this.seltoday==0){
						this.gettodaylist()
					}else{
						this.getListData('refresh')
					}
				}, 500)
			}
		}
	}
</script>

<style lang="scss">
	.container {
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 14px;
		color: #222;
		display: flex;
		flex-direction: column;
		/* #ifdef MP-WEIXIN */
		height: 100vh;
		/* #endif */
	}

	.boxnav {
		flex-wrap: wrap;
		padding: 0 0 0 10px;

		&-item {
			width: 50%;
			overflow: hidden;
		}

		&-item-info {
			border: 1px solid #f5f5f5;
			text-align: center;
			color: #222;
			background-color: #f5f5f5;
			border-radius: 3px;
			padding: 5px;
			margin: 0 10px 10px 0;
		}

		&-active {
			border: 1px solid #1677ff !important;
			color: #1677ff !important;
			background-color: #e7f1fe !important;
		}
	}

	.headbox {
		background-color: #fff;
	}

	.headnav {
		padding: 15px 12px;
		margin: 0 -10px -10px 0;
		box-sizing: border-box;

		image {
			height: 60px;
			width: 50px;
			position: absolute;
			z-index: 0;
			bottom: 0;
			right: 0;
			display: none;
		}

		&-item {
			flex: 1;
			font-size: 16px;
			padding: 10px;
			border-radius: 10px;
			background: #f5f5f5;
			margin: 0 10px 10px 0;
			position: relative;
			z-index: 1;
			overflow: hidden;
			color: #555;
		}

		&-active {
			background-color: rgb(231, 241, 255) !important;

			.headnav-num {
				color: #1677ff !important;
			}

			image {
				display: block !important;
			}
		}

		&-num {
			font-family: DIN, DIN;
			font-weight: bold;
			font-size: 25px;
			margin: 5px 0;
		}
	}

	.searchbar {
		flex: 1;
		padding-bottom: 10px;

		.arrow-down {
			margin-left: 5px;
		}

		&-active {
			color: #1677ff !important;

			.arrow-down {
				transform: rotate(180deg);
			}
		}
	}
	.container-right {
		flex: 1;
		height: calc(100% - 80rpx);
		overflow-y: scroll;
		// padding-bottom: 80rpx;
		.list {
			padding: 10px;
			box-sizing: border-box;
			width: 100%;
		}
	}
</style>