File size: 9,719 Bytes
12e06d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6227b63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12e06d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6227b63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12e06d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6227b63
12e06d8
 
 
 
6227b63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

// Handle mobile menu toggle
document.addEventListener('DOMContentLoaded', function() {
    const mobileMenuButton = document.querySelector('.user-menu button');
    if (mobileMenuButton) {
        mobileMenuButton.addEventListener('click', function() {
            const navLinks = document.querySelector('.nav-links');
            navLinks.classList.toggle('hidden');
        });
    }

    // Handle user dropdown
    const userAvatar = document.querySelector('.user-avatar');
    if (userAvatar) {
        userAvatar.addEventListener('click', function() {
            const dropdown = this.nextElementSibling;
            dropdown.classList.toggle('hidden');
        });
    }

    // Close dropdowns when clicking outside
    document.addEventListener('click', function(event) {
        if (!event.target.closest('.user-menu')) {
            const dropdowns = document.querySelectorAll('.user-menu .dropdown-menu');
            dropdowns.forEach(dropdown => {
                if (!dropdown.classList.contains('hidden')) {
                    dropdown.classList.add('hidden');
                }
            });
        }
    });

    // Initialize tooltips
document.addEventListener('DOMContentLoaded', function() {
    // Initialize any tooltips if needed
    const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
    tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl);
    });

    // Handle modal toggles
    const modalTriggers = document.querySelectorAll('[data-modal-toggle]');
    modalTriggers.forEach(trigger => {
        trigger.addEventListener('click', function() {
            const modalId = this.getAttribute('data-modal-toggle');
            const modal = document.getElementById(modalId);
            modal.classList.toggle('hidden');
        });
    });

    // Close modals when clicking outside
    const modals = document.querySelectorAll('.modal');
    modals.forEach(modal => {
        modal.addEventListener('click', function(e) {
            if (e.target === this) {
                this.classList.add('hidden');
            }
        });
    });

    // Close modals with close button
    const closeButtons = document.querySelectorAll('[data-modal-hide]');
    closeButtons.forEach(button => {
        button.addEventListener('click', function() {
            const modalId = this.getAttribute('data-modal-hide');
            const modal = document.getElementById(modalId);
            modal.classList.add('hidden');
        });
    });

    // Tab functionality
    const tabButtons = document.querySelectorAll('[data-tab-toggle]');
    tabButtons.forEach(button => {
        button.addEventListener('click', function() {
            const tabId = this.getAttribute('data-tab-toggle');
            const tabContent = document.getElementById(tabId);
            
            // Hide all tab contents
            document.querySelectorAll('.tab-content').forEach(content => {
                content.classList.add('hidden');
            });
            
            // Deactivate all tab buttons
            document.querySelectorAll('[data-tab-toggle]').forEach(btn => {
                btn.classList.remove('bg-blue-100', 'text-blue-700');
                btn.classList.add('text-gray-600', 'hover:text-gray-800');
            });
            
            // Show selected tab content
            tabContent.classList.remove('hidden');
            
            // Activate current tab button
            this.classList.remove('text-gray-600', 'hover:text-gray-800');
            this.classList.add('bg-blue-100', 'text-blue-700');
        });
    });

    // Accordion functionality
    const accordionButtons = document.querySelectorAll('[data-accordion-toggle]');
    accordionButtons.forEach(button => {
        button.addEventListener('click', function() {
            const accordionId = this.getAttribute('data-accordion-toggle');
            const accordionContent = document.getElementById(accordionId);
            const icon = this.querySelector('i');
            
            // Toggle content
            accordionContent.classList.toggle('hidden');
            
            // Toggle icon
            if (accordionContent.classList.contains('hidden')) {
                icon.setAttribute('data-feather', 'plus');
            } else {
                icon.setAttribute('data-feather', 'minus');
            }
            feather.replace();
        });
    });
});

