<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once '../includes/db_connect.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;


// require 'includes/db_connect.php';
// require 'vendor/autoload.php';  // PhpSpreadsheet autoload

// use PhpOffice\PhpSpreadsheet\Spreadsheet;
// use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

// Fetch all streets
$query = "SELECT * FROM registered_streets ORDER BY street_name ASC";
$result = $conn->query($query);
var_dump(class_exists('PhpOffice\PhpSpreadsheet\Spreadsheet'));
exit;

// Create Spreadsheet
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// Sheet Title
$sheet->setTitle("Streets");

// Headers
$sheet->setCellValue("A1", "S/N");
$sheet->setCellValue("B1", "Street Name");
$sheet->setCellValue("C1", "Street Code");
$sheet->setCellValue("D1", "Zone");
$sheet->setCellValue("E1", "Date Registered");

$rowNumber = 2;
$sn = 1;

while ($row = $result->fetch_assoc()) {
    $sheet->setCellValue("A" . $rowNumber, $sn);
    $sheet->setCellValue("B" . $rowNumber, $row['street_name']);
    $sheet->setCellValue("C" . $rowNumber, $row['street_code']);
    $sheet->setCellValue("D" . $rowNumber, $row['zone']);
    $sheet->setCellValue("E" . $rowNumber, date("d M Y", strtotime($row['created_at'])));
    $rowNumber++;
    $sn++;
}

// Autosize Columns
foreach (range('A', 'E') as $columnID) {
    $sheet->getColumnDimension($columnID)->setAutoSize(true);
}

// Output Excel
$writer = new Xlsx($spreadsheet);

header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=streets_list.xlsx");

$writer->save("php://output");
exit;
?>
