<template>
	<view class="main">
		<view class="one">
			<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="list" v-for="(item, index) in list" :key="index">
								<view class="list_1">
									{{item.content||'暂无'}}
								</view>
								<view class="list_2" @tap="toInfo(item)">
									<view class="left">
										<image class="image"
											:src="item.source.file&&item.source.file.length>0?item.source.file[0].url:''"
											mode="aspectFill">
									</view>
									<view class="right">
										<view class="right_1 textOver">{{item.source.title||'暂无标题'}}</view>
										<view class="right_2"><text>发布人：</text>{{item.source.contact_name||'暂无'}}</view>
										<view class="right_2"><text>发布时间：</text>{{item.source.create_time||'暂无'}}
										</view>
									</view>
								</view>
								<view class="button">
									<button size="mini" type="warn" @tap.stop="toDel(item)">删除</button>
								</view>
							</view>
						</view>
						<view class="is_bottom" v-if="is_bottom">
							<text>{{config.bottom_title||'到底了！'}}</text>
						</view>
					</scroll-view>
				</view>
			</tabs>
		</view>
	</view>
</template>

<script>
	import tabs from '../../components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				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,
					status: that.tabs.active,
					user: that.user._id,
				}
				const res = await that.$api(`/comment`, 'GET', {
					...info,
				})
				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.source._id}`
				})
			},
			// 删除
			async toDel(e) {
				const that = this;
				uni.showModal({
					title: '提示',
					content: '确定删除该评论吗?',
					success: async function(res) {
						if (res.confirm) {
							const res = await that.$api(`/comment/${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 {
			background-color: var(--f9Color);
			margin: 2vw 0 0 0;

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

				.list {
					background-color: var(--mainColor);
					border: 1px solid var(--f5Color);
					padding: 2vw;
					margin: 2vw 2vw 0 2vw;
					border-radius: 5px;
					font-size: var(--font12Size);

					.list_1 {
						padding: 1vw;
					}

					.list_2 {
						display: flex;
						padding: 1vw;
						border-radius: 5px;
						border: 1px solid var(--f9Color);

						.left {
							width: 15vw;

							.image {
								width: 100%;
								height: 15vw;
							}
						}

						.right {
							padding: 2vw;
							width: 75vw;

							.right_1 {
								font-size: var(--font14Size);
								padding: 0 0 2px 0;
							}

							.right_2 {
								padding: 0 0 2px 0;

								text {
									color: var(--f85Color);
								}
							}
						}
					}

					.button {
						text-align: center;
						margin: 1vw 0 0 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>