/**
 * 结束通话界面逻辑
 * 处理通话结束后的操作，如重新连接或返回连接界面
 */

import { showNotification } from '../shared/utils.js';

// 重新连接
function reconnectCall() {
    showNotification('正在重新连接...');

    // 跳转到通话界面
    window.location.href = '/';
}

// 离开
function leaveCall() {
    // 清除本地存储中的连接ID
    localStorage.removeItem('connectionId');

    // 跳转到连接界面
    window.location.href = '/connect/';
}

// 绑定事件监听器
function bindEvents() {
    // 重新连接按钮
    const reconnectBtn = document.getElementById('reconnectBtn');
    if (reconnectBtn) {
        reconnectBtn.addEventListener('click', reconnectCall);
    }

    // 离开按钮
    const leaveBtn = document.getElementById('leaveBtn');
    if (leaveBtn) {
        leaveBtn.addEventListener('click', leaveCall);
    }
}

// 页面加载完成后初始化
window.addEventListener('DOMContentLoaded', () => {
    bindEvents();

    // 更新断开连接信息
    const disconnectConnectionId = document.getElementById('disconnectConnectionId');
    const disconnectTime = document.getElementById('disconnectTime');

    if (disconnectConnectionId) {
        disconnectConnectionId.textContent = localStorage.getItem('connectionId') || '--';
    }

    if (disconnectTime) {
        disconnectTime.textContent = new Date().toLocaleString();
    }
});

// 导出全局函数
window.reconnectCall = reconnectCall;
window.leaveCall = leaveCall;
