<template>
    <view><trtc-room id="trtc-room" :config="trtcConfig"></trtc-room></view>
</template>

<script>
import dayjs from 'dayjs';
export default {
    data() {
        return {
            trtcConfig: {
                sdkAppID: this.tencent.trtc.appid,
                userID: null, // 用户唯一标识符
                userSig: null,
                template: 'grid' // 画面排版模式
            },
            roomId: null,
            expectEnd: null
        };
    },
    methods: {},
    onLoad: function(options) {
        let that=this
		that.roomId = options.roomId;
		that.trtcConfig.userID = options.userId;
		that.trtcConfig.userSig = options.userSig;
		that.expectEnd = options.expectEnd;
		
		let trtcRoomContext = that.selectComponent('#trtc-room');
		let timer=setInterval(function(){
			let now=new Date().getTime()
			let expectEnd = dayjs(that.expectEnd).valueOf();
			if(now>=expectEnd){
				that.trtcRoomContext.exitRoom()
				.then(() => {
				    console.log('退出问诊');
				})
				.catch(resp => {
				    console.error('退出问诊失败:', resp);
				});
				uni.showToast({
				    icon: 'error',
				    title: '问诊结束'
				});
				clearInterval(timer)
			}
		},1000)
		
		uni.setKeepScreenOn({
			keepScreenOn:true
		})
		
		let EVENT = trtcRoomContext.EVENT;
		if (trtcRoomContext) {
		    //监听是否进入TRTC的ROOM
		    trtcRoomContext.on(EVENT.LOCAL_JOIN, event => {
		        // 把本地音视频流向远端推送
		        trtcRoomContext.publishLocalVideo();
		        trtcRoomContext.publishLocalAudio();
		    });
		    // 监听远端推送来视频流
		    trtcRoomContext.on(EVENT.REMOTE_VIDEO_ADD, event => {
		        let userID = event.data.userID;
		        let streamType = event.data.streamType;
		        //订阅远端的视频流
		        trtcRoomContext.subscribeRemoteVideo({ userID: userID, streamType: streamType });
		    });
		    // 监听远端推送来音频流
		    trtcRoomContext.on(EVENT.REMOTE_AUDIO_ADD, event => {
		        let userID = event.data.userID;
		        //订阅远端的音频流
		        trtcRoomContext.subscribeRemoteAudio({ userID: userID });
		    });
		}
		that.trtcRoomContext = trtcRoomContext;
    },
    onShow: function() {
        let that=this
		uni.setKeepScreenOn({
			keepScreenOn:true
		})
		that.$nextTick(function() {
		    that.trtcRoomContext.enterRoom({ roomID: that.roomId })
		    .then(() => {
		        console.log('成功进入问诊室');
		    })
		    .catch(res => {
		        console.error('进入问诊室失败:', res);
		    });
		});
    }
};
</script>

<style lang="less">
@import url('video_diagnose.less');
</style>
