<template>
	<view>
		<view class="search-wrap">
			<image class="bg" src="@/static/image/study-bg.jpg"></image>
			<view class="info">
				<view class="text">销售必备技能</view>
				<uni-search-bar class="search" radius="30" placeholder="请输入文章名称，标签" v-model="keyword" clearButton="auto" cancelButton="none" bgColor="#fff" />
			</view>
		</view>
		
		<view class="type">
			<view v-for="(item, i) in classifications.slice(0, 4)" :key="i" :class="['item', {active: active == item.id}]" @click="classificationClick(item, 1)">{{ item.classificationName }}</view>
			<image class="unfold" src="@/static/image/unfold.png" mode="widthFix" @click="typeVisible = true"></image>
		</view>
		
		<template v-if="list.length">
			<view class="list">
				<view v-for="(item, i) in list" :key="i" class="item" @click="toDetail(item)">
					<image class="pic" :src="item.bannerImg"></image>
					<view class="right">
						<view class="title ell">{{ item.title }}</view>
						<view v-if="item.labelName" class="labels">
							<view v-for="(label, j) in item.labelName.split(',').slice(0, 2)" :key="j" class="label">{{ label }}</view>
						</view>
						<view class="metas">
							<view class="meta">
								<uni-icons class="icon" type="eye" size="22" color="#007FFF"></uni-icons>
								{{ item.learnerNumber }}人学过
							</view>
							<!-- <view class="meta">
								<uni-icons class="icon" type="star" size="22" color="#007eff"></uni-icons>
								123
							</view> -->
						</view>
						<view v-if="item.lastReading" class="last">上次阅读</view>
					</view>
				</view>
			</view>
			<uni-load-more :status="status" />
		</template>
		<empty v-else></empty>
		
		<view class="type-popup" v-show="typeVisible">
			<uni-icons class="close" type="closeempty" size="20" color="#757575" @click="closeType"></uni-icons>
			<view class="title">所属分类</view>
			<view class="types">
				<view v-for="(item, i) in classifications" :key="i" :class="['item', {active: active == item.id}]" @click="classificationClick(item)">{{ item.classificationName }}</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { partnerOperatingList, queryClassificationByType } from '@/apis/modules/article.js'
	export default {
		data() {
			return {
				classifications: [],
				active: '',
				typeVisible: false,
				reachBottom: 0, // 是否是上拉加载。0->否,1->是,-1->加载完所有数据
				status: 'more', // 上拉加载状态 more|loading|noMore
				searchTimer: null,
				articleNameSort: '',
				keyword: '',
				list: [],
				page: 1,
				pageSize: 10,
			}
		},
		watch: {
			keyword () {
				clearTimeout(this.searchTimer)
				this.searchTimer = setTimeout(() => {
					this.initList()
				}, 500)
			}
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.initList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1500)
		},
		// 上拉加载
		onReachBottom() {
			if (this.reachBottom >= 0) {
				this.reachBottom = 1
				this.status = 'loading'
				this.getList()
			}
		},
		onShow() {
			this.keyword = ''
			this.active = ''
			this.typeVisible = false
			this.initRole()
		},
		methods: {
			// 初始化权限
			initRole() {
				if (!uni.getStorageSync('auth').includes('学习')) {
					this.per = false
				}
				this.initList()
				this.getClassification()
			},
			getList() {
				const data = {
					keyWord: this.keyword,
					pageNum: this.page,
					pageSize: this.pageSize,
					querySource: 4,
					typeId: 1,
					articleNameSort: this.articleNameSort,
					classificationId: this.active
				}
				uni.showLoading({
					title: '加载中'
				})
				partnerOperatingList(data).then(({ page, total }) => {
					uni.hideLoading()
					// 未加载完所有数据，并且不是筛选，则拼接list，否则直接赋值
					const list = page
					this.list = this.reachBottom > 0 ? [...this.list, ...list] : list
					this.page++ // 每次获取了数据后page+1
					const noMore = this.list.length === total // 是否加载完所有数据
					this.status = noMore ? 'noMore' : 'more' // 加载完了则设置为noMore
					this.reachBottom = noMore ? -1 : 0 // 加载完了则设置为-1
				}).catch(e => {
					uni.hideLoading()
				})
			},
			initList() {
				this.page = 1
				this.reachBottom = 0
				this.getList()
			},
			// 获取所属分类
			getClassification() {
				queryClassificationByType(1).then(({ data }) => {
					this.classifications = [
						{
							id: '',
							classificationName: '不限'
						}
					]
					this.classifications.push(...data)
				}).catch(e => {})
			},
			// 所属分类点击回调
			classificationClick(item, query) {
				this.active = item.id
				query && this.initList()
			},
			// 关闭所属分类弹框
			closeType() {
				this.typeVisible = false
				this.initList()
			},
			// 展开
			toggle(item) {
				item.toggle = !item.toggle
			},
			// 排序
			switchSort() {
				this.articleNameSort = this.articleNameSort === 'desc' ? 'asc' : 'desc'
				this.initList()
			},
			// 跳转详情
			toDetail(item) {
				this.$util.to(`/team/article/article?id=` + item.id)
			},
		}
	}
</script>

<style scoped lang="scss">
.search-wrap {
	position: relative;
	height: 300rpx;
	padding: 120rpx 10px 0;
	.bg {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
	}
	.info {
		position: relative;
	}
	.text {
		margin: 0 10px 10rpx;
		font-size: 50rpx;
		color: #fff;
	}
}
.type {
	position: relative;
	display: flex;
	padding: 30rpx;
	background-color: #fff;
	.item {
		padding: 0 15rpx;
		line-height: 1.6;
		font-size: 28rpx;
		color: #333;
		white-space: nowrap;
		&.active {
			color: #007FFF;
			font-weight: 600;
		}
	}
	.unfold {
		position: absolute;
		right: 40rpx;
		width: 40rpx;
	}
}
.list {
	background-color: #fff;
	.item {
		position: relative;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 10rpx;
		padding: 20rpx 40rpx;
		border-bottom: 1px solid #f1f1f1;
	}
	.pic {
		width: 260rpx;
		height: 180rpx;
		border-radius: 8px;
	}
	.right {
		width: calc(100% - 280rpx);
	}
	.title {
		margin-bottom: 15rpx;
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
	}
	.labels {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
	}
	.label {
		padding: 2px 6px;
		margin: 0 20rpx 15rpx 0;
		font-size: 24rpx;
		color: #fff;
		background-color: #ccc;
		border-radius: 4px;
	}
	.metas {
		display: flex;
		align-items: center;
	}
	.meta {
		display: inline-flex;
		align-items: center;
		margin-right: 20rpx;
		font-size: 24rpx;
	}
	.icon {
		margin-right: 5rpx;
	}
	.last {
		position: absolute;
		bottom: 20rpx;
		right: 40rpx;
		font-size: 28rpx;
		color: #007FFF;
	}
}
</style>
