<?php
include('../includes/session.php');
include('../includes/config.php');
include('../template/tngweb/header.php');

// Filter settings (Optional: Date wise)
$from = $_GET['from'] ?? date('Y-m-d');
$to   = $_GET['to']   ?? date('Y-m-d');

$fromSql = mysqli_real_escape_string($tng_conn, $from . ' 00:00:00');
$toSql   = mysqli_real_escape_string($tng_conn, $to . ' 23:59:59');
?>

<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap5.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">

<div class="content-wrap">
    <div class="main">
        <div class="page-wrapper">
            <div class="page-content">
                <div class="card shadow-sm" style="border-radius:15px; border:none;">
                    <div class="card-body">
                        
                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <h5 class="fw-bold text-primary"><i class="bi bi-file-earmark-text"></i> DL PDF History</h5>
                            <a href="dl_pdf.php" class="btn btn-sm btn-success rounded-pill px-3">+ New Request</a>
                        </div>

                        <hr>

                        <form method="get" class="row g-2 mb-4 align-items-end">
                            <div class="col-md-3">
                                <label class="small fw-bold">From Date</label>
                                <input type="date" name="from" value="<?= $from ?>" class="form-control form-control-sm rounded-pill">
                            </div>
                            <div class="col-md-3">
                                <label class="small fw-bold">To Date</label>
                                <input type="date" name="to" value="<?= $to ?>" class="form-control form-control-sm rounded-pill">
                            </div>
                            <div class="col-md-4">
                                <button type="submit" class="btn btn-sm btn-primary rounded-pill px-4">Filter</button>
                                <a href="dl_pdf_list.php" class="btn btn-sm btn-light border rounded-pill px-4">Reset</a>
                            </div>
                        </form>

                        <div class="table-responsive">
                            <table id="dlTable" class="table table-hover align-middle text-center" style="font-size: 14px;">
                                <thead class="table-light">
                                    <tr>
                                        <th>Sr.No</th>
                                        <th>Name</th>
                                        <th>DL Number</th>
                                        <th>Applied Date</th>
                                        <th>Status</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php
                                $phone = mysqli_real_escape_string($tng_conn, $udata['phone']);
                                
                                // Sirf 'dl_pdf' service ka data fetch hoga
                                $query = "SELECT * FROM all_pdf_records 
                                          WHERE service_name = 'dl_pdf' 
                                          AND username = '$phone'
                                          AND date BETWEEN '$fromSql' AND '$toSql'
                                          ORDER BY id DESC";
                                
                                $res = mysqli_query($tng_conn, $query);
                                $sr = 1;

                                while ($row = mysqli_fetch_assoc($res)) {
                                    $pdfData = $row['pdf'];
                                    $fileName = "DL_" . $row['number'] . ".pdf";
                                ?>
                                    <tr>
                                        <td><?= $sr++ ?></td>
                                        <td class="fw-bold"><?= htmlspecialchars($row['name']) ?></td>
                                        <td><code><?= htmlspecialchars($row['number']) ?></code></td>
                                        <td><?= date("d-m-Y h:i A", strtotime($row['date'])) ?></td>
                                        <td><span class="badge bg-success">Success</span></td>
                                        <td>
                                            <?php if (!empty($pdfData)): ?>
                                                <button class="btn btn-sm btn-primary rounded-pill px-3" 
                                                        onclick="downloadBase64PDF('<?= $pdfData ?>', '<?= $fileName ?>')">
                                                    <i class="bi bi-download"></i> Download
                                                </button>
                                            <?php else: ?>
                                                <span class="text-muted">No File</span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include('../template/tngweb/footer.php'); ?>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap5.min.js"></script>

<script>
$(document).ready(function() {
    $('#dlTable').DataTable({
        "pageLength": 10,
        "ordering": false,
        "language": { "search": "Quick Search:" }
    });
});

/**
 * Function to download PDF from Base64 string
 */
function downloadBase64PDF(base64String, fileName) {
    const linkSource = `data:application/pdf;base64,${base64String.replace(/^data:application\/pdf;base64,/, '')}`;
    const downloadLink = document.createElement("a");
    downloadLink.href = linkSource;
    downloadLink.download = fileName;
    downloadLink.click();
}
</script>

<style>
.card { transition: 0.3s; }
.table thead th { border: none; font-weight: 600; color: #555; }
.badge { font-weight: 500; padding: 6px 12px; }
</style>