<template>
	<view>
		<view class="search">
			<u-search placeholder="搜索标题" v-model="param.title" bgColor="white" :showAction="false" @search="(current = 0), (param.type = ''), refresh()" @clear="(current = 0), (param.type = ''), (param.title = ''), refresh()"></u-search>
		</view>
		<view class="tab">
			<u-tabs :list="tab" :current="current" keyName="dictLabel" @click="click"></u-tabs>
		</view>
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index" @click="go('/pages/knowledge/detail?id=' + item.id)">
				<view class="title omit">
					<text class="icon" v-if="item.top === 1">&#xe61f;</text>
					<text>{{ item.title }}</text>
				</view>
				<view class="desc">
					<text>{{ item.type }}</text>
					<text>发布于 {{ item.createTime }}</text>
				</view>
			</view>
			<view class="loading" v-if="loadMore"><u-loadmore :status="loadMore ? 'loading' : 'nomore'" /></view>
			<u-empty v-if="!loadMore && list.length == 0"></u-empty>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			tab: [],
			current: 0,
			list: [],
			param: { pageNum: 1, pageSize: 10 },
			loadMore: true
		};
	},
	onLoad(e) {
		this.getType();
		this.getData();
	},
	methods: {
		click(e) {
			this.current = e.index;
			this.param.type = e.dictValue;
			this.refresh();
		},
		getData() {
			this.http.request({
				url: '/app/knowledge/list',
				data: this.param,
				loading: 'false',
				success: (res) => {
					this.loadMore = res.data.pages > this.param.pageNum ? true : false;
					res.data.rows.forEach((item) => {
						item.createTime = uni.$u.timeFrom(Date.parse(item.createTime));
						this.list.push(item);
					});
				}
			});
		},
		getType() {
			this.http.request({
				url: '/app/common/type/knowledge_type',
				loading: 'false',
				success: (res) => {
					this.tab = res.data.data;
					this.tab.unshift({ dictLabel: '全部类型', dictValue: '' });
				}
			});
		},
		go(url) {
			uni.navigateTo({ url: url });
		},
		//刷新数据
		refresh() {
			this.loadMore = true;
			this.param.pageNum = 1;
			this.list = [];
			this.getData();
		}
	},
	//下拉刷新
	onPullDownRefresh() {
		setTimeout(() => {
			this.refresh();
			uni.stopPullDownRefresh();
		}, 1000);
	},
	//上拉加载
	onReachBottom() {
		if (this.loadMore) {
			this.param.pageNum++;
			this.getData();
		}
	}
};
</script>

<style lang="scss">
.list {
	padding: 10px 12px;
	background-color: white;
	margin: 10px;
	border-radius: 10px;
	.item {
		border-radius: 5px;
		padding: 13px 12px 13px 12px;
		margin-bottom: 10px;
		overflow: hidden;
		border-bottom: 1px solid $line;
		&:last-child{
			border: 0px;
		}
		.title {
			font-size: 15px;
			font-weight: bold;
			.icon {
				color: orangered;
				padding-right: 3px;
			}
		}
		.desc {
			font-size: 14px;
			padding-top: 10px;
			color: $font-c;
			text {
				padding-right: 30px;
			}
		}
	}
}
</style>
