<?php
require 'includes/db_connect.php';
// require_once('tcpdf/tcpdf.php');
require_once __DIR__ . '/../tcpdf/tcpdf.php';

// Fetch all streets
$query = "SELECT * FROM registered_streets ORDER BY street_name ASC";
$result = $conn->query($query);

// PDF Init
$pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator("Street System");
$pdf->SetAuthor("Admin");
$pdf->SetTitle("Registered Streets List");
$pdf->SetMargins(10, 10, 10);
$pdf->AddPage();

// Header
$html = '
<h2 style="color:#f57c00; text-align:center;">Registered Streets</h2>
<table border="1" cellpadding="5">
<thead>
    <tr style="background-color:#f57c00; color:white;">
        <th>S/N</th>
        <th>Street Name</th>
        <th>Town</th>
        <th>Reg Number</th>
        <th>Date</th>
    </tr>
</thead>
<tbody>
';

$sn = 1;
while ($row = $result->fetch_assoc()) {
    $html .= "
    <tr>
        <td>{$sn}</td>
        <td>{$row['street_name']}</td>
        <td>{$row['town']}</td>
        <td>{$row['reg_no']}</td>
        <td>" . date("d M Y", strtotime($row['approval_year'])) . "</td>
    </tr>
    ";
    $sn++;
}

$html .= "</tbody></table>";

$pdf->writeHTML($html, true, false, true, false, '');

// Output PDF
$pdf->Output("streets_list.pdf", "D");
?>
