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

// Handle PDF download
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['token'], $_POST['type'])) {
    $token = $_POST['token'];
    $type = $_POST['type'];
    $apikey = $tng_apikey; // Your API key
    $url = $tng_apiurl . "/aadhar_org_pdf_downoad?token=$token&type=$type&apikey=$apikey";

    // Call API
    $curl = curl_init();
    curl_setopt_array($curl, [
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 60,
    ]);
    $resp = curl_exec($curl);
    curl_close($curl);

    $pdfResp = json_decode($resp, true);


if ($pdfResp['Message'] === 'Success' && !empty($pdfResp['pdf'])) {
    $pdfData = $pdfResp['pdf'];

    // Remove the 'data:application/pdf;base64,' prefix
    if (strpos($pdfData, 'base64,') !== false) {
        $pdfData = substr($pdfData, strpos($pdfData, 'base64,') + 7);
    }

    // Decode base64
    $pdfData = base64_decode($pdfData);

    if ($pdfData === false) {
        die('Failed to decode PDF data');
    }

    // Clear any previous output buffers
    if (ob_get_length()) ob_end_clean();

    // Send headers for download
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename="Aadhar_'.$type.'.pdf"');
    header('Content-Length: ' . strlen($pdfData));
    header('Cache-Control: private');
    header('Pragma: public');
    header('Expires: 0');

    // Output PDF content
    echo $pdfData;
    exit;
} else {
    die('Unable to generate PDF');
}

}
?>
<!--start page wrapper -->
<div class="page-wrapper">
<div class="page-content">
    <div class="card radius-10">
        <div class="card-body">
            <h5 class="mb-0">AADHAR ORG PDF LIST</h5>
            <hr>
            <div class="table-responsive">
                <table id="example2" class="table align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th class="text-center">SL.</th>
                            <th class="text-center">Name </th>
                            <th class="text-center">Apply Date</th>
                            <th class="text-center">Aadhar No</th>
                            <th class="text-center">Type</th>
                            <th class="text-center">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if($udata['type']=='admin') {
                            $res = mysqli_query($ahk_conn, "SELECT * FROM aadhar_org_pdf ORDER BY id DESC");
                        } else {
                            $res = mysqli_query($ahk_conn, "SELECT * FROM aadhar_org_pdf WHERE username='".$udata['phone']."' ORDER BY id DESC");
                        }

                        if(mysqli_num_rows($res) > 0){
                            $x = 0;
                            while($data = mysqli_fetch_assoc($res)){
                                $x++;
                        ?>
                                <tr>
                                    <td class="text-center"><?= $x ?></td>
                                    <td class="text-center"><?= htmlspecialchars($data['name']) ?></td>
                                    <td class="text-center"><?= strtoupper($data['date']) ?></td>
                                    <td class="text-center"><?= strtoupper($data['aadhar']) ?></td>
                                    <td class="text-center">
                                        <form method="POST" style="display:flex; gap:5px;">
                                            <input type="hidden" name="token" value="<?= $data['token'] ?>">
                                            <select name="type" class="form-select form-select-sm" required>
                                                <option value="">--Select Type--</option>
                                                <option value="e_print">E Print</option>
                                                <option value="pvc_pdf">PVC PDF</option>
                                                <option value="uid_pdf">UID PDF</option>
                                            </select>
                                    </td>
                                    <td class="text-center">
                                            <button type="submit" class="btn btn-success btn-sm">Download</button>
                                        </form>
                                    </td>
                                </tr>
                        <?php
                            }
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</div>
<!--end page wrapper -->
<?php include('../template/ahkweb/footer.php'); ?>

<!-- Bootstrap & Plugins JS -->
<script src="../template/ahkweb/assets/js/bootstrap.bundle.min.js"></script>
<script src="../template/ahkweb/assets/js/jquery.min.js"></script>
<script src="../template/ahkweb/assets/plugins/simplebar/js/simplebar.min.js"></script>
<script src="../template/ahkweb/assets/plugins/metismenu/js/metisMenu.min.js"></script>
<script src="../template/ahkweb/assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
<script src="../template/ahkweb/assets/js/index.js"></script>
<script src="../template/ahkweb/assets/js/app.js"></script>

<!-- Datatables -->
<script src="../template/ahkweb/assets/plugins/datatable/js/jquery.dataTables.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/dataTables.bootstrap5.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/dataTables.buttons.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/buttons.bootstrap5.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/jszip.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/pdfmake.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/vfs_fonts.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/buttons.html5.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/buttons.print.min.js"></script>
<script src="../template/ahkweb/assets/plugins/datatable/js/buttons.colVis.min.js"></script>

<script>
$(document).ready(function() {
    $('#example2').DataTable({
        lengthChange: false,
        buttons: [ 'copy', 'excel', 'pdf', 'print', 'colvis' ]
    }).buttons().container().appendTo('#example2_wrapper .col-md-6:eq(0)');
});
</script>
