<template>
	<view>
		<view class="training-camp">
			<view class="training-camp-btn" @click="choose">{{title}}</view>
		</view>
		<u-picker ref="upicker" :show="show" :columns="columns" title="训练营选择" keyName="courseName" @confirm="confirm"
			@cancel="cancel"></u-picker>
		<view class="container-body x-start" :style="{height: contentH,width:'100%'}">
			<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="0" v-for="(item,index) in dataList" :key="index"
							:info="item" />
						<u-loadmore :status="loadStatus" />
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getFsCourseList,
		getCourseVdieoList
	} from "@/api/courseManage.js"
	import courseItem from "../components/courseItem.vue"
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import MescrollMoreItemMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mixins/mescroll-more-item.js";
	export default {
		mixins: [MescrollMixin, MescrollMoreItemMixin],
		components: {
			courseItem
		},
		data() {
			return {
				title: '请选择训练营',
				contentH: 0,
				show: false,
				columns: [
					[]
				],
				downOption: {
					auto: false // 不自动加载 (mixin已处理第一个tab触发downCallback)
				},
				upOption: {
					auto: false, // 不自动加载
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量
					},
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					toTop: {
						width: 0,
					}
				},
				keyword: "",
				dataList: [],
				user: {},
				courseList: [],
				chooseIndex: [0],
				courseId: '',
				mescroll: null,
				params: {
					pageNum: 1,
					pageSize: 10
				},
				triggered: false,
				loadStatus: 'loadmore',
			}
		},
		mounted() {
			console.log("======cou")
			const windowHeight = uni.getSystemInfoSync().windowHeight
			this.contentH = `calc(${windowHeight}px - 52px - 56px)`
			this.user = uni.getStorageSync("companyUserInfo") ? JSON.parse(uni.getStorageSync("companyUserInfo")) : {}
			this.getFsCourseList()
		},
		methods: {
			choose() {
				this.show = true
				this.$refs.upicker.setIndexs(this.chooseIndex)
			},
			cancel() {
				this.show = false
			},
			confirm(e) {
				this.chooseIndex = e.indexs
				this.courseId = e.value[0].courseId
				this.title = e.value[0].courseName
				this.show = false
				// this.mescroll.resetUpScroll()
				this.getListInit()
			},
			// 训练营
			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.columns = [this.courseList]
						this.courseId = this.courseList && this.courseList.length > 0 ? this.courseList[0]
							.courseId : ''
						this.title = this.courseList && this.courseList.length > 0 ? this.courseList[0]
							.courseName : '请选择训练营'
						this.getListInit()
						// this.mescroll.resetUpScroll()
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg,
						});
					}
				})
			},
			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>
	page {
		background-color: #f5f4f5;
	}
</style>
<style lang="scss" scoped>
	.training-camp {
		padding: 10px;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 14px;
		color: #222222;

		&-btn {
			font-size: 15px;
			width: 100%;
			height: 32px;
			line-height: 32px;
			text-align: center;
			background-color: #fff;
			border-radius: 50px;
			border: 1px solid #ccc;
		}
	}

	.container-left {
		flex-shrink: 0;
		background-color: #fff;
		width: 80px;
		height: 100%;
		overflow-y: auto;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		font-size: 14px;
		color: #222222;

		.active {
			background-color: #f5f4f5;
			font-weight: bold;
			position: relative;

			&::after {
				content: "";
				height: 15px;
				width: 4px;
				background-color: #1677ff;
				position: absolute;
				top: 50%;
				left: 0;
				transform: translateY(-50%);
			}
		}

		.classification {
			padding: 20px 16px;
			box-sizing: border-box;
		}
	}

	.container-right {
		flex: 1;
		height: 100%;
		overflow-y: scroll;

		.list {
			padding: 10px;
			box-sizing: border-box;
			width: 100%;
		}
	}
</style>