// Format numbers with commas
function formatNumber(num) {
    return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

// Calculate percentage difference
function calculatePercentageDifference(actual, expected) {
    if (expected === 0) return 0;
    return ((actual - expected) / expected) * 100;
}

// Validate POD entries
function validatePODEntry(entry) {
    const { format5, format11, format15, format45 } = entry;
    const totalKg = (format5 * 5) + (format11 * 11) + (format15 * 15) + (format45 * 45);
    
    return {
        totalKg,
        isValid: totalKg > 0
    };
}
// View toggle functionality
function toggleView(viewType) {
    const cardView = document.querySelector('.grid');
    const tableView = document.querySelector('.hidden > div');
    
    if (viewType === 'table') {
        cardView.classList.add('hidden');
        tableView.classList.remove('hidden');
    } else if (viewType === 'card') {
        cardView.classList.remove('hidden');
        tableView.classList.add('hidden');
    }
}

// Initialize view toggle buttons
document.querySelectorAll('[data-view-toggle]').forEach(button => {
    button.addEventListener('click', function() {
        const viewType = this.getAttribute('data-view-toggle');
        toggleView(viewType);
        
        // Update active state
        document.querySelectorAll('[data-view-toggle]').forEach(btn => {
            btn.classList.remove('active');
        });
        this.classList.add('active');
    });
});

// Map functions
function openMapWithTruck(vehicleId) {
    // In a real app, this would open a modal with live map tracking
    console.log(`Opening map for vehicle ${vehicleId}`);
    alert(`Opening live map tracking for vehicle ${vehicleId}`);
}

// Add click handlers for map buttons
document.addEventListener('DOMContentLoaded', function() {
    const mapButtons = document.querySelectorAll('[data-feather="map"]');
    mapButtons.forEach(button => {
        button.addEventListener('click', function() {
            const vehicleCell = this.closest('tr').querySelector('td:nth-child(8)');
            const vehicleId = vehicleCell.textContent.trim();
            if (vehicleId && vehicleId !== '-') {
                openMapWithTruck(vehicleId);
            }
        });
    });
});
// Enhanced API functions with loading states
async function fetchTransfers(filters = {}) {
    // Show loading state
    document.getElementById('transfers-container').innerHTML = `
        <div class="flex justify-center items-center h-64">
            <div class="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-blue-500"></div>
        </div>
    `;
    
    try {
        // In a real app, this would call your backend API
        const response = await mockApiCall(filters);
        renderTransfers(response);
    } catch (error) {
        showError(error);
    }
}

async function mockApiCall(filters) {
    return new Promise(resolve => {
        setTimeout(() => {
            // Mock data
            const mockData = [
                // Your mock transfer data here
            ];
            resolve(mockData);
        }, 1000);
    });
}

function renderTransfers(transfers) {
    const container = document.getElementById('transfers-container');
    if (transfers.length === 0) {
        container.innerHTML = `
            <div class="text-center py-12">
                <i data-feather="package" class="mx-auto h-12 w-12 text-gray-400"></i>
                <h3 class="mt-2 text-sm font-medium text-gray-900">No transfers found</h3>
                <p class="mt-1 text-sm text-gray-500">Try adjusting your search or filter criteria.</p>
            </div>
        `;
        feather.replace();
        return;
    }
    
    // Render transfers
    // ...
}

function showError(error) {
    const container = document.getElementById('transfers-container');
    container.innerHTML = `
        <div class="rounded-md bg-red-50 p-4">
            <div class="flex">
                <div class="flex-shrink-0">
                    <i data-feather="alert-triangle" class="h-5 w-5 text-red-400"></i>
                </div>
                <div class="ml-3">
                    <h3 class="text-sm font-medium text-red-800">Error loading transfers</h3>
                    <div class="mt-2 text-sm text-red-700">
                        ${error.message || 'Please try again later.'}
                    </div>
                </div>
            </div>
        </div>
    `;
    feather.replace();
}

// Refresh data every 30 seconds
setInterval(() => {
    fetchTransfers();
}, 30000);
async function fetchTransfers(filters = {}) {
// In a real app, this would call your backend API
    console.log('Fetching transfers with filters:', filters);
    return [];
}

async function createTransfer(transferData) {
    // In a real app, this would call your backend API
    console.log('Creating transfer:', transferData);
    return { success: true, transferId: 'TRF-' + Math.floor(Math.random() * 10000) };
}

async function registerHandover(handoverData) {
    // In a real app, this would call your backend API
    console.log('Registering handover:', handoverData);
    return { success: true };
}