<?php
$page_title = "Add New Certificate";
include "includes/header.php";

// Check if admin is logged in
if(!isset($_SESSION['user_id']) || $_SESSION['user_role'] != 'admin'){
    header("Location: login.php");
    exit();
}

include "db.php";

// Auto-generate Registration No: MCDOLIR-0126-XXXXXXX (auto increment)
$reg_prefix = 'MCDOLIR-0126-';
$reg_start = 1104990;
$last_reg = mysqli_query($conn, "SELECT registration_no FROM birth_certificate ORDER BY id DESC LIMIT 1");
if ($last_reg && mysqli_num_rows($last_reg) > 0) {
    $last_row = mysqli_fetch_assoc($last_reg);
    $last_num = intval(preg_replace('/^MCDOLIR-\d+-/', '', $last_row['registration_no']));
    $next_num = $last_num + 1;
} else {
    $next_num = $reg_start;
}
$auto_reg_no = $reg_prefix . $next_num;

// Handle form submission
if (isset($_POST['savedata'])) {
    $registration_no = mysqli_real_escape_string($conn, trim($_POST['registration_no']));
    $name = mysqli_real_escape_string($conn, trim($_POST['name']));
    $gender = mysqli_real_escape_string($conn, trim($_POST['gender']));
    $date_of_birth = mysqli_real_escape_string($conn, trim($_POST['date_of_birth']));
    $place_of_birth = mysqli_real_escape_string($conn, trim($_POST['place_of_birth']));
    $date_of_registration = mysqli_real_escape_string($conn, trim($_POST['date_of_registration']));
    $father_name = mysqli_real_escape_string($conn, trim($_POST['father_name']));
    $mother_name = mysqli_real_escape_string($conn, trim($_POST['mother_name']));
    $present_address = mysqli_real_escape_string($conn, trim($_POST['present_address']));
    $permanent_address = mysqli_real_escape_string($conn, trim($_POST['permanent_address']));
    $date_of_issue = mysqli_real_escape_string($conn, trim($_POST['date_of_issue']));
    $name_of_zone = mysqli_real_escape_string($conn, trim($_POST['name_of_zone']));

    if (empty($registration_no)) {
        $msgno = 'Please Enter Registration No';
    } elseif (empty($name)) {
        $msgno = 'Please Enter Full Name';
    } elseif (empty($gender)) {
        $msgno = 'Please Select Gender';
    } elseif (empty($date_of_birth)) {
        $msgno = 'Please Enter Date of Birth';
    } elseif (empty($place_of_birth)) {
        $msgno = 'Please Enter Place of Birth';
    } elseif (empty($father_name)) {
        $msgno = 'Please Enter Father Name';
    } elseif (empty($mother_name)) {
        $msgno = 'Please Enter Mother Name';
    } elseif (empty($present_address)) {
        $msgno = 'Please Enter Present Address';
    } elseif (empty($permanent_address)) {
        $msgno = 'Please Enter Permanent Address';
    } else {
        $query = "INSERT INTO `birth_certificate`
            (`registration_no`, `name`, `gender`, `date_of_birth`, `place_of_birth`, `date_of_registration`,
            `father_name`, `mother_name`, `present_address`, `permanent_address`, `date_of_issue`, `name_of_zone`, `created_at`)
            VALUES
            ('$registration_no', '$name', '$gender', '$date_of_birth', '$place_of_birth', '$date_of_registration',
            '$father_name', '$mother_name', '$present_address', '$permanent_address', '$date_of_issue', '$name_of_zone', NOW())";

        $result = mysqli_query($conn, $query);

        if ($result) {
            echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>";
            echo "<script>
                Swal.fire('Certificate Added Successfully!', '', 'success');
                setTimeout(() => { window.location = 'admin_certificates.php'; }, 1500);
            </script>";
        } else {
            $msgno = "Error saving data: " . mysqli_error($conn);
        }
    }
}

if (!empty($msgno)) {
    echo '<script>alert("' . $msgno . '");</script>';
}
?>

<?php include "includes/admin_sidebar.php"; ?>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>

