<template>
	<view class="main">
		<view class="one">
			<view class="one_1">
				<input type="text" v-model="searchInfo.title" @input="toInput" placeholder="搜索标题">
			</view>
			<view class="one_2">
				<uni-icons @click="toAdd" class="add" type="plusempty" size="40" color="#007AFF"></uni-icons>
			</view>
		</view>
		<view class="two">
			<tabs :tabs="tabs" @tabsChange="tabsChange">
				<view class="tabsList">
					<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 v-if="item.is_like" class="iconfont icon-aixin1"></text>
													<text v-else class="iconfont icon-aixin"></text>
													{{item.like_num||'0'}}
												</view>
											</view>
										</view>
										<view class="button">
											<button size="mini" class="button" @tap.stop="toEdit(item)">修改</button>
											<button size="mini" type="warn" @tap.stop="toDel(item)">删除</button>
										</view>
									</view>
								</view>
							</view>
							<view class="is_bottom" v-if="is_bottom">
								<text>{{config.bottom_title||'到底了！'}}</text>
							</view>
						</view>
					</scroll-view>
				</view>
			</tabs>
		</view>
	</view>
</template>

<script>
	import tabs from '../../components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				searchInfo: {},
				config: {},
				user: {},
				tabs: {
					active: '0',
					bgColor: '#ffffff',
					menu: []
				},
				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;
				let info = {
					skip: that.skip,
					limit: that.limit,
					contact: that.user._id,
					status: that.tabs.active,
					user: that.user._id,
				}
				const res = await that.$api(`/article/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,
					});
				}
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.clearPage();
				that.search()
			},
			// 查看详情
			toInfo(item) {
				uni.navigateTo({
					url: `/pagesHome/article/index?id=${item.id||item._id}`
				})
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (that.searchInfo.title) that.$set(that.searchInfo, `title`, e.detail.value)
				else that.$set(that, `searchInfo`, {})
				that.clearPage();
				that.search();
			},
			// 新增
			toAdd() {
				uni.navigateTo({
					url: `/pagesMy/article/add`
				})
			}, // 修改
			toEdit(e) {
				uni.navigateTo({
					url: `/pagesMy/article/add?id=${e._id}`
				})
			},
			// 取消点赞
			async toLike(item) {
				const that = this;
				let res;
				res = await that.$api(`/like/${item.like}`, 'DELETE', {})
				if (res.errcode == '0') {
					that.clearPage();
					that.search()
				}
			},
			// 删除
			async toDel(e) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定删除该作品吗?',
					success: async function(res) {
						if (res.confirm) {
							const res = await that.$api(`/article/${e._id}`, 'DELETE', {})
							if (res.errcode == 0) {
								that.clearPage();
								that.search();
							} else {
								uni.showToast({
									title: res.errmsg,
									icon: 'none'
								})
							}
						}
					}
				});
			},
			async searchOther() {
				const that = this;
				let res;
				//状态
				res = await that.$api('/dictData', 'GET', {
					type: 'exam_status',
					is_use: '0'
				})
				if (res.errcode == '0') {
					const menu = res.data.map((item) => {
						return {
							title: item.label,
							active: item.value
						}
					})
					that.$set(that.tabs, `menu`, menu)
				}
			},
			// 分页
			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 {
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 2vw;

			.one_1 {
				padding: 0 2vw;
				width: 85vw;

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

		.two {
			background-color: var(--f9Color);
			margin: 2vw 0 0 0;

			.tabsList {
				position: relative;
				width: 100vw;
				height: 82vh;

				.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;
							}
						}
					}
				}

				.button {
					text-align: center;
					margin: 0 0 1vw 0;

					.button {
						background-color: var(--f3CColor);
						color: var(--mainColor);
						margin: 0 1vw 0 0;
					}
				}
			}
		}
	}

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

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

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

		text {
			padding: 2vw 0;
			display: inline-block;
			color: var(--f85Color);
			font-size: var(--font14Size);
		}
	}
</style>