<?php
session_start();
include('con.php');
include('header.php');

if (!isset($_SESSION['username'])) {
    header("Location: login.php");
    exit;
}

$username = $_SESSION['username'];

// Fetch from NEW table dl_history
$query = mysqli_query($ahk_conn, "SELECT * FROM dl_history WHERE custno='$username' ORDER BY id DESC");
?>

<div class="container mt-5">
    <h4 class="mb-4 text-primary text-center fw-bold">DL Download History</h4>
    <div class="table-responsive">
        <table class="table table-bordered table-striped align-middle">
            <thead class="table-dark">
                <tr>
                    <th>DL Number</th>
                    <th>DOB</th>
                    <th>Application No</th>
                    <th>Order ID</th>
                    <th>Date</th>
                    <th class="text-center">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php if (mysqli_num_rows($query) > 0): ?>
                    <?php while ($row = mysqli_fetch_assoc($query)): ?>
                    <tr>
                        <td><strong><?= htmlspecialchars($row['number']) ?></strong></td>
                        <td><?= htmlspecialchars($row['dob']) ?></td>
                        <td><?= htmlspecialchars($row['apiid']) ?></td>
                        <td><small><?= htmlspecialchars($row['tnsid']) ?></small></td>
                        <td><small><?= $row['date_time'] ?></small></td>
                        <td class="text-center">
    <a href="download.php?file=<?php echo urlencode($row['link']); ?>" 
   class="btn btn-sm btn-success">
   <i class="fa fa-download"></i> Download Now
</a>
                        </td>
                    </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr><td colspan="6" class="text-center">No records found</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>