<template>
	<view class="main">
		<view class="one">
			<swiper class="swiper" circular :indicator-dots="true" indicator-color="#F5F5F5"
				indicator-active-color="#ffffff" :autoplay="true" :interval="3000" :duration="1000">
				<swiper-item class="list" v-for="(item,index) in imgList" :key="index">
					<image class="image" :src="item.url" mode="">
					</image>
				</swiper-item>
			</swiper>
		</view>
		<view class="bottom">
			<view class="two">
				<swiper class="swiper" circular :indicator-dots="true" indicator-color="#F5F5F5"
					indicator-active-color="#007AFF" :autoplay="false">
					<swiper-item class="swiper-item" v-for="(item,index) in moduleList" :key="index">
						<view class="list" v-for="(as,indexs) in item" :key="indexs" @tap="toChange(as)">
							<image class="image" :src="as.url&&as.url.length>0?as.url[0].url:''" mode="aspectFill">
							</image>
							<text class="text">{{ as.name }}</text>
						</view>
					</swiper-item>
				</swiper>
			</view>
			<view class="thr">
				<uni-notice-bar speed='50' showGetMore='true' moreText='更多' @getmore="toMore" show-icon scrollable
					:text="content" />
			</view>
			<view class="four" v-if="advertList.length>0">
				<swiper class="swiper" circular :indicator-dots="true" indicator-color="#F5F5F5"
					indicator-active-color="#007AFF" :autoplay="false">
					<swiper-item class="swiper-item" v-for="(item,index) in advertList" :key="index">
						<view class="list" v-for="(as,indexs) in item" :key="indexs" @tap="toChange(as)">
							<image class="image" :src="as.url&&as.url.length>0?as.url[0].url:''" mode="aspectFill">
							</image>
							<text class="text">{{ as.name }}</text>
						</view>
					</swiper-item>
				</swiper>
			</view>
			<view class="five">
				<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>
									</view>
								</view>
								<view class="is_bottom" v-if="is_bottom">
									<text>{{config.bottom_title||'到底了！'}}</text>
								</view>
							</view>
						</scroll-view>
					</view>
				</tabs>
			</view>
		</view>
	</view>
</template>

<script>
	import moment from 'moment';
	import tabs from '../../components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				user: {},
				config: {},
				// 轮播图
				imgList: [],
				// 模块
				moduleList: [],
				// 广告
				advertList: [],
				// 公告内容
				content: '',
				tabs: {
					active: '0',
					bgColor: '#ffffff',
					menu: []
				},
				list: [],
				total: 0,
				skip: 0,
				limit: 6,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			}
		},
		onShow: async function(e) {
			const that = this;
			that.searchToken();
			await that.searchOther();
			that.clearPage();
			await that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			await that.searchOther();
			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
					});
				}
			},
			// 查询
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit,
					is_use: '0',
					status: '1',
					type: that.tabs.active,
				}
				if (that.user._id) info.user = that.user._id
				let res;
				res = await that.$api(`/article/article`, '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,
						icon: 'none'
					})
				}
			},
			// 点击模块
			toChange(item) {
				uni.navigateTo({
					url: `${item.route}?title=${item.name}&type=${item.type}`,
				})
			},
			// 查看更多公告
			toMore() {
				uni.navigateTo({
					url: `/pagesHome/notice/index`,
				})
			},
			// 查看详情
			toInfo(item) {
				uni.navigateTo({
					url: `/pagesHome/article/index?id=${item.id||item._id}`
				})
			},
			// 点赞/取消点赞
			async toLike(item) {
				const that = this;
				let res;
				if (item.is_like == false) {
					const form = {
						user: that.user._id,
						source: item._id,
						source_type: "article",
						type: '0',
						create_time: moment().format('YYYY-MM-DD HH:mm:ss'),
					}
					res = await that.$api(`/like`, 'POST', form);
				} else res = await that.$api(`/like/${item.like}`, 'DELETE', {})
				if (res.errcode == '0') {
					that.clearPage();
					that.search()
				}
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 基本设置
				res = await that.$api(`/config`, 'GET', {});
				if (res.errcode == '0') {
					that.$set(that, `imgList`, res.data.file);
					that.$set(that, `config`, res.data);
				}
				//模块
				res = await that.$api(`/module`, 'GET', {
					is_use: '0'
				});
				if (res.errcode == '0') {
					const data = res.data.reduce((acc, cur, i) => {
						if (i % 10 === 0) acc.push([cur]); // 新增一个小数组
						else acc[acc.length - 1].push(cur); // 将当前元素加入最后一个小数组中
						return acc;
					}, []);
					that.$set(that, `moduleList`, data);
				}
				// 公告
				res = await that.$api(`/notice`, 'GET', {
					is_use: '0',
					limit: 1
				});
				if (res.errcode == '0') that.$set(that, `content`, res.data[0].name);
				// 查询状态
				res = await that.$api(`/dictData`, 'GET', {
					type: 'home_tabs',
					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)
				}
				// 广告
				res = await that.$api(`/advert`, 'GET', {
					is_use: '0'
				});
				if (res.errcode == '0') that.$set(that, `advertList`, res.data);
			},
			// 选择选项卡
			tabsChange(e) {
				const that = this;
				that.$set(that.tabs, `active`, e.active)
				that.clearPage();
				that.search()
			},
			// 分页
			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 {
		.one {
			padding: 0 0 2vw 0;

			.swiper {
				height: 70vw;

				.list {
					.image {
						width: 100%;
						height: 100%;
					}
				}
			}
		}

		.bottom {
			position: absolute;
			top: 65vw;
			left: 0;
			right: 0;
			background-color: var(--mainColor);
			border-radius: 20px;
			padding: 2vw 0 0 0;

			.two {

				.swiper {
					height: 48vw;
					margin: 2vw 0 0 0;

					.swiper-item {
						display: flex;
						flex-wrap: wrap;

						.list {
							display: flex;
							flex-direction: column;
							align-items: center;
							width: 20vw;

							.image {
								width: 14vw;
								height: 14vw;
								border-radius: 14vw;
							}

							.text {
								font-size: var(--font14Size);
								margin-top: 5px;
								color: var(--f85Color);
							}
						}
					}
				}
			}

			.thr {
				padding: 0 2vw;
			}

			.four {
				padding: 2vw;
			}

			.five {
				background-color: var(--footColor);

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

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

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