<template>
	<view class="container">
		<view class="headbox">
			<!-- <view class="headnav x-bc">
				<view :class="activeTab == 0 ? 'headnav-item headnav-active':'headnav-item'" @click="handleNav(0)">
					<view>直播计划</view>
					<view class="headnav-num">3</view>
					<image src="@/static/images/idle.png" mode="aspectFill"></image>
				</view>
				<view :class="activeTab == 1? 'headnav-item headnav-active':'headnav-item'" @click="handleNav(1)">
					<view>今日直播</view>
					<view class="headnav-num">3</view>
					<image src="@/static/images/streaming.png" mode="aspectFill"></image>
				</view>
				<view :class="activeTab == 2 ? 'headnav-item headnav-active':'headnav-item'" @click="handleNav(2)">
					<view>往日直播</view>
					<view class="headnav-num">3</view>
					<image src="@/static/images/finished.png" mode="aspectFill"></image>
				</view>
			</view> -->
			<!-- <view class="x-bc">
				<view :class="searchbarNav == index ? 'searchbar x-c searchbar-active':'searchbar x-c'" v-for="(item,index) in searchbar" :key="index" @click="clickSearchbar(index)">
					<text>{{item.name}}</text><u-icon class="arrow-down" name="arrow-down" :color="searchbarNav == index ?'#1677ff':'#999'" size="12"></u-icon>
				</view>
			</view> -->
			<view>
				<dropdownPanel :filterData='filterData' @onChange="onChange" @confirm="confirm" @reset="reset">
					<view v-if="searchbarNav == 0">
						<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 == item.courseId ? 'boxnav-active':''"
									@click="handleCourse(item)">{{item.courseName}}</view>
							</view>
						</view>
					</view>
				</dropdownPanel>
			</view>
		</view>
		<view class="container-right" >
			<scroll-view style="height:100%" :scroll-y="true" :refresher-enabled="true" :refresher-triggered="triggered"
				refresher-background="rgba(0,0,0,0)" @refresherrefresh="pullDownRefresh"
				@refresherrestore="triggered = false" :upper-threshold="100" :lower-threshold="100"
				@refresherabort="triggered = false" @scrolltolower="reachBottom">
				<view class="list">
					<courseItem :from="'course'" :activeTab="1" v-for="(item,index) in dataList" :key="index"
						:info="item" />
					<u-loadmore :status="loadStatus" />
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import {
		getFsCourseList,
		getCourseVdieoList
	} from "@/api/courseManage.js"
	import dropdownPanel from "../components/dropdownPanel.vue"
	import courseItem from "../components/courseItem.vue"
	export default {
		mixins: [MescrollMixin],
		components: {
			courseItem,
			dropdownPanel
		},
		data() {
			return {
				user: {},
				filterData: [{
						name: '训练营-营期',
						value: 0,
					},
					// {
					// 	name: '课程状态',
					// 	value: 1,
					// },
				],
				defaultIndex: [0, 0],
				contentH: 0,
				activeTab: 1,
				courseList: [],
				courseId: '',
				searchbarNav: 0,
				courserIndex: 0,
				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',
			}
		},
		mounted() {
			const windowHeight = uni.getSystemInfoSync().windowHeight
			this.contentH = `calc(${windowHeight}px - 132px - 56px)`
			this.user = uni.getStorageSync("companyUserInfo") ? JSON.parse(uni.getStorageSync("companyUserInfo")) : {}
			this.getFsCourseList()
		},
		methods: {
			onChange(index) {
				this.searchbarNav = index
			},
			reset() {
				this.courserIndex = ''
			},
			handleCourse(item) {
				this.courserIndex = item.courseId
			},
			confirm() {
				this.courseId = this.courserIndex
				console.log(this.courseId)
				this.getFsCourseList()
			},
			// 训练营
			getFsCourseList() {
				const day = uni.$u.timeFormat(new Date(), 'yyyy-mm-dd')
				const param = {
					companyId: this.user.companyId,
					companyUserId: this.user.userId,
					type: this.user.userType == '00' ? 0 : 1, // 0:经销商/1:群管
				}
				getFsCourseList(param).then(res => {
					if (res.code == 200) {
						this.courseList = res.data || []
						// this.courseId = this.courseList && this.courseList.length > 0 ? this.courseList[0]
						// 	.courseId : ''
						// this.searchbar[0].name = this.courseList && this.courseList.length > 0 ? this.courseList[0]
						// 	.courseName : '训练营-营期'
							console.log(this.courseId)
						// this.mescroll.resetUpScroll()
						this.getListInit()
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg,
						});
					}
				})
			},
			handleNav(type) {
				this.activeTab = type
			},
			clickSearchbar(type) {
				this.searchbarNav = type == this.searchbarNav ? 'colse' : type
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			getListInit() {
				this.params.pageNum = 1
				this.getListData('refresh')
			},
			async getListData(type = 'refresh') {
				uni.showLoading({
					title: "加载中..."
				})
				this.loadStatus = 'loading'
				const result = await getCourseVdieoList({
					courseId: this.courseId,
					status: '',
					...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';
					}
					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')
						uni.hideNavigationBarLoading()
					}, 500);
				}
			},
			/**
			 * 下拉列表页
			 */
			pullDownRefresh(options) {
				this.triggered = true;
				setTimeout(() => {
					this.triggered = false;
					uni.stopPullDownRefresh()
					this.params.pageNum = 1;
					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;
	}

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

	.coursebox {
		position: relative;
		overflow-y: auto;
		box-sizing: border-box;
	}

	.courselist {
		padding: 12px;
		box-sizing: border-box;

	}

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