<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<input type="text" v-model="searchInfo.title" @blur="toInput" placeholder="请输入活动名称">
			</view>
			<view class="two">
				<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
					<view class="list-scroll-view">
						<view class="list" v-for="(item,index) in list" :key="index" @tap="toInfo(item)">
							<view class="img">
								<image class="image" :src="item.cover&&item.cover.length>0?item.cover[0].url:''" mode="">
								</image>
							</view>
							<view class="title">
								<text>{{item.title}}</text>
							</view>
							<view class="time" v-if="item.act_time.is_use=='0'">
								<text>{{item.act_time.value}}</text>
							</view>
						</view>
						<view class="is_bottom" v-if="is_bottom">
							<text>我们也是有底线的！</text>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				searchInfo: {},
				list: [],
				total: 0,
				page: 0,
				skip: 0,
				limit: 6,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			};
		},
		onLoad: function(e) {},
		onShow: function() {
			const that = this;
			that.search();
		},
		methods: {
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit
				}
				let res = await that.$api(`/platformAct`, 'GET', {
					...info,
					...that.searchInfo
				});
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data]
					that.$set(that, `list`, list);
					that.$set(that, `total`, res.total);
				}
			},
			toPage() {
				const that = this;
				let list = that.list;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();

				} else that.$set(that, `is_bottom`, true)
			},
			toScroll(e) {
				const that = this;
				let up = that.scrollTop;
				that.$set(that, `scrollTop`, e.detail.scrollTop);
				let num = Math.sign(up - e.detail.scrollTop);
				if (num == 1) that.$set(that, `is_bottom`, false);
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (e.detail.value) that.$set(that.searchInfo, `title`, e.detail.value);
				else that.$set(that, `searchInfo`, {});
				that.clearPage();
				that.search();
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			},
			toInfo(e) {
				const that = this;
				that.clearPage();
				uni.navigateTo({
					url: `/pagesRest/activity/info?id=${e._id}`
				})
			}
		}
	}
</script>

<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			border-bottom: 1px solid var(--f85Color);
			padding: 2vw;

			input {
				padding: 2vw;
				background-color: var(--f1Color);
				font-size: var(--font14Size);
				border-radius: 5px;
			}
		}

		.two {
			position: relative;
			flex-grow: 1;

			.list {
				margin: 2vw 2vw 0 2vw;
				width: 96vw;
				border-radius: 10px;
				box-shadow: 0 0 5px #858585;
				position: relative;

				.img {
					.image {
						width: 100%;
						height: 14vh;
						box-shadow: 0 0 5px #f1f1f1;
						border-radius: 10px;
					}
				}

				.title {
					position: absolute;
					top: 5vw;
					text-align: center;
					width: 95%;
					left: 2vw;

					text {
						font-size: 20px;
						font-family: cursive;
						color: #000000;
						font-weight: bold;
						overflow: hidden;
						text-overflow: ellipsis;
						-webkit-line-clamp: 2;
						word-break: break-all;
						display: -webkit-box;
						-webkit-box-orient: vertical;
					}
				}

				.time {
					position: absolute;
					bottom: 2vw;
					right: 2vw;
					font-size: 12px;
					width: 100%;
					text-align: right;
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
		}
	}

	.is_bottom {
		text-align: center;

		text {
			padding: 1vw 0;
			display: inline-block;
		}
	}

	.is_bottom {
		text-align: center;
		width: 100vw;

		text {
			padding: 1vw 0;
			display: inline-block;
		}
	}
</style>
