<template>
	<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/news/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 {
			current: 0,
			tab: [],
			list: [],
			param: { pageNum: 1, pageSize: 10, type: '新闻资讯' },
			loadMore: true
		};
	},
	onLoad(e) {
		this.param.type = e.type || '新闻资讯';
		this.getType();
		this.getData();
	},
	methods: {
		click(e) {
			this.current = e.index;
			this.param.type = e.dictValue;
			this.refresh();
		},
		getData() {
			this.http.request({
				url: '/app/news/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/news_type',
				loading: 'false',
				success: (res) => {
					this.tab = res.data.data;
					let obj = this.tab.filter((item) => item.dictLabel == this.param.type);
					if (obj.length > 0) {
						this.current = this.tab.indexOf(obj[0]);
					}
				}
			});
		},
		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: 12px;
	.item {
		background-color: white;
		border-radius: 5px;
		padding: 12px;
		margin-bottom: 10px;
		.title {
			font-size: 15px;
			font-weight: bold;
			.icon {
				color: orangered;
				padding-right: 3px;
			}
		}
		.desc {
			font-size: 14px;
			color: $font-c;
			padding-top: 7px;
			text {
				padding-right: 20px;
			}
		}
	}
}
</style>
