<?php
/**
 * Common Footer
 * Dozar & Tripper Management System
 */

if (isLoggedIn()):
?>
                </div>
            </div>
        </div>
    </div>
<?php endif; ?>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/js/all.min.js"></script>
    <script>
        // Auto-hide alerts after 5 seconds
        setTimeout(function() {
            var alerts = document.querySelectorAll('.alert');
            alerts.forEach(function(alert) {
                var bsAlert = new bootstrap.Alert(alert);
                bsAlert.close();
            });
        }, 5000);
        
        // GPS Location capture function
        function captureGPS(hiddenLatId, hiddenLngId, addressId) {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function(position) {
                    document.getElementById(hiddenLatId).value = position.coords.latitude;
                    document.getElementById(hiddenLngId).value = position.coords.longitude;
                    
                    fetch('https://nominatim.openstreetmap.org/reverse?format=json&lat=' + position.coords.latitude + '&lon=' + position.coords.longitude)
                        .then(response => response.json())
                        .then(data => {
                            if (addressId && document.getElementById(addressId)) {
                                document.getElementById(addressId).value = data.display_name || '';
                            }
                        })
                        .catch(err => console.log('Geocoding error:', err));
                    
                    alert('GPS captured successfully!');
                }, function(error) {
                    alert('Error getting location: ' + error.message);
                });
            } else {
                alert('Geolocation is not supported by this browser.');
            }
        }
        
        // Smart odometer - get last end odometer for selected vehicle
        function getLastOdometer(vehicleId, odometerType, callback) {
            if (vehicleId && odometerType) {
                fetch('get_last_odometer.php?vehicle_id=' + vehicleId + '&type=' + odometerType)
                    .then(response => response.json())
                    .then(data => {
                        if (data.success && data.odometer) {
                            callback(data.odometer);
                        }
                    })
                    .catch(err => console.log('Error:', err));
            }
        }
        
        // Nepali Date conversion
        function convertToNepali(dateString) {
            if (!dateString) return '';
            
            var englishMonths = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
            var nepaliMonths = ['Baisakh', 'Jestha', 'Ashadh', 'Shrawan', 'Bhadra', 'Ashwin', 'Kartik', 'Mangsir', 'Poush', 'Margh', 'Chaitra', 'Baisakh'];
            
            var date = new Date(dateString);
            var year = date.getFullYear();
            var month = date.getMonth();
            var day = date.getDate();
            
            // This is still a simplified conversion but calibrated for 2083
            // For a production app, consider passing the BS date from PHP to JS
            var nepaliYear = year + 56;
            var nepaliMonth = month;
            var nepaliDay = day - 13; // April 14 is Baishakh 1 (Difference of 13)
            
            if (month == 3) { // April
                nepaliYear = year + 57;
                nepaliMonth = 0; // Baisakh
                nepaliDay = day - 13;
            } else if (month < 3) {
                nepaliYear = year + 56;
            }
            
            if (nepaliDay <= 0) {
                nepaliMonth = (nepaliMonth - 1 + 12) % 12;
                nepaliDay = 30 + nepaliDay; // Rough estimation
            }
            
            return nepaliDay + ' ' + nepaliMonths[nepaliMonth] + ' ' + nepaliYear;
        }
        
        // Update Nepali date picker hidden input
        function updateNepaliDate(selectElement) {
            var container = selectElement.closest('.nepali-date-picker');
            if (!container) return;
            
            var daySelect = container.querySelector('.nepali-day');
            var monthSelect = container.querySelector('.nepali-month');
            var yearSelect = container.querySelector('.nepali-year');
            var hiddenInput = container.querySelector('input[type="hidden"]');
            
            if (daySelect && monthSelect && yearSelect && hiddenInput) {
                var day = daySelect.value;
                var month = monthSelect.value;
                var year = yearSelect.value;
                hiddenInput.value = year + '-' + month + '-' + day;
            }
        }
        
        // Nepali days in each month
        var nepaliMonthDays = {
            1: 31, 2: 32, 3: 31, 4: 32, 5: 31, 6: 32,
            7: 31, 8: 31, 9: 30, 10: 29, 11: 30, 12: 30
        };
        
        // Confirm delete
        function confirmDelete(message) {
            return confirm(message || 'Are you sure you want to delete this record?');
        }
        
        // Print specific element
        function printElement(elementId) {
            var element = document.getElementById(elementId);
            if (element) {
                var printWindow = window.open('', '', 'height=600,width=800');
                printWindow.document.write('<html><head><title>Print</title>');
                printWindow.document.write('<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">');
                printWindow.document.write('</head><body>');
                printWindow.document.write(element.innerHTML);
                printWindow.document.write('</body></html>');
                printWindow.document.close();
                printWindow.print();
            }
        }
        
        // Responsive Sidebar Toggle
        document.addEventListener('DOMContentLoaded', function() {
            var menuToggle = document.getElementById('menuToggle');
            var sidebar = document.getElementById('sidebar');
            var sidebarOverlay = document.getElementById('sidebarOverlay');
            var sidebarClose = document.getElementById('sidebarClose');
            
            if (menuToggle && sidebar) {
                menuToggle.addEventListener('click', function() {
                    sidebar.classList.toggle('show');
                    sidebarOverlay.classList.toggle('active');
                });
            }
            
            if (sidebarClose) {
                sidebarClose.addEventListener('click', function() {
                    sidebar.classList.remove('show');
                    sidebarOverlay.classList.remove('active');
                });
            }
            
            if (sidebarOverlay) {
                sidebarOverlay.addEventListener('click', function() {
                    sidebar.classList.remove('show');
                    sidebarOverlay.classList.remove('active');
                });
            }
            
            // Close sidebar on link click (mobile)
            var sidebarLinks = document.querySelectorAll('.sidebar a');
            sidebarLinks.forEach(function(link) {
                link.addEventListener('click', function() {
                    if (window.innerWidth < 992) {
                        sidebar.classList.remove('show');
                        sidebarOverlay.classList.remove('active');
                    }
                });
            });
            
            // Handle window resize
            window.addEventListener('resize', function() {
                if (window.innerWidth >= 992) {
                    sidebar.classList.remove('show');
                    sidebarOverlay.classList.remove('active');
                }
            });
        });
    </script>
</body>
</html>
