<?php
// export_handler.php

// Include necessary files and configurations
require_once('includes/load.php'); // Adjust the path as needed
// Include other necessary files here

// Fetch data from the database
$products = get_all_products(); // Assuming this function is defined in the included files

// Set headers for CSV download
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="products.csv"');

// Create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// Write CSV headers
fputcsv($output, array('Part Number', 'Product', 'Category', 'In-Stock', 'Product Added'));

// Write each data row
foreach ($products as $product) {
    fputcsv($output, array(
        $product['part_number'],
        $product['name'],
        $product['categorie_id'],
        get_product_stock($product['id']),
        read_date($product['date'])
    ));
}

// Close the file pointer
fclose($output);
