<template>
	<view class="study-type">
		<!-- 导航栏 -->
		<zs-header :background="background" color="#000" :title="title"></zs-header>
		<image class="banner" :src="bg" mode=""></image>

		<view style="margin-bottom: 0;" class="type-title more-title">
			推荐线路
			<view class="more-box" @click="jump('./community/goods?columnId='+ query.columnId)">
				<view>
					更多
				</view>
				<image class="more" src="../static/jiantou-icon.png" mode=""></image>
			</view>
		</view>

		<zs-list class="store-recommend-box" mt="0">
			<view class="left">
				<view class="store-item" v-for="(item, index) in recommendList" :key="index"
					@click="goGoodsDetail(item.goodsId)">
					<zs-img :src="item.goodsPath" width="344rpx" height="344rpx" mode=""></zs-img>
					<view class="info">
						<view class="title">
							{{ item.goodsName }}
						</view>
						<view class="user-info">
							<!-- <image class="head" :src="item.goodsPath"></image> -->
							<view class="user-name">
								{{ item.goodsDescribe }}
							</view>
						</view>
						<view class="price-box">
							<view class="left">
								<view class="unit">
									￥
								</view>
								<view class="price">
									{{ item.realPrice }}
								</view>
								<view class="old-price">
									￥{{ item.marketPrice }}
								</view>
							</view>

							<view class="right">
								{{ item.saleNum }}人已购
							</view>
						</view>
					</view>

				</view>
			</view>
			<view class="right">

				<view class="store-item" v-for="(item, index) in recommendList1" :key="index"
					@click="goGoodsDetail(item.goodsId)">
					<zs-img :src="item.goodsPath" width="344rpx" height="344rpx" mode=""></zs-img>
					<view class="info">
						<view class="title">
							{{ item.goodsName }}
						</view>
						<view class="user-info">
							<!-- <image class="head" :src="item.goodsPath" mode=""></image> -->
							<view class="user-name">
								{{ item.goodsDescribe }}
							</view>
						</view>
						<view class="price-box">
							<view class="left">
								<view class="unit">
									￥
								</view>
								<view class="price">
									{{ item.realPrice }}
								</view>
								<view class="old-price">
									￥{{ item.marketPrice }}
								</view>
							</view>
							<view class="right">
								{{ item.saleNum }}人已购
							</view>
						</view>
					</view>

				</view>
			</view>
		</zs-list>


		<view class="type-title more-title">
			互动社区
			<view class="more-box" @click="jump('./community/index?id=' + tab)">
				<view>
					更多
				</view>
				<image class="more" src="../static/jiantou-icon.png" mode=""></image>
			</view>
		</view>



		<scroll-view class="tab-box" enable-flex scroll-x>
			<view class="tab" :class="[tab == item.id ? 'active' : '']" v-for="(item, index) in tabList" :key="index"
				@click="handleTab(item.id)">
				{{ item.communityName }}
			</view>

		</scroll-view>

		<template>
			<!-- 列表 -->
			<zs-list class="store-box" mt="0">
				<view class="left">
					<view class="store-item" v-for="(item, index) in communityList" :key="index" @click="goCommunity(item.tabId)">
						<!-- <image class="play-icon" src="../static/play.png" mode="widthFix"></image> -->
						<zs-img :src="item.coverImg" width="344rpx" height="256rpx"></zs-img>
						<view class="info">
							<view class="title">
								{{ item.communityTitle }}
							</view>
							<view class="user-info">
								<view class="head">
									{{ item.communityName }}
								</view>
								<view class="user-name">
									观看 {{ item.viewedCount }}
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="right">
					<view class="store-item" v-for="(item, index) in communityList1" :key="index"
						@click="goCommunity(item.tabId)">
						<image class="play-icon" src="../static/play.png" mode=""></image>
						<zs-img :src="item.coverImg" width="344rpx" height="256rpx"></zs-img>
						<view class="info">
							<view class="title">
								{{ item.communityTitle }}
							</view>
							<view class="user-info">
								<view class="head">
									{{ item.communityName }}
								</view>
								<view class="user-name">
									观看 {{ item.viewedCount }}
								</view>
							</view>
						</view>
					</view>
				</view>
				<view v-if="communityList.length == 0 && communityList1.length == 0" class="noneContent">
					<view>没有数据</view>
				</view>
			</zs-list>
		</template>

		<view class="type-title more-title">
			科普视频
			<view class="more-box" @click="jump('./community/video?columnId=' + query.columnId)">
				<view>
					更多
				</view>
				<image class="more" src="../static/jiantou-icon.png" mode=""></image>
			</view>
		</view>

		<template>
			<!-- 列表 -->
			<zs-list class="store-box" mt="0" @load="loadMore" :status="status">
				<view class="left">
					<view class="store-item" v-for="(item, index) in list" :key="index" @click="goCourse(item.id)">
						<image class="play-icon" src="../static/play.png" mode="widthFix"></image>
						<zs-img :src="item.courseImg" width="344rpx" height="256rpx"></zs-img>
						<view class="info">
							<view class="title">
								{{ item.courseName }}
							</view>
							<view class="user-info">
								<view class="head" mode=""> 科普视频</view>
								<view class="user-name">
									观看{{ item.viewedCount }}
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="right">
					<view class="store-item" v-for="(item, index) in list1" :key="index" @click="goCourse(item.id)">
						<image class="play-icon" src="../static/play.png" mode=""></image>
						<zs-img :src="item.courseImg" width="344rpx" height="256rpx"></zs-img>
						<view class="info">
							<view class="title">
								{{ item.courseName }}
							</view>
							<view class="user-info">
								<view class="head">科普视频</view>
								<!-- <image class="head" src="../static/logo.png" mode=""></image> -->
								<view class="user-name">
									观看{{ item.viewedCount }}
								</view>
							</view>
						</view>
					</view>
				</view>

			</zs-list>
		</template>


	</view>
