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

// --- 1. DATA INITIALIZATION ---
$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."); }

// --- 2. HELPER FUNCTIONS ---
function h($text) { return htmlspecialchars($text ?? '', ENT_QUOTES, 'UTF-8'); }

function en2bn($number) {
    $eng = ['0','1','2','3','4','5','6','7','8','9'];
    $bng = ['০','১','২','৩','৪','৫','৬','৭','৮','৯'];
    return str_replace($eng, $bng, $number);
}

function getBanglaDay($dob) {
    if (empty($dob) || $dob == '-') return '-';
    $days_en = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    $days_bn = ['রবিবার', 'সোমবার', 'মঙ্গলবার', 'বুধবার', 'বৃহস্পতিবার', 'শুক্রবার', 'শনিবার'];
    try {
        $day_en = date('l', strtotime($dob));
        $index = array_search($day_en, $days_en);
        return $index !== false ? $days_bn[$index] : '-';
    } catch (Exception $e) { return '-'; }
}

function calculateAgeDetails($dob) {
    if (empty($dob) || $dob == '-') return null;
    try {
        $birthDate = new DateTime($dob);
        $today = new DateTime();
        $diff = $today->diff($birthDate);
        return ['years' => $diff->y, 'months' => $diff->m, 'days' => $diff->d];
    } catch (Exception $e) { return null; }
}

// --- 3. PREPARE VARIABLES ---
$nationalId  = h($order['nationalId']);
$nameBn      = h($order['nameBn']);
$nameEn      = h($order['nameEn']);
$father      = h($order['father']);
$mother      = h($order['mother']);
$dob         = h($order['dateOfBirth']);
$bloodGroup  = h($order['bloodGroup']);
$gender      = h($order['gender']);
$photo       = !empty($order['photo_path']) ? h($order['photo_path']) : 'https://surokkha.gov.bd/favicon.png';

$birthDay = getBanglaDay($dob);
$ageData = calculateAgeDetails($dob);
$ageStr = ($ageData) ? en2bn($ageData['years']) . ' বছর, ' . en2bn($ageData['months']) . ' মাস, ' . en2bn($ageData['days']) . ' দিন' : '-';

$qrUrl = "https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=NID:" . $nationalId;
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NID_Official_Server_Copy_<?php echo $nationalId; ?></title>
    <link href="https://fonts.maateen.me/solaiman-lipi/font.css" rel="stylesheet">
    
    <style>
        :root {
            --ec-green: #006a4e;
            --ec-red: #da291c;
            --text-muted: #555;
        }

        body {
            margin: 0; padding: 0;
            background-color: #525659;
            font-family: 'SolaimanLipi', Arial, sans-serif;
            display: flex;
            justify-content: center;
            min-height: 100vh;
        }

        /* --- RESPONSIVE CONTAINER --- */
        .a4-page {
            width: 100%;
            max-width: 210mm; /* Strict max width for desktop */
            min-height: 297mm;
            background: #fff;
            padding: 40px 50px;
            box-sizing: border-box;
            position: relative;
            box-shadow: 0 0 15px rgba(0,0,0,0.5);
            margin: 20px auto;
            display: flex;
            flex-direction: column;
        }

        .watermark {
            position: absolute;
            top: 50%; left: 50%;
            transform: translate(-50%, -50%);
            width: 350px;
            opacity: 0.05;
            z-index: 0;
            pointer-events: none;
        }

        /* --- HEADER --- */
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 3px double var(--ec-green);
            padding-bottom: 10px;
            margin-bottom: 25px;
            z-index: 1; position: relative;
        }
        .header img { width: 70px; }
        .header-text { text-align: center; flex: 1; }
        .header-text h1 { margin: 0; font-size: 24px; color: var(--ec-red); font-weight: bold; }
        .header-text h2 { margin: 2px 0; font-size: 18px; color: var(--ec-green); font-weight: bold; }
        .header-text p { margin: 0; font-size: 13px; color: #333; font-weight: bold;}

        .title-bar {
            background-color: var(--ec-green) !important;
            color: white !important;
            text-align: center;
            padding: 8px;
            font-size: 18px;
            font-weight: bold;
            border-radius: 4px;
            margin-bottom: 25px;
            z-index: 1; position: relative;
            -webkit-print-color-adjust: exact;
            print-color-adjust: exact;
        }

        /* --- MAIN CONTENT LAYOUT --- */
        .main-content {
            display: flex;
            gap: 20px;
            z-index: 1; position: relative;
            align-items: flex-start;
        }

        /* TABLE STYLES */
        .data-table { flex: 1; border-collapse: collapse; width: 100%; }
        .data-table td {
            padding: 10px 5px;
            border-bottom: 1px dotted #ccc;
            font-size: 16px;
            color: #000;
            vertical-align: middle;
        }
        .label { width: 160px; color: #444; font-weight: normal; }
        .value { font-weight: bold; color: #000; }

        /* SIDEBAR STYLES */
        .sidebar { width: 140px; text-align: center; margin-top: 5px; }
        
        .photo-box {
            width: 110px; height: 135px;
            border: 1px solid #333;
            margin: 0 auto 15px;
            background: #fff;
            padding: 3px;
            border-radius: 0;
        }
        .photo-box img { width: 100%; height: 100%; object-fit: cover; border-radius: 0; }
        
        .qr-box img { width: 110px; height: 110px; border: 1px solid #eee; }

        .status-badge {
            margin-top: 15px;
            display: inline-block;
            border: 2px solid var(--ec-green);
            color: var(--ec-green);
            padding: 3px 10px;
            font-weight: bold;
            font-size: 13px;
            background: #fff;
            border-radius: 4px;
        }

        /* --- FOOTER --- */
        .footer {
            margin-top: auto; /* Pushes footer to bottom */
            position: absolute;
            bottom: 40px; left: 50px; right: 50px;
            text-align: center;
            z-index: 1;
        }
        .footer-line { border-top: 1px solid var(--ec-green); margin-bottom: 5px; }
        .legal-text { font-size: 14px; color: var(--ec-red); font-weight: bold; margin-bottom: 2px; line-height: 1.2; }
        .software-note { font-size: 11px; color: var(--text-muted); line-height: 1.2; font-style: italic; margin-bottom: 5px; }
        .ec-info {
            display: flex; justify-content: space-between;
            margin-top: 5px; font-size: 11px; color: #333; font-weight: bold;
            border-top: 1px dotted #ccc; padding-top: 5px;
        }

        /* --- PRINT BUTTON --- */
        .print-btn {
            position: fixed; bottom: 30px; right: 30px;
            background: var(--ec-red); color: #fff;
            padding: 12px 25px; border: none;
            border-radius: 50px; cursor: pointer;
            z-index: 9999; font-weight: bold;
            box-shadow: 0 4px 10px rgba(0,0,0,0.3);
            font-size: 16px;
        }

        /* --- MOBILE PREVIEW FIXES (SCREEN ONLY) --- */
        @media screen and (max-width: 768px) {
            .a4-page {
                margin: 0; 
                width: 100%;
                min-height: 100vh;
                padding: 20px; /* Less padding on mobile */
                box-shadow: none;
            }
            
            .header img { width: 50px; }
            .header-text h1 { font-size: 18px; }
            .header-text h2 { font-size: 14px; }
            
            .main-content {
                flex-direction: column-reverse; /* Stack: Photo Top, Info Bottom */
                align-items: center;
            }
            
            .sidebar {
                width: 100%;
                display: flex;
                flex-direction: column;
                align-items: center;
                margin-bottom: 20px;
                border-bottom: 1px dashed #ddd;
                padding-bottom: 15px;
            }
            
            .data-table td { font-size: 14px; padding: 8px 2px; }
            .label { width: 130px; }
            
            .footer {
                position: relative; /* Release footer from absolute on mobile */
                bottom: auto; left: auto; right: auto;
                margin-top: 40px;
            }
        }

        /* --- PRINT CONFIGURATION (STRICT A4) --- */
        @page { size: A4; margin: 0; }

        @media print {
            body { 
                background: white; 
                margin: 0; 
                display: block; 
            }
            .print-btn { display: none; }
            
            /* Force A4 Geometry */
            .a4-page {
                width: 210mm !important;
                height: 297mm !important;
                margin: 0 !important;
                padding: 40px 50px !important;
                box-shadow: none !important;
                border: none !important;
                /* Reset Flex for Print */
                display: block !important; 
            }
            
            /* Reset Header */
            .header img { width: 70px !important; }
            .header-text h1 { font-size: 24px !important; }
            .header-text h2 { font-size: 18px !important; }
            
            /* Reset Main Content Layout */
            .main-content {
                display: flex !important;
                flex-direction: row !important; /* Side-by-side */
                align-items: flex-start !important;
            }
            
            /* Reset Sidebar */
            .sidebar {
                width: 140px !important;
                display: block !important;
                border-bottom: none !important;
                margin-top: 5px !important;
            }
            
            /* Reset Table Fonts */
            .data-table td { font-size: 16px !important; }
            .label { width: 170px !important; }

            /* Reset Footer */
            .footer {
                position: absolute !important;
                bottom: 45px !important;
                left: 50px !important;
                right: 50px !important;
                margin-top: 0 !important;
            }
            
            /* Ensure Colors Print */
            * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
        }
    </style>
</head>
<body>

<button class="print-btn" onclick="window.print()">Download Server Copy</button>

<div class="a4-page">
    <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Government_Seal_of_Bangladesh.svg/1200px-Government_Seal_of_Bangladesh.svg.png" class="watermark" alt="Watermark">

    <div class="header">
        <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Government_Seal_of_Bangladesh.svg/1200px-Government_Seal_of_Bangladesh.svg.png" alt="Govt Logo">
        <div class="header-text">
            <h1>Bangladesh Election Commission</h1>
            <h2>National Identity Registration Wing</h2>
            <p>NID Service Portal - Server Copy</p>
        </div>
        <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTYLJtZGPIxJiyNtZqAp_ARQG91N2_hcyjitQ&s" alt="EC Logo">
    </div>

    <div class="title-bar">জাতীয় পরিচয়পত্রের তথ্য (নির্বাচন কমিশন ডাটাবেজ)</div>

    <div class="main-content">
        <div style="flex: 1; width: 100%;">
            <table class="data-table">
                <tr><td class="label">জাতীয় পরিচয় নম্বর</td><td class="value" style="color:var(--ec-green); font-size:20px;"><?php echo $nationalId; ?></td></tr>
                <tr><td class="label">নাম (বাংলা)</td><td class="value"><?php echo $nameBn; ?></td></tr>
                <tr><td class="label">Name (English)</td><td class="value" style="text-transform:uppercase;"><?php echo $nameEn; ?></td></tr>
                <tr><td class="label">পিতার নাম</td><td class="value"><?php echo $father; ?></td></tr>
                <tr><td class="label">মাতার নাম</td><td class="value"><?php echo $mother; ?></td></tr>
                <tr><td class="label">জন্ম তারিখ</td><td class="value"><?php echo $dob; ?></td></tr>
                <tr><td class="label">জাতীয়তা</td><td class="value">Bangladeshi</td></tr>
                <tr><td class="label">জন্মবার</td><td class="value"><?php echo $birthDay; ?></td></tr>
                <tr><td class="label">বর্তমান বয়স</td><td class="value"><?php echo $ageStr; ?></td></tr>
                <tr><td class="label">লিঙ্গ</td><td class="value"><?php echo $gender; ?></td></tr>
                <tr><td class="label">রক্তের গ্রুপ</td><td class="value" style="color:var(--ec-red);"><?php echo $bloodGroup; ?></td></tr>
            </table>
        </div>

        <div class="sidebar">
            <div class="photo-box">
                <img src="<?php echo $photo; ?>" alt="NID Photo">
            </div>
            <div class="qr-box">
                <img src="<?php echo $qrUrl; ?>" alt="QR">
            </div>
            <div class="status-badge">Status: Checked</div>
        </div>
    </div>

    <div class="footer">
        <div class="footer-line"></div>
        <div class="legal-text">
            জাতীয় পরিচয়পত্র সংশ্লিষ্ট, ভোটার তালিকার সাথে সরাসরি সম্পর্কযুক্ত নয়।
        </div>
        <div class="software-note">
            This is Software Generated Report From Bangladesh Election Commission, Signature &amp; Seal Aren't Required.
        </div>
        <div class="ec-info">
            <div>Verification Date: <?php echo date('d-m-Y'); ?></div>
            <div>www.nidw.gov.bd</div>
        </div>
    </div>
</div>

</body>
</html>