<template>
	<view>
		<view class="filter">
			<uni-search-bar class="search" radius="30" placeholder="请输入文章名称，标签" v-model="keyword" clearButton="auto" cancelButton="none" />
			<view :class="['sort', sort]" @click="switchSort"></view>
			<uni-icons class="icon" custom-prefix="iconfont" type="icon-filter" size="22" color="#007eff" @click="popup = true"></uni-icons>
		</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">{{ item.title }}</view>
						<view v-if="item.labelName" class="labels">
							<view v-for="(label, j) in item.labelName.split(',')" :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>
		
		<filter-popup :data="filterData" :form.sync="filterForm" v-model="popup" title="全部筛选" height="1104rpx" @finsh="subFinsh"></filter-popup>
	</view>
</template>

<script>
	import { partnerOperatingList, queryClassificationByType } from '@/apis/modules/article.js'
	export default {
		data() {
			return {
				popup: false,
				//筛选表单数据
				filterData: [
					{
						children: false,//是否有子项
						title: "所属类型",
						key: "classificationId", //键名 接收对象名字
						keyValue: "id", //获取的值是哪个
						isRadio: true, //是否单选 否则多选
						data: [],
					}
				],
				filterForm: {
					classificationId: []
				},
				classificationId: '',
				reachBottom: 0, // 是否是上拉加载。0->否,1->是,-1->加载完所有数据
				status: 'more', // 上拉加载状态 more|loading|noMore
				searchTimer: null,
				articleNameSort: 'desc',
				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.initRole()
		},
		methods: {
			// 初始化权限
			initRole() {
				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.classificationId
				}
				uni.showLoading({
					title: '加载中'
				})
				partnerOperatingList(data).then(({ data }) => {
					uni.hideLoading()
					// 未加载完所有数据，并且不是筛选，则拼接list，否则直接赋值
					const list = data.records
					this.list = this.reachBottom > 0 ? [...this.list, ...list] : list
					this.page++ // 每次获取了数据后page+1
					const noMore = this.list.length === data.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 }) => {
					data.forEach(e => {
						e.title = e.classificationName
						e.value = e.id
					})
					this.filterData[0].data = data
					console.log(11, this.filterData[0].data)
				}).catch(e => {})
			},
			// 展开
			toggle(item) {
				item.toggle = !item.toggle
			},
			// 筛选确定回调
			subFinsh(val) {
				const { classificationId } = val
				this.classificationId = classificationId.length ? classificationId[0] : ''
				this.initList()
			},
			// 排序
			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">
.list {
	margin-top: 20rpx;
	background-color: #fff;
	.item {
		position: relative;
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 10rpx;
		padding: 20rpx 40rpx;
		border-bottom: 1px solid #f1f1f1;
	}
	.pic {
		width: 200rpx;
		height: 140rpx;
	}
	.right {
		width: calc(100% - 230rpx);
	}
	.title {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
	}
	.labels {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		margin-top: 15rpx;
	}
	.label {
		padding: 2px 6px;
		margin-bottom: 15rpx;
		margin-right: 20rpx;
		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;
	}
	.last {
		position: absolute;
		bottom: 20rpx;
		right: 40rpx;
		font-size: 28rpx;
		color: #007FFF;
	}
}
</style>
