File size: 3,562 Bytes
feb6f9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
// Toggle mobile menu
function toggleMobileMenu() {
    const sidebar = document.querySelector('.sidebar-container');
    sidebar.classList.toggle('active');
}

// Initialize when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
    // Add any global initialization code here
    
    // Example: Set up mobile menu toggle if it exists
    const menuToggle = document.getElementById('menu-toggle');
    if (menuToggle) {
        menuToggle.addEventListener('click', toggleMobileMenu);
    }
    
    // Close sidebar when clicking outside on mobile
    document.addEventListener('click', function(event) {
        const sidebar = document.querySelector('.sidebar-container');
        const menuButton = document.getElementById('menu-toggle');
        
        if (sidebar && 
            !sidebar.contains(event.target) && 
            menuButton && 
            !menuButton.contains(event.target) && 
            window.innerWidth < 768) {
            sidebar.classList.remove('active');
        }
    });
});

// Simple chart drawing function (placeholder)
function drawChart(canvasId, data) {
    const canvas = document.getElementById(canvasId);
    if (!canvas) return;
    
    const ctx = canvas.getContext('2d');
    const width = canvas.width;
    const height = canvas.height;
    
    // Clear canvas
    ctx.clearRect(0, 0, width, height);
    
    // Draw simple bar chart
    const barWidth = width / data.length - 10;
    const maxValue = Math.max(...data);
    
    data.forEach((value, index) => {
        const barHeight = (value / maxValue) * (height - 20);
        const x = index * (barWidth + 10) + 10;
        const y = height - barHeight - 10;
        
        ctx.fillStyle = '#3b82f6';
        ctx.fillRect(x, y, barWidth, barHeight);
        
        ctx.fillStyle = '#1e293b';
        ctx.font = '12px Inter, sans-serif';
        ctx.textAlign = 'center';
        ctx.fillText(value, x + barWidth/2, y - 5);
    });
}

// Notification system
function showNotification(message, type = 'info') {
    const notificationContainer = document.createElement('div');
    notificationContainer.className = `fixed top-4 right-4 p-4 rounded-lg shadow-lg text-white z-50 fade-in ${
        type === 'success' ? 'bg-green-500' : 
        type === 'error' ? 'bg-red-500' : 
        type === 'warning' ? 'bg-yellow-500' : 
        'bg-blue-500'
    }`;
    notificationContainer.textContent = message;
    
    document.body.appendChild(notificationContainer);
    
    setTimeout(() => {
        notificationContainer.remove();
    }, 3000);
}

// Form validation helper
function validateForm(formId, rules) {
    const form = document.getElementById(formId);
    if (!form) return false;
    
    let isValid = true;
    
    Object.keys(rules).forEach(fieldName => {
        const field = form.querySelector(`[name="${fieldName}"]`);
        const rule = rules[fieldName];
        
        if (field && rule.required && !field.value.trim()) {
            isValid = false;
            field.classList.add('border-red-500');
            const errorElement = document.createElement('p');
            errorElement.className = 'text-red-500 text-sm mt-1';
            errorElement.textContent = rule.message || 'This field is required';
            field.parentNode.appendChild(errorElement);
        }
    });
    
    return isValid;
}

// Export functions for global use
window.toggleMobileMenu = toggleMobileMenu;
window.drawChart = drawChart;
window.showNotification = showNotification;
window.validateForm = validateForm;