<script>
var generate_csv = "_service_page_ajax.php";
function generateCSV(customerId) {
    $.ajax({
        url: generate_csv,
        type: 'GET',
        data: {sells_history : 1 , id_customer: customerId},
        dataType: 'json',
        success: function(response) {
            if (response.filepath) {
                downloadCSV(response.filepath);
            }
        },
        error: function(xhr, status, error) {
            console.error('Erreur lors de la génération du CSV:', error);
        }
    });
}

function downloadCSV(filepath) {
    var a = document.createElement('a');
    a.href = filepath;
    a.download = filepath.split('/').pop();
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);

    $.ajax({
        url: generate_csv,
        type: 'POST',
        data: { filepath: filepath, action: 'remove_csv' },
        success: function(response) {
            console.log('file deleted.');
        }
    });
}
</script>