</template>

<script>
import { getPage, videoList, studyGoods, studyGoodsByUser, getCommunitys, getItem, } from '@/api/study.js';
export default {
	data() {
		return {
			bg: '',
			title: '',
			background: false,
			pageContent: [],
			status: 'more',
			list: [],
			list1: [],
			recommendList: [],
			recommendList1: [],
			communityList: [],
			communityList1: [],
			tab: 0,
			tabList: [],
			query: {
				columnId: 0,
				currentPage: 1,
				pageSize: 10,
			}
		}
	},
	methods: {
		jump(url) {
			uni.navigateTo({
				url
			});
		},
		// 去商品详情
		goGoodsDetail(id) {
			uni.navigateTo({
				url: '/study/studyGoodsDetail?id=' + id
			})
		},
		goTypeList(id, title) {
			uni.navigateTo({
				url: `/study/studyList?id=${id}&title=${title}`
			})
		},
		goDetail() { },
		// 去课程详情页
		goCourse(id) {
			uni.navigateTo({
				url: './courseDetail?id=' + id
			})
		},
		goCommunity(id) {
			console.log(id)
			uni.navigateTo({
				url: './community/detail?id=' + id
			});
		},
		loadMore() {
			this.videoList()
		},
		// 获取顶部栏目
		getPage(columnId) {
			return new Promise((resolve, reject) => {
				getPage({ currentPage: 1, pageSize: 10, columnId, status: 1 }).then(res => {
					if (res.state == 'Success') {
						res.content.map(item => {
							let obj = {
								id: item.id,
								title: item.columnName,
								list: []
							}
							this.pageContent.push(obj)
						})
						console.log(this.pageContent);
						resolve()
					}
				})
			})
		},
		// 课程视频
		videoList() {
			this.status = 'loading'
			videoList(this.query).then(res => {
				if (res.state == 'Success') {
					let list = []
					let list1 = []
					res.content.records.map((item, index) => {
						if (index % 2) {
							list1.push(item)
						} else {
							list.push(item)
						}
					})
					this.list = this.list.concat(list)
					this.list1 = this.list1.concat(list1)
					let total = this.list.length + this.list1.length
					if (total >= res.content.total) {
						this.status = 'noMore'
					} else {
						this.status = 'more'
						this.query.currentPage++
					}
				}
			})
		},
		getStudyGoods() {
			this.pageContent.map(item => {
				studyGoods({ columnId: item.id, currentPage: 1, pageSize: 4 }).then(res => {
					if (res.state == 'Success') {
						item.list = res.content.records
					}
				})
			})
		},
		studyRecommendByUser() {
			studyGoodsByUser({
				columnId: this.query.columnId,
				currentPage: 1,
				pageSize: 4
			}).then(res => {
				if (res.state == 'Success') {
					let recommendList = []
					let recommendList1 = []
					res.content.records.map((item, index) => {
						if (index % 2) {
							recommendList1.push(item)
						} else {
							recommendList.push(item)
						}
					})
					this.recommendList = this.recommendList.concat(recommendList)
					this.recommendList1 = this.recommendList1.concat(recommendList1)
				}
			})
		},
		getColumnTabs() {
			getCommunitys({ columnId: this.query.columnId }).then(res => {
				if (res.state == 'Success') {
					this.tabList = res.content
					if (this.tabList.length > 0) {
						this.tab = this.tabList[0].id
						this.getItem(this.tab)
					}
				}
			})
		},
		// 点击社区tab
		handleTab(tab) {
			this.tab = tab
			this.communityList = []
			this.communityList1 = []
			this.getItem(tab)
		},
		//获取社区分类列表
		getItem(communityId) {
			getItem({ communityId, currentPage: 1, pageSize: 2, state: 2 }).then(res => {
				if (res.state == 'Success') {
					let communityList = []
					let communityList1 = []
					res.content.records.map((item, index) => {
						if (index % 2) {
							communityList1.push(item)
						} else {
							communityList.push(item)
						}
					})
					this.communityList = this.communityList.concat(communityList)
					this.communityList1 = this.communityList1.concat(communityList1)
				}
			})
		},
	},
	onLoad(options) {
		this.query.columnId = options.id
		// this.getPage(options.id).then(()=>{
		// 	this.getStudyGoods()
		// })
		this.getColumnTabs()
		this.studyRecommendByUser()
		let that = this
		const eventChannel = this.getOpenerEventChannel();
		if (JSON.stringify(eventChannel) !== '{}') {
			eventChannel.on('img', function (img, name) {
				that.bg = img
				that.title = name
			})
		}
	},
	onPageScroll(e) {
		if (e.scrollTop >= 30) {
			this.background = true
		} else {
			this.background = false
		}
	},
}
</script>

