Path : /home/vishqocm/pcib.in/enroll/
File Upload :
Current File : //home/vishqocm/pcib.in/enroll/enroll.php

<?php
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Start session
session_start();

// Include database configuration
require_once '../admin/database/db_config.php';

// Function to generate a unique verification token
function generateVerificationToken($length = 12) {
    return bin2hex(random_bytes($length));
}

// If user is not logged in, redirect to login page with return URL
if (!isset($_SESSION['user_id'])) {
    $_SESSION['redirect_url'] = 'enroll/enroll.php' . (isset($_GET['course_id']) ? '?course_id=' . $_GET['course_id'] : '');
    header('Location: ../login.php');
    exit;
}

// Get course ID from URL parameter
$course_id = isset($_GET['course_id']) ? intval($_GET['course_id']) : 0;

// If course ID is not provided, redirect to courses page
if ($course_id <= 0) {
    header('Location: ../courses.php');
    exit;
}

// Get user info
$user_id = $_SESSION['user_id'];
$user_query = "SELECT * FROM users WHERE id = ?";
$stmt = $conn->prepare($user_query);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$user_result = $stmt->get_result();
$user = $user_result->fetch_assoc();

// Get course info
$course_query = "SELECT c.*, u.first_name, u.last_name FROM courses c 
                LEFT JOIN users u ON c.instructor_id = u.id 
                WHERE c.id = ?";
$stmt = $conn->prepare($course_query);
$stmt->bind_param("i", $course_id);
$stmt->execute();
$course_result = $stmt->get_result();
$course = $course_result->fetch_assoc();

// Check if course exists
if (!$course) {
    header('Location: ../courses.php');
    exit;
}

// Check if user is already enrolled in this course
$check_enrollment_query = "SELECT * FROM enrollments WHERE user_id = ? AND course_id = ?";
$stmt = $conn->prepare($check_enrollment_query);
$stmt->bind_param("ii", $user_id, $course_id);
$stmt->execute();
$existing_enrollment = $stmt->get_result()->fetch_assoc();

// Check if user has a pending application for this course
$check_application_query = "SELECT * FROM enrollment_applications WHERE user_id = ? AND course_id = ? AND status != 'rejected'";
$stmt = $conn->prepare($check_application_query);
$stmt->bind_param("ii", $user_id, $course_id);
$stmt->execute();
$existing_application = $stmt->get_result()->fetch_assoc();

