<?php
include('db_connection.php');

$student_id = $_GET['id'];

// Fetch student details
$student_query = "SELECT s.*, u.username, u.email FROM students s 
                  JOIN users u ON s.user_id = u.user_id 
                  WHERE s.student_id = ?";
$stmt = $conn->prepare($student_query);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$result = $stmt->get_result();
$student = $result->fetch_assoc();
$stmt->close();

// Fetch class options
$class_query = "SELECT class_id, class_name FROM classes";
$class_result = mysqli_query($conn, $class_query);

// Fetch parent options
$parent_query = "SELECT parent_id, name FROM parents";
$parent_result = mysqli_query($conn, $parent_query);

// Check if profile picture exists
$profile_picture = "";
if (!empty($student['profile_picture'])) {
    $profile_picture = $student['profile_picture'];
} else {
    // Default avatar
    $profile_picture = "https://ui-avatars.com/api/?name=" . urlencode($student['name']) . "&size=200&background=3a57e8&color=fff&bold=true";
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Edit Student - School Management System</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary: #3a57e8;
            --primary-dark: #2a42c5;
            --primary-light: #eef0ff;
            --secondary: #6c757d;
            --success: #1cbb8c;
            --info: #17a2b8;
            --warning: #f5b759;
            --danger: #fc5b69;
            --light: #f8f9fa;
            --dark: #343a40;
            --gray-100: #f8f9fa;
            --gray-200: #e9ecef;
            --gray-300: #dee2e6;
            --border-radius: 12px;
            --box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
            --transition: all 0.3s ease;
        }
        
        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #f5f7ff 0%, #eef0ff 100%);
            color: #333;
            min-height: 100vh;
            padding-top: 0;
            margin: 0;
        }
        
        /* Header Styles */
        .header {
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            color: white;
            padding: 1.2rem 0;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            margin-bottom: 2.5rem;
            position: relative;
            overflow: hidden;
        }
        
        .header::before {
            content: '';
            position: absolute;
            top: -10px;
            right: -10px;
            width: 120px;
            height: 120px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 50%;
        }
        
        .header::after {
            content: '';
            position: absolute;
            bottom: -20px;
            left: -20px;
            width: 80px;
            height: 80px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 50%;
        }
        
        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: relative;
            z-index: 2;
        }
        
        .logo {
            display: flex;
            align-items: center;
            gap: 12px;
            font-weight: 700;
            font-size: 1.5rem;
        }
        
        .logo-icon {
            font-size: 2rem;
            color: white;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .user-avatar {
            width: 45px;
            height: 45px;
            border-radius: 50%;
            object-fit: cover;
            border: 2px solid rgba(255, 255, 255, 0.3);
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }
        
        /* Main Content Styles */
        .container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 0 20px;
        }
        
        .card {
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            margin-bottom: 2rem;
            border: none;
            overflow: hidden;
            background: white;
            transition: var(--transition);
        }
        
        .card:hover {
            box-shadow: 0 12px 30px rgba(0, 0, 0, 0.15);
        }
        
        .card-header {
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            color: white;
            padding: 1.5rem 2rem;
            font-weight: 600;
            border-bottom: 0;
            position: relative;
            overflow: hidden;
        }
        
        .card-header::after {
            content: '';
            position: absolute;
            top: 0;
            right: 0;
            width: 100%;
            height: 100%;
            background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1440 320'%3E%3Cpath fill='%23ffffff' fill-opacity='0.1' d='M0,288L48,272C96,256,192,224,288,197.3C384,171,480,149,576,165.3C672,181,768,235,864,250.7C960,267,1056,245,1152,224C1248,203,1344,181,1392,170.7L1440,160L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z'%3E%3C/path%3E%3C/svg%3E");
            background-size: cover;
            opacity: 0.5;
        }
        
        .card-body {
            padding: 2.5rem;
        }
        
        /* Button Styles */
        .btn-primary {
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            border: none;
            padding: 0.75rem 1.75rem;
            font-weight: 500;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(58, 87, 232, 0.3);
            transition: var(--transition);
        }
        
        .btn-primary:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 12px rgba(58, 87, 232, 0.4);
            background: linear-gradient(135deg, var(--primary-dark) 0%, var(--primary) 100%);
        }
        
        .btn-outline-primary {
            border: 2px solid var(--primary);
            color: var(--primary);
            border-radius: 10px;
            padding: 0.75rem 1.75rem;
            font-weight: 500;
            transition: var(--transition);
        }
        
        .btn-outline-primary:hover {
            background: var(--primary);
            color: white;
            transform: translateY(-3px);
            box-shadow: 0 6px 12px rgba(58, 87, 232, 0.3);
        }
        
        /* Avatar Styles */
        .avatar-container {
            text-align: center;
            padding: 2rem;
            background: var(--primary-light);
            border-radius: var(--border-radius);
            margin-bottom: 2rem;
            position: relative;
            overflow: hidden;
        }
        
        .avatar-container::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 5px;
            background: linear-gradient(90deg, var(--primary) 0%, var(--primary-dark) 100%);
        }
        
        .avatar-preview {
            width: 160px;
            height: 160px;
            border-radius: 50%;
            object-fit: cover;
            border: 4px solid white;
            box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
            margin-bottom: 1.5rem;
            transition: var(--transition);
        }
        
        .avatar-preview:hover {
            transform: scale(1.05);
        }
        
        .avatar-upload {
            position: relative;
            display: inline-block;
        }
        
        .avatar-upload-label {
            display: inline-block;
            padding: 0.75rem 1.5rem;
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            color: white;
            border-radius: 8px;
            cursor: pointer;
            transition: var(--transition);
            font-size: 0.95rem;
            box-shadow: 0 4px 8px rgba(58, 87, 232, 0.3);
        }
        
        .avatar-upload-label:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 12px rgba(58, 87, 232, 0.4);
        }
        
        .avatar-input {
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            width: 100%;
            height: 100%;
            cursor: pointer;
        }
        
        /* Form Styles */
        .form-label {
            font-weight: 600;
            color: var(--dark);
            margin-bottom: 0.75rem;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .form-label i {
            color: var(--primary);
            font-size: 1.1rem;
        }
        
        .form-control, .form-select {
            border-radius: 10px;
            padding: 0.85rem 1.25rem;
            border: 2px solid var(--gray-300);
            transition: var(--transition);
            font-size: 1rem;
        }
        
        .form-control:focus, .form-select:focus {
            border-color: var(--primary);
            box-shadow: 0 0 0 0.3rem rgba(58, 87, 232, 0.2);
            transform: translateY(-2px);
        }
        
        .section-title {
            position: relative;
            padding-left: 1.25rem;
            margin: 2.5rem 0 1.75rem;
            font-weight: 600;
            color: var(--dark);
            font-size: 1.3rem;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            height: 24px;
            width: 6px;
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            border-radius: 3px;
        }
        
        .section-title i {
            color: var(--primary);
            font-size: 1.5rem;
        }
        
        .password-note {
            font-size: 0.9rem;
            color: var(--secondary);
            margin-top: -0.5rem;
            margin-bottom: 1.25rem;
            padding-left: 1.9rem;
        }
        
        .action-buttons {
            display: flex;
            gap: 1.25rem;
            margin-top: 2.5rem;
            padding-top: 2rem;
            border-top: 1px solid var(--gray-200);
        }
        
        /* Stats Cards */
        .stats-card {
            background: white;
            border-radius: var(--border-radius);
            padding: 1.75rem;
            box-shadow: var(--box-shadow);
            margin-bottom: 1.75rem;
            text-align: center;
            transition: var(--transition);
            border: 1px solid var(--gray-200);
        }
        
        .stats-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 12px 25px rgba(0, 0, 0, 0.15);
        }
        
        .stats-number {
            font-size: 2.25rem;
            font-weight: 700;
            color: var(--primary);
            margin-bottom: 0.5rem;
            background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .stats-label {
            font-size: 0.95rem;
            color: var(--secondary);
            text-transform: uppercase;
            letter-spacing: 0.5px;
            font-weight: 600;
        }
        
        /* Quick Actions */
        .quick-actions {
            display: grid;
            grid-template-columns: 1fr;
            gap: 0.75rem;
        }
        
        .quick-action-btn {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 1rem 1.25rem;
            background: white;
            border: 1px solid var(--gray-200);
            border-radius: 10px;
            text-decoration: none;
            color: var(--dark);
            transition: var(--transition);
        }
        
        .quick-action-btn:hover {
            background: var(--primary-light);
            transform: translateX(5px);
            border-color: var(--primary);
            color: var(--primary);
        }
        
        .quick-action-btn i {
            font-size: 1.25rem;
            color: var(--primary);
        }
        
        /* Responsive Design */
        @media (max-width: 992px) {
            .header-content {
                flex-direction: column;
                text-align: center;
                gap: 1rem;
            }
            
            .user-info {
                justify-content: center;
            }
            
            .card-body {
                padding: 2rem;
            }
            
            .action-buttons {
                flex-direction: column;
            }
        }
        
        @media (max-width: 768px) {
            .container {
                padding: 0 15px;
            }
            
            .card-body {
                padding: 1.5rem;
            }
            
            .avatar-preview {
                width: 140px;
                height: 140px;
            }
        }
        
        /* Animation for form elements */
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .form-group {
            animation: fadeIn 0.5s ease-out;
        }
        
        /* Custom checkbox for improved UX */
        .form-check-input:checked {
            background-color: var(--primary);
            border-color: var(--primary);
        }
    </style>
</head>
<body>
    <!-- Header -->
    <header class="header">
        <div class="container">
            <div class="header-content">
                <div class="logo">
                    <i class="fas fa-graduation-cap logo-icon"></i>
                    <span>EduManage Pro</span>
                </div>
                <h2 class="mb-0">Edit Student Information</h2>
                <div class="user-info">
                    <img src="https://randomuser.me/api/portraits/men/75.jpg" alt="User" class="user-avatar">
                    <span>Administrator</span>
                </div>
            </div>
        </div>
    </header>

    <div class="container">
        <div class="row">
            <!-- Sidebar with Stats and Actions -->
            <div class="col-lg-4">
                <div class="stats-card">
                    <div class="stats-number"><?php echo $student_id; ?></div>
                    <div class="stats-label">Student ID</div>
                </div>
                
                <div class="stats-card">
                    <div class="stats-number"><?php echo htmlspecialchars($student['class_id']); ?></div>
                    <div class="stats-label">Class ID</div>
                </div>
                
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0"><i class="fas fa-bolt me-2"></i>Quick Actions</h5>
                    </div>
                    <div class="card-body">
                        <div class="quick-actions">
                            <a href="manage_students.php" class="quick-action-btn">
                                <i class="fas fa-list"></i>
                                <span>View All Students</span>
                            </a>
                            <a href="dashboard.php" class="quick-action-btn">
                                <i class="fas fa-tachometer-alt"></i>
                                <span>Dashboard</span>
                            </a>
                            <a href="attendance.php" class="quick-action-btn">
                                <i class="fas fa-calendar-check"></i>
                                <span>Attendance</span>
                            </a>
                            <a href="grades.php" class="quick-action-btn">
                                <i class="fas fa-chart-bar"></i>
                                <span>Grades</span>
                            </a>
                            <a href="reports.php" class="quick-action-btn">
                                <i class="fas fa-file-alt"></i>
                                <span>Reports</span>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Main Form Content -->
            <div class="col-lg-8">
                <div class="card">
                    <div class="card-header">
                        <h4 class="mb-0"><i class="fas fa-user-edit me-2"></i>Edit Student Information</h4>
                    </div>
                    <div class="card-body">
                        <form action="process_edit_student.php" method="post" enctype="multipart/form-data">
                            <input type="hidden" name="student_id" value="<?php echo $student_id; ?>">
                            <input type="hidden" name="user_id" value="<?php echo $student['user_id']; ?>">
                            
                            <div class="avatar-container">
                                <img id="avatar-preview" src="<?php echo $profile_picture; ?>" class="avatar-preview" alt="Profile Picture">
                                <div class="avatar-upload mt-3">
                                    <label for="avatar-input" class="avatar-upload-label">
                                        <i class="fas fa-camera me-2"></i>Change Photo
                                    </label>
                                    <input type="file" id="avatar-input" name="profile_picture" class="avatar-input" accept="image/*">
                                </div>
                                <div class="mt-2">
                                    <small class="text-muted">Allowed JPG, PNG. Max 2MB</small>
                                </div>
                            </div>
                            
                            <h5 class="section-title">
                                <i class="fas fa-user-circle"></i>
                                Personal Information
                            </h5>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3 form-group">
                                    <label for="name" class="form-label">
                                        <i class="fas fa-signature"></i>Full Name
                                    </label>
                                    <input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($student['name']); ?>" required>
                                </div>
                                
                                <div class="col-md-6 mb-3 form-group">
                                    <label for="gender" class="form-label">
                                        <i class="fas fa-venus-mars"></i>Gender
                                    </label>
                                    <select class="form-select" id="gender" name="gender">
                                        <option value="">Select Gender</option>
                                        <option value="Male" <?php if (isset($student['gender']) && $student['gender'] == 'Male') echo 'selected'; ?>>Male</option>
                                        <option value="Female" <?php if (isset($student['gender']) && $student['gender'] == 'Female') echo 'selected'; ?>>Female</option>
                                        <option value="Other" <?php if (isset($student['gender']) && $student['gender'] == 'Other') echo 'selected'; ?>>Other</option>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3 form-group">
                                    <label for="date_of_birth" class="form-label">
                                        <i class="fas fa-birthday-cake"></i>Date of Birth
                                    </label>
                                    <input type="date" class="form-control" id="date_of_birth" name="date_of_birth" value="<?php if (isset($student['date_of_birth'])) echo $student['date_of_birth']; ?>">
                                </div>
                                
                                <div class="col-md-6 mb-3 form-group">
                                    <label for="phone" class="form-label">
                                        <i class="fas fa-phone"></i>Phone Number
                                    </label>
                                    <input type="text" class="form-control" id="phone" name="phone" value="<?php if (isset($student['phone'])) echo htmlspecialchars($student['phone']); ?>">
                                </div>
                            </div>
                            
                            <div class="mb-3 form-group">
                                <label for="address" class="form-label">
                                    <i class="fas fa-map-marker-alt"></i>Address
                                </label>
                                <textarea class="form-control" id="address" name="address" rows="2"><?php if (isset($student['address'])) echo htmlspecialchars($student['address']); ?></textarea>
                            </div>
                            
                            <h5 class="section-title">
                                <i class="fas fa-user-lock"></i>
                                Account Information
                            </h5>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3 form-group">
                                    <label for="username" class="form-label">
                                        <i class="fas fa-user"></i>Username
                                    </label>
                                    <input type="text" class="form-control" id="username" name="username" value="<?php echo htmlspecialchars($student['username']); ?>" required>
                                </div>
                                
                                <div class="col-md-6 mb-3 form-group">
                                    <label for="email" class="form-label">
                                        <i class="fas fa-envelope"></i>Email
                                    </label>
                                    <input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($student['email']); ?>" required>
                                </div>
                            </div>
                            
                            <div class="mb-3 form-group">
                                <label for="password" class="form-label">
                                    <i class="fas fa-key"></i>Password
                                </label>
                                <input type="password" class="form-control" id="password" name="password">
                                <div class="password-note">Leave blank if you don't want to change the password</div>
                            </div>
                            
                            <h5 class="section-title">
                                <i class="fas fa-graduation-cap"></i>
                                Academic Information
                            </h5>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3 form-group">
                                    <label for="class" class="form-label">
                                        <i class="fas fa-chalkboard"></i>Class
                                    </label>
                                    <select class="form-select" id="class" name="class" required>
                                        <?php while ($row = mysqli_fetch_assoc($class_result)): ?>
                                            <option value="<?php echo $row['class_id']; ?>" <?php if ($row['class_id'] == $student['class_id']) echo 'selected'; ?>>
                                                <?php echo $row['class_name']; ?>
                                            </option>
                                        <?php endwhile; ?>
                                    </select>
                                </div>
                                
                                <div class="col-md-6 mb-3 form-group">
                                    <label for="parent" class="form-label">
                                        <i class="fas fa-users"></i>Parent/Guardian
                                    </label>
                                    <select class="form-select" id="parent" name="parent" required>
                                        <?php 
                                        // Reset pointer for parent result
                                        mysqli_data_seek($parent_result, 0);
                                        while ($row = mysqli_fetch_assoc($parent_result)): ?>
                                            <option value="<?php echo $row['parent_id']; ?>" <?php if ($row['parent_id'] == $student['parent_id']) echo 'selected'; ?>>
                                                <?php echo $row['name']; ?>
                                            </option>
                                        <?php endwhile; ?>
                                    </select>
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3 form-group">
                                    <label for="fees_paid" class="form-label">
                                        <i class="fas fa-money-check"></i>Fees Paid (₦)
                                    </label>
                                    <input type="number" step="0.01" class="form-control" id="fees_paid" name="fees_paid" value="<?php echo htmlspecialchars($student['fees_paid']); ?>" required>
                                </div>
                                
                                <div class="col-md-6 mb-3 form-group">
                                    <label for="fees_outstanding" class="form-label">
                                        <i class="fas fa-money-bill-wave"></i>Fees Outstanding (₦)
                                    </label>
                                    <input type="number" step="0.01" class="form-control" id="fees_outstanding" name="fees_outstanding" value="<?php echo htmlspecialchars($student['fees_outstanding']); ?>" required>
                                </div>
                            </div>
                            
                            <div class="action-buttons">
                                <a href="manage_students.php" class="btn btn-outline-primary">
                                    <i class="fas fa-arrow-left me-2"></i> Back to Students
                                </a>
                                <button type="submit" class="btn btn-primary ms-auto">
                                    <i class="fas fa-save me-2"></i> Update Student
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Avatar preview functionality
            const avatarInput = document.getElementById('avatar-input');
            const avatarPreview = document.getElementById('avatar-preview');
            
            avatarInput.addEventListener('change', function() {
                const file = this.files[0];
                if (file) {
                    // Check file size (max 2MB)
                    if (file.size > 2 * 1024 * 1024) {
                        alert('File size exceeds 2MB. Please choose a smaller file.');
                        this.value = '';
                        return;
                    }
                    
                    const reader = new FileReader();
                    reader.onload = function(e) {
                        avatarPreview.src = e.target.result;
                    }
                    reader.readAsDataURL(file);
                }
            });
            
            // Form validation
            const form = document.querySelector('form');
            form.addEventListener('submit', function(e) {
                let valid = true;
                
                // Check required fields
                const requiredFields = form.querySelectorAll('[required]');
                requiredFields.forEach(field => {
                    if (!field.value.trim()) {
                        valid = false;
                        field.classList.add('is-invalid');
                    } else {
                        field.classList.remove('is-invalid');
                    }
                });
                
                // Validate email format
                const emailField = document.getElementById('email');
                const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
                if (emailField.value && !emailPattern.test(emailField.value)) {
                    valid = false;
                    emailField.classList.add('is-invalid');
                }
                
                if (!valid) {
                    e.preventDefault();
                    // Scroll to first invalid field
                    const firstInvalid = form.querySelector('.is-invalid');
                    if (firstInvalid) {
                        firstInvalid.scrollIntoView({ behavior: 'smooth', block: 'center' });
                    }
                }
            });
            
            // Add animation delay to form groups
            const formGroups = document.querySelectorAll('.form-group');
            formGroups.forEach((group, index) => {
                group.style.animationDelay = `${index * 0.05}s`;
            });
        });
    </script>
</body>
</html>

<?php
// Close database connection
$conn->close();
?>