<style lang="scss">
.study-type {
	padding: 0 20rpx;
	background: #F9F9F9;

	.banner {
		width: 750rpx;
		height: 480rpx;
		vertical-align: bottom;
		position: relative;
		left: -20rpx;
	}

	.title-box {
		display: flex;
		align-items: center;
		justify-content: space-between;

		.more {
			font-size: 20rpx;
			color: #AAAAAA;
		}
	}

	.more-title {
		display: flex;
		justify-content: space-between;
		align-items: center;

		.more-box {
			display: flex;
			align-items: center;
			font-size: 24rpx;
			color: #AAAAAA;

			.more {
				width: 20rpx;
				height: 20rpx;
				display: block;
				margin-left: 6rpx;
			}
		}
	}

	.tab-box {
		display: flex;
		align-items: flex-start;

		.tab {
			flex-shrink: 0;
			padding: 10rpx 24rpx;
			// margin-top: 28rpx;
			color: #AAAAAA;
			background: #EEEEEE;
			font-size: 28rpx;
			margin-right: 20rpx;
			border-radius: 8rpx;
		}

		.tab.active {
			color: #222222;
			// background: #CEE0FF;
		}
	}

	.type-title {
		font-weight: bold;
		color: #222222;
		font-size: 32rpx;
		margin: 20rpx 0 20rpx;
	}

	.type-box {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;

		.type-item {
			width: 340rpx;
			margin-bottom: 20rpx;
			// box-shadow: 0rpx 0rpx 24rpx 2rpx rgba(0, 0, 0, 0.08);
			border-radius: 16rpx;
			background: #fff;

			.icon {
				width: 100%;
				height: 300rpx;
				border-radius: 16rpx 16rpx 0 0;
			}

			.info {
				flex: 1;
				padding: 16rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				position: relative;

				.title {
					font-size: 28rpx;
					font-weight: bold;
					width: 100%;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
					margin-top: 10rpx;
				}

				.desc {
					font-size: 24rpx;
					color: #AAAAAA;
					overflow: hidden;
					text-overflow: ellipsis;
					/* 弹性伸缩盒子模型显示 */
					display: -webkit-box;
					/* 限制在一个块元素显示的文本的行数 */
					-webkit-line-clamp: 2;
					/* 设置或检索伸缩盒对象的子元素的排列方式 */
					-webkit-box-orient: vertical;
					margin-top: 12rpx;
				}

				.price-box {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-top: 12rpx;

					.left {
						display: flex;
						align-items: flex-end;

						.unit {
							font-size: 20rpx;
							color: $uni-color-primary;
							font-weight: bold;
						}

						.price {
							font-size: 32rpx;
							color: $uni-color-primary;
							font-weight: bold;
						}
					}

					.right {
						font-size: 24rpx;
						color: #AAAAAA;
					}
				}

			}
		}
	}

	.swiper {
		height: 272rpx;
		border-radius: 16rpx;
		margin-top: 28rpx;

		.swiper-item {
			width: 100%;
			height: 100%;
			border-radius: 16rpx;
			object-fit: cover;
		}
	}

	.noneContent {
		display: flex;
		justify-content: center;
		width: 100%;
		padding-top: 10rpx;
		color: #777777;
		font-size: 28rpx;
	}

	.zs-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;

		.left {}

		.right {}

		.store-item {
			box-shadow: 0rpx 0rpx 24rpx 2rpx rgba(0, 0, 0, 0.08);
			border-radius: 16rpx;
			width: 344rpx;
			margin-top: 20rpx;
			position: relative;

			.play-icon {
				position: absolute;
				top: 20rpx;
				right: 30rpx;
				width: 30rpx;
				height: 30rpx;
				z-index: 2;
			}

			.info {
				padding: 20rpx;

				.title {
					color: #222222;
					height: 76rpx;
					font-size: 28rpx;
					font-weight: bold;
					width: 100%;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					/* 显示的最大行数 */
					overflow: hidden;
				}

				.user-info {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-top: 20rpx;
					font-size: 20rpx;
					color: #AAAAAA;

					.head {
						font-size: 20rpx;
					}

					.user-name {
						font-size: 20rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 1; 
					}
				}

				.price-box {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-top: 12rpx;

					.left {
						display: flex;
						align-items: flex-end;

						.unit {
							font-size: 24rpx;
							color: $uni-color-primary;
							font-weight: bold;
						}

						.price {
							font-size: 32rpx;
							color: $uni-color-primary;
							font-weight: bold;
						}

						.old-price {
							font-size: 20rpx;
							color: #AAAAAA;
							text-decoration: line-through;
							margin-left: 10rpx;
						}
					}

					.right {
						font-size: 20rpx;
						color: #AAAAAA;
					}
				}
			}
		}
	}
}
</style>