File size: 8,142 Bytes
083ec07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
class MDTApplication {
    constructor() {
        this.currentPage = 'home';
        this.serverUrl = ''; // To be configured by server
        this.eventHandlers = new Map();
        this.initializeEventListeners();
    }

    init() {
        console.log('MDT Application Initialized');
        this.updateDateTime();
        setInterval(() => this.updateDateTime(), 60000); // Update time every minute
        
        // Simulate server connection (in real implementation, this would be WebSocket)
        this.simulateServerConnection();
    }

    updateDateTime() {
        const now = new Date();
        const dateStr = now.toLocaleDateString('en-US', { 
            weekday: 'long', 
            year: 'numeric', 
            month: 'long', 
            day: 'numeric' 
        });
        const timeStr = now.toLocaleTimeString('en-US', { 
            hour: '2-digit', 
            minute: '2-digit' 
        });

        const timeElement = document.getElementById('current-time');
        const dateElement = document.getElementById('current-date');

        if (timeElement) timeElement.textContent = timeStr;
        if (dateElement) dateElement.textContent = dateStr;
    }

    simulateServerConnection() {
        // This is a placeholder for actual server connection
        setTimeout(() => {
            this.showToast('Connected to LSPD Server', 'success');
        }, 1000);
    }

    // Navigation
    navigateTo(page) {
        this.currentPage = page;
        this.updateActiveNavigation();
        this.loadPageContent(page);
    }

    updateActiveNavigation() {
        const sidebar = document.querySelector('mdt-sidebar');
        if (sidebar) {
            sidebar.setAttribute('active-page', this.currentPage);
        }
    }

    loadPageContent(page) {
        const contentElement = document.getElementById('page-content');
        if (!contentElement) return;

        // In a real implementation, this would fetch HTML/component for the page
        // For now, we'll update the page title
        const pageTitles = {
            'home': 'Dashboard',
            'citizens': 'Citizen Files',
            'vehicles': 'Vehicle Files',
            'announcements': 'Announcements',
            'police-list': 'Police List',
            'radio-codes': 'Radio Codes',
            'dispatch': 'Dispatch & Map'
        };

        contentElement.innerHTML = `
            <div class="animate-fade-in">
                <header class="mb-8">
                    <h1 class="text-3xl font-bold text-white">${pageTitles[page] || page}</h1>
                    <p class="text-gray-400">Loading ${pageTitles[page] || page} data from server...</p>
                </header>
                <div class="glass-panel rounded-xl p-8 text-center">
                    <i data-feather="database" class="w-16 h-16 mx-auto mb-4 text-gray-600"></i>
                    <p class="text-gray-400 mb-2">Waiting for server response</p>
                    <p class="text-sm text-gray-500">All data is dynamically loaded from the FiveM server</p>
                    <div class="mt-6 inline-flex items-center gap-2 text-sm text-blue-400">
                        <i data-feather="refresh-cw" class="w-4 h-4 animate-spin"></i>
                        <span>Establishing secure connection...</span>
                    </div>
                </div>
            </div>
        `;
        feather.replace();
    }

    // Toast Notifications
    showToast(message, type = 'info', duration = 5000) {
        const container = document.getElementById('toast-container');
        if (!container) return;

        const toast = document.createElement('div');
        toast.className = `toast toast-${type} glass-panel rounded-lg p-4 flex items-start gap-3`;
        
        const icons = {
            'success': 'check-circle',
            'error': 'alert-circle',
            'warning': 'alert-triangle',
            'info': 'info'
        };

        toast.innerHTML = `
            <i data-feather="${icons[type]}" class="w-5 h-5 mt-0.5 flex-shrink-0"></i>
            <div class="flex-1">
                <p class="text-sm">${message}</p>
            </div>
            <button onclick="this.parentElement.remove()" class="text-gray-400 hover:text-white">
                <i data-feather="x" class="w-4 h-4"></i>
            </button>
        `;

        container.appendChild(toast);
        feather.replace();

        // Auto remove
        if (duration > 0) {
            setTimeout(() => {
                if (toast.parentElement === container) {
                    toast.remove();
                }
            }, duration);
        }
    }

    // Server Request Simulation
    async makeServerRequest(endpoint, data = {}) {
        // In a real implementation, this would make an HTTP/WebSocket request
        this.showToast(`Request sent to ${endpoint}`, 'info');
        
        // Simulate network delay
        return new Promise(resolve => {
            setTimeout(() => {
                resolve({ success: true, message: 'Server response simulated' });
            }, 1000);
        });
    }

    // Initialize Event Listeners
    initializeEventListeners() {
        // Search functionality
        document.addEventListener('keydown', (e) => {
            if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
                e.preventDefault();
                const searchInput = document.querySelector('mdt-topbar')?.shadowRoot?.querySelector('input[type="search"]');
                if (searchInput) {
                    searchInput.focus();
                }
            }
        });

        // Handle status changes
        document.addEventListener('status-change', (e) => {
            const { status } = e.detail;
            this.showToast(`Status updated to: ${status}`, 'success');
            this.makeServerRequest('/api/status/update', { status });
        });
    }

    // Citizen Search
    searchCitizens(criteria) {
        this.showToast('Searching citizens...', 'info');
        return this.makeServerRequest('/api/citizens/search', criteria);
    }

    // Update Officer Status
    updateOfficerStatus(officerId, status) {
        return this.makeServerRequest('/api/officers/status', { officerId, status });
    }

    // Dispatch Actions
    createDispatchCall(data) {
        return this.makeServerRequest('/api/dispatch/create', data);
    }

    // Audit Log
    logAuditEvent(action, details) {
        return this.makeServerRequest('/api/audit/log', { action, details });
    }
}

// Initialize global MDT application
window.mdtApp = new MDTApplication();

// Utility Functions
window.MDTUtils = {
    formatDate: (dateString) => {
        const date = new Date(dateString);
        return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
    },

    formatCurrency: (amount) => {
        return new Intl.NumberFormat('en-US', {
            style: 'currency',
            currency: 'USD'
        }).format(amount);
    },

    formatDuration: (minutes) => {
        if (minutes < 60) return `${minutes} min`;
        const hours = Math.floor(minutes / 60);
        const mins = minutes % 60;
        return `${hours}h ${mins}m`;
    },

    validateInput: (input, type) => {
        const validators = {
            'phone': /^[\+]?[1-9][\d]{0,15}$/,
            'id': /^[A-Z0-9]{6,12}$/,
            'name': /^[A-Za-z\s]{2,50}$/
        };
        return validators[type]?.test(input) || false;
    },

    debounce: (func, wait) => {
        let timeout;
        return function executedFunction(...args) {
            const later = () => {
                clearTimeout(timeout);
                func(...args);
            };
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
        };
    }
};

// Initialize Feather Icons on dynamic content
document.addEventListener('DOMContentLoaded', () => {
    // Observe DOM changes for dynamic icon replacement
    const observer = new MutationObserver(() => {
        feather.replace();
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
});