<template>
	<view class="container"> 
		<view class="content">
			<!-- 如果页面中的cell高度是固定不变的，则不需要设置cell-height-mode，如果页面中高度是动态改变的，则设置cell-height-mode="dynamic" -->
			<!-- 原先的v-model修改为@virtualListChange="virtualListChange"并赋值处理后的虚拟列表 -->
			<z-paging ref="paging" :auto="false" defaultPageSize="20" use-virtual-list :auto-clean-list-when-reload="false" :force-close-inner-list="true" :paging-style="{ paddingTop: paddingTop + 'px', paddingBottom: paddingBottom + 'rpx' }" cell-height-mode="dynamic" @virtualListChange="virtualListChange" @query="queryList">
				<!-- 需要固定在顶部不滚动的view放在slot="top"的view中，如果需要跟着滚动，则不要设置slot="top" -->
				<template #top>
					<nav-bar :tabList="tabList" @initNav="initNav" @change="change"></nav-bar>
				</template>
				
				<friend v-if="tabType == 'friend'" :virtualList="virtualList" @reload="reload"></friend>
				<peiwan v-if="tabType == 'peiwan'"></peiwan>
				
				<!-- 自定义没有更多数据view -->
				<template #empty v-if="showLoad">
					<view></view>
				</template>
				<!-- 自定义没有更多数据view -->
				<template #loadingMoreNoMore v-if="showLoad">
					<view></view>
				</template>
			</z-paging>
			
			
		</view>
	</view>
</template>
  
<script>
	import NavBar from '@/pages/tabbar/components/message/navBar.vue';
	import friend from '@/pages/tabbar/components/message/friend.vue';
	import peiwan from '@/pages/tabbar/components/message/peiwan.vue';
	import ImConversationApi from '@/sheep/api/im/memberConversation';
	import TradeConfigApi from '@/sheep/api/trade/config';
	import sheep from '@/sheep';
	export default {
		components: {
			NavBar, 
			friend,
			peiwan,
		},
		data() {
			return {
				tabList: [
				{
					name: '达人',
					enabled: false,
					type: 'peiwan',
				},{
					name: '交友',
					enabled: false,
					type: 'friend',
				},],
				
				tabType: '',
				
				// 虚拟列表数组，通过@virtualListChange监听获得最新数组
				virtualList: [],
				
				paddingTop: 0,
				paddingBottom: 100,
			}
		},
		created() {
			this.getTradeConfig();
		},
		computed: {
			showLoad() {
				return this.tabType == 'peiwan';
			},
			current: {
				get() { 
				  return sheep.$store('sys').messageTabIndex;
				},
			},
		},
		methods: {
			initNav(e) {
				this.height = e.height;
				this.paddingTop = this.height;
			},
			// 监听虚拟列表数组改变并赋值给virtualList进行重新渲染
			virtualListChange(vList) {
				this.virtualList = vList;
			},
			queryList(pageNo, pageSize) {
				if(this.tabType == 'peiwan'){
					this.$refs.paging.complete(true);
					return;
				}
				// 组件加载时会自动触发此方法，因此默认页面加载时会自动触发，无需手动调用
				// 这里的pageNo和pageSize会自动计算好，直接传给服务器即可
				// 模拟请求服务器获取分页数据，请替换成自己的网络请求
				const params = {
					pageNo: pageNo,
					pageSize: pageSize,
					random: this.tabIndex === 1
				}
				ImConversationApi.getMemberConversationPage(params).then(res => {
					// 将请求的结果数组传递给z-paging
					this.$refs.paging.complete(res.data.list);
				}).catch(res => {
					// 如果请求失败写this.$refs.paging.complete(false);
					// 注意，每次都需要在catch中写这句话很麻烦，z-paging提供了方案可以全局统一处理
					// 在底层的网络请求抛出异常时，写uni.$emit('z-paging-error-emit');即可
					this.$refs.paging.complete(false);
				})
			},
			change(e) {
				sheep.$store('sys').setMessageTabIndex(e);
				this.tabChange();
			},
			tabChange() {
				this.tabList = this.tabList.filter(function(tab) {
				    return tab.enabled;
				}); 
				if(this.tabList[this.current]){
					this.tabType = this.tabList[this.current].type;
				}else{
					sheep.$store('sys').setMessageTabIndex(0);
					this.tabType = this.tabList[0].type;
				}
			}, 
			reload() {
				this.$refs.paging.reload();
			},
			getTradeConfig() {
				TradeConfigApi.getTradeConfig().then(res => {
					this.tabList[0].enabled = res.data.peiwanEnabled;
					this.tabList[1].enabled = res.data.friendEnabled;
					this.tabChange(); 
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		background-color: #fafafa;
		height: calc(100vh);
		padding-bottom: env(safe-area-inset-bottom);
	}
	

</style>