<?php
$page_title = "Apply for Birth Certificate";
include "includes/header.php";

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

include "db.php";

// Get fee and wallet balance
$user_id = $_SESSION['user_id'];
$fee_q = mysqli_query($conn, "SELECT setting_value FROM settings WHERE setting_key='birth_certificate_fee'");
$fee = ($fee_q && mysqli_num_rows($fee_q) > 0) ? floatval(mysqli_fetch_assoc($fee_q)['setting_value']) : 0;
$bal_q = mysqli_query($conn, "SELECT balance FROM wallet WHERE user_id=$user_id");
$balance = ($bal_q && mysqli_num_rows($bal_q) > 0) ? floatval(mysqli_fetch_assoc($bal_q)['balance']) : 0;
$can_apply = ($fee == 0 || $balance >= $fee);
?>

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

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

    <div class="page-content">
        <!-- Fee & Balance Info -->
        <?php if($fee > 0): ?>
        <div class="fee-info-box">
            <i class="fas fa-info-circle"></i>
            <div style="flex:1;">
                <strong>Application Fee: Rs. <?php echo number_format($fee, 2); ?></strong>
                <span style="float:right;font-weight:600;color:<?php echo $can_apply ? '#059669' : '#dc2626'; ?>;">
                    <i class="fas fa-wallet"></i> Wallet: Rs. <?php echo number_format($balance, 2); ?>
                </span>
            </div>
        </div>
        <?php endif; ?>

        <?php if(!$can_apply): ?>
        <div class="insufficient-alert" style="margin-bottom:20px;">
            <i class="fas fa-exclamation-triangle"></i>
            <strong>Insufficient Balance!</strong> You need Rs. <?php echo number_format($fee, 2); ?> but have Rs. <?php echo number_format($balance, 2); ?>.
            <a href="wallet.php" style="color:#dc2626;font-weight:600;margin-left:10px;">Add Money <i class="fas fa-arrow-right"></i></a>
        </div>
        <?php endif; ?>

        <div class="card">
            <div class="card-header">
                <h3><i class="fas fa-file-signature"></i> Birth Certificate Application Form</h3>
            </div>
            <div class="card-body">
                <form action="submit_form.php" method="POST">

                    <div class="form-row">
                        <div class="form-group">
                            <label><i class="fas fa-user"></i> Child's Full Name</label>
                            <input type="text" name="name" placeholder="Enter child's full name" required>
                        </div>
                        <div class="form-group">
                            <label><i class="fas fa-venus-mars"></i> Gender</label>
                            <select 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="form-row">
                        <div class="form-group">
                            <label><i class="fas fa-calendar-alt"></i> Date of Birth</label>
                            <input type="date" name="dob" required>
                        </div>
                        <div class="form-group">
                            <label><i class="fas fa-map-marker-alt"></i> Name of Zone</label>
                            <select 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 class="form-row">
                        <div class="form-group">
                            <label><i class="fas fa-map-marker-alt"></i> Place of Birth</label>
                            <input type="text" name="place_of_birth" placeholder="SOUTH DELHI" required>
                        </div>
                        <div class="form-group">
                            <label><i class="fas fa-calendar-check"></i> Date of Registration</label>
                            <input type="date" name="date_of_registration" required>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label><i class="fas fa-male"></i> Father's Name</label>
                            <input type="text" name="father_name" placeholder="Enter father's name" required>
                        </div>
                        <div class="form-group">
                            <label><i class="fas fa-female"></i> Mother's Name</label>
                            <input type="text" name="mother_name" placeholder="Enter mother's name" required>
                        </div>
                    </div>

                    <!-- Mobile & Email hidden -->
                    <input type="hidden" name="mobile" value="">
                    <input type="hidden" name="email" value="<?php echo isset($_SESSION['user_email']) ? $_SESSION['user_email'] : ''; ?>">

                    <div class="form-group">
                        <label><i class="fas fa-home"></i> Present Address (Address at time of Birth)</label>
                        <textarea name="present_address" rows="2" placeholder="Enter complete present address" required></textarea>
                    </div>

                    <div class="form-group">
                        <label><i class="fas fa-map"></i> Permanent Address</label>
                        <textarea name="permanent_address" rows="2" placeholder="Enter complete permanent address" required></textarea>
                    </div>

                    <div class="form-group">
                        <label><i class="fas fa-clock"></i> Date & Time of Issue</label>
                        <input type="datetime-local" name="date_of_issue" required>
                    </div>

                    <div style="display: flex; gap: 15px; margin-top: 10px;">
                        <button type="submit" class="btn btn-success" style="flex:1;">
                            <i class="fas fa-paper-plane"></i> Submit Application
                        </button>
                        <a href="user_dashboard.php" class="btn btn-danger" style="flex:1; justify-content: center;">
                            <i class="fas fa-times"></i> Cancel
                        </a>
                    </div>

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

        <!-- Info Card -->
        <div class="card" style="margin-top: 25px;">
            <div class="card-header">
                <h3><i class="fas fa-info-circle"></i> Important Information</h3>
            </div>
            <div class="card-body">
                <ul style="list-style: none; color: #64748b; line-height: 2;">
                    <li><i class="fas fa-check-circle" style="color: #10b981; margin-right: 10px;"></i> All fields marked are mandatory</li>
                    <li><i class="fas fa-check-circle" style="color: #10b981; margin-right: 10px;"></i> Please provide accurate information as per official records</li>
                    <li><i class="fas fa-check-circle" style="color: #10b981; margin-right: 10px;"></i> You will receive application status updates on your registered email</li>
                    <li><i class="fas fa-check-circle" style="color: #10b981; margin-right: 10px;"></i> Certificate will be available for download after approval</li>
                </ul>
            </div>
        </div>
    </div>

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