<template>
	<view class="content">

		<!-- 导航 -->
		<view class="nav">
			<view class="leftView" @click="back">
				<image src="../../static/mobile/backBtn.png" mode=""
					style="width: 22rpx; height: 40rpx; padding-left: 24rpx;"></image>
			</view>
			<view class="navTitle">{{topName}}</view>
			<view class="rightView">
				<image src="../../static/mobile/icon_search@2x.png" mode="" class="rightImg" style="margin-left: 40rpx;"
					@click="goSearch"></image>
			</view>
		</view>

		<!-- content -->
		<view class="commend">
			<view class="top">{{comment}}</view>

			<!-- tab -->
			<view class="scrollBg">
				<scroll-view scroll-x="true" @scroll="scroll">
					<view class="tabBg">
						<view v-for="(item,index) in bannerArr" :key="index" @click="tabClick(index)">
							<view class="tabTitle" :class="{tabActive:tabIndex==index}">{{item.name}}</view>

						</view>
					</view>
				</scroll-view>
			</view>

		</view>

		<!-- item -->

		<view class="itemBg">
			<view v-for="(item,index) in itemArr" :key="index" class="twoItem" @click="goList(item)">
				<image :src="item.LogoImg" mode="" class="img2"></image>
				<view class="rightItem">
					<view class="title">{{item.Title}}</view>
					<view class="title2">{{item.Comment}}</view>
				</view>
			</view>
		</view>


		<!-- 上拉 加载更多 -->
		<view class="noMore" v-if="noMoreShow">没有更多数据</view>
		<!-- 无数据空白页 -->
		<nodata v-if="itemArr.length==0"></nodata>

	</view>
</template>

<script>
	import nodata from '../../components/nodata/nodata.vue'
	export default {
		components: {
			nodata,
		},
		data() {
			return {
				topName: '',
				topCode: '',
				comment: '',
				page: 1,

				bannerArr: [],
				scrollTop: 0,
				old: {
					scrollTop: 0
				},
				itemArr: [],
				tabIndex: 0,
				noMoreShow: false,
			}
		},
		onLoad(opt) {
			this.topName = opt.topName
			this.topCode = opt.topCode
			this.comment = opt.comment
			this.page = 1
			this.getItemData()
			// uni.setNavigationBarTitle({
			// 	title: this.topName
			// })
		},
		methods: {
			goList(item){
				
				uni.navigateTo({
					url:'list?topCode=' + item.Code + '&comment=' + item.Comment + '&topName=' + item.Title
				})
				return;
				
				// 1分类2文章
				if (item.type == 1) {
					uni.navigateTo({
						url:'list?topCode=' + item.Code + '&comment=' + item.Comment + '&topName=' + item.Title
					})
				}
				if (item.type == 2) {
					// uni.navigateTo({
					// 	url:''
					// })
				}
			},
			getItemData() {
				uni.showLoading({
					title: '加载中'
				})
				let parentCode;
				if (this.tabIndex == 0) {
					parentCode = this.topCode
				} else {
					parentCode = this.bannerArr[this.tabIndex].code
				}
				let url = '/trainingOpenApi/categoryPageData',
					params = {
						page: this.page,
						limit: 10,
						parentCode: parentCode,
						topCode: this.topCode
					}
				this.$http(url, params, 'GET').then(res => {
					uni.hideLoading();
					var data = res.data


					// 处理 undefined和null转为空白字符串

					for (const key in data) {
						data[key] = this.$praseStrEmpty(data[key])
					}



					if (this.page == 1) {
						this.itemArr = data.Items
					} else {
						this.itemArr = this.itemArr.concat(data.Items)
					}
					if (data.Items.length < 10) {
						this.noMoreShow = true
					} else {
						this.noMoreShow = false
					}
					if (this.itemArr.length == 0) {
						this.noMoreShow = false
					}

					this.bannerArr = data.dynamicCol
					let dic = {
						code: this.topCode,
						name: '全部',
					}
					this.bannerArr.splice(0, 0, dic)

				})
			},
			goSearch() {
				uni.navigateTo({
					url: 'search'
				})
			},
			back() {
				uni.navigateBack({

				})
			},
			scroll: function(e) {
				console.log(e)
				this.old.scrollTop = e.detail.scrollTop
			},
			tabClick(num) {
				this.tabIndex = num;
				this.page = 1;
				this.getItemData()
			},



			// 下拉刷新 上拉加载更多
			onPullDownRefresh() {
				this.page = 1;
				this.getItemData()

				// this.getDetailData()
				setTimeout(function() {
					uni.stopPullDownRefresh();
				}, 1000);
			},
			onReachBottom() {
				this.page++;

				this.getItemData()

			}
		}
	}
</script>

<style scoped>
	.content {
		min-height: 100vh;
		background-color: #FFFFFF;
		padding-top: 88rpx;
		padding-bottom: 20rpx;

	}

	.nav {
		position: fixed;
		left: 0;
		top: 0;
		width: 100vw;
		height: 88rpx;
		background-color: #FFFFFF;
		display: flex;
		justify-content: space-between;
		align-items: center;
		z-index: 999;
		border-bottom: #eeeeee 2rpx solid;
	}

	.navTitle {
		font-size: 36rpx;
		font-weight: bold;
		color: #3c3c3c;

	}

	.leftView {
		width: 30%;
	}

	.rightView {
		width: 30%;
		display: flex;
		justify-content: flex-end;
		padding-right: 24rpx;
	}

	.rightImg {
		width: 40rpx;
		height: 40rpx;

	}

	.commend {
		position: fixed;
		left: 0;
		top: 90rpx;
		width: calc(100vw - 50rpx);

		background-color: #FFFFFF;

		padding: 0 25rpx 30rpx;
		z-index: 999;
		height: 300rpx;

	}

	.top {
		padding: 40rpx 0;
		text-align: center;
		height: 160rpx;
		color: #999999;
		font-size: 24rpx;

		//超过固定行数 隐藏
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		word-wrap: break-word;
		white-space: normal !important;
		-webkit-line-clamp: 3;
		-webkit-box-orient: vertical;
	}



	.scrollBg {
		background-color: #FFFFFF;

	}

	.scroll-view {
		white-space: nowrap;
	}

	.tabBg {
		display: flex;
		margin-top: 0;
		margin-bottom: 0;
	}



	.tabTitle {
		margin-right: 60rpx;
		font-size: 30rpx;
		color: #3c3c3c;
		text-overflow: ellipsis;
		white-space: nowrap;
		height: 60rpx;
		line-height: 60rpx;
	}

	.tabActive {
		color: #FF4F00;
	}

	.itemBg {

		background-color: #FFFFFF;
		padding: 300rpx 25rpx 30rpx;

	}

	.twoItem {
		display: flex;

		background-color: #FFFFFF;
		padding: 30rpx 0;
		border-top: #eeeeee 2rpx solid;
	}

	.title {
		font-size: 28rpx;
		font-weight: bold;
		color: #3c3c3c;

	}

	.title2 {

		font-size: 24rpx;
		font-weight: bold;
		color: #999999;

		//超过固定行数 隐藏
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		word-wrap: break-word;
		white-space: normal !important;
		-webkit-line-clamp: 3;
		-webkit-box-orient: vertical;
	}


	.img2 {
		width: 240rpx;
		height: 160rpx;
	}

	.rightItem {
		margin-left: 30rpx;
		width: calc(100vw - 300rpx);
	}
	.noMore {
		text-align: center;
		line-height: 50rpx;
		color: #999999;
		font-size: 28rpx;
	}
</style>
