<?php
include('../../includes/config.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);

$q = $_GET['searchid'] ?? '';

if (!is_numeric($q)) {
    die('Invalid ID');
}

$q = (int)$q;

// Changed: Now fetch from all_pdf_records table
$sql = "SELECT * FROM all_pdf_records WHERE id = $q AND service_name = 'PAN_MANUAL' LIMIT 1";
$res = mysqli_query($tng_conn, $sql);

if (!$res || mysqli_num_rows($res) == 0) {
    die('Record not found');
}

$row = mysqli_fetch_assoc($res);

// Decode the base64 JSON data from pdf column
$pdfData = json_decode(base64_decode($row['pdf']), true);

if (!$pdfData) {
    die('Invalid data format');
}

$a1 = strtoupper($pdfData['name'] ?? $row['name'] ?? '');
$a2 = strtoupper($pdfData['fathername'] ?? '');
$a3 = strtoupper($pdfData['panno'] ?? $row['number'] ?? '');
$a4 = $pdfData['gender'] ?? '';
$a5 = str_replace('-', '/', $pdfData['dob'] ?? '');

// Images are already in base64 format with data:image prefix
$img = $pdfData['image'] ?? '';
$sign = $pdfData['signimage'] ?? '';

$xmlData = '<?xml version="1.0" encoding="UTF-8"?><PrintLetterBarcodeData uid="' 
    . htmlspecialchars($a3) . '" name="' 
    . htmlspecialchars($a1) . '" dob="' 
    . ($pdfData['dob'] ?? '') . '" co="' 
    . htmlspecialchars($a1) . '"/>';

$qr = 'https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=' . urlencode($xmlData);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pan Card Preview</title>
<link href="https://fonts.googleapis.com/css?family=Hind" rel="stylesheet">
<link href="pan.css" rel="stylesheet">
<style>
.bg{background:url('pan.jpg') no-repeat;width:800px;height:1200px;overflow:visible;display:block;background-size:100% auto;}
img.mrmins.mrninbigs{top:924px;position:absolute;left:302px;width:91px;}
img.mrminss.mrninbigss{position:absolute;top:505px;width:159px;left:459px;}
.cimg{top:923px;position:absolute;left:102px;}
.mainno{top:935px;position:absolute;left:194px;font-weight:700;font-size:14px;}
.name{top:982px;position:absolute;left:105px;font-weight:600;font-size:9px;}
.mainno2{position:absolute;top:181px;left:347px;font-size:19px;font-weight:600;}
.signs{position:absolute;top:556px;left:250px;}
.names{position:absolute;top:249px;left:253px;font-size:15px;font-weight:500;}
.fathername{top:1009px;position:absolute;left:106px;font-weight:600;font-size:9px;}
.cimgs{position:absolute;top:525px;left:66px;}
.fathernames{position:absolute;top:314px;left:253px;font-size:15px;font-weight:500;}
.gender{position:absolute;top:443px;left:253px;font-size:15px;font-weight:500;}
.bod{top:1043px;position:absolute;left:105px;font-weight:600;font-size:9px;}
.bods{position:absolute;top:379px;left:253px;font-size:15px;font-weight:500;}
.sign{top:1035px;position:absolute;left:189px;}
.settext{position:absolute;font-size:9px;top:568px;left:631px;font-weight:bold;}
</style>
</head>
<body>
<main class="bg"> 
<img class="mrmins mrninbigs" src="<?php echo $qr; ?>&ecLevel=H&margin=5&size=5=&chld=L|0&chf=bg,s,FFFFFF00">
<img class="mrminss mrninbigss" src="<?php echo $qr; ?>&ecLevel=H&margin=5&size=5=&chld=L|0&chf=bg,s,FFFFFF00">
<img class="cimg" src="<?php echo $img; ?>" width="58px" height="56px">
<img class="cimgs" src="<?php echo $img; ?>" width="130px" height="130px">
<p class="mainno2"><?php echo $a3; ?></p>
<p class="mainno"><?php echo $a3; ?></p>
<p class="name"><?php echo $a1; ?></p>
<p class="names"><?php echo $a1; ?></p>
<p class="fathername"><?php echo $a2; ?></p>
<p class="fathernames"><?php echo $a2; ?></p>
<p class="gender"><?php echo $a4; ?></p>
<p class="bod"><?php echo $a5; ?></p>
<p class="bods"><?php echo $a5; ?></p>
<img class="sign" src="<?php echo $sign; ?>" width="90px" height="20px">
<img class="signs" src="<?php echo $sign; ?>" width="190px" height="50px">
<p class="settext">
Date: 2019.12.17 22:37:21 IST <br>
Reason : NSDL ePAN Sign <br>
Location : Mumbai
</p>
</main>
</body>
</html>
<script>
window.onload = function () {
    window.print();
};
</script>
