<?php
require_once("../includes/session.php");
require_once("../includes/config.php");

$id = intval($_GET['id'] ?? 0);

$rec = mysqli_fetch_assoc(mysqli_query($ahk_conn, "SELECT * FROM pan_aadhaar_otp_requests WHERE id=$id AND username='{$udata['name']}'"));

if ($rec && $rec['status'] == 'Success' && !empty($rec['pdf_file'])) {
    if (strpos($rec['pdf_file'], 'base64,') !== false) {
        $base64 = explode('base64,', $rec['pdf_file'])[1];
        $pdfData = base64_decode($base64);
        
        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment; filename="PAN_' . $rec['order_id'] . '.pdf"');
        header('Content-Length: ' . strlen($pdfData));
        echo $pdfData;
        exit;
    }
}

echo "File not found!";
?>