<?php
require 'config.php';
require_login();

// Get the order ID and fetch the data from YOUR database
$order_id = (int)($_GET['id'] ?? 0);
if ($order_id <= 0) { die("Invalid Order ID."); }

$stmt = $pdo->prepare("SELECT * FROM server_copy WHERE id = ? AND user_id = ?");
$stmt->execute([$order_id, $_SESSION['user_id']]);
$order = $stmt->fetch();
if (!$order) { die("Order not found."); }

// Prepare data variables
$nameEn = h($order['nameEn'] ?? 'N/A');
$nationalId = h($order['nationalId'] ?? 'N/A');
$dob = h($order['dateOfBirth'] ?? 'N/A');
$qrData = urlencode("$nameEn $nationalId $dob");
$qrUrl = "https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=$qrData";

// Helper function to escape output
function h($text) {
    return htmlspecialchars($text ?? '', ENT_QUOTES, 'UTF-8');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title><?php echo h($order['nameEn']); ?></title>
    <link href="https://surokkha.gov.bd/favicon.png" rel="icon">
    <link href="https://surokkha.gov.bd/favicon.png" rel="apple-touch-icon">
    <link rel="stylesheet" href="https://site-assets.fontawesome.com/releases/v6.1.1/css/all.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" type="text/javascript"></script>
    <style>
        @import url('https://fonts.maateen.me/solaiman-lipi/font.css');
        @page {
            size: A4;
            margin: auto;
        }
        body {
            margin: 0;
            font-family: Solaimanlipi;
           
        }
        .background {
            background-color: lightgrey;
            position: relative;
            width: 750px;
            height: 1065px;
            margin: auto;
        }
        .crane {
            max-width: 100%;
            height: 100%;
        }
        .topTitle {
            position: absolute;
            left: 21%;
            top: 8%;
            width: auto;
            font-size: 42px;
            color: rgb(255, 182, 47);
        }
        #loadMe {
            visibility: hidden;
        }
        @media print {
            html, body {
                width: 210mm !important;
                height: 297mm !important;
                background-color: #fff !important;
            }
            .print {
                display: none !important;
            }
        }
        #print {
            background: linear-gradient(45deg, #03a9f4, #1e88e5);
            padding: 10px 20px;
            width: auto;
            height: auto;
            border: none;
            font-size: 20px;
            font-weight: bold;
            cursor: pointer;
            box-shadow: 2px 5px 10px rgba(0, 0, 0, 0.2);
            color: #fff;
            border-radius: 25px;
            margin: 25px;
            display: none;
            text-transform: uppercase;
            transition: all 0.3s ease-in-out;
            letter-spacing: 1px;
        }

        #print:hover {
            background: linear-gradient(45deg, #1e88e5, #03a9f4);
            transform: translateY(-5px) scale(1.05);
            box-shadow: 2px 8px 15px rgba(0, 0, 0, 0.3);
        }

        #print:active {
            transform: translateY(0) scale(0.95);
            box-shadow: 2px 4px 8px rgba(0, 0, 0, 0.3);
        }
        
        #present_addr, #permanent_addr {
            text-align: left;
        }
    </style>
</head>
<body onload="showprint()" style="text-align: center;">
    <div class="background">
        <img class="crane" src="https://raw.githubusercontent.com/matherofsetuxyz/hfhtf/main/cbimage.png" height="1000px" width="750px">

        <!-- Header Section -->
        <div style="position: absolute; left: 30%; top: 8%; width: auto; font-size: 16px; color: rgb(255 224 0);"><b>National Identity Registration Wing (NIDW)</b></div>
        <div style="position: absolute; left: 37%; top: 11%; width: auto; font-size: 14px; color: rgb(255, 47, 161);"><b>Select Your Search Category</b></div>
        <div style="position: absolute; left: 45%; top: 12.8%; width: auto; font-size: 12px; color: rgb(8, 121, 4);">Search By NID / Voter No.</div>
        <div style="position: absolute; left: 45%; top: 14.3%; width: auto; font-size: 12px; color: rgb(7, 119, 184);">Search By Form No.</div>
        <div style="position: absolute; left: 30%; top: 16.9%; width: auto; font-size: 12px; color: rgb(252, 0, 0);"><b>NID or Voter No*</b></div>
        <div style="position: absolute; left: 45%; top: 16.9%; width: auto; font-size: 12px; color: rgb(143, 143, 143);"><?php echo h($order['nationalId']); ?></div>
        <div style="position: absolute; left: 62.9%; top: 17.1%; width: auto; font-size: 11px; color: rgb(255 255 255);">Submit</div>
        <div style="position: absolute; left: 89%; top: 11.55%; width: auto; font-size: 11px; color: #fff;">Home</div>

        <!-- National Identity Information -->
        <div style="position: absolute; left: 37%; top: 27%; width: auto; font-size: 16px; color: rgb(7, 7, 7);"><b>জাতীয় পরিচিতি তথ্য</b></div>
        <div style="position: absolute; left: 37%; top: 29.7%; width: auto; font-size: 13px; color: rgb(7, 7, 7);">জাতীয় পরিচয় পত্র নম্বর</div>
        <div id="nid_no" style="position: absolute; left: 55%; top: 29.7%; width: auto; font-size: 14px; color: rgb(7, 7, 7);"><?php echo h($order['nationalId']); ?></div>
        <div style="position: absolute; left: 37%; top: 32.5%; width: auto; font-size: 13px; color: rgb(7, 7, 7);">অবস্থান</div>
        <div id="nid_father" style="position: absolute; left: 55%; top: 32.5%; width: auto; font-size: 14px; color: rgb(7, 7, 7);"><?php echo h($order['birthPlace']); ?> জেলা </div>
        <div style="position: absolute; left: 37%; top: 35%; width: auto; font-size: 13px; color: rgb(7, 7, 7);">জেলা কোড</div>
        <div id="from_number" style="position: absolute; left: 55%; top: 35%; width: auto; font-size: 14px; color: rgb(7, 7, 7);">26</div>
        <div style="position: absolute; left: 37%; top: 37.5%; width: auto; font-size: 14px; color: rgb(7, 7, 7);">জন্মদিন</div>
        <div id="spouse" style="position: absolute; left: 55%; top: 37.5%; width: auto; font-size: 14px; color: rgb(7, 7, 7);"><?php echo h($order['day']); ?></div>
        <div style="position: absolute; left: 37%; top: 40.2%; width: auto; font-size: 14px; color: rgb(7, 7, 7);">ভোটার  এলাকা  </div>
        <div id="voter_area_code" style="position: absolute; left: 55%; top: 40.2%; width: auto; font-size: 14px; color: rgb(7, 7, 7);"><?php echo h($order['voterArea']); ?></div>

        <!-- Personal Information -->
        <div style="position: absolute; left: 37%; top: 43%; width: auto; font-size: 16px; color: rgb(7, 7, 7);"><b>ব্যক্তিগত তথ্য</b></div>
        <div style="position: absolute; left: 37%; top: 45.6%; width: auto; font-size: 14px; color: rgb(7, 7, 7);">নাম (বাংলা)</div>
        <div id="name_bn" style="position: absolute; font-weight: bold; left: 55%; top: 45.6%; width: auto; font-size: 14px; color: rgb(7, 7, 7);"><b><?php echo h($order['nameBn']); ?></b></div>
        <div style="position: absolute; left: 37%; top: 48.5%; width: auto; font-size: 14px; color: rgb(7, 7, 7);">নাম (ইংরেজি)</div>
        <div id="name_en" style="position: absolute; left: 55%; top:48.5%; width: auto; font-size: 14px; color: rgb(7, 7, 7);"><?php echo h($order['nameEn']); ?></div>
        <div style="position: absolute; left: 37%; top: 51%; width: auto; font-size: 14px; color: rgb(7, 7, 7);">জন্ম তারিখ</div>
        <div id="dob" style="position: absolute; left: 55%; top: 51%; width: auto; font-size: 14px; color: rgb(7, 7, 7);"><?php echo h($order['dateOfBirth']); ?></div>
        <div style="position: absolute; left: 37%; top: 53.7%; width: auto; font-size: 14px; color: rgb(7, 7, 7);">পিতার নাম</div>
        <div id="fathers_name" style="position: absolute; left: 55%; top: 53.7%; width: auto; font-size: 14px; color: rgb(7, 7, 7);"><?php echo h($order['father']); ?></div>
        <div style="position: absolute; left: 37%; top: 56.2%; width: auto; font-size: 14px; color: rgb(7, 7, 7);">মাতার নাম</div>
        <div id="mothers_name" style="position: absolute; left: 55%; top: 56.2%; width: auto; font-size: 14px; color: rgb(7, 7, 7);"><?php echo h($order['mother']); ?></div>

        <!-- Other Information -->
        <div style="position: absolute; left: 37%; top: 59%; width: auto; font-size: 16px; color: rgb(7, 7, 7);"><b>অন্যান্য তথ্য</b></div>
        <div style="position: absolute; left: 37%; top: 62.2%; width: auto; font-size: 14px; color: rgb(7, 7, 7);">লিঙ্গ</div>
        <div id="gender" style="position: absolute; left: 55%; top: 62.2%; width: auto; font-size: 14px; color: rgb(7, 7, 7);"><?php echo h($order['gender']); ?></div>
        <div style="position: absolute; left: 37%; top: 64.8%; width: auto; font-size: 14px; color: rgb(7, 7, 7);">জন্মস্থান</div>
        <div id="birth_place" style="position: absolute; left: 55%; top: 64.8%; width: auto; font-size: 14px; color: rgb(7, 7, 7);"><?php echo h($order['birthPlace']); ?></div>
        <div style="position: absolute; left: 37%; top: 67.5%; width: auto; font-size: 14px; color: rgb(7, 7, 7);">জাতীয়তা</div>
        <div id="blood_group" style="position: absolute; left: 55%; top: 67.5%; width: auto; font-size: 14px; color: rgb(7, 7, 7);">বাংলাদেশী</div>
        <div style="position: absolute; left: 37%; top: 70%; width: auto; font-size: 14px; color: rgb(7, 7, 7);">বয়স</div>
        <div id="religion" style="position: absolute; left: 55%; top: 70.3%; width: auto; font-size: 14px; color: rgb(7, 7, 7);"><?php echo h($order['age']); ?></div>

        <!-- Address Information -->
        <div style="position: absolute; left: 37%; top: 73%; width: auto; font-size: 16px; color: rgb(7, 7, 7);"><b>বর্তমান ঠিকানা</b></div>
        <div id="present_addr" style="position: absolute; left: 37%; top: 75.5%; width: 48%; font-size: 12px; color: rgb(7, 7, 7);"><?php echo h($order['preAddress']); ?></div>
        <div style="position: absolute; left: 37%; top: 82%; width: auto; font-size: 16px; color: rgb(7, 7, 7);"><b>স্থায়ী ঠিকানা</b></div>
        <div id="permanent_addr" style="position: absolute; left: 37%; top: 84.5%; width: 48%; font-size: 12px; color: rgb(7, 7, 7);"><?php echo h($order['perAddress']); ?></div>

        <!-- Footer Section -->
        <div style="position: absolute; top: 92%; width: 100%; font-size: 12px; text-align: center; color: rgb(255, 0, 0);">উপরে প্রদর্শিত তথ্যসমূহ জাতীয় পরিচয়পত্র সংশ্লিষ্ট, ভোটার তালিকার সাথে সরাসরি সম্পর্কযুক্ত নয়।</div>
        <div style="position: absolute; top: 93.5%; width: 100%; text-align: center; font-size: 12px; color: rgb(3, 3, 3);">This is Software Generated Report From Bangladesh Election Commission, Signature &amp; Seal Aren't Required.</div>

        <!-- Photo Section -->
        <div style="position: absolute; left: 16%; top: 25.7%; width: auto; font-size: 12px; color: rgb(3, 3, 3);">
            <img id="photo" src="<?php echo h($order['photo_path']); ?>" height="140px" width="121px" style="border-radius: 10px" />
        </div>

        <!-- QR Code Section -->
        <div id="name_en2" style="position: absolute; font-weight: bold; left: 15.5%; top: 44.0%; height: 32px; width: 130px; font-size: 13px; color: rgb(7, 7, 7); margin: auto; align-items: center;" align="center">
            <img id="qr" src="<?php echo $qrUrl; ?>" height="100px" width="100px">
        </div>
        
        <!-- name title section -->
        <div id="name_en2" style="position: absolute;display: flex;font-weight: bold;left: 15.5%;top: 39.6%;height: 32px;width: 130px;font-size: 13px;color: rgb(7, 7, 7);margin: auto;align-items: center;" align="center">
            <div style="flex: 1;"><?php echo h($order['nameEn']); ?></div>
        </div>

        <!-- Print Button -->
        <button class="print" id="print" onclick="window.print()">SAVE</button>
    </div>

    <script>
        function showprint() {
            $("#print").show(500);
        }
        window.onload = function() { 
            showprint(); 
            setTimeout(function() { 
                window.print(); 
            }, 1000); 
        }
    </script>
</body>
</html>