<?php
ob_start();

require_once("../../config/function.php");

/* Validate record ID */
$encodedData = $_GET['M0peiXzhJkM0eiXzhJkR'] ?? '';
$q = base64_decode($encodedData, true);

if ($q === false || !ctype_digit((string)$q)) {
    die('Invalid record ID.');
}

$data_fetch = db_fetch_array(
    "printRecords",
    "id='" . (int)$q . "'"
);

if (empty($data_fetch['printData'])) {
    die('Record not found.');
}

$decodedPrintData = base64_decode($data_fetch['printData'], true);
$b = json_decode($decodedPrintData, true);

if (!is_array($b)) {
    die('Invalid print data.');
}

/* Applicant data */
$a1 = strtoupper(trim($b['name'] ?? ''));
$a2 = strtoupper(trim($b['fathername'] ?? ''));
$a3 = strtoupper(trim($b['panNumber'] ?? ''));
$a4 = trim($b['gender'] ?? '');
$a5 = str_replace('-', '/', trim($b['dob'] ?? ''));

$img  = $b['imgdata'] ?? '';
$sign = $b['signdata'] ?? '';

/* Extra text and number */
$qrTopText   = 'RAPID X SERVICES';
$qrNumber    = $a3;

/* XML-safe values */
$xmlName = htmlspecialchars(
    $a1,
    ENT_QUOTES | ENT_XML1,
    'UTF-8'
);

$xmlFatherName = htmlspecialchars(
    $a2,
    ENT_QUOTES | ENT_XML1,
    'UTF-8'
);

$xmlNumber = htmlspecialchars(
    $a3,
    ENT_QUOTES | ENT_XML1,
    'UTF-8'
);

$xmlGender = htmlspecialchars(
    $a4,
    ENT_QUOTES | ENT_XML1,
    'UTF-8'
);

$xmlDob = htmlspecialchars(
    $a5,
    ENT_QUOTES | ENT_XML1,
    'UTF-8'
);

$xmlTopText = htmlspecialchars(
    $qrTopText,
    ENT_QUOTES | ENT_XML1,
    'UTF-8'
);

/* Generate automatic 18-digit number */
$autoNumber = '';

for ($i = 0; $i < 18; $i++) {
    $autoNumber .= ($i === 0)
        ? random_int(1, 9)
        : random_int(0, 9);
}

/* Generate automatic 1000-character text */
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ';
$autoText = '';

for ($i = 0; $i < 1000; $i++) {
    $autoText .= $characters[random_int(0, strlen($characters) - 1)];
}

/* XML-safe values */
$xmlAutoNumber = htmlspecialchars(
    $autoNumber,
    ENT_QUOTES | ENT_XML1,
    'UTF-8'
);

$xmlAutoText = htmlspecialchars(
    $autoText,
    ENT_QUOTES | ENT_XML1,
    'UTF-8'
);

/* QR content */
$qrData = '<?xml version="1.0" encoding="UTF-8"?>'
    . '<PrintLetterBarcodeData '
    . 'uid="' . $xmlNumber . '" '
    . 'name="' . $xmlName . '" '
    . 'fathername="' . $xmlFatherName . '" '
    . 'dob="' . $xmlDob . '" '
    . 'gender="' . $xmlGender . '" '
    . 'co="' . $xmlName . '" '
    . 'text="' . $xmlTopText . '" '
    . 'number="' . $xmlNumber . '" '
    . 'encripted-data="' . $xmlAutoNumber . '" '
    . 'autotext="' . $xmlAutoText . '"'
    . '/>';

/* QR image URL */
$qr = 'https://api.qrserver.com/v1/create-qr-code/'
    . '?size=600x600'
    . '&ecc=L'
    . '&margin=5'
    . '&format=png'
    . '&data=' . rawurlencode($qrData);
    
    $randomNumber = str_pad(
    (string) random_int(1, 99999999999999),
    14,
    '0',
    STR_PAD_LEFT
);

// Example: 20260705145314
$systemDateTime = date('YmdHis');

// Example: 09072026171050
$downloadDateTime = date('dmYHis');

$formattedAadhaar = 'ePan_'
    . $randomNumber
    . $systemDateTime
    . '_'
    . $downloadDateTime;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?= htmlspecialchars($formattedAadhaar, ENT_QUOTES, 'UTF-8') ?></title>
<link href="https://fonts.googleapis.com/css?family=Hind" rel="stylesheet">
<link href="pan.css" rel="stylesheet">
<style>
.bg{background:url('logs-iuyutuy765465875.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>
