<template>
	<view class="container">
		<view class="top">
			<div class="s u-flex u-col-center u-row-between">
				<view class="l">
					<image src="@/static/images/index/s.png" mode="aspectFill"></image>
				</view>
				<view class="c">
					<input type="text" v-model="s" placeholder="搜索名称">
				</view>
				<view class="r" @click="search">
					查询
				</view>
			</div>
		</view>
		<view class="navlist">
			<scroll-view scroll-x="true" class="scroll-X" scroll-with-animation :scroll-into-view="sid">
				<view class="item" :id="'ref'+index" :class="{on:nIndex == index}" v-for="(item,index) in navlist"
					:key="index" @click="navChange(index)">{{item.name}}</view>
			</scroll-view>
		</view>
		<view class="list  u-flex u-row-between u-col-center">
			<view class="item" v-for="(item,index) in list" :key="index"  @click="$pageTo(`/pages/index/train/video?id=${item.id}`)">
				<view class="img">
					<image :src="$config.imageBaseUrl + item.coverImg" mode="aspectFill" v-if="item.coverImg"></image>
					<image src="@/static/images/index/news.png" mode="aspectFill" v-else></image>
					<text>{{item.count}}人学习</text>
				</view>
				<view class="con">
					<view class="title u-l-1">
						{{item.title}}
					</view>
					<!-- <view class="type">
						共{{item.number}}课
					</view> -->
				</view>
			</view>
			<view class="nolist" v-if="nolist">
				暂无课程
			</view>
		</view>
	</view>
</template>

<script>
	import { trainingList,trainiCngourse } from '@/api/home.js'
	import config from "@/config";
	export default {
		data() {
			return {
				s: "",
				sid: 'ref0',
				nIndex: 0,
				list: [],
				page: 1,
				total: 0,
				nolist: false,
				navlist: [],
				id: '',				
			}
		},
		onLoad(o) {
			this.id = o.id
			this.getList()
			this.getList2()
		},
		methods: {
			search(){
				this.list = []
				this.getList2()
			},
			getList2() {
				trainiCngourse({
					needId:this.id
				}).then(res => {
					if (res.code == 200) {
						console.log(res)
						if(this.nIndex == 0){
							this.list = res.data
						}else{
							res.data.forEach(item=>{
								if(item.categoryId == this.id){
									this.list.push(item)
								}
							})
						}
						
						this.nolist = this.list.length == 0
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 1500
						});
					}
				})
			},
			getList() {
				trainingList({
					type: 3
				}).then(res => {
					if (res.code == 200) {
						console.log(res)
						this.navlist = res.data
						this.navlist.unshift({
							id:'',
							name:'全部'
						})
						this.nIndex = this.navlist.findIndex(item=>item.id == this.id)
						if(this.nIndex == -1){
							this.nIndex = 0
							this.id = ''
						}
						console.log(this.navlist,'this.navlist',this.nIndex)
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 1500
						});
					}
				})
			},
			navChange(index) {
				console.log('13131',index,this.nIndex)
				if (this.nIndex != index) {
					if (this.nIndex > 1 && this.nIndex < index) {
						this.sid = 'ref' + (this.nIndex - 1)
						console.log(this.sid)
					}
					this.nIndex = index
					this.id = this.navlist[index].id
					this.list = []
					
						console.log('23323232322',index,this.nIndex)
					this.getList2()
				}
			},

		}
	}
</script>

<style lang="scss" scoped>
	$color: #4892FD;

	.navlist {
		width: 100%;

		.scroll-X {
			white-space: nowrap;
			width: 100%;
			box-sizing: border-box;

			.item {
				display: inline-block;
				padding: 0 20rpx;
				font-size: 28rpx;
				font-weight: 500;
				color: #737373;
				line-height: 80rpx;
				height: 80rpx;
			}

			.on {
				font-size: 30rpx;
				font-weight: bold;
				color: #333333;
				position: relative;

				&::after {
					content: '';
					position: absolute;
					bottom: 0;
					left: 50%;
					width: 36rpx;
					height: 8rpx;
					background: #34A2FF;
					border-radius: 4rpx;
					margin-left: -18rpx;
				}
			}
		}
	}

	.top {
		padding: 20rpx 30rpx;
		border-bottom: 2rpx solid #eee;
		background-color: #fff;
		z-index: 9999;
		width:100%;

		.s {
			height: 72rpx;
			background: #F4F5F7;
			border-radius: 36rpx;
			padding-left: 30rpx;
		}

		.l {
			width: 28rpx;
			height: 28rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.c {
			flex: 1;
			padding: 0 20rpx;
			width: 1rpx;

			input {
				width: 100%;
				height: 70rpx;
				line-height: 70rpx;
				font-size: 28rpx;
				color: #333;
			}
		}

		.r {
			width: 140rpx;
			height: 65rpx;
			background: #FFFFFF;
			border: 1px solid #4595FE;
			border-radius: 33rpx;
			line-height: 65rpx;
			color: #4595FE;
			font-size: 28rpx;
			text-align: center;
		}
	}

	.container {
		background: #fff;

		.list {
			padding: 30rpx;
			flex-wrap: wrap;
			 width: 100%;
			.item {
				width: calc(50% - 10rpx);
				margin-bottom: 20rpx;

				.img {
					width: 100%;
					height: 210rpx;
					position: relative;
					border-radius: 20rpx;
					overflow: hidden;

					image {
						width: 100%;
						height: 100%;
					}

					text {
						position: absolute;
						bottom: 0;
						left: 0;
						height: 40rpx;
						line-height: 40rpx;
						background-color: rgba(0, 0, 0, 0.6);
						color: #E6E6E6;
						padding: 0 30rpx 0 20rpx;
						border-radius: 0rpx 39rpx 0rpx 12rpx;
						font-size: 22rpx;
					}
				}

				.con {
					padding: 10rpx 20rpx;

					.title {
						font-size: 30rpx;
						color: #333;
						line-height: 50rpx;
						height: 50rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 1;
						-webkit-box-orient: vertical;
						white-space: normal;
					}

					.type {
						font-size: 26rpx;
						color: #999;
						line-height: 40rpx;
						height: 40rpx;
					}
				}
			}
		}
	}
</style>