<?php
include('../includes/config.php');
if (!isset($_SESSION['loggedin'])) { header("Location: ../index"); exit; }

$reg_no = isset($_GET['reg_no']) ? mysqli_real_escape_string($conn, $_GET['reg_no']) : '';
if (empty($reg_no)) { die("Registration number required"); }

$sql = "SELECT * FROM delhi_birth WHERE registration_no='$reg_no'";
$result = mysqli_query($conn, $sql);
if (!$result || mysqli_num_rows($result) == 0) { die("Record not found"); }

$row = mysqli_fetch_assoc($result);

$name = $row['name'];
$dob = $row['date_of_birth'];
$date_of_registration = $row['date_of_registration'];
$present_address = $row['present_address'];
$permanent_address = $row['permanent_address'];
$date_of_issue = $row['date_of_issue'];

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Birth Certificate - <?php echo $name; ?></title>
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+Devanagari:wght@400;500;600;700&display=swap" rel="stylesheet">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: 'Times New Roman', Times, serif; background: #e0e0e0; padding: 20px; }

.action-buttons {
    text-align: center; margin: 20px 0; padding: 15px 0;
    position: relative; z-index: 999; background: #e0e0e0;
}
.btn {
    padding: 10px 22px; border: none; border-radius: 6px; cursor: pointer;
    text-decoration: none; display: inline-block; margin: 5px; font-size: 14px;
    font-weight: 600; color: #fff; transition: all 0.3s;
}
.btn-download { background: #27ae60; }
.btn-download:hover { background: #219a52; }
.btn-print { background: #2980b9; }
.btn-print:hover { background: #2472a4; }
.btn-back { background: #7f8c8d; }
.btn-back:hover { background: #6c7a7b; }

.certificate {
    width: 210mm; min-height: 297mm; margin: 0 auto 30px auto;
    background: #fff; position: relative; overflow: hidden;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3); padding: 3mm; display: block;
}
.certificate-border {
    border: 8px solid #00008B; padding: 6mm 7mm 6mm 7mm;
    position: relative; min-height: 290mm;
}
.certificate-inner { position: relative; }
.watermark {
    position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
    width: 500px; height: 500px; opacity: 0.10; z-index: 0; pointer-events: none;
}

.header { display: flex; align-items: flex-start; justify-content: space-between; position: relative; z-index: 1; margin-bottom: 5px; margin-top: -2mm; }
.logo-left { width: 100px; flex-shrink: 0; padding-top: 35px; }
.logo-left img { width: 120px; height: 120px; object-fit: contain; }
.header-center { flex: 1; text-align: center; padding: 0 5px; }
.header-center .ashoka-emblem { width: 70px; height: 78px; object-fit: contain; margin-bottom: 2px; margin-top: -2px; }
.header-center .header-text-img { width: 100%; max-width: 380px; height: auto; object-fit: contain; display: inline-block; }
.logo-right { width: 100px; flex-shrink: 0; text-align: center; padding-top: 38px; }
.logo-right .delhi-symbol { width: 115px; height: 140px; object-fit: contain; display: block; margin: 0 auto; }
.logo-right .right-reg-no { font-size: 13px; font-weight: bold; color: #000; margin-top: 2px; text-align: center; white-space: nowrap; }

.form-no { font-size: 10px; color: #000; font-family: 'Noto Sans Devanagari', serif; }
.hindi-govt { font-size: 11px; font-weight: 600; color: #000; font-family: 'Noto Sans Devanagari', serif; }
.english-govt { font-size: 12px; font-weight: bold; color: #000; }
.mcd-hindi { font-size: 20px; font-weight: 700; color: #000; font-family: 'Noto Sans Devanagari', serif; margin: 2px 0; }
.mcd-english { font-size: 14px; font-weight: bold; color: #000; letter-spacing: 0.5px; }
.certificate-title { font-size: 13px; font-weight: 700; color: #008000; font-family: 'Noto Sans Devanagari', serif; margin-top: 2px; }

.legal-text { font-size: 11px; line-height: 1.55; color: #000; margin: 10px 0; position: relative; z-index: 1; text-align: justify; }

.details-table { width: 100%; border-collapse: collapse; position: relative; z-index: 1; margin-top: 5px; }
.details-table td { padding: 7px 4px; vertical-align: top; font-size: 12px; line-height: 1.4; }
.label-cell { width: 38%; color: #000; font-family: 'Noto Sans Devanagari', 'Times New Roman', serif; font-size: 12px; }
.value-cell { width: 62%; color: #000; font-weight: 500; font-size: 12px; }

.qr-section { margin-top: 15px; position: relative; z-index: 1; text-align: center; }
.qr-large { width: 200px; height: 200px; display: inline-block; }
.note-text { font-size: 9.5px; font-weight: bold; color: #000; margin-top: 10px; position: relative; z-index: 1; line-height: 1.45; }
.footer-text { text-align: center; color: #000; font-weight: bold; margin-top: 12px; position: relative; z-index: 1; padding-bottom: 5px; }
.footer-hindi { font-size: 12px; font-family: 'Noto Sans Devanagari', serif; }
.footer-english { font-size: 10px; letter-spacing: 0.5px; }

@media print {
    @page { size: A4 portrait; margin: 0; }
    * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
    .action-buttons { display: none !important; }
    body { background: white; margin: 0; padding: 0; width: 210mm; height: 297mm; }
    .certificate { box-shadow: none; margin: 0; padding: 7mm; width: 210mm; height: 297mm; }
    .certificate-border { border: 4px solid #00008B !important; }
    .watermark { opacity: 0.09 !important; }
}
</style>
</head>
<body>

<div class="action-buttons">
    <button type="button" class="btn btn-print" onclick="window.print();">&#128424; Print</button>
    <a href="dehli_birth_pdf?reg_no=<?php echo urlencode($row['registration_no']); ?>" class="btn btn-download" target="_blank">&#128190; Download PDF</a>
    <button type="button" class="btn btn-back" onclick="window.history.back();">&#8592; Back</button>
</div>

<div class="certificate">
<div class="certificate-border">
    <div class="certificate-inner">

        <img src="../assets/images/mcd_watermark.png" class="watermark" alt="">

        <div class="header">
            <div class="logo-left">
                <img src="../assets/images/mcd_logo.png" alt="MCD Logo">
            </div>
            <div class="header-center">
                <img src="../assets/images/ashoka_emblem.png" class="ashoka-emblem" alt=""><br>
                <?php if(file_exists('../assets/images/header_text.png')): ?>
                <img src="../assets/images/header_text.png" class="header-text-img" alt="Header Text">
                <?php else: ?>
                <span class="form-no">फॉर्म संख्या / Form No. 5</span><br>
                <span class="hindi-govt">राष्ट्रीय राजधानी क्षेत्र, दिल्ली सरकार</span><br>
                <span class="english-govt">Govt. of National Capital Territory of Delhi</span><br>
                <span class="mcd-hindi">दिल्ली नगर निगम</span><br>
                <span class="mcd-english">MUNICIPAL CORPORATION OF DELHI</span><br>
                <span class="certificate-title">जन्म प्रमाण पत्र / Birth Certificate</span>
                <?php endif; ?>
            </div>
            <div class="logo-right">
                <img src="../assets/images/state1.png" class="delhi-symbol" alt="">
                <div class="right-reg-no"><?php echo preg_replace('/^MCDOLIR-/', '', $row['registration_no']); ?></div>
            </div>
        </div>

        <div class="legal-text">
            (Issued under section 17 of the Registration of Birth and Death Act, 1969 and 8/13 of Delhi Registration of Birth Rule,1999)
            <br><br>
            This is to certify that the following information has been taken from the original record of BIRTH which is the register for Municipal Corporation Of Delhi of <?php echo strtoupper($row['place_of_birth']); ?> of N.C.T. Delhi
        </div>

        <table class="details-table">
            <tr><td class="label-cell">नाम / Name</td><td class="value-cell"><?php echo strtoupper($name); ?></td></tr>
            <tr><td class="label-cell">लिंग / Gender</td><td class="value-cell"><?php echo strtoupper($row['gender']); ?></td></tr>
            <tr><td class="label-cell">जन्म की तिथि / Date Of Birth</td><td class="value-cell"><?php echo date('d/m/Y', strtotime($dob)); ?></td></tr>
            <tr><td class="label-cell">जन्म का स्थान / Place Of Birth</td><td class="value-cell"><?php echo strtoupper($row['place_of_birth']); ?></td></tr>
            <tr><td class="label-cell">पंजीकरण की तिथि / Date Of Registration</td><td class="value-cell"><?php echo date('d/m/Y', strtotime($date_of_registration)); ?></td></tr>
            <tr><td class="label-cell">पंजीकरण संख्या / Registration No</td><td class="value-cell"><?php echo $row['registration_no']; ?></td></tr>
            <tr><td class="label-cell">माता का नाम / Name of Mother</td><td class="value-cell"><?php echo strtoupper($row['mother_name']); ?></td></tr>
            <tr><td class="label-cell">पिता का नाम / Name of Father</td><td class="value-cell"><?php echo strtoupper($row['father_name']); ?></td></tr>
            <tr><td class="label-cell">वर्तमान पता / Present Address</td><td class="value-cell"><?php echo strtoupper($present_address); ?></td></tr>
            <tr><td class="label-cell">स्थाई पता / Permanent Address</td><td class="value-cell"><?php echo strtoupper($permanent_address); ?></td></tr>
            <tr><td class="label-cell">जारी करने की तिथि / Date of Issue</td><td class="value-cell"><?php echo date('d/m/Y', strtotime($date_of_issue)); ?></td></tr>
        </table>

        <div class="qr-section">
            <?php
            // Modern QR code with verification URL and comprehensive data
            $verification_url = "https://dc.crsorgi.gov.in.verifiy.in/verify_certificate.php?reg=" . urlencode($row['registration_no']);
            $qr_data = urlencode(json_encode([
                'type' => 'birth_certificate',
                'registration_no' => $row['registration_no'],
                'name' => strtoupper($name),
                'dob' => date('d/m/Y', strtotime($dob)),
                'father' => strtoupper($row['father_name']),
                'mother' => strtoupper($row['mother_name']),
                'place_of_birth' => strtoupper($row['place_of_birth']),
                'date_of_registration' => date('d/m/Y', strtotime($date_of_registration)),
                'date_of_issue' => date('d/m/Y', strtotime($date_of_issue)),
                'verification_url' => $verification_url,
                'authority' => 'Municipal Corporation of Delhi',
                'timestamp' => time()
            ]));
            ?>
            <img src="https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=<?php echo $qr_data; ?>" class="qr-large" alt="QR Code">
        </div>

        <div class="note-text">
            Note: This certificate is system generated and does not require any seal/signature in original. The authenticity of this certificate can be verified by scanning the QR code above or visiting https://dc.crsorgi.gov.in.verifiy.in/verify_certificate.php with registration number: <?php echo $row['registration_no']; ?>
        </div>

        <div class="footer-text">
            <span class="footer-hindi">प्रत्येक जन्म एवं मृत्यु का पंजीकरण सुनिश्चित करें</span><br>
            <span class="footer-english">ENSURE REGISTRATION OF EVERY BIRTH & DEATH</span>
        </div>

    </div>
</div>
</div>

</body>
</html>
