<?php
include 'db.php';

// Set headers for Excel
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=perishables_report.xls");

// Fetch data
$perishables = $conn->query("
SELECT pp.*, d.name AS department
FROM perishable_products pp
LEFT JOIN departments d ON pp.department_id=d.id
ORDER BY pp.expiry_date ASC
");

// Output column headers
echo "Product\tDepartment\tQuantity\tBatch Number\tExpiry Date\tRecorded By\tCreated At\n";

// Output data
while($row = $perishables->fetch_assoc()){
    echo $row['product_name']."\t".
         $row['department']."\t".
         $row['quantity']."\t".
         $row['batch_number']."\t".
         $row['expiry_date']."\t".
         $row['recorded_by']."\t".
         $row['created_at']."\n";
}
?>