<template>
	<view class="news-page-info">
		<view class="tabs-info">
			<u-tabs :list="listTabs" @click="click"
			 lineColor="#387DF7"
			 lineWidth="35"
			 lineHeight="4"
			 :activeStyle="{
			 color: '#333333',
			 fontWeight: '600',
			 transform: 'scale(1.1)'
			}"
			:inactiveStyle="{
			 color: '#666666',
			 }"
			></u-tabs>
		</view>
		<view class="" v-show="tab == 0">
			<system-news :items="message.list" @loadMessage="loadMessage"></system-news>
		</view>
		<view class="" v-show="tab == 1">
			<at-my :items="data"></at-my>
		</view>
		<view v-if="total>query.pageSize">
			<uni-load-more :status="loadStatus"></uni-load-more>
		</view>
		
	</view>
</template>

<script>
	import apiQa from '@/api/modules/qa.js'
	import apiComments from '../../api/modules/comments.js';
	import apiUser from '@/api/system/user.js'
	import apiMessage from '@/api/system/message.js';
	export default {
		data() {
			return {
				listTabs:[
					{name:'系统'},
					{name:'@我的'}
				],
				tab:0,
				data:[],//拿到的数据
				total:0,//总条数
				loadStatus:'noMore',//more,loading,noMore
				query:{
					pageIndex:1,//当前页
					type:null,//当前只限于文章
					pageSize:10,//条数
				},//查询条件
				message:{
					list:[],
					total:0
				},
				queryData: {
					pageIndex: 1,
					pageSize: 10
				},
			}
		},
		onLoad() {
			this.findData(true);
			this.queryMessage(true);
		},
		onPullDownRefresh() {
			this.onReachBottom();
		},
		onReachBottom() {
			this.loadStatus='loading';//more,loading,noMore
			if(this.tab == 0) {
				if(this.message.list.length<this.message.total){
					this.queryData.pageIndex++;
					this.queryMessage(false);
					this.loadStatus='more'
				}else{
					this.loadStatus='noMore'
				}
			} else {
				if(this.data.length<this.total){
					this.query.pageIndex++;
					this.findData(false);
					this.loadStatus='more'
				}else{
					this.loadStatus='noMore'
				}
			}
			
		},
		methods: {
			loadMessage() {
				this.queryMessage(true);
			},
			queryMessage(flag) {
				if (flag) {
					this.message.list = [];
					this.queryData.pageIndex = 1;
				}
				let $this = this;
				uni.showLoading({ title: '加载中...' });
				apiMessage.list(this.queryData).then(res => {
					if (res.status == 200) {
						let ids = [];
						this.emptyControl=true
						this.message.total = res.result.count;
						if(res.result.list.length!=0){
							res.result.list.forEach(item => {
								item.authorInfo={avatar:'',name:'',code:'',sex:null};
								ids.push(item.sendAid);
								item.checked = false;
								this.message.list.push(item);
							});
							this.loadAuthorInfo(res.result.list,ids,0)
						}
					} else {
						this.$refs.toast.show({ message: '获取数据失败', type: 'error' })
					}
					uni.hideLoading();
				});
			},
			click(item) {
				this.tab = item.index;
			},
			ubtab(num){
				this.tab = num
			},
			findData(flag) {
				uni.showLoading({ title: '加载中...' });
				if(flag){
					this.data=[]
					this.pageIndex=1
				}
			 apiComments.pagelist(this.query).then(res=>{
				 if (res.status == 200) {
					 this.total=res.result.count;
					 let ids = [];
				 	if (res.result.list.length != 0) {
						res.result.list.forEach(item=>{
							ids.push(item.sysCreateAid);
							item.authorInfo={avatar:'',name:'',code:'',sex:null};
							this.data.push(item)
						})
						this.loadAuthorInfo(res.result.list,ids,1);
				 	}
				 }else{
					this.$refs.interactToast.show({message:'加载@我的评论失败',type:'error'});
				 }
				 uni.hideLoading();
			 })
			},
			loadAuthorInfo(list, ids,num) {
		    //加载作者信息，头像，机构信息
		    if (ids.length == 0) {
		      return;
		    }
			let $this = this;
		    const noReapetIds = [...new Set(ids)];
		    apiUser.getByIds(ids).then(res => {
		      if (res.status == 200) {
		        list.forEach((item, index) => {
		          res.result.some(author => {
		            if (num == 0 && author.aid == item.sendAid) {
		              item.authorInfo = author;
		              return true;
		            } else if(num == 1 && author.aid == item.sysCreateAid){
						item.authorInfo = author;
		            } else {
						return false;
					}
		          });
		        });
		      } else {
		        console.log('加载用户信息失败:' + res.error);
		      }
		    });
		  },
		}
	}
</script>

<style scoped lang="scss">

	.news-page-info{
		background-color: #fff;
		.tabs-info{
			padding: 0 254upx;
		}
	
	 
	}
</style>
