/**
 * ComptaFlow Export Handler
 * Handles CSV export button clicks
 */
document.addEventListener('DOMContentLoaded', function() {
    // Handle export buttons
    var exportButtons = document.querySelectorAll('.comptaflow-export-btn');
    
    if (exportButtons.length === 0) {
        return;
    }
    
    exportButtons.forEach(function(btn) {
        btn.addEventListener('click', function(e) {
            e.preventDefault();
            
            var startDate = document.getElementById('export_start_date');
            var endDate = document.getElementById('export_end_date');
            
            if (!startDate || !endDate || !startDate.value || !endDate.value) {
                var errorMsg = (typeof comptaflowExport !== 'undefined' && comptaflowExport.pleaseSelectPeriod) 
                    ? comptaflowExport.pleaseSelectPeriod 
                    : 'Please select a period.';
                alert(errorMsg);
                return;
            }
            
            var baseUrl = this.dataset.url;
            var nonce = this.dataset.nonce;
            
            var url = baseUrl 
                + '&start=' + encodeURIComponent(startDate.value) 
                + '&end=' + encodeURIComponent(endDate.value) 
                + '&_wpnonce=' + encodeURIComponent(nonce);
            
            window.location.href = url;
        });
    });
});