<style>
.delhi-card {
    max-width: 950px;
    margin: 25px auto;
    background: linear-gradient(135deg, #ffffff 0%, #f0f4ff 100%);
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.12);
    overflow: hidden;
}
.delhi-header {
    background: linear-gradient(135deg, #00008B 0%, #1a3a8a 100%);
    color: #fff;
    padding: 20px 30px;
    display: flex;
    align-items: center;
    gap: 15px;
}
.delhi-header i { font-size: 28px; }
.delhi-header h4 { margin: 0; font-weight: 600; letter-spacing: 0.5px; }
.delhi-body { padding: 30px; }
.section-title {
    color: #00008B;
    font-weight: 700;
    margin-bottom: 18px;
    padding-bottom: 8px;
    border-bottom: 2px solid #e2e8f0;
}
.form-group { margin-bottom: 18px; }
.form-group label {
    font-weight: 600;
    color: #344767;
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 6px;
    display: block;
}
.form-group .form-control, .form-group select, .form-group textarea {
    border: 2px solid #e2e8f0;
    border-radius: 10px;
    padding: 10px 14px;
    font-size: 14px;
    transition: all 0.3s ease;
    background: #fff;
    width: 100%;
}
.form-group .form-control:focus, .form-group select:focus, .form-group textarea:focus {
    border-color: #00008B;
    box-shadow: 0 0 0 3px rgba(0,0,139,0.15);
    outline: none;
}
.input-icon { position: relative; }
.input-icon i {
    position: absolute; right: 14px; top: 50%; transform: translateY(-50%);
    color: #94a3b8; pointer-events: none;
}
.input-icon .form-control { padding-right: 40px; }
.btn-submit {
    background: linear-gradient(135deg, #00008B 0%, #1a3a8a 100%);
    color: #fff;
    border: none;
    padding: 12px 40px;
    border-radius: 10px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
}
.btn-submit:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0,0,139,0.4);
    color: #fff;
}
.btn-cancel {
    background: #dc3545;
    color: #fff;
    border: none;
    padding: 12px 30px;
    border-radius: 10px;
    font-size: 16px;
    font-weight: 600;
    text-decoration: none;
    transition: all 0.3s ease;
}
.btn-cancel:hover { transform: translateY(-2px); color: #fff; text-decoration: none; }
.copy-btn {
    background: #6c757d;
    color: #fff;
    border: none;
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 11px;
    cursor: pointer;
    margin-left: 8px;
}
</style>

<!-- Main Content -->
<div class="main-content">
    <?php include "includes/topbar.php"; ?>

    <div class="page-content">
        <div class="delhi-card">
            <div class="delhi-header">
                <i class="fas fa-baby"></i>
                <div>
                    <h4>Admin - Add Birth Certificate</h4>
                    <small style="opacity:0.8;">Fill the form to generate Delhi Municipal Corporation Birth Certificate</small>
                </div>
            </div>
            <div class="delhi-body">
                <form method="post" autocomplete="off" action="">

                    <!-- Section 1: Registration Details -->
                    <h6 class="section-title"><i class="fas fa-file-alt"></i> Registration Details</h6>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label><i class="fas fa-hashtag"></i> Registration No (Auto Generated)</label>
                                <div class="input-icon">
                                    <input class="form-control" name="registration_no" type="text" required value="<?php echo $auto_reg_no; ?>" readonly style="background:#f0f4ff; font-weight:700; color:#00008B;">
                                    <i class="fas fa-hashtag"></i>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label><i class="fas fa-calendar-check"></i> Date of Registration</label>
                                <div class="input-icon">
                                    <input class="form-control" name="date_of_registration" id="reg_date_picker" type="text" required placeholder="Select Date" readonly>
                                    <i class="fas fa-calendar-alt"></i>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label><i class="fas fa-clock"></i> Date of Issue</label>
                                <div class="input-icon">
                                    <input class="form-control" name="date_of_issue" id="issue_date_picker" type="text" required placeholder="Select Date & Time" readonly>
                                    <i class="fas fa-clock"></i>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label><i class="fas fa-map-marker-alt"></i> Place of Birth</label>
                                <div class="input-icon">
                                    <input class="form-control" name="place_of_birth" type="text" required placeholder="e.g. DR ANITAS GYNAE CENTRE A-3/129 PASCHIM VIHAR">
                                    <i class="fas fa-hospital"></i>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Section 2: Personal Details -->
                    <h6 class="section-title" style="margin-top:10px;"><i class="fas fa-user"></i> Personal Details</h6>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label><i class="fas fa-user"></i> Full Name</label>
                                <div class="input-icon">
                                    <input class="form-control" name="name" id="child_name" type="text" required placeholder="e.g. RABBI NOOR">
                                    <i class="fas fa-user"></i>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label><i class="fas fa-venus-mars"></i> Gender</label>
                                <select class="form-control" name="gender" required>
                                    <option value="">-- Select Gender --</option>
                                    <option value="MALE">Male</option>
                                    <option value="FEMALE">Female</option>
                                    <option value="OTHER">Other</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label><i class="fas fa-calendar-alt"></i> Date of Birth</label>
                                <div class="input-icon">
                                    <input class="form-control" name="date_of_birth" id="dob_picker" type="text" required placeholder="Select DOB" readonly>
                                    <i class="fas fa-calendar-alt"></i>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label><i class="fas fa-male"></i> Father's Name</label>
                                <div class="input-icon">
                                    <input class="form-control" name="father_name" type="text" required placeholder="e.g. MUSTAFA">
                                    <i class="fas fa-male"></i>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label><i class="fas fa-female"></i> Mother's Name</label>
                                <div class="input-icon">
                                    <input class="form-control" name="mother_name" type="text" required placeholder="e.g. MEENA">
                                    <i class="fas fa-female"></i>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label><i class="fas fa-map-marker-alt"></i> Name of Zone</label>
                                <select class="form-control" name="name_of_zone" required>
                                    <option value="">-- Select Zone --</option>
                                    <option value="CENTRAL ZONE">CENTRAL ZONE</option>
                                    <option value="CITY SP ZONE">CITY SP ZONE</option>
                                    <option value="CIVIL LINES ZONE">CIVIL LINES ZONE</option>
                                    <option value="KAROL BAGH ZONE">KAROL BAGH ZONE</option>
                                    <option value="NAJAFGARH ZONE">NAJAFGARH ZONE</option>
                                    <option value="NARELA ZONE">NARELA ZONE</option>
                                    <option value="ROHINI ZONE">ROHINI ZONE</option>
                                    <option value="SADAR PAHARGANJ ZONE">SADAR PAHARGANJ ZONE</option>
                                    <option value="SHAHDARA NORTH ZONE">SHAHDARA NORTH ZONE</option>
                                    <option value="SHAHDARA SOUTH ZONE">SHAHDARA SOUTH ZONE</option>
                                    <option value="SOUTH ZONE">SOUTH ZONE</option>
                                    <option value="WEST ZONE">WEST ZONE</option>
                                    <option value="KESHAVPURAM ZONE">KESHAVPURAM ZONE</option>
                                    <option value="NEHRU NAGAR ZONE">NEHRU NAGAR ZONE</option>
                                    <option value="SEELAMPUR ZONE">SEELAMPUR ZONE</option>
                                    <option value="DEFENCE COLONY ZONE">DEFENCE COLONY ZONE</option>
                                    <option value="VASANT VIHAR ZONE">VASANT VIHAR ZONE</option>
                                    <option value="RAJINDER NAGAR ZONE">RAJINDER NAGAR ZONE</option>
                                    <option value="DWARKA ZONE">DWARKA ZONE</option>
                                    <option value="PUNJABI BAGH ZONE">PUNJABI BAGH ZONE</option>
                                    <option value="JANAKPURI ZONE">JANAKPURI ZONE</option>
                                    <option value="MUNDKA ZONE">MUNDKA ZONE</option>
                                    <option value="ALIPUR ZONE">ALIPUR ZONE</option>
                                    <option value="BADLI ZONE">BADLI ZONE</option>
                                    <option value="SARITA VIHAR ZONE">SARITA VIHAR ZONE</option>
                                    <option value="GREATER KAILASH ZONE">GREATER KAILASH ZONE</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <!-- Section 3: Address Details -->
                    <h6 class="section-title" style="margin-top:10px;"><i class="fas fa-map-marker-alt"></i> Address Details</h6>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label><i class="fas fa-home"></i> Present Address (at time of Birth)</label>
                                <textarea class="form-control" name="present_address" id="present_addr" rows="2" required placeholder="Enter complete present address"></textarea>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label><i class="fas fa-map"></i> Permanent Address <button type="button" class="copy-btn" onclick="copyAddress()"><i class="fas fa-copy"></i> Same as Present</button></label>
                                <textarea class="form-control" name="permanent_address" id="permanent_addr" rows="2" required placeholder="Enter complete permanent address"></textarea>
                            </div>
                        </div>
                    </div>

                    <!-- Submit Section -->
                    <div class="row">
                        <div class="col-12" style="margin-top:15px;">
                            <hr style="border-color:#e2e8f0;">
                            <div class="d-flex justify-content-between align-items-center flex-wrap" style="gap:15px;">
                                <div style="display:flex; gap:10px; flex-wrap:wrap;">
                                    <a href="admin_certificates.php" class="btn-cancel">
                                        <i class="fas fa-times"></i> Cancel
                                    </a>
                                    <button type="submit" name="savedata" class="btn-submit">
                                        <i class="fas fa-save"></i> Save Certificate
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
    function copyAddress() {
        document.getElementById('permanent_addr').value = document.getElementById('present_addr').value;
    }

    flatpickr("#dob_picker", { dateFormat: "Y-m-d", maxDate: "today", allowInput: false, disableMobile: true });
    flatpickr("#reg_date_picker", { dateFormat: "Y-m-d", allowInput: false, disableMobile: true });
    flatpickr("#issue_date_picker", { dateFormat: "Y-m-d H:i", enableTime: true, time_24hr: true, allowInput: false, disableMobile: true });
    </script>

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