// Toggle sidebar on mobile
const menuToggle = document.getElementById('menuToggle');
const sidebar = document.getElementById('sidebar');

if (menuToggle && sidebar) {
    menuToggle.addEventListener('click', () => {
        sidebar.classList.toggle('active');
    });
}

// Modal functionality
const modal = document.getElementById('formModal');
const openModalBtn = document.querySelector('[data-target="#formModal"]');
const closeModalBtns = document.querySelectorAll('.close-modal');

if (modal) {
    // Open modal
    if (openModalBtn) {
        openModalBtn.addEventListener('click', () => {
            modal.classList.add('active');
        });
    }
    
    // Close modal
    closeModalBtns.forEach(btn => {
        btn.addEventListener('click', () => {
            modal.classList.remove('active');
        });
    });
    
    // Close modal when clicking outside
    modal.addEventListener('click', (e) => {
        if (e.target === modal) {
            modal.classList.remove('active');
        }
    });
}

// Add customer functionality
const realuserBtn = document.getElementById('realuser');

if (realuserBtn) {
    realuserBtn.addEventListener('click', function() {
        const name = document.getElementById('name').value;
        
        Swal.fire({
            title: "Confirm Registration",
            text: "Are you sure you want to add " + name + "?",
            icon: "warning",
            showCancelButton: true,
            confirmButtonColor: "#1a73e8",
            cancelButtonColor: "#d33",
            confirmButtonText: "Yes, add customer"
        }).then((result) => {
            if (result.isConfirmed) {
                // Show loading indicator
                Swal.showLoading();
                
                // Simulate AJAX request (replace with your actual AJAX call)
                setTimeout(() => {
                    Swal.fire(
                        'Success!',
                        name + ' has been successfully added.',
                        'success'
                    );
                    
                    // Close the modal
                    modal.classList.remove('active');
                    
                    // Reset the form
                    document.getElementById('add-customer').reset();
                }, 1500);
            }
        });
    });
}

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

// Initialize data tables
document.addEventListener('DOMContentLoaded', function() {
    const table = document.querySelector('.transactions-table table');
    if (table) {
        // Simple client-side sorting functionality
        const headers = table.querySelectorAll('th');
        headers.forEach((header, index) => {
            header.style.cursor = 'pointer';
            header.addEventListener('click', () => {
                sortTable(index);
            });
        });
        
        function sortTable(column) {
            const tbody = table.querySelector('tbody');
            const rows = Array.from(tbody.querySelectorAll('tr'));
            
            rows.sort((a, b) => {
                const aText = a.children[column].textContent.trim();
                const bText = b.children[column].textContent.trim();
                
                // Simple numeric sorting for columns that might contain numbers
                if (!isNaN(aText) && !isNaN(bText)) {
                    return aText - bText;
                }
                
                // Default to text sorting
                return aText.localeCompare(bText);
            });
            
            // Reverse if already sorted
            if (tbody.dataset.sortedColumn === String(column)) {
                rows.reverse();
                tbody.dataset.sortedColumn = '';
            } else {
                tbody.dataset.sortedColumn = column;
            }
            
            // Re-add rows in sorted order
            rows.forEach(row => tbody.appendChild(row));
        }
    }
});