<?php
session_start();
require_once(__DIR__ . '/../config.php');
include(__DIR__ . '/../header/header.php');
$error = '';
$success = '';

// Fetch available courses for the application form
$courses = $pdo->query("SELECT id, course_name FROM courses ORDER BY course_name ASC")->fetchAll();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $full_name = $_POST['full_name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $course_id = $_POST['course_id'] ?: null;
    $message = $_POST['message'];

    try {
        $stmt = $pdo->prepare("INSERT INTO applications (full_name, email, phone, course_id, message) VALUES (?, ?, ?, ?, ?)");
        $stmt->execute([$full_name, $email, $phone, $course_id, $message]);
        $success = "Your application has been submitted successfully! Our team will review it and get back to you soon.";
    } catch (PDOException $e) {
        $error = "Something went wrong. Please try again.";
    }
}
?>

<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card shadow-sm border-0 animate__animated animate__fadeIn">
                <div class="card-body p-5">
                    <h2 class="text-center fw-bold mb-4">College Application Form</h2>
                    <p class="text-center text-muted mb-5">Join Hessar College and start your journey today. Please fill out the form below.</p>
                    
                    <?php if($error): ?>
                        <div class="alert alert-danger"><?php echo $error; ?></div>
                    <?php endif; ?>

                    <?php if($success): ?>
                        <div class="alert alert-success"><?php echo $success; ?></div>
                    <?php else: ?>
                        <form action="application.php" method="POST">
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label fw-bold">Full Name</label>
                                    <input type="text" name="full_name" class="form-control" placeholder="Enter your full name" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label fw-bold">Email Address</label>
                                    <input type="email" name="email" class="form-control" placeholder="Enter your email" required>
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="form-label fw-bold">Phone Number</label>
                                    <input type="tel" name="phone" class="form-control" placeholder="Enter your phone number">
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="form-label fw-bold">Intended Course</label>
                                    <select name="course_id" class="form-select">
                                        <option value="">Select a course (Optional)</option>
                                        <?php foreach($courses as $course): ?>
                                            <option value="<?php echo $course['id']; ?>"><?php echo htmlspecialchars($course['course_name']); ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            </div>

                            <div class="mb-4">
                                <label class="form-label fw-bold">Why do you want to join Hessar College?</label>
                                <textarea name="message" class="form-control" rows="4" placeholder="Briefly tell us about your goals"></textarea>
                            </div>

                            <button type="submit" class="btn btn-primary w-100 py-3 fw-bold shadow-sm">Submit Application</button>
                        </form>
                    <?php endif; ?>

                    <div class="text-center mt-4">
                        <p class="mb-0">Already a student? <a href="login.php" class="text-decoration-none">Login here</a></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include('../footer/footer.php'); ?>