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

/* ================================
   SESSION & CONFIG
================================ */
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/session.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/config.php';

/* ================================
   HEADER
================================ */
require_once $_SERVER['DOCUMENT_ROOT'] . '/template/tngweb/header.php';

$type  = $udata['type'];
$phone = $udata['phone'];

/* ================================
   FETCH DATA
================================ */
if ($type === 'admin') {
    $sql = "SELECT * FROM dl_updates ORDER BY id DESC";
} else {
    $sql = "SELECT * FROM dl_updates WHERE phone='$phone' ORDER BY id DESC";
}

$result = mysqli_query($tng_conn, $sql);
?>

<div class="container mt-5">
    <div class="card shadow">
        <div class="card-header bg-primary text-white">
            📋 DL UPDATE REQUEST LIST
        </div>

        <div class="card-body table-responsive">
            <table class="table table-bordered table-hover text-center">
                <thead class="table-dark">
                    <tr>
                        <th>#</th>
                        <th>DL Number</th>
                        <th>Mobile</th>
                        <th>State</th>
                        <th>Price</th>
                        <th>Status</th>
                        <th>Date</th>

                        <?php if ($type === 'admin') { ?>
                            <th>User</th>
                        <?php } ?>
                    </tr>
                </thead>
                <tbody>
                <?php
                if (mysqli_num_rows($result) > 0) {
                    $i = 1;
                    while ($row = mysqli_fetch_assoc($result)) {
                        $badge = 'secondary';

                        if ($row['status'] == 'Success') $badge = 'success';
                        elseif ($row['status'] == 'Rejected') $badge = 'danger';
                        elseif ($row['status'] == 'Pending') $badge = 'warning';

                        echo "<tr>
                            <td>{$i}</td>
                            <td>{$row['dl_number']}</td>
                            <td>{$row['mobile']}</td>
                            <td>{$row['state']}</td>
                            <td>₹ {$row['price']}</td>
                            <td><span class='badge bg-{$badge}'>{$row['status']}</span></td>
                            <td>".date('d-m-Y', strtotime($row['created_at']))."</td>";

                        if ($type === 'admin') {
                            echo "<td>{$row['phone']}</td>";
                        }

                        echo "</tr>";
                        $i++;
                    }
                } else {
                    echo "<tr><td colspan='8'>No Records Found</td></tr>";
                }
                ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/template/tngweb/footer.php';
?>
