<template>
	<page-meta>
		<navigation-bar :title="categoryName" title-align="center" background-color="#F1F3F9" front-color="#000000" />
	</page-meta>

	<view class="trend-category">
		<u-sticky zIndex="98" bgColor="#F1F3F9">
			<view class="nav-bar">
				<view v-if="!isSearch" style="margin-right: 20rpx;">
					<u-icon name="search" size="27" @click="isSearch = true"></u-icon>
				</view>
				<view v-else style="width: 100%;">
					<!-- #ifndef APP-NVUE -->
					<u-input :customStyle="{ padding: '17rpx 36rpx', background: '#FFFFFF' }"
						v-model="queryParams.title" :placeholder="$tt('group.inputContent')" shape="circle"
						@clear="handleClearSearch" clearable>
					<!-- #endif -->
						<!-- #ifdef APP-NVUE -->
						<u--input :customStyle="{ padding: '17rpx 36rpx', background: '#FFFFFF' }"
							v-model="queryParams.title" :placeholder="$tt('group.inputContent')" shape="circle"
							@clear="handleClearSearch" clearable>
						<!-- #endif -->
							<template slot="prefix">
								<u-icon name="search" color="rgb(192, 196, 204)" size="26"
									@click="isSearch = false"></u-icon>
							</template>
							<template slot="suffix">
								<view style="display: flex; flex-direction: row; align-items: center;">
									<span
										style="width: 0px; height: 14px; border: 1px solid #000000; opacity: 0.1;"></span>
									<span style="font-size: 14px; font-weight: 400; color: #3378FE; margin-left: 24rpx;"
										@click="handleSearch">{{$tt('common.search')}}</span>
								</view>
							</template>
					<!-- #ifndef APP-NVUE -->
					</u-input>
					<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
					</u--input>
					<!-- #endif -->
				</view>
			</view>
		</u-sticky>

		<view class="card-wrap">
			<view class="item" v-for="(item, index) in list" :key="index">
				<view style="margin-right: 10px">
					<u--image :src="item.imgUrl" @click="gotoDetail(item.newsId)" mode="aspectFill" radius="5"
						width="120" height="80">
						<template v-slot:loading>
							<u-loading-icon></u-loading-icon>
						</template>
					</u--image>
				</view>
				<view>
					<u--text lines="2" lineHeight="24" size="12" bold @click="gotoDetail(item.newsId)"
						:text="item.title"></u--text>
					<view style="display: flex; align-items: center; margin-top: 5px">
						<view style="margin-right: 32rpx">
							<u--text iconStyle="color:#606266;font-size:14px;margin-right:3px;" size="12"
								color="#606266" mode="name" :text="item.author"></u--text>
						</view>
						<view>
							<u--text iconStyle="color:#606266;font-size:14px;margin-right:3px;" size="12"
								color="#606266" mode="date" :text="item.createTime"></u--text>
						</view>
					</view>
				</view>
			</view>
			<u-loadmore :status="status" v-if="total > queryParams.pageSize" marginTop="20" />
		</view>

		<u-empty mode="list" :show="total == 0" marginTop="30"></u-empty>
	</view>
</template>

<script>
	import projectConfig from '@/env.config.js';
	import { listTrend } from '@/apis/modules/trend';

	export default {
		data() {
			return {
				isSearch: true,
				status: 'loadmore',
				list: [],
				total: 0,
				queryParams: {
					pageNum: 1,
					pageSize: 7,
					status: 1,
					categoryId: null,
				},
				categoryName: ''
			};
		},
		//option为object类型，会序列化上个页面传递的参数
		onLoad: function(option) {
			this.queryParams.categoryId = option.categoryId;
			this.categoryName = option.categoryName;
		},
		created() {
			this.getlistTrend();
		},
		methods: {
			gotoDetail(newsId) {
				uni.navigateTo({
					url: '/pagesA/trend/detail?newsId=' + newsId
				});
			},
			// 列表
			getlistTrend() {
				listTrend(this.queryParams).then(res => {
					let list = null;
					let rows = res.rows
						.map((item, i) => {
							item.imgUrl = projectConfig.baseUrl + item.imgUrl;
							return item;
						})
					if (this.queryParams.pageNum == 1) {
						this.list = rows;
					} else {
						this.list = this.list.concat(rows);
					}
					this.total = res.total;
					this.loading = false;
					uni.stopPullDownRefresh();
				});
			},
			// 搜索
			handleSearch(value) {
				this.list = [];
				this.queryParams.pageNum = 1;
				this.getlistTrend();
			},
			handleClearSearch() {
				this.handleSearch();
			},
			// 下拉刷新
			onPullDownRefresh() {
				this.list = [];
				this.queryParams.pageNum = 1;
				this.getlistTrend();
			},
			// 上拉加载
			onReachBottom() {
				this.status = 'loading';
				this.queryParams.pageNum = this.queryParams.pageNum + 1;
				if ((this.queryParams.pageNum - 1) * this.queryParams.pageSize > this.total) {
					this.status = 'nomore';
				} else {
					this.getlistTrend();
					this.status = 'loading';
				}
			}
		}
	};
</script>

<style lang="scss">
	page {
		background: $uni-bg-color-grey;
	}


	.trend-category {

		.nav-bar {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			padding: 24rpx 30rpx 28rpx;
		}

		.card-wrap {
			padding: 0 30rpx 40rpx 30rpx;

			.item {
				display: flex;
				align-items: center;
				border-radius: 20rpx;
				padding: 20rpx;
				background-color: #ffffff;
				margin-top: 26rpx;
			}

			.item:first-child {
				margin-top: 0;
			}
		}
	}
</style>