<template>
	<view class="page">
		<view class="page-header" :style="{ paddingTop: geStatusBarHeight() + 'px'}">
			<view class="hcon">
				<view class="leftarr">
					<uni-icons type="back" size="24" color="#fff"  @tap="gotoBack"></uni-icons>
				</view>
				<view class="text-center">通讯录</view>
				<!-- <uni-icons type="personadd" size="30" color="#ffffff" @click="$u.throttle(addContacts(), 2000)"></uni-icons> -->
			</view>
		    <view class="concon">
				<!-- coach -->
		    	<view v-if="ifxy" class="concell" @click="tabSelect(0)">
		    		<image class="cimg" src="/static/image/jl.png"></image>
		    		<view class="ctxt" :class="list[tabCurrentIndex].id=='coach'?'cur1':''">教练</view>
		    	</view>
				<!-- customerService -->
				<view v-if="ifxy" class="concell" @click="tabSelect(1)">
					<image class="cimg" src="/static/image/kff.png"></image>
					<view class="ctxt" :class="list[tabCurrentIndex].id=='customerService'?'cur2':''">客服</view>
				</view>
				<!-- adviser -->
				<view v-if="ifxy" class="concell" @click="tabSelect(2)">
					<image class="cimg" src="/static/image/gw.png"></image>
					<view class="ctxt" :class="list[tabCurrentIndex].id=='adviser'?'cur3':''">课程顾问</view>
				</view>
				<!-- storeManager -->
				<view v-if="ifxy" class="concell" @click="tabSelect(3)">
					<image class="cimg" src="/static/image/dzh.png"></image>
					<view class="ctxt" :class="list[tabCurrentIndex].id=='storeManager'?'cur4':''">店长</view>
				</view>
				<!-- consumer -->
				<view v-if="!ifxy" class="concell" @click="tabSelect(5)">
					<image class="cimg" src="/static/image/xy.png"></image>
					<view class="ctxt" :class="list[tabCurrentIndex].id=='consumer'?'cur6':''">学员</view>
				</view>
				<!-- groupchat -->
		    	<view class="concell" @click="tabSelect(4)">
		    		<image class="cimg" src="/static/image/ql.png"></image>
		    		<view class="ctxt" :class="list[tabCurrentIndex].id=='groupchat'?'cur5':''" >群聊</view>
		    	</view>
		    </view>
		</view>

		<view class="wrapcon">
			<!-- 列表 -->
			<view v-if="list[tabCurrentIndex].grouplist.length==0 && list[tabCurrentIndex].loadStatus=='nomore'" class="nodata">
				暂无数据..
			</view>
			<view class="listcell">
				<view class="lcon" v-for="(info, index) in list[tabCurrentIndex].grouplist" :key="index" @click="gotoGroup(info)">
					<view class="limg">
						<image class="img" :src="info.headImage?info.headImage:(tabCurrentIndex==0?'/static/image/kff.png':(tabCurrentIndex==1?'/static/image/gw.png':(tabCurrentIndex==2?'/static/image/gw.png':(tabCurrentIndex==3?'/static/image/dzh.png':'/static/image/ql.png'))))" mode="aspectFill"></image>
					</view>
					<view class="lright">
						<view class="pname">{{info.nickName}}</view>
					</view>
				</view>
			</view>
			<u-loadmore v-if="list[tabCurrentIndex].grouplist.length==0 && list[tabCurrentIndex].loadStatus=='loading'"
			:status="list[tabCurrentIndex].loadStatus" :marginTop="20"></u-loadmore>
		</view>

		<!-- 提示信息弹窗 -->
		<uni-popup ref="message" type="message">
			<uni-popup-message :type="msgType" :message="messageText" :duration="2000"></uni-popup-message>
		</uni-popup>

	</view>
</template>

<script>
import { mapState, mapActions, mapGetters } from 'vuex'
	import { myCache } from '../../utils/utils.js';
	import dateTime from '@/common/dateTime.js';
	export default {
		data() {
			return {
				userid:"",
				userName:"",
				messageText:'', //错误提示框
				msgType :'error',
				tabCurrentIndex:0,
				imgurl:uni.$http.baseUrl,
				list:[
					{
						id:"coach",
						name:'教练',
						loadStatus:'loadmore',
						grouplist:[]
					},
					{
						id:"customerService",
						name:'客服',
						loadStatus:'loadmore',
						grouplist:[]
					},
					{
						id:"adviser",
						name:'课程顾问',
						loadStatus:'more',
						grouplist:[]
					},
					{
						id:"storeManager",
						name:'店长',
						loadStatus:'more',
						grouplist:[]
					},
					{
						id:"groupchat",
						name:'群聊',
						loadStatus:'loadmore',
						grouplist:[]
					},
					{
						id:"consumer",
						name:'学员',
						loadStatus:'loadmore',
						grouplist:[]
					},
				],
				ifxy:false,
			}
		},
    computed: {
      ...mapGetters('chatStore', ['findChatIdx']),   // 正确
    },
		onLoad(options) {
			var userInfo=myCache('userInfo');
			this.userid = userInfo.userId? userInfo.userId:'';
			this.userName = userInfo.nickName?userInfo.nickName:"";
			var roleId=userInfo&&userInfo.roleId?userInfo.roleId:[];
			if(roleId.length<1){
				this.ifxy=true;
				this.$forceUpdate();
			}
			roleId.forEach(cell=>{
				if(cell=='107'&&roleId.length<2){
					this.ifxy=true;
					this.$forceUpdate();
				}
				else{
					this.ifxy=false;
					this.$forceUpdate();
				}
			});

			if(this.ifxy){
				this.tabCurrentIndex=0;
				this.$forceUpdate();
			}
			else{
				this.tabCurrentIndex=5;
				this.$forceUpdate();
			}

			// 未选择门店，则退出至首页选择门店
			if(myCache('myshopid')){}
			this.loadData();
		},
		onShow(){
			// this.loadData();
			// id:"coach",
			// name:'教练',
			// id:"customerService",
			// name:'客服',
			// id:"adviser",
			// name:'课程顾问',
			// id:"storeManager",
			// name:'店长',
			// id:"groupchat",
			// name:'群聊',
			// id:"consumer",
			// name:'学员',
		},
		onPullDownRefresh() {
			console.log('onPullDownRefresh');
			setTimeout(()=>{
				uni.stopPullDownRefresh()
			},500);
		},
		onReachBottom() {
			console.log('onReachBottom');
			// 暂不分页
			this.loadData();
		},
		methods: {
      ...mapActions('chatStore', ['openChat']),

			// 新增通讯录
			addContacts(){},
			gotoBack(){
				// 返回
				uni.navigateBack({
					delta: 1
				});
			},
			// 获取群信息
			async getGroupInfo(id) {
				const {data: res} = await uni.$http.get('/api/group/find/'+id);
				if(res.data){
					var data = res.data;
					// info.name=data.name;
					// info.img=data.headImage||'/static/image/qltx.png';
					// info.notice=data.notice;
					// info.remarkNickName=data.remarkNickName;
					// info.showNickName=data.showNickName;
					// info.showGroupName=data.showGroupName;
					// info.remarkGroupName=data.remarkGroupName;
					// info.customerService=data.customerService;
					// info.instructor=data.instructor;
					// info.productId=data.productId;
					// info.productName=data.productName;
				}
			},
			// 跳转聊天框

			gotoGroup(item){
				var timestamp = new Date().getTime();
				if(this.tabCurrentIndex==4){
					// 群聊
          let chat = {
            type: 'GROUP',
            targetId: item.id,
            showName: item.name,
            headImage: item.headImage,
          };
          this.openChat(chat);
          let chatIdx = this.findChatIdx(chat);
          uni.navigateTo({ url: `/pages/chatbox/chat-box?chatIdx=${chatIdx}` });
          /**
					this.getGroupInfo(item.id);
					var chatid="groupchat-" + item.id;
					var info={
						chatId: chatid,
						groupId: item.id,
						chatName: item.name,
						chatAvatar: item.headImage?item.headImage:'/static/image/kfr.png',
						chatTime: timestamp,
						friendId: item.customerService,
						teacherId: item.instructor,
						minId: "",
						sort:"groupchat", // privatechat 私聊  groupchat 群聊
						from:"message", // yh 用户咨询进入聊天框，message 从消息进入聊天框
						notice: item.notice,
						remarkNickName: item.remarkNickName,
						remarkGroupName: item.remarkGroupName,
						showNickName: item.showNickName,
						showGroupName: item.showGroupName,
						reason: item.reason,
						customerService: item.customerService,
						instructor: item.instructor,
						ownerId: item.ownerId,
						productId: item.productId,
						productName: item.productName
					}
					var data=encodeURIComponent(JSON.stringify(info));
					uni.navigateTo({
						url: `/pages/chat/groupchat?data=${data}`
					});
          **/
				}
				else{
          let chat = {
            type: 'PRIVATE',
            targetId: item.id,
            showName: item.nickName,
            headImage: item.headImage,
          };
          this.openChat(chat);
          let chatIdx = this.findChatIdx(chat);
          console.log("chatIdx",chatIdx)
          uni.navigateTo({ url: `/pages/chatbox/chat-box?chatIdx=${chatIdx}` });
					// 直接跳转到聊天框
          /**
					var info={
						chatId: "privatechat-" + this.userid +"-"+ item.id,
						chatType: (this.tabCurrentIndex==0?"coach":(this.tabCurrentIndex==1?"customerService":(this.tabCurrentIndex==2?"adviser":
						(this.tabCurrentIndex==3?"storeManager":(this.tabCurrentIndex==4?"groupchat":(this.tabCurrentIndex==5?"consumer":"")))))),
						chatName: item.nickName,
						chatAvatar: item.headImage,
						chatTime: timestamp,
						userid: this.userid,
						friendId: item.id, // 会话对象
						minId: null, // 已读消息的最大id
						sort: this.tabCurrentIndex==4?"groupchat":"privatechat", // privatechat 私聊  groupchat 群聊
						from: "message" // yh 用户咨询进入聊天框，message 从消息进入聊天框
					}
					console.log(info);
					if(this.tabCurrentIndex==0){
						// 教练
						var data=encodeURIComponent(JSON.stringify(info));
						uni.navigateTo({
							url: `/pages/chat/chat?data=${data}`
						});
					}
					else if(this.tabCurrentIndex==1){
						// 客服
						var data=encodeURIComponent(JSON.stringify(info));
						uni.navigateTo({
							url: `/pages/chat/chat?data=${data}`
						});
					}
					else if(this.tabCurrentIndex==2){
						// 顾问
						var data=encodeURIComponent(JSON.stringify(info));
						uni.navigateTo({
							url: `/pages/chat/chat?data=${data}`
						});
					}
					else if(this.tabCurrentIndex==3){
						// 店长
						var data=encodeURIComponent(JSON.stringify(info));
						uni.navigateTo({
							url: `/pages/chat/chat?data=${data}`
						});
					}
					else if(this.tabCurrentIndex==5){
						// 学员
						var data=encodeURIComponent(JSON.stringify(info));
						uni.navigateTo({
							url: `/pages/chat/chat?data=${data}`
						});
					}
           **/
				}

			},
			loadData(){
				// 底部数据加载
				if(this.list[this.tabCurrentIndex].loadStatus=="loadmore") {
					this.list[this.tabCurrentIndex].loadStatus="loading";
					setTimeout(() => {
						this.getgroupsmembers();
					}, 300);
				}
			},
			tabSelect(index) {
				this.tabCurrentIndex = index;
				// 加载
				this.loadData();
			},
			// 获取群聊
			async getgroupsmembers(){
				if(this.list[this.tabCurrentIndex].loadStatus!=="nomore")
				{
					// 教练、客服、顾问、店长列表
					if(this.tabCurrentIndex==0||this.tabCurrentIndex==1||this.tabCurrentIndex==2||this.tabCurrentIndex==3||this.tabCurrentIndex==5){
						const {data: res} = await uni.$http.get('/api/friend/list');
						this.list[0].loadStatus="nomore";
						this.list[0].grouplist=[];
						this.list[1].loadStatus="nomore";
						this.list[1].grouplist=[];
						this.list[2].loadStatus="nomore";
						this.list[2].grouplist=[];
						this.list[3].loadStatus="nomore";
						this.list[3].grouplist=[];
						this.list[5].loadStatus="nomore";
						this.list[5].grouplist=[];
						// 教练列表
						if(res.data&&res.data.coach&&res.data.coach.length>0){
							this.list[0].grouplist=res.data.coach;
							this.$forceUpdate();
						}
						// 客服列表
						if(res.data&&res.data.customerService&&res.data.customerService.length>0){
							this.list[1].grouplist=res.data.customerService;
							this.$forceUpdate();
						}
						// 顾问列表
						if(res.data&&res.data.adviser&&res.data.adviser.length>0){
							this.list[2].grouplist=res.data.adviser;
							this.$forceUpdate();
						}
						// 店长列表
						if(res.data&&res.data.storeManager&&res.data.storeManager.length>0){
							this.list[3].grouplist=res.data.storeManager;
							this.$forceUpdate();
						}

						// 学员列表
						if(res.data&&res.data.consumer&&res.data.consumer.length>0){
							this.list[5].grouplist=res.data.consumer;
							this.$forceUpdate();
						}
					}

					// 群聊
					if(this.tabCurrentIndex==4){
						const {data: res} = await uni.$http.get('/api/group/list');
						this.list[this.tabCurrentIndex].loadStatus="nomore";
						this.list[this.tabCurrentIndex].grouplist=[];
						if(res.data&&res.data.length>0){
							res.data.forEach(cell => {
								this.list[this.tabCurrentIndex].grouplist.push({...cell,
									nickName: cell.name,
									headImage: cell.headImage?cell.headImage:'/static/image/ql.png'
								})
							});
							this.$forceUpdate();
						}
					}
				}
			},
			// 获取状态栏高度
			geStatusBarHeight(){
			    return uni.getSystemInfoSync()['statusBarHeight'];
			},
			// 获取导航栏高度
			getNavBarHeight(){
				return 45+uni.getSystemInfoSync()['statusBarHeight'];
			},

		}
	}
</script>

<style lang="scss" scoped>
	.page{
		padding: 0;
		position: relative;
		background-image: url('@/static/image/bg.jpg');
		background-attachment: fixed;
		background-size: cover;
		background-position: center center;
		min-height: calc(100vh - var(--window-top) - var(--window-bottom));
	}
	.page-header {
		position: fixed;
		width: 100%;
		z-index: 999;
		background-color: #00a89b;

	    .edit {
	        position: absolute;
	        top: 50%;
	        transform: translateY(-50%);
	        right: 30rpx;
	        font-size: 32rpx;
	    }
	}
	.hcon{
		width: 100%;
		background-color: #00a89b;
		margin: 0;
		padding: 0 30rpx;
		height: 100rpx;
		display: flex;
		flex-direction: row;
		color: #fff;
		font-size: 36rpx;
		align-items: center;
		.leftarr{
			background-color: rgba(255, 255,255, 0.3);
			border-radius: 10rpx;
			width: 60rpx;
			height: 60rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
		.text-center {
			display: flex;
			flex: 1;
		    width: 100%;
		    align-items: center;
			justify-content: center;
		}
	}
	.concon{
		display: flex;
		flex-direction: row;
		background: #fff;
		padding: 20rpx 0;
		margin:  0;
		height: 190rpx;
		align-items: center;
		justify-content: left;
		.concell{
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			width: 20%;
			.cimg{
				width: 90rpx;
				height: 90rpx;
			}
			.ctxt{
				font-size: 28rpx;
				color: #333;
				margin-top: 16rpx;
			}
			.cur1{
				color:#ff7530;
				font-size: 32rpx;
				font-weight: 600;
			}
			.cur2{
				color:#46a0fb;
				font-size: 32rpx;
				font-weight: 600;
			}
			.cur3{
				color:#5bd171;
				font-size: 32rpx;
				font-weight: 600;
			}
			.cur5{
				color:#d4237a;
				font-size: 32rpx;
				font-weight: 600;
			}
			.cur6{
				color:#00a89b;
				font-size: 32rpx;
				font-weight: 600;
			}
			.cur4{
				color:#fca600;
				font-size: 32rpx;
				font-weight: 600;
			}
		}
	}
	.wrapcon{
		margin: 370rpx 0 0 0;
		/* #ifdef H5 */
		margin: 300rpx 0 0 0;
		/* #endif */
		padding: 0;
		width: 100%;
		position: relative;
		min-height: calc(100vh - 350rpx);
		/* #ifdef H5 */
		min-height: calc(100vh - var(--window-top) - var(--window-bottom) - 350rpx);
		/* #endif */
		overflow-y: auto;
	}
	.listcell{
		margin: 0;
		.lcon{
			background: #fff;
			margin-bottom: 20rpx;
			padding-top: 20rpx;
			padding-bottom: 20rpx;
			opacity: 1;
			display: flex;
			flex-direction: row;
			align-items: center;
			.limg{
				width: 120rpx;
				height: 120rpx;
				margin-right: 24rpx;
				margin-left: 24rpx;
				display: flex;
				flex-direction: row;
				flex-wrap: wrap;
				align-items: center;
				justify-content: center;
				border-radius: 50%;
				.img{
					width: 80rpx;
					height: 80rpx;
					border-radius: 10rpx;
				}
			}
			.lright{
				display: flex;
				flex: 1;
				flex-direction: column;
			}
			.lrow{
				display: flex;
				flex-direction: row;
				margin-bottom: 20rpx;
				margin-right: 20rpx;
			}
			.pname{
				display: flex;
				flex: 1;
				line-height: 50rpx;
				font-size: 32rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 600;
				color: #000000;
			}
			.ptime{
				line-height: 50rpx;
				font-size: 24rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #595959;
			}
			.pnr{
				display: flex;
				flex: 1;
				font-size: 24rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color: #595959;
			}
			.lnum{
				background-color: #de0000;
				padding: 0 10rpx;
				min-width: 32rpx;
				height: 32rpx;
				line-height: 28rpx;
				color: #fff;
				font-size: 24rpx;
				border-radius: 28rpx;
			}
		}
	}
	.tips{
		margin-top: 20rpx;
		margin-bottom: 20rpx;
		width: 100%;
		line-height: 44rpx;
		font-size: 26rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #595959;
		text-align: center;
	}
	.submitcon{
		margin-top: 200rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.bbtn{
		margin: 20rpx 10rpx 54rpx;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FCFCFD;
		width: 560rpx;
		height: 76rpx;
		line-height: 76rpx;
		background: #00a89b;
		box-shadow: 0rpx 12rpx 64rpx 2rpx rgba(137,150,95,0.4);
		border-radius: 10rpx;
		opacity: 1;
		&::after{
			border:none;
		}
	}
	.nodata{
		background-image: url('../../static/image/nomsg.png');
		background-repeat: no-repeat;
		background-position: center center;
		background-size: 100% auto;
		height: 220rpx;
		width: 168rpx;
		margin: 100rpx auto;
		padding-top: 180rpx;
		text-align: center;
		font-size: 28rpx;
		color: #00a89b;
	}
</style>
