<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" @tap="toBanner(item)">
					<image class="image" :src="item.url" mode="">
					</image>
				</swiper-item>
			</swiper>
		</view>
		<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,index) 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">
			<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" @tap="toInfo(item)">
								1111
								<view class="is_bottom" v-if="is_bottom">
									<text>{{config.bottom_title}}</text>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</tabs>
		</view>
	</view>
</template>

<script>
	import tabs from '../../components/tabs/index.vue';
	export default {
		components: {
			tabs
		},
		data() {
			return {
				// 轮播图
				imgList: [],
				// 模块
				moduleList: [],
				// 公告内容
				content: '',
				tabs: {
					active: '0',
					bgColor: '#ffffff',
					menu: []
				},
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
				// 数据是否触底
				is_bottom: false,
				scrollTop: 0,
			}
		},
		onLoad: async function() {
			const that = this;
			await that.searchOther();
			await that.search();
		},
		onShow: async function(e) {
			const that = this;
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.search();
			uni.stopPullDownRefresh();
		},
		methods: {
			// 查询
			async search() {
				const that = this;
				let info = {
					skip: that.skip,
					limit: that.limit,
				}
				let res;
				res = await that.$api(`/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'
					})
				}
			},

			// 点击轮播图图片
			toBanner(item) {
				console.log(item)
			},
			// 点击模块
			toChange(item) {
				console.log(item)
			},
			// 查看更多公告
			toMore() {
				console.log('公告')
			},
			// 查询其他信息
			async searchOther() {
				const that = this;
				let res;
				// 基本设置
				res = await that.$api(`/config`, 'GET', {});
				if (res.errcode == '0') {
					that.$set(that, `imgList`, res.data.file);
				}
				//模块
				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)
				}
			},
			// 选择选项卡
			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`, 10)
				that.$set(that, `page`, 0)
			}
		},
	}
</script>

<style lang="scss">
	.main {
		.one {
			padding: 0 0 2vw 0;

			.swiper {
				height: 65vw;

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

		.two {

			.swiper {
				height: 50vw;

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

					.list {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						align-items: center;
						width: 20vw;
						padding: 1vw 0 0 0;

						.image {
							width: 15vw;
							height: 15vw;
							border-radius: 15vw;
							box-shadow: 0 0 5px var(--f1Color);
						}

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

		.thr {
			padding: 0 2vw;
		}

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

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

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

				.list:first-child {
					margin: 2vw 0 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>