<template>
	<view>
		<lf-nav :spreadOut="true" title="发现"></lf-nav>
		<view>
			<u-tabs :list="tab_list" active-color="#15716E" inactive-color='#777777' :is-scroll="true" :current="tab_current" @change="tabChange"></u-tabs>
		</view>
		<swiper :style="{height: autoHeight}" :current="tab_current" @change="swiperChange">
			<swiper-item v-for="(tab, tabIndex) in tab_list" :key="tabIndex">
				<scroll-view :style="{height: autoHeight}" :scroll-y="true" :refresher-enabled="true" :refresher-triggered="isRefresher"
					@scrolltolower="onScrolltolower" @refresherrefresh="onRefresherrefresh">
					<view v-for="(item,index) of tab.list">
						<view class="lf-p-32">
							<view class="lf-flex" @click="$url('/pages/user/my/my')">
								<view class="tag-father">
									<image src="https://hainan.lanzulive.com/storage/images/v2-deb89623e0ee2a2dad34bcded6dfd1ed_1440w.png" mode="aspectFill" class="head-img"></image>
									<view class="head-tag">V</view>
								</view>
								<view class="lf-flex-column lf-m-l-20">
									<view class="lf-font-32 lf-color-black lf-font-bold">最新FUEJFEI</view>
									<view class="lf-font-24 lf-color-777 lf-m-t-15">{{item.created_at}}</view>
								</view>
							</view>
							<view class="lf-font-28 lf-color-333 lf-m-t-20" @click="$url('/pages/discover/discoverdetails')">
								{{item.content}}
							</view>
							<view class="lf-flex-wrap lf-m-t-20">
								<image v-for="(picture,index) in item.attachs" class="qzone-img" :src="picture.url" mode="aspectFill"></image>
							</view>
							<view class="lf-m-t-30 lf-row-between lf-p-l-50 lf-p-r-50">
								<view class="lf-row-center">
									<text class="lf-iconfont icon-shoucang"></text>
									<text class="lf-font-24 lf-color-777 lf-m-l-10">{{item.likes_count}}</text>
								</view>
								<view class="lf-row-center">
									<text class="lf-iconfont icon-fabu"></text>
									<text class="lf-font-24 lf-color-777 lf-m-l-10">{{item.view_count}}</text>
								</view>
								<view class="lf-row-center">
									<text class="lf-iconfont icon-fenxiang"></text>
									<text class="lf-font-24 lf-color-777 lf-m-l-10">{{item.comments_count}}</text>
								</view>
							</view>
						</view>
						<self-line/>
					</view>
					<!-- 空数据的情况 -->
					<view class="loading-more">
						<text v-if="tab.list.length"
							:class="{'loading-more-text': tab.loadingClass}">{{ tab.loadingText }}</text>
						<lf-nocontent v-else></lf-nocontent>
					</view>
					<view class="fixed-right">
						<view class="fixed-btn" hover-class="lf-opacity" @click="$url('/pages/discover/publish')">
							<text class="lf-iconfont icon-fabu lf-font-50"></text>
						</view>
					</view>
					<view style="height: 30rpx;"></view>
				</scroll-view>
			</swiper-item>
		</swiper>
		<lf-tabbar></lf-tabbar>
	</view>
</template>

<script>
	import lfTabbar from '@/components/lf-tabbar/lf-tabbar.vue';
	export default {
		components: {
			lfTabbar
		},
		data() {
			let _public = {
				page: 1,
				isPage: true,
				loadingClass: true,
				loadingText: '正在加载中'
			}
			return {
				tab_list: [
					{name: '最新',type: 'created_at',list:[]},
					{name: '最热',type: 'view_count',list:[]},
					{name: '已关注',type: 'view_count',list:[]}
				],
				tab_current: 0,
				scrollH: 0,
				nav_height: 0,
				isRefresher: true,
				pageSize: 10
			}
		},
		computed: {
			autoHeight(){
				return `calc(${this.scrollH}px - ${this.nav_height}px - 86rpx)`;
			}
		},
		onLoad(){
			let info = uni.getSystemInfoSync();
			this.scrollH = info.screenHeight;
			this.getHotActivity()
		},
		methods: {
			getMyAttention() {
				let tab_item = this.tab_list[this.tab_current];
				this.$http
				    .get({
				        api: 'api/discover/follow',
						header: {
						    Authorization: this.$cookieStorage.get('user_token')
						},
				    })
				    .then(res => {
						console.log(res)
				        if (res.data.code == 200) {
				            if (res.data.status) {
								let isPage = res.data.next_page_url == null?false:true;
								tab_item.isPage = isPage;
								if(!isPage) {
									tab_item.loadingClass = false;
									tab_item.loadingText = '没有更多数据啦~';
								}
								if(options.type == 'pageRefresh') {
									uni.stopPullDownRefresh();
								}else if(options.type == 'scrollRefresh') {
									this.isRefresher = false;
								}
								if(tab_item.page == 1) {
									tab_item.list = res.data.data.data;
								}else {
									tab_item.list.push(...res.data.data.data);
								}
				               console.log('发现列表',tab_item.list)
				            } else {
				                wx.showModal({
				                    content: res.message || '请下拉页面刷新重试',
				                    showCancel: false
				                });
				            }
				        } else {
				            wx.showModal({
				                content: '请下拉页面刷新重试',
				                showCancel: false
				            });
				        }
				        wx.hideLoading();
				    })
				    .catch(() => {
				        wx.hideLoading();
				        wx.showModal({
				            content: '请求失败',
				            showCancel: false
				        });
				    });
			},
			// 页面触底，加载下一页
			onScrolltolower(){
				let tab_item = this.tab_list[this.tab_current];
				if(tab_item.isPage){
					tab_item.page = tab_item.page + 1;
					this.getHotActivity();
				}
			},
			// 下拉刷新处理
			refreshFn(options){
				let tab_item = this.tab_list[this.tab_current];
				tab_item.page = 1;
				tab_item.isPage = true;
				tab_item.loadingClass = true;
				tab_item.list = []
				tab_item.loadingText = '正在加载中';
				this.getHotActivity(options);
			},
			// scroll-view 下拉刷新
			onRefresherrefresh(){
				this.isRefresher = true;
				this.refreshFn({type: 'scrollRefresh'});
			},
			tabChange(event){
				this.tab_current = event;
				if(this.tab_current == 2) {
					this.getMyAttention();
					console.log('执行关注列表');
				}else {
					this.getHotActivity();
				}
			},
			swiperChange(event){
				this.tab_current = event.detail.current;
				if (event.detail.source == '') return; // 如果是被动出发，没有事件类型则不做处理
				if(this.tab_current == 2) {
					this.getMyAttention();
					console.log('执行关注列表');
				}else {
					this.getHotActivity();
				}
			},
			getHotActivity(options = {}) {
				let discover_type;
				let tab_item = this.tab_list[this.tab_current];
				if(this.tab_current == 0) {
					discover_type = 'created_at';
				}else {
					discover_type = 'view_count';
				}
				this.$http
				    .get({
				        api: 'api/discover',
						page: tab_item.page,
						page_size: this.pageSize,
						data:{
							order: discover_type
						}
				    })
				    .then(res => {
				        if (res.data.code == 200) {
				            if (res.data.status) {
								let isPage = res.data.next_page_url == null?false:true;
								tab_item.isPage = isPage;
								if(!isPage) {
									tab_item.loadingClass = false;
									tab_item.loadingText = '没有更多数据啦~';
								}
								if(options.type == 'pageRefresh') {
									uni.stopPullDownRefresh();
								}else if(options.type == 'scrollRefresh') {
									this.isRefresher = false;
								}
								if(tab_item.page == 1) {
									tab_item.list = res.data.data.data;
								}else {
									tab_item.list.push(...res.data.data.data);
								}
				               console.log('数组列表',tab_item.list)
				            } else {
				                wx.showModal({
				                    content: res.message || '请下拉页面刷新重试',
				                    showCancel: false
				                });
				            }
				        } else {
				            wx.showModal({
				                content: '请下拉页面刷新重试',
				                showCancel: false
				            });
				        }
				        wx.hideLoading();
				    })
				    .catch(() => {
				        wx.hideLoading();
				        wx.showModal({
				            content: '请求失败',
				            showCancel: false
				        });
				    });
			}
		}
	}
</script>

<style>
	page {
		background-color: white;
	}
</style>

<style lang="scss" scoped>
	.tag-father {
		position: relative;
	}
	.head-tag {
		color: white;
		display: flex;
		align-items: center;
		justify-content: center;
		text-align: center;
		font-size: 24rpx;
		width: 36rpx;
		height: 36rpx;
		border-radius: 50%;
		background-color: #15716E;
		border: 1rpx solid #FFFFFF;
		position: absolute;
		left: 66rpx;
		top: 70rpx;
		z-index: 99;
	}
	.qzone-img {
		width: 220rpx;
		height: 220rpx;
		border-radius: 10rpx;
		margin-right: 12rpx;
		&:nth-child(3n) {
			margin-right: 0;
		}
		&:nth-child(n + 4) {
			margin-top: 12rpx;
		}
	}
	.head-img {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50%;
	}
	.fixed-right{
		position: fixed;
		right: 32rpx;
		bottom: 188rpx;
		width: max-content;
		height: max-content;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		.fixed-btn{
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
			background-color: #15716E;
			display: flex;
			justify-content: center;
			align-items: center;
			color: #FFFFFF;
		}
	}
	
	
	// tab样式
	/deep/.u-scroll-box {
		display: flex;
		justify-content: center;
		align-items: center;
		border-bottom: 1rpx solid rgba(0, 0, 0, 0.1);
	}
	/deep/.u-scroll-box .u-tab-bar {
		background-color: #15716E!important;
		width: 80rpx!important;
		position: absolute;
		left: 0;
		bottom: -14rpx;
	}
	/deep/ .u-tab-item {
		font-size: 28rpx!important;
	}
</style>
