<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once 'functions.php';

// Get all records
$records = getRecords();
if (empty($records)) {
    die("No records found.");
}

// Force CSV download
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="test_export.csv"');
$out = fopen('php://output', 'w');
fputcsv($out, ['Date', 'Airline', 'Flight', 'Oil1', 'Oil2', 'APU', 'Hyd', 'Tires', 'Hours']);
foreach ($records as $r) {
    fputcsv($out, [
        $r['arrival_datetime'],
        $r['airline'],
        $r['inbound_flight'] . '/' . $r['outbound_flight'],
        $r['engine_oil1_qty'],
        $r['engine_oil2_qty'],
        $r['apu_oil_qty'],
        $r['hydraulic_qty'],
        $r['tires_serviced'],
        $r['employee_hours']
    ]);
}
fclose($out);
exit;
?>