let rooms = {};
function openModal(roomName) {
    currentRoom = roomName;
    document.getElementById('passwordModal').style.display = 'block';
}
function closeModal() {
    document.getElementById('passwordModal').style.display = 'none';
}
function validatePassword() {
    const enteredPassword = document.getElementById('roomPassword').value;
    if (enteredPassword === rooms[currentRoom]) {
        alert('Вы вошли в комнату: ' + currentRoom);
        closeModal();
    } else {
        alert('Неверный пароль. Попробуйте снова.');
    }
}
function openCreateRoomModal() {
    document.getElementById('createRoomModal').style.display = 'block';
}
function closeCreateRoomModal() {
    document.getElementById('createRoomModal').style.display = 'none';
}
function createRoom() {
    const roomName = document.getElementById('newRoomName').value.trim();
    const roomPassword = document.getElementById('newRoomPassword').value.trim();
    
    if (roomName === '' || roomPassword === '') {
        alert('Пожалуйста, заполните все поля.');
        return;
    }
    if (rooms[roomName]) {
        alert('Комната с таким названием уже существует.');
        return;
    }
    rooms[roomName] = roomPassword;
    addRoomToList(roomName);
    closeCreateRoomModal();
}
function addRoomToList(roomName) {
    const roomList = document.querySelector('.room-list');
    
    const existingRoomItem = Array.from(roomList.children).find(item => item.querySelector('.room-name').textContent === roomName);
    if (existingRoomItem) {
        existingRoomItem.remove();
    }
    const roomItem = document.createElement('li');
    roomItem.classList.add('room-item');
    roomItem.innerHTML = `
        ${roomName}
        
    `;
    roomList.appendChild(roomItem);
}
function initializeRoomList() {
    Object.keys(rooms).forEach(roomName => {
        addRoomToList(roomName);
    });
}
initializeRoomList();
window.onclick = function(event) {
    if (event.target === document.getElementById('passwordModal')) {
        closeModal();
    }
    if (event.target === document.getElementById('createRoomModal')) {
        closeCreateRoomModal();
    }
}