<template>
	<view class="main">
		<view class="one">
			<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage" @scroll="toScroll">
				<view class="list-scroll-view">
					<!-- 瀑布流布局列表 -->
					<view class="pubuBox">
						<view class="pubuItem">
							<view class="list" v-for="(item, index) in list" :key="index" @tap="toInfo(item)">
								<image :src="item.file&&item.file.length>0?item.file[0].url:''" mode="widthFix">
								</image>
								<view class="title"> <!-- 这是没有高度的父盒子（下半部分） -->
									<view class="title_1">{{ item.title }}</view>
									<view class="title_2">
										<view class="left">
											{{item.contact_name||'暂无昵称'}}
										</view>
										<view class="right" @tap.stop="toLike(item)">
											<text class="iconfont icon-shoucang"></text>
											{{item.collect_num||'0'}}
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="is_bottom" v-if="is_bottom">
						<text>{{config.bottom_title}}</text>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchInfo: {},
				config: {},
				user: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 6,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			}
		},
		onLoad: async function(e) {
			const that = this;
			that.searchToken();
			that.searchConfig();
			await that.searchOther();
		},
		onShow: async function() {
			const that = this;
			that.clearPage();
			await that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) that.$set(that, `user`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			searchConfig() {
				const that = this;
				try {
					const res = uni.getStorageSync('config');
					if (res) that.$set(that, `config`, res);
				} catch (e) {
					uni.showToast({
						title: err.errmsg,
						icon: 'error',
						duration: 2000
					});
				}
			},
			async search() {
				const that = this;
				if (!that.user._id) return
				let info = {
					skip: that.skip,
					limit: that.limit,
					user: that.user._id,
					type: '1'
				}
				const res = await that.$api(`/like/article`, '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)
				} else {
					uni.showToast({
						title: res.errmsg,
					});
				}
			},
			// 查看详情
			toInfo(item) {
				uni.navigateTo({
					url: `/pagesHome/article/index?id=${item.id||item._id}`
				})
			},
			// 取消收藏
			async toLike(item) {
				const that = this;
				let res;
				res = await that.$api(`/like/${item.collect}`, 'DELETE', {})
				if (res.errcode == '0') {
					that.clearPage();
					that.search()
				}
			},
			async searchOther() {
				const that = this;
				let res;
			},
			// 分页
			toPage(e) {
				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);
			},
			// 清空列表
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 6)
				that.$set(that, `page`, 0)
			}
		}
	}
</script>

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

		.one {
			position: relative;
			flex-grow: 1;
			background-color: var(--f9Color);
			margin: 2vw 0 0 0;

			.pubuBox {
				padding: 2vw;
			}

			.pubuItem {
				column-count: 2;
				column-gap: 2vw;
			}

			.list {
				box-sizing: border-box;
				border-radius: 2vw;
				overflow: hidden;
				background-color: var(--mainColor);
				break-inside: avoid;
				/*避免在元素内部插入分页符*/
				box-sizing: border-box;
				margin-bottom: 2vw;
			}

			.list image {
				width: 100%;
			}

			.title {
				padding: 2vw;

				.title_1 {
					font-size: var(--font14Size);
					line-height: 4vw;
					text-overflow: -o-ellipsis-lastline;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					line-clamp: 2;
					-webkit-box-orient: vertical;
					min-height: 6vw;
					max-height: 20vw;
				}

				.title_2 {
					display: flex;
					justify-content: space-between;
					font-size: var(--font12Size);
					color: var(--f69Color);
					padding: 1vw 0;

					.right {
						display: flex;
						align-items: center;

						text:first-child {
							padding: 0 1vw 0 0;
						}
					}
				}
			}
		}
	}
</style>