<template>
	<view class="home-content">
		<Index v-if="current===0" key="home-index" ref='homeIndex' />
		<middle v-if="current===1" key="home-middle" />
		<my-center v-if="current===2" key="home-center" />
		<tab-bar :list='tabBars' :current="current" @change='changeTab' ref="tabBar" key="home-tabBars"   />
	</view>
</template>
<script>	
	import Index from '@/pages/index/index';
	import middle from '@/views/record/index.vue';
	import myCenter from "@/pages/myCenter/index";
	
	import TabBar from '@/components/TabBar/index';
	import tabBars from '@/config/tabBar';
	import config from '@/config';
	import {MyWebSokcet} from '@/libs/websocket';
	const socketBaseUrl=config.socketUrl;
	
	import {ReceivedOrder} from '@/api/doctor';
	
	export default {
		components:{
			Index,
			middle,
			myCenter,
			TabBar
		},		
		data() {
			return {
				userType:null,
				tabBars:[],
				current:0
			}
		},
		created() {
			this.$nextTick(()=>{
				this.init();
			})
		},
		onLoad({type}) {
			if(type==='consult'){
				this.current=1;
				uni.setNavigationBarTitle({
					title:'记录'
				})
				this.$forceUpdate();
			}
		},
		methods: {
			init(){			
				let accountInfo=uni.getStorageSync('accountInfo');
				this.userType=accountInfo.userType;
				if(accountInfo.userType===1){//医生
					let tabBarList=tabBars.filter((item)=>{
						return item.permissions.indexOf("doctor")>-1;
					})
					this.tabBars=tabBarList;
					this.initSocket();
				}else{
					let tabBarList=tabBars.filter((item)=>{
						return item.permissions.indexOf("patient")>-1;
					})				
					this.tabBars=tabBarList;
				}
			},
			changeTab(index){
				this.current=index;
			},
			initSocket(){//启动接单监听
					let accountInfo=uni.getStorageSync('accountInfo');
					let doctorId=accountInfo.id;
					let socketUrl=`${socketBaseUrl}/${doctorId}`;
					MyWebSokcet.sokcet(
						socketUrl,
						this.onReceive, 
						this.onErrorEvent,
						this.onErrorSucceed					
					)						
			},
			onReceive(receivedata){
				let self=this;
                if(receivedata.indexOf('seekId')<0){
                    return ;
                }
                var info=JSON.parse(receivedata);
                let patientName=info.patientName;
                let seekId=info.seekId;
				console.log({
					seekId,patientName,msg:'接单'
				})
				uni.showModal({
					title: '提示',
					content:`你收到了${patientName}的一条咨询，是否接受?`,
					success: function (res) {
						if (res.confirm) {
								let accountInfo=uni.getStorageSync('accountInfo');
								let doctorId=accountInfo.id;
								ReceivedOrder(seekId,doctorId).then((res)=>{
									if(res.code===0){
										uni.showToast({
											title:'已接单',
											complete() {
												if(self.current===0){
													self.$refs.homeIndex.reload('doctor');												
												}											
											}
										})	
									}
								})
						} else if (res.cancel) {
							uni.showToast({
								title:'已放弃'
							})
						}
					}
				});
			},
			onErrorEvent(res){
				console.log('onErrorEvent------------'+JSON.stringify(res))
			},
			onErrorSucceed(res){
				console.log('onErrorSucceed-------------'+JSON.stringify(res))
			}
		}
	}
</script>

<style lang="scss" scoped>
	.home-content{
		padding-bottom: 120upx;
	}
</style>
