document.addEventListener('DOMContentLoaded', function() {
    // Toggle sidebar
    const sidebarCollapse = document.getElementById('sidebarCollapse');
    const sidebar = document.querySelector('.sidebar');
    const content = document.querySelector('.content');
    const footer = document.querySelector('.admin-footer');
    
    if (sidebarCollapse) {
        sidebarCollapse.addEventListener('click', function() {
            sidebar.classList.toggle('active');
            content.classList.toggle('active');
            if (footer) {
                footer.classList.toggle('active');
            }
        });
    }
    
    // Image preview for file uploads
    const imageInputs = document.querySelectorAll('input[type="file"][accept*="image"]');
    imageInputs.forEach(input => {
        input.addEventListener('change', function() {
            const previewId = this.dataset.preview;
            const preview = document.getElementById(previewId);
            
            if (preview && this.files && this.files[0]) {
                const reader = new FileReader();
                
                reader.onload = function(e) {
                    preview.src = e.target.result;
                    preview.style.display = 'block';
                };
                
                reader.readAsDataURL(this.files[0]);
            }
        });
    });
    
    // Form validation
    const forms = document.querySelectorAll('.needs-validation');
    forms.forEach(form => {
        form.addEventListener('submit', function(event) {
            if (!form.checkValidity()) {
                event.preventDefault();
                event.stopPropagation();
            }
            
            form.classList.add('was-validated');
        });
    });
    
    // Delete confirmation modals
    const deleteButtons = document.querySelectorAll('[data-bs-toggle="modal"][data-bs-target="#deleteModal"]');
    deleteButtons.forEach(button => {
        button.addEventListener('click', function() {
            const id = this.dataset.id;
            const name = this.dataset.name;
            const type = this.dataset.type;
            
            const modal = document.getElementById('deleteModal');
            if (modal) {
                const modalBody = modal.querySelector('.modal-body');
                const modalForm = modal.querySelector('form');
                
                if (modalBody) {
                    modalBody.textContent = `Êtes-vous sûr de vouloir supprimer ${type} : ${name} ?`;
                }
                
                if (modalForm) {
                    modalForm.action = this.dataset.action;
                }
            }
        });
    });
    
    // Initialize Bootstrap tooltips
    var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
    var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl);
    });
    
    // Initialize DataTables if present
    const dataTables = document.querySelectorAll('.datatable');
    if (dataTables.length > 0 && typeof $.fn.DataTable !== 'undefined') {
        dataTables.forEach(table => {
            $(table).DataTable({
                responsive: true,
                language: {
                    url: '//cdn.datatables.net/plug-ins/1.10.24/i18n/French.json'
                }
            });
        });
    }
    
    // Mark messages as read
    const messageLinks = document.querySelectorAll('.message-link');
    messageLinks.forEach(link => {
        link.addEventListener('click', function() {
            const messageId = this.dataset.id;
            const row = document.querySelector(`tr[data-id="${messageId}"]`);
            
            if (row) {
                row.classList.remove('table-warning');
                
                // Update unread count in the sidebar
                const unreadBadge = document.getElementById('unreadMessageCount');
                if (unreadBadge) {
                    let count = parseInt(unreadBadge.textContent);
                    if (count > 0) {
                        unreadBadge.textContent = count - 1;
                    }
                }
            }
        });
    });
});
