File size: 3,750 Bytes
58c1398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
 * Admin Panel Core Logic - Routing and Global Functions
 */

/**
 * Custom Toast Notification
 */
function showToast(type, title, message) {
    const container = document.getElementById('toast-container');
    if (!container) return;

    const toast = document.createElement('div');
    toast.className = `toast-message ${type}`;

    let iconClass = 'fa-solid fa-circle-info';
    if (type === 'success') iconClass = 'fa-solid fa-circle-check';
    if (type === 'error') iconClass = 'fa-solid fa-circle-exclamation';

    toast.innerHTML = `
        <div class="toast-icon"><i class="${iconClass}"></i></div>
        <div class="toast-content">
            <h4>${title}</h4>
            <p>${message}</p>
        </div>
    `;

    container.appendChild(toast);

    // Remove toast after 5 seconds
    setTimeout(() => {
        toast.classList.add('fade-out');
        setTimeout(() => toast.remove(), 400); // Wait for animation
    }, 5000);
}

document.addEventListener('DOMContentLoaded', () => {
    const urlParams = new URLSearchParams(window.location.search);
    const tab = urlParams.get('tab') || 'dashboard';
    switchTab(tab);
    
    // Periodically check for open tickets and update stats
    setInterval(() => {
        if(typeof updateTicketBadges === 'function') updateTicketBadges();
        if (document.getElementById('section-dashboard').classList.contains('active') && typeof loadDashboardStats === 'function') {
            loadDashboardStats();
        }
    }, 30000);
    
    if(typeof updateTicketBadges === 'function') updateTicketBadges();
});

function switchTab(tabName) {
    const sections = document.querySelectorAll('.admin-section');
    const tabBtns = document.querySelectorAll('.admin-tab-btn');
    const sidebarLinks = document.querySelectorAll('.sidebar-menu .side-btn');
    const breadcrumb = document.getElementById('breadcrumb-active');
    
    sections.forEach(s => s.classList.remove('active'));
    tabBtns.forEach(b => b.classList.remove('active'));
    sidebarLinks.forEach(l => l.classList.remove('active'));
    
    const activeSection = document.getElementById(`section-${tabName}`);
    const activeTabBtn = document.getElementById(`tab-btn-${tabName}`);
    const activeSidebarLink = document.getElementById(`nav-${tabName}-link`);

    if (activeSection) activeSection.classList.add('active');
    if (activeTabBtn) activeTabBtn.classList.add('active');
    if (activeSidebarLink) activeSidebarLink.classList.add('active');
    
    if (tabName === 'dashboard') {
        if (breadcrumb) breadcrumb.innerText = 'Overview';
        if(typeof loadDashboardStats === 'function') loadDashboardStats();
    } else if (tabName === 'payments') {
        if (breadcrumb) breadcrumb.innerText = 'Payment Queue';
        if(typeof loadTransactions === 'function') loadTransactions();
    } else if (tabName === 'wallet') {
        if (breadcrumb) breadcrumb.innerText = 'Wallet Management';
        if(typeof loadUserWallets === 'function') loadUserWallets();
    } else if (tabName === 'users') {
        if (breadcrumb) breadcrumb.innerText = 'User Directory';
        if(typeof loadUserManagement === 'function') loadUserManagement();
    } else if (tabName === 'downloads') {
        if (breadcrumb) breadcrumb.innerText = 'Download History';
        if(typeof loadDownloadHistory === 'function') loadDownloadHistory();
    } else {
        if (breadcrumb) breadcrumb.innerText = 'Complaints';
        if(typeof loadTickets === 'function') loadTickets();
    }

    // Update URL without reload
    const newUrl = window.location.pathname + '?tab=' + tabName;
    window.history.pushState({ path: newUrl }, '', newUrl);
}

window.switchTab = switchTab;
window.showToast = showToast;