<template>
	<view>
		<lf-nav :spreadOut="true" title="发现" @changeHeight="e => nav_height = e"></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" :scroll-top="scrollTop" @scroll="scroll">
					<view v-for="(item,index) of tab.list" :key="index">
						<view class="lf-p-32">
							<view class="lf-flex" @click="$url('/pages/user/my/my?user_id='+item.user_id)">
								<view class="tag-father">
									<image :src="item.user.avatar" v-if="item.user.avatar" mode="widthFix" class="head-img"></image>
									<image src="/static/images/empty.png" v-else mode="widthFix" class="head-img"></image>
									<view class="head-tag">V</view>
								</view>
								<view class="lf-flex-column lf-m-l-20">
									<view class="lf-flex" style="align-items: center;">
										<view class="lf-font-32 lf-color-black lf-font-bold" v-if="item.user.nick_name">{{item.user.nick_name}}</view>
										<view class="lf-font-32 lf-color-black lf-font-bold" v-else>{{item.user.mobile_replace}}</view>
										<text class="lf-iconfont icon-xiangyou lf-color-777 lf-m-l-10 lf-text-vertical" style="font-size: 18rpx;"></text>
									</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="goDetails(item.id)">
								{{item.content}}
							</view>
							<view class="lf-flex-wrap lf-m-t-20">
								<image v-for="(picture,index2) in item.attachs" @click.stop="checkPicture(item.attachs,index2)" :key="index2" 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" @click.stop="addLike(item.id)">
									<text class="lf-iconfont icon-xihuanlike lf-color-price lf-font-40" v-if="item.is_like"></text>
									<text class="lf-iconfont icon-xihuan lf-font-40" v-else></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-chakan lf-font-40"></text>
									<text class="lf-font-24 lf-color-777 lf-m-l-10">{{item.view_count}}</text>
								</view>
								<view class="lf-row-center" @click="goDetails(item.id)">
									<text class="lf-iconfont icon-pinglun- lf-font-40"></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 src="/static/images/empty.png" v-else></lf-nocontent>
					</view>
				</scroll-view>
			</swiper-item>
		</swiper>
		<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 class="fixed-right1" v-if="showTotop">
			<view class="fixed-btn" hover-class="lf-opacity" @click="goTop()">
				<text class="lf-iconfont icon-zhiding lf-font-50"></text>
			</view>
		</view>
		<!-- <view style="height: 30rpx;"></view> -->
		<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 {
				scrollTop: 0,
				oldScrollTop:0,
				showTotop: false,
				tab_list: [
					{name: '最新',type: 'created_at',list:[],..._public},
					{name: '最热',type: 'view_count',list:[],..._public},
					{name: '已关注',type: 'view_count',list:[],..._public}
				],
				tab_current: 0,
				scrollH: 0,
				nav_height: 0,
				isRefresher: true,
				pageSize: 10
			}
		},
		computed: {
			autoHeight(){
				return `calc(${this.scrollH}px - ${this.nav_height}px - 130rpx - 86rpx)`;
			}
		},
		onShow() {
			this.getHotActivity()
		},
		onLoad(){
			let info = uni.getSystemInfoSync();
			this.scrollH = info.screenHeight;
			this.getHotActivity()
		},
		methods: {
			scroll (e) {
				//记录scroll  位置
				this.oldScrollTop = e.detail.scrollTop;
				if(this.oldScrollTop > 900) {
					this.showTotop = true;
				}else {
					this.showTotop = false;
				}
			},
			goTop(e) {
				//视图会发生重新渲染
				this.scrollTop = this.oldScrollTop;
				//当视图渲染结束 重新设置为0
				this.$nextTick(() =>{
					this.scrollTop = 0;
				});
			},
			checkPicture(image_list,current) {
				if(image_list.length <= 0) return;
				let list = [];
				image_list.forEach((item,index) => {
					list.push(item.url)
				})
				this.$u.throttle(() => {
					uni.previewImage({
						urls: list,
						current: current
					});
				}, 500);
			},
			getCurrentPageUrlWithArgs() {
				var pages = getCurrentPages()    //获取加载的页面
				var currentPage = pages[pages.length-1]    //获取当前页面的对象
				var url = currentPage.route    //当前页面url
				var options = currentPage.options    //如果要获取url中所带的参数可以查看options
			
				//拼接url的参数
				var urlWithArgs = url + '?'
				for(var key in options){
					var value = options[key]
					urlWithArgs += key + '=' + value + '&'
				}
				urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length-1)
			
				return encodeURIComponent(urlWithArgs)
			},
			goDetails(id) {
				let token = this.$cookieStorage.get('user_token');
				if(!token) {
					var url = this.getCurrentPageUrlWithArgs();
					wx.showModal({
					    content:'请重新登录',
					    duration:1500,
					    showCancel: false,
					    success:(res)=>{
					        if (res.confirm) {
								// wx.navigateTo  邓平艺 于2021.09.24修改，
								// 原因：未登录点击后跳转到登录页，当登录成功，
								// 再次跳转回来时，用户点击页面返回，原有页面和弹窗还存在
					            uni.redirectTo({
					                url:`/pages/user/register/register?url=${url}`
					            })
					            return;
					        }
					    },
					    cancel:()=>{
					        uni.redirectTo({
					            url:`/pages/user/register/register?url=${url}`
					        })
					        return;
					    }
					})
				}else {
					this.$url('/pages/discover/discoverdetails?discover_id='+id)
				}
				
			},
			addLike(id) {
				this.$http
				    .post({
				        api: 'api/discover/like',
						data: {
							discover_id: id
						},
						header: {
						   Authorization: this.$cookieStorage.get('user_token')
						},
				    })
				    .then(res => {
				        if (res.data.code == 200) {
				            if (res.data.status) {
				        		this.getHotActivity();
				            } else {
				                wx.showModal({
				                    content: res.data.message || '请下拉页面刷新重试',
				                    showCancel: false
				                });
				            }
				        } else {
				            wx.showModal({
				                content: res.data.message || '请下拉页面刷新重试',
				                showCancel: false
				            });
				        }
				        wx.hideLoading();
				    })
				    .catch(() => {
				        wx.hideLoading();
				    });
			},
			getMyAttention(options = {}) {
				let tab_item = this.tab_list[this.tab_current];
				this.$http
				    .get({
				        api: 'api/discover/follow',
						data: {
							page: tab_item.page,
							page_size: this.pageSize,
						},
						header: {
						    Authorization: this.$cookieStorage.get('user_token')
						},
				    })
				    .then(res => {
				        if (res.data.code == 200) {
				           let isPage;
				           if(res.data.data.per_page == res.data.data.data.length) {
								isPage = true;
							}else {
								isPage = false;
							}
				            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);
				            }
				        } else {
				            wx.showModal({
				                content: '请下拉页面刷新重试',
				                showCancel: false
				            });
				        }
				        wx.hideLoading();
				    })
				    .catch(() => {
				        wx.hideLoading();
				       
				    });
			},
			// 页面触底，加载下一页
			onScrolltolower(){
				let tab_item = this.tab_list[this.tab_current];
				if(tab_item.isPage){
					tab_item.page = tab_item.page + 1;
					if(this.tab_current == 2) {
						this.getMyAttention();
					}else {
						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 = '正在加载中';
				if(this.tab_current == 2) {
					this.getMyAttention(options);
				}else {
					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();
				}else {
					this.getHotActivity();
				}
			},
			swiperChange(event){
				this.tab_current = event.detail.current;
				if (event.detail.source == '') return; // 如果是被动出发，没有事件类型则不做处理
				if(this.tab_current == 2) {
					this.getMyAttention();
				}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',
						data:{
							page: tab_item.page,
							page_size: this.pageSize,
							order: discover_type
						},
						header: {
						    Authorization: this.$cookieStorage.get('user_token')
						},
				    })
				    .then(res => {
				        if (res.data.code == 200) {
				            if (res.data.status) {
								let isPage;
								if(res.data.data.per_page == res.data.data.data.length) {
									isPage = true;
								}else {
									isPage = false;
								}
								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);
								}
				            } else {
				                wx.showModal({
				                    content: res.message || '请下拉页面刷新重试',
				                    showCancel: false
				                });
				            }
				        } else {
				            wx.showModal({
				                content: '请下拉页面刷新重试',
				                showCancel: false
				            });
				        }
				        wx.hideLoading();
				    })
				    .catch(() => {
				        wx.hideLoading();
				    });
			}
		}
	}
</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;
		}
	}
	
	.fixed-right1{
		position: fixed;
		right: 32rpx;
		bottom: 320rpx;
		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>