// Process form submission
$error_message = '';
$success_message = '';
$application_id = 0;

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit_enrollment'])) {
    // Get form data
    $address = trim($_POST['address']);
    $city = trim($_POST['city']);
    $state = trim($_POST['state']);
    $postal_code = trim($_POST['postal_code']);
    $education = trim($_POST['education']);
    $dob = trim($_POST['dob']);
    $emergency_contact = trim($_POST['emergency_contact']);
    
    // Validate form data
    if (empty($address) || empty($city) || empty($state) || empty($postal_code) || empty($education) || empty($dob)) {
        $error_message = "Please fill in all required fields.";
    } else {
        // Store personal details as JSON
        $personal_details = json_encode([
            'address' => $address,
            'city' => $city,
            'state' => $state,
            'postal_code' => $postal_code,
            'education' => $education,
            'dob' => $dob,
            'emergency_contact' => $emergency_contact
        ]);
        
        // Generate verification token
        $verification_token = generateVerificationToken();
        
        // Start transaction
        $conn->begin_transaction();
        
        try {
            // Insert into enrollment_applications
            $application_query = "INSERT INTO enrollment_applications 
                                  (user_id, course_id, status, verification_token, personal_details) 
                                  VALUES (?, ?, 'pending', ?, ?)";
            $stmt = $conn->prepare($application_query);
            $stmt->bind_param("iiss", $user_id, $course_id, $verification_token, $personal_details);
            $stmt->execute();
            $application_id = $conn->insert_id;
            
            // If course doesn't require documents, set status to payment_pending
            if (!$course['document_required']) {
                $update_query = "UPDATE enrollment_applications SET status = 'payment_pending' WHERE id = ?";
                $stmt = $conn->prepare($update_query);
                $stmt->bind_param("i", $application_id);
                $stmt->execute();
            }
            
            // Commit transaction
            $conn->commit();
            
            // Set success message
            $success_message = "Your enrollment application has been submitted successfully.";
            
            // Redirect to the document upload or payment page depending on course requirements
            if ($course['document_required']) {
                header("Location: upload_documents.php?application_id=$application_id");
                exit;
            } else {
                header("Location: payment.php?application_id=$application_id");
                exit;
            }
            
        } catch (Exception $e) {
            // Rollback transaction
            $conn->rollback();
            $error_message = "An error occurred while processing your enrollment. Please try again later.";
            error_log("Enrollment error: " . $e->getMessage());
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Enrollment Application - <?php echo htmlspecialchars($course['title']); ?></title>
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <!-- Modern UI CSS -->
    <link rel="stylesheet" href="css/modern-ui.css">
    
    <style>
        body {
            font-family: 'Poppins', sans-serif;
            background-color: #f8f9fa;
            min-height: 100vh;
            overflow-x: hidden;
        }
        
        .logo-container {
            text-align: center;
            margin: 20px 0;
        }
        
        .logo-container img {
            max-height: 60px;
        }
        
        .logo-container h2 {
            color: white;
            margin: 10px 0;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
        }
        
        .return-link {
            position: absolute;
            top: 20px;
            left: 20px;
            color: white;
            text-decoration: none;
            z-index: 100;
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            padding: 8px 15px;
            border-radius: 50px;
            font-weight: 500;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .return-link:hover {
            background: rgba(255, 255, 255, 0.2);
            transform: translateY(-2px);
            color: white;
        }
    </style>
</head>
<body>

<!-- Return to Course Link -->
<a href="../course-details.php?id=<?php echo $course_id; ?>" class="return-link">
    <i class="fas fa-arrow-left me-2"></i> Back to Course
</a>

<!-- Logo Container -->
<div class="logo-container">
    <?php 
    // Get school settings from site_settings table
    $school_name = 'Popular Computer Institute';
    $school_logo = '../assets/img/logo.png';
    
    $site_settings_query = "SELECT setting_key, setting_value FROM site_settings WHERE setting_key IN ('site_name', 'site_logo')";
    try {
        $site_settings_result = $conn->query($site_settings_query);
        if ($site_settings_result && $site_settings_result->num_rows > 0) {
            while ($row = $site_settings_result->fetch_assoc()) {
                if ($row['setting_key'] == 'site_name') {
                    $school_name = $row['setting_value'];
                } else if ($row['setting_key'] == 'site_logo') {
                    $school_logo = $row['setting_value'];
                    // Add path prefix if not already present
                    if ($school_logo && substr($school_logo, 0, 1) !== '/' && substr($school_logo, 0, 4) !== 'http') {
                        $school_logo = '../' . $school_logo;
                    }
                }
            }
        }
    } catch (Exception $e) {
        // Silently handle the error - use default values
    }
    ?>
    <img src="<?php echo $school_logo; ?>" alt="<?php echo htmlspecialchars($school_name); ?>" class="logo" onerror="this.src='../assets/img/logo.png'; this.onerror='';">
    <h2><?php echo htmlspecialchars($school_name); ?></h2>
</div>

<!-- Animated Background -->
<div class="animated-bg">
    <div class="animated-shape shape-1"></div>
    <div class="animated-shape shape-2"></div>
    <div class="animated-shape shape-3"></div>
    <div class="animated-shape shape-4"></div>
</div>

<!-- Begin Page Content -->
<div class="container mt-5 mb-5">
    <div class="row justify-content-center">
        <div class="col-lg-10">
            <div class="glass-card shadow fade-in">
                <div class="modern-card-header">
                    <div class="glow-effect glow-1"></div>
                    <div class="glow-effect glow-2"></div>
                    <div class="modern-card-header-content">
                        <i class="fas fa-graduation-cap"></i>
                    <h4 class="mb-0">Enrollment Application: <?php echo htmlspecialchars($course['title']); ?></h4>
                    </div>
                    <span class="floating-label">Step 1</span>
                </div>
                <div class="card-body">
                    
                    <?php if ($existing_enrollment): ?>
                        <div class="alert" style="background: rgba(255, 255, 255, 0.2); color: white;">
                            <h5><i class="fas fa-info-circle me-2"></i>You are already enrolled in this course.</h5>
                            <p>You can access your course materials in your <a href="../student/index.php" class="text-white"><u>student dashboard</u></a>.</p>
                            <a href="../course-details.php?id=<?php echo $course_id; ?>" class="btn btn-3d btn-primary-3d mt-3">Return to Course Details</a>
                        </div>
                    <?php elseif ($existing_application): ?>
                        <div class="alert" style="background: rgba(255, 255, 255, 0.2); color: white;">
                            <h5><i class="fas fa-info-circle me-2"></i>You already have a pending application for this course.</h5>
                            <p>Current status: <strong><?php echo ucfirst($existing_application['status']); ?></strong></p>
                            
                            <?php if ($existing_application['status'] === 'pending'): ?>
                                <p>Your application is being reviewed. You can upload required documents if you haven't already.</p>
                                <a href="upload_documents.php?application_id=<?php echo $existing_application['id']; ?>" class="btn btn-3d btn-primary-3d mt-2">
                                    <i class="fas fa-upload me-2"></i>Upload Documents
                                </a>
                            <?php elseif ($existing_application['status'] === 'payment_pending'): ?>
                                <p>Your application has been approved. Please complete the payment to finalize your enrollment.</p>
                                <a href="payment.php?application_id=<?php echo $existing_application['id']; ?>" class="btn btn-3d btn-success-3d mt-2">
                                    <i class="fas fa-credit-card me-2"></i>Complete Payment
                                </a>
                            <?php endif; ?>
                            
                            <a href="../course-details.php?id=<?php echo $course_id; ?>" class="btn btn-3d btn-outline-3d mt-2 ms-2">Return to Course Details</a>
                        </div>
                    <?php else: ?>
                        <?php if ($error_message): ?>
                            <div class="alert alert-danger"><?php echo $error_message; ?></div>
                        <?php endif; ?>
                        
                        <?php if ($success_message): ?>
                            <div class="alert alert-success"><?php echo $success_message; ?></div>
                        <?php endif; ?>
                        
                        <div class="row mb-4">
                            <div class="col-md-8">
                                <h5 class="text-white">Course: <?php echo htmlspecialchars($course['title']); ?></h5>
                                <p class="text-white-50">Instructor: <?php echo htmlspecialchars($course['first_name'] . ' ' . $course['last_name']); ?></p>
                                <?php if ($course['discount_price'] && $course['discount_price'] < $course['price']): ?>
                                    <p class="text-white-50">Original Price: <s>₹<?php echo number_format($course['price'], 2); ?></s></p>
                                    <p class="text-white-50">Discounted Price: <strong class="text-white">₹<?php echo number_format($course['discount_price'], 2); ?></strong></p>
                                <?php else: ?>
                                    <p class="text-white-50">Price: <strong class="text-white">₹<?php echo number_format($course['price'], 2); ?></strong></p>
                                <?php endif; ?>
                                <p class="text-white-50">Duration: <?php echo htmlspecialchars($course['duration']); ?></p>
                                <p class="text-white-50">Level: <?php echo ucfirst($course['level']); ?></p>
                            </div>
                            <div class="col-md-4 text-center">
                                <img src="<?php echo $course['image'] ? '../' . $course['image'] : '../assets/img/course-placeholder.jpg'; ?>" 
                                     class="img-fluid rounded bounce-in" alt="<?php echo htmlspecialchars($course['title']); ?>"
                                     style="max-height: 180px; border: 3px solid rgba(255,255,255,0.2);">
                            </div>
                        </div>
                        
                        <form method="post" action="" class="needs-validation fade-in" novalidate>
                            <div class="p-4 mb-4 rounded" style="background: rgba(255, 255, 255, 0.1);">
                                <h5 class="text-white mb-4"><i class="fas fa-user me-2"></i>Personal Information</h5>
                            
                            <div class="row mb-3">
                                <div class="col-md-6">
                                        <label for="first_name" class="form-label text-white">First Name</label>
                                    <input type="text" class="form-control" id="first_name" value="<?php echo htmlspecialchars($user['first_name']); ?>" readonly>
                                </div>
                                <div class="col-md-6">
                                        <label for="last_name" class="form-label text-white">Last Name</label>
                                    <input type="text" class="form-control" id="last_name" value="<?php echo htmlspecialchars($user['last_name']); ?>" readonly>
                                </div>
                            </div>
                            
                            <div class="row mb-3">
                                <div class="col-md-6">
                                        <label for="email" class="form-label text-white">Email</label>
                                    <input type="email" class="form-control" id="email" value="<?php echo htmlspecialchars($user['email']); ?>" readonly>
                                </div>
                                <div class="col-md-6">
                                        <label for="phone" class="form-label text-white">Phone</label>
                                    <input type="text" class="form-control" id="phone" value="<?php echo htmlspecialchars($user['phone']); ?>" readonly>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="p-4 mb-4 rounded" style="background: rgba(255, 255, 255, 0.1);">
                                <h5 class="text-white mb-4"><i class="fas fa-address-card me-2"></i>Additional Information</h5>
                            
                            <div class="row mb-3">
                                <div class="col-md-12">
                                        <label for="address" class="form-label text-white">Address *</label>
                                    <input type="text" class="form-control" id="address" name="address" required>
                                    <div class="invalid-feedback">Please enter your address.</div>
                                </div>
                            </div>
                            
                            <div class="row mb-3">
                                <div class="col-md-4">
                                        <label for="city" class="form-label text-white">City *</label>
                                    <input type="text" class="form-control" id="city" name="city" required>
                                    <div class="invalid-feedback">Please enter your city.</div>
                                </div>
                                <div class="col-md-4">
                                        <label for="state" class="form-label text-white">State *</label>
                                    <input type="text" class="form-control" id="state" name="state" required>
                                    <div class="invalid-feedback">Please enter your state.</div>
                                </div>
                                <div class="col-md-4">
                                        <label for="postal_code" class="form-label text-white">Postal Code *</label>
                                    <input type="text" class="form-control" id="postal_code" name="postal_code" required>
                                    <div class="invalid-feedback">Please enter your postal code.</div>
                                </div>
                            </div>
                            
                            <div class="row mb-3">
                                <div class="col-md-6">
                                        <label for="education" class="form-label text-white">Highest Education *</label>
                                        <select class="form-select" id="education" name="education" required>
                                            <option value="">Select Education Level</option>
                                            <option value="High School">High School</option>
                                            <option value="Diploma">Diploma</option>
                                            <option value="Bachelor's Degree">Bachelor's Degree</option>
                                            <option value="Master's Degree">Master's Degree</option>
                                            <option value="Ph.D">Ph.D</option>
                                            <option value="Other">Other</option>
                                        </select>
                                        <div class="invalid-feedback">Please select your education level.</div>
                                </div>
                                <div class="col-md-6">
                                        <label for="dob" class="form-label text-white">Date of Birth *</label>
                                    <input type="date" class="form-control" id="dob" name="dob" required>
                                    <div class="invalid-feedback">Please enter your date of birth.</div>
                                </div>
                            </div>
                            
                                <div class="row">
                                    <div class="col-md-12">
                                        <label for="emergency_contact" class="form-label text-white">Emergency Contact</label>
                                        <input type="text" class="form-control" id="emergency_contact" name="emergency_contact" placeholder="Name: Contact Number">
                                    </div>
                                </div>
                            </div>
                            
                            <div class="alert" style="background: rgba(255, 255, 255, 0.1); color: white;">
                                <h6 class="mb-0"><i class="fas fa-info-circle me-2"></i>Note:</h6>
                                <p class="mb-0">By submitting this form, you agree to share your personal information with our institute for enrollment purposes.</p>
                                <?php if ($course['document_required']): ?>
                                <p class="mb-0 mt-2"><strong>Document Requirement:</strong> You will need to upload supporting documents in the next step.</p>
                                <?php endif; ?>
                            </div>
                            
                            <div class="text-center mt-4">
                                <button type="submit" name="submit_enrollment" class="btn btn-3d btn-primary-3d btn-lg px-5">
                                    <i class="fas fa-paper-plane me-2"></i>Submit Application
                                </button>
                                <a href="../course-details.php?id=<?php echo $course_id; ?>" class="btn btn-3d btn-outline-3d btn-lg px-5 ms-2">
                                    <i class="fas fa-times me-2"></i>Cancel
                                </a>
                            </div>
                        </form>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
// Form validation
document.addEventListener('DOMContentLoaded', function() {
    const form = document.querySelector('.needs-validation');
    if (form) {
            form.addEventListener('submit', function(event) {
            if (!form.checkValidity()) {
                    event.preventDefault();
                    event.stopPropagation();
                }
                form.classList.add('was-validated');
        });
        
        // Add 3D hover effect to buttons
        const buttons = document.querySelectorAll('.btn-3d');
        
        buttons.forEach(button => {
            button.addEventListener('mousemove', function(e) {
                const rect = this.getBoundingClientRect();
                const x = (e.clientX - rect.left) / this.offsetWidth;
                const y = (e.clientY - rect.top) / this.offsetHeight;
                
                const rotateY = 5 * (0.5 - x);
                const rotateX = 5 * (y - 0.5);
                
                this.style.transform = `perspective(500px) rotateX(${rotateX}deg) rotateY(${rotateY}deg) scale3d(1.05, 1.05, 1.05)`;
            });
            
            button.addEventListener('mouseleave', function() {
                this.style.transform = '';
            });
        });
    }
});
</script>

<!-- Bootstrap JS Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>