<?php
include('../../includes/database.php');
error_reporting(0);

// Function to fix double-encoded UTF-8 text (Hindi garbled as à¤ªà¤¤à¤¾)
function fixEncoding($text) {
    if (empty($text)) return $text;
    // Check if text contains double-encoded UTF-8 patterns (Latin-1 chars like Ã, Â)
    if (preg_match('/[\xC3][\x80-\xBF]/', $text)) {
        $fixed = @mb_convert_encoding($text, 'ISO-8859-1', 'UTF-8');
        if ($fixed !== false && mb_check_encoding($fixed, 'UTF-8')) {
            return $fixed;
        }
    }
    return $text;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="UTF-8">
<title>Aadhaar Card Preview</title>
<link href="https://fonts.googleapis.com/css2?family=Hind:wght@300;400;500;600;700&family=Noto+Sans+Devanagari:wght@400;500;600;700&family=Noto+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
<link href="aadhar.css" type="text/css" rel="stylesheet">
<?php
if (isset($_GET['searchid'])) {
    $searchid = mysqli_real_escape_string($conn, $_GET['searchid']);
    mysqli_set_charset($conn, "utf8mb4");
    $a = mysqli_query($conn, "SELECT * FROM aadharauto WHERE aadharautoid='" . $searchid . "'");
    $b = mysqli_fetch_array($a);

    // Fix double-encoded Hindi/local language fields
    $b['localname'] = fixEncoding($b['localname']);
    $b['localaddress'] = fixEncoding($b['localaddress']);
    $b['sexinlocal'] = fixEncoding($b['sexinlocal']);
    $b['dobinlocal'] = fixEncoding($b['dobinlocal']);
    $b['pata'] = fixEncoding($b['pata']);
    $b['fulladdress'] = fixEncoding($b['fulladdress']);
}
?>
<style type="text/css">
.bg {
    background: url('<?php echo $b['locallanguage'] . '.jpg'; ?>') no-repeat;
    width: 800px;
    height: 1000px;
    overflow: visible;
    display: block;
    background-size: 100% auto;
}
</style>

<?php if ($b['locallanguage'] == 'TE' || $b['locallanguage'] == 'GU' || $b['locallanguage'] == 'TA' || $b['locallanguage'] == 'KN' || $b['locallanguage'] == 'BN') { ?>
<style>
.btopsec { padding-left: 0px; padding-top: 55px; position: relative; top: -22px !important; left: 24px; width: 231px; }
.btopthird { text-align: center; padding-top: 31px; padding-left: 0px; }
</style>
<?php }

if ($b['locallanguage'] == 'HI') { ?>
<style>.btopthird img { top: 12px !important; }</style>
<?php }

if ($b['locallanguage'] == 'PA' || $b['locallanguage'] == 'OR') { ?>
<style>.btopthird img { width: 133px; height: 130px; position: relative; left: -6px; top: 9px; }</style>
<?php }

if ($b['locallanguage'] == 'MR') { ?>
<style>
.btopsec { left: -37px !important; }
.btopthird img { width: 133px; height: 130px; position: relative; left: -77px !important; top: 10px !important; }
.bpart { padding-left: 20px; position: absolute; bottom: 129px; width: 783px; top: 691px; }
p.download-date { transform: rotate(90deg); position: absolute; font-size: 9px; top: 338px; font-weight: 400; left: 12px; }
.enroll { position: absolute; top: 270px !important; left: 246px !important; font-size: 14px; font-weight: 600; }
table.upperpart { position: absolute; top: 295px; left: 84px; font-weight: 400; width: 175px; }
span.addharnopan.addharnopan2 { margin-top: -99px; }
p.download-date.ddate2 { top: 79px !important; left: -36px !important; }
.addharnopan1 { margin-top: -98px !important; }
p.download-date.idate2 { top: 87px !important; }
p.v1 { top: 623px !important; }
p.v2 { top: -78px !important; }
p.v3 { top: -88px !important; }
.upperaddhar { top: 588px !important; }
img.mrmin.mrninbig { top: 526px; position: absolute; left: 222px; width: 144px; }
.idate1 { left: 22px !important; top: 468px !important; }
</style>
<?php } ?>
</head>
<body>
<center>
<main class="bg" id="content">
    <div class="enroll"><?php if ($b['eno'] == '') { ?>1429/70044/0020<?php echo rand(1, 9); } else { echo $b['eno']; } ?></div>
    <table class="upperpart">
        <tbody>
            <tr><td>To</td></tr>
            <tr><td><?php echo $b['localname']; ?></td></tr>
            <tr><td><?php echo $b['aadharname']; ?></td></tr>
            <tr><td><?php echo $b['fulladdress']; ?></td></tr>
            <tr><td class="address"><?php echo $b['houseno']; ?><br><?php echo $b['street']; ?><br><?php echo $b['vtcandpost']; ?><br><?php echo $b['dist'] . ' ' . $b['statename'] . ' - ' . $b['pincode']; ?></td></tr>
        </tbody>
    </table>

<?php
if ($b['gender'] == 'MALE') { $sex = 'M'; } else { $sex = 'F'; }

libxml_use_internal_errors(true);
$simplexml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><books/>');
$book1 = $simplexml->addChild('book', "<PrintLetterBarcodeData uid=&quot;" . $b['aadharno'] . "&quot; name=&quot;" . $b['aadharname'] . "&quot; gender=&quot;" . $sex . "&quot; dob=&quot;" . $b['dob'] . "&quot; co=&quot;S/O: " . $b['fathername'] . "&quot; house=&quote;" . $b['houseno'] . "&quot; street=&quote;" . $b['street'] . "&quot; vtc=&quote;" . $b['vtcandpost'] . "&quot; po=&quot;" . $b['vtcandpost'] . "&quot; dist=&quot;" . $b['dist'] . "&quot; state=&quot;" . $b['statename'] . "&quot; pc=&quot;" . $b['pincode'] . "&quot;/>");
$str = '<?xml version="1.0" encoding="UTF-8"?>' . $book1;
$codeContents = $str;
?>

    <p class="upperaddhar" style="top:673px; position:absolute; left:127px; letter-spacing:2px; font-size:20px; font-weight:700;"><?php echo $b['originalaadharno']; ?></p>
    <p class="v1" style="top:707px; font-size:10px; position:absolute; left:127px; letter-spacing:1px; font-weight:900; display:none;">
        <?php $vid = chunk_split(mt_rand(1111111111111111, 9999999999999999), 4, ' '); echo 'VID : ' . $vid; ?>
    </p>
    <p class="download-date">Download Date: <?php echo date("d/m/Y"); ?></p>
    <p class="download-date idate1" style="top:535px; left:20px;">Issue Date: <?php echo date("d/m/Y"); ?></p>

    <img class="mrmin mrninbig" src='https://api.qrserver.com/v1/create-qr-code/?size=75x75&amp;data=<?php echo $codeContents; ?>&chld=L|0&chf=bg,s,FFFFFF00'>

    <table class="bpart">
        <thead>
            <tr>
                <td>
                    <p class="download-date ddate2" style="top:82px; left:-48px;">Download Date: <?php echo date("d/m/Y"); ?></p>
                    <p class="download-date idate2" style="top:87px; left:268px;">Issue Date: <?php echo date("d/m/Y"); ?></p>
                </td>
                <td width=""><img src="<?php echo $b['imagepathoriginal']; ?>" width="80" height="80" class="imgm" style="margin-top:40px; margin-left:11px;"></td>
                <td width="269" class="bpartone" style="padding-top:41px;">
                    <p style="margin:0px"><?php echo $b['localname']; ?><br /><?php echo $b['aadharname']; ?></p>
                    <p style="margin:0px"><span class="dob"><?php echo $b['dobinlocal'] . ' / DOB : ' . $b['dob']; ?></span></p>
                    <p style="margin:0px"><?php echo $b['sexinlocal'] . ' / ' . $b['gender']; ?></p>
                </td>
                <td width="245" class="btopsec">
                    <strong><?php echo $b['pata']; ?>:</strong><br />
                    <span class="maxheight" style="margin-bottom:7px;"><?php echo $b['localaddress']; ?></span>
                    <br /><strong>Address:</strong><br />
                    <span class="maxheight"><?php echo $b['fulladdress']; ?><br /></span>
                </td>
                <td class="btopthird"><img src='https://quickchart.io/qr?size=200&text=<?php echo $codeContents; ?>&chld=L|0&chf=bg,s,FFFFFF00'></td>
            </tr>
        </thead>
    </table>
    <table class="bpart-bottom">
        <tr>
            <td class="cpartfirst"><span class="addharnopan addharnopan1" style="margin-top:-10px; margin-left:73px; font-weight:700;"><?php echo $b['originalaadharno']; ?></span>
                <p class="v2" style="top:10px; font-size:10px; position:absolute; left:132px; letter-spacing:1px; font-weight:900; display:none;"><?php echo 'VID : ' . $vid; ?></p>
            </td>
            <td class="paddingbtm insiderelative"><td>
            <td class="cpartthird"><span class="addharnopan addharnopan2" style="font-weight:700;"><?php echo $b['originalaadharno']; ?></span>
                <p class="v3" style="top:-4px; font-size:10px; margin-left:94px; position:relative; letter-spacing:1px; font-weight:900; display:none;"><?php echo 'VID : ' . $vid; ?></p>
            </td>
        </tr>
    </table>
</main>
</center>
<script>window.print();</script>
</body>
</html>
