// main.js - Custom JavaScript for SI-INAL Asset IT System

document.addEventListener('DOMContentLoaded', function() {
    console.log('main.js loaded successfully!');


});

function copyToClipboard(elementId) {
    var copyText = document.getElementById(elementId).href;
    navigator.clipboard.writeText(copyText).then(function() {
        alert("Link berhasil disalin!");
    }, function(err) {
        console.error('Tidak dapat menyalin teks: ', err);
    });
}

// --- Notification Bell Logic ---
function fetchUnreadHelpdeskCount() {
    fetch('?page=get_unread_helpdesk_count')
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            return response.json();
        })
        .then(data => {
            if (data.error) {
                console.error('Notification API Error:', data.error);
                return;
            }
            const count = data.unread_count;
            const notificationCountBadge = document.getElementById('notification-count');

            if (notificationCountBadge) {
                if (count > 0) {
                    notificationCountBadge.textContent = count > 9 ? '9+' : count;
                    notificationCountBadge.style.display = 'block';
                } else {
                    notificationCountBadge.style.display = 'none';
                }
            }
        })
        .catch(error => console.error('Error fetching notification count:', error));
}

document.addEventListener('DOMContentLoaded', function() {
    // Only start polling if user is logged in (we can check by seeing if the notification bell exists)
    if (document.getElementById('notification-bell')) {
        fetchUnreadHelpdeskCount(); // Fetch count immediately on page load
        setInterval(fetchUnreadHelpdeskCount, 30000); // Then fetch every 30 seconds (30000 ms)
    }
});
