<?php
include 'includes/header.php';
require_once 'db_connect.php';

$office    = $_GET['office_name'] ?? '';
$area      = $_GET['area_name'] ?? '';
$reg_num   = $_GET['reg_num'] ?? '';
$dob       = $_GET['dob'] ?? '';
$gender    = $_GET['gender'] ?? 'Male';
$name_bn   = $_GET['name_bn'] ?? '';
$name_en   = $_GET['name_en'] ?? '';
$father_bn = $_GET['father_bn'] ?? '';
$father_en = $_GET['father_en'] ?? '';
$mother_bn = $_GET['mother_bn'] ?? '';
$mother_en = $_GET['mother_en'] ?? '';
$p_birth   = $_GET['p_birth'] ?? '';
$perm_addr = $_GET['perm_addr'] ?? '';
$reg_date  = $_GET['reg_date'] ?? date('d/m/Y');

$id = $_GET['id'] ?? null;
if ($id && isset($pdo) && $pdo instanceof PDO) {
    try {
        $stmt = $pdo->prepare("SELECT * FROM births WHERE brn = ?");
        $stmt->execute([$id]);
        $data = $stmt->fetch(PDO::FETCH_ASSOC);
        if ($data) {
            $reg_num = $data['brn'] ?? $reg_num;
            $dobRaw = $data['dob'] ?? null;
            if ($dobRaw) {
                $dob = date('d/m/Y', strtotime($dobRaw));
            }
            $name_en   = $data['name_en'] ?? $name_en;
            $mother_en = $data['mother_en'] ?? $mother_en;
            $father_en = $data['father_en'] ?? $father_en;
            $area      = $data['district'] ?? $area;
            $perm_addr = $data['address_full'] ?? $perm_addr;
            $regDateRaw = $data['reg_date'] ?? null;
            if ($regDateRaw) {
                $reg_date = date('d/m/Y', strtotime($regDateRaw));
            }
        }
    } catch (Throwable $e) {
        // DB থেকে ডাটা লোড ব্যর্থ হলেও ফর্ম খালি অবস্থায় দেখাবে
    }
}
?>

<div class="container py-4">
    <div class="card shadow-sm border-0">
        <div class="card-header bg-light d-flex justify-content-between align-items-center py-3">
            <h5 class="mb-0 fw-bold">নিবন্ধন সনদ ফর্ম</h5>
            <?php if (!empty($reg_num)): ?>
                <a href="search-birth.php?ubrn=<?= urlencode($reg_num) ?>" class="btn btn-sm btn-outline-secondary">
                    R Data
                </a>
            <?php endif; ?>
        </div>
        <div class="card-body p-4">
            <form action="api/make-birth-pdf.php" method="post" target="_blank">
                
                <div class="row g-3">
                    <div class="col-md-6">
                        <label class="small fw-bold">প্রজিঃ অফিসের নাম</label>
                        <input type="text" name="office_name" class="form-control" value="<?= htmlspecialchars($office) ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="small fw-bold">উপজেলা/পৌরসভা/সিটি কর্পোরেশন, জেলা</label>
                        <input type="text" name="area_name" class="form-control" value="<?= htmlspecialchars($area) ?>">
                    </div>

                    <div class="col-md-6">
                        <label class="small fw-bold">QR Code URL/Text</label>
                        <div class="input-group">
                            <input type="text" name="qr_url" class="form-control" value="https://bdris.gov.bd/certificate/verify?key=<?= $reg_num ?>">
                            <button class="btn btn-primary" type="button">Upload QR</button>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <label class="small fw-bold">QR কোডের নিচের লেখা</label>
                        <input type="text" name="qr_id" class="form-control" value="ETHR">
                    </div>

                    <div class="col-md-4">
                        <label class="small fw-bold">নিবন্ধন নম্বর</label>
                        <input type="text" name="reg_num" class="form-control" value="<?= htmlspecialchars($reg_num) ?>">
                    </div>
                    <div class="col-md-4">
                        <label class="small fw-bold">জন্ম তারিখ</label>
                        <input type="text" name="dob" class="form-control" value="<?= htmlspecialchars($dob) ?>">
                    </div>
                    <div class="col-md-4">
                        <label class="small fw-bold">লিঙ্গ</label>
                        <select name="gender" class="form-select">
                            <option value="Male" <?= ($gender == 'MALE' || $gender == 'Male') ? 'selected' : '' ?>>Male</option>
                            <option value="Female" <?= ($gender == 'FEMALE' || $gender == 'Female') ? 'selected' : '' ?>>Female</option>
                        </select>
                    </div>

                    <div class="col-md-12">
                        <label class="small fw-bold">জন্ম তারিখ কথায় (In Word)</label>
                        <input type="text" name="dob_word" class="form-control" placeholder="Auto-generated or Type Manually">
                    </div>

                    <div class="col-md-6">
                        <label class="small fw-bold">নাম (বাংলা)</label>
                        <input type="text" name="name_bn" class="form-control" value="<?= htmlspecialchars($name_bn) ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="small fw-bold">নাম (ইংরেজি)</label>
                        <input type="text" name="name_en" class="form-control" value="<?= htmlspecialchars($name_en) ?>">
                    </div>

                    <div class="col-md-6">
                        <label class="small fw-bold">পিতার নাম (বাংলা)</label>
                        <input type="text" name="father_bn" class="form-control" value="<?= htmlspecialchars($father_bn) ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="small fw-bold">পিতার নাম (ইংরেজি)</label>
                        <input type="text" name="father_en" class="form-control" value="<?= htmlspecialchars($father_en) ?>">
                    </div>

                    <div class="col-md-6">
                        <label class="small fw-bold">মাতার নাম (বাংলা)</label>
                        <input type="text" name="mother_bn" class="form-control" value="<?= htmlspecialchars($mother_bn) ?>">
                    </div>
                    <div class="col-md-6">
                        <label class="small fw-bold">মাতার নাম (ইংরেজি)</label>
                        <input type="text" name="mother_en" class="form-control" value="<?= htmlspecialchars($mother_en) ?>">
                    </div>

                    <div class="col-md-12">
                        <label class="small fw-bold">জন্মস্থান (বাংলা ও ইংরেজি)</label>
                        <textarea name="p_birth" class="form-control" rows="2"><?= htmlspecialchars($p_birth) ?></textarea>
                    </div>
                    <div class="col-md-12">
                        <label class="small fw-bold">স্থায়ী ঠিকানা (বাংলা ও ইংরেজি)</label>
                        <textarea name="perm_addr" class="form-control" rows="2"><?= htmlspecialchars($perm_addr) ?></textarea>
                    </div>

                    <div class="col-12 mt-4">
                        <div class="row g-2">
                            <div class="col-md-6">
                                <a href="https://office.birthsupportbd.xyz/view-registration.php" class="btn btn-outline-dark w-100 py-2 fw-bold">তালিকা দেখুন</a>
                            </div>
                            <div class="col-md-6">
                                <button type="submit" class="btn btn-success w-100 py-2 fw-bold">তালিকা দেখুন ও প্রিন্ট করুন</button>
                            </div>
                        </div>
                    </div>

                    <input type="hidden" name="brn" value="<?= htmlspecialchars($reg_num) ?>">
                    <input type="hidden" name="name" value="<?= htmlspecialchars($name_en) ?>">
                    <input type="hidden" name="mother" value="<?= htmlspecialchars($mother_en) ?>">
                    <input type="hidden" name="father" value="<?= htmlspecialchars($father_en) ?>">
                    <input type="hidden" name="district" value="<?= htmlspecialchars($area) ?>">
                    <input type="hidden" name="address_full" value="<?= htmlspecialchars($perm_addr) ?>">
                    <input type="hidden" name="registration_date" value="<?= htmlspecialchars($reg_date) ?>">

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

<?php include 'includes/footer.php'; ?>
