<?php
// ✅ પહેલા session start કરો
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

require_once '../config.php';

// Check if super admin is logged in
if (!isLoggedIn() || !isSuperAdmin()) {
    redirect('../index.php');
}

// ✅ SIMPLE DELETE - DATABASE માંથી PERMANENT DELETE
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    
    // Get student info for alert message
    $student_query = "SELECT full_name, enrollment_no FROM students WHERE id=$id";
    $student_result = mysqli_query($conn, $student_query);
    
    if(mysqli_num_rows($student_result) > 0) {
        $student = mysqli_fetch_assoc($student_result);
        $student_name = $student['full_name'];
        $enrollment = $student['enrollment_no'];
        
        // ✅ ONLY DELETE STUDENT
        $delete_query = "DELETE FROM students WHERE id=$id";
        
        if(mysqli_query($conn, $delete_query)) {
            // Show success alert
            echo "<script>
                alert('✅ Student DELETED successfully!\\\\n\\\\nName: $student_name\\\\nEnrollment: $enrollment\\\\n\\\\n✓ Removed from database');
                window.location.href = 'students.php';
            </script>";
            exit();
        } else {
            echo "<script>
                alert('❌ Delete failed! Error: " . addslashes(mysqli_error($conn)) . "');
                window.history.back();
            </script>";
            exit();
        }
    } else {
        echo "<script>
            alert('❌ Student not found!');
            window.history.back();
        </script>";
        exit();
    }
}

// Get all active students
$students_query = "SELECT s.*, c.center_name 
                   FROM students s 
                   LEFT JOIN centers c ON s.center_id = c.id 
                   WHERE s.status='active' 
                   ORDER BY s.created_at DESC";
$students_result = mysqli_query($conn, $students_query);

// Get student count
$student_count = mysqli_num_rows($students_result);

// If no students, add demo students
if ($student_count == 0) {
    // Get all active centers
    $centers_query = "SELECT * FROM centers WHERE status='active'";
    $centers_result = mysqli_query($conn, $centers_query);
    $centers = [];
    while($center = mysqli_fetch_assoc($centers_result)) {
        $centers[] = $center;
    }
    
    // Demo student data
    $demo_students = [
        [
            'enrollment_no' => 'STU2026001',
            'full_name' => 'Rajesh Patel',
            'father_name' => 'Ramesh Patel',
            'mobile' => '9876543210',
            'email' => 'rajesh.patel@email.com',
            'address' => 'Gandhinagar, Gujarat',
            'id_proof_type' => 'Aadhar Card',
            'id_proof_number' => '1234 5678 9012'
        ],
        [
            'enrollment_no' => 'STU2026002',
            'full_name' => 'Priya Sharma',
            'father_name' => 'Ravi Sharma',
            'mobile' => '8765432109',
            'email' => 'priya.sharma@email.com',
            'address' => 'Ahmedabad, Gujarat',
            'id_proof_type' => 'Aadhar Card',
            'id_proof_number' => '2345 6789 0123'
        ],
        [
            'enrollment_no' => 'STU2026003',
            'full_name' => 'Amit Verma',
            'father_name' => 'Sunil Verma',
            'mobile' => '7654321098',
            'email' => 'amit.verma@email.com',
            'address' => 'Vadodara, Gujarat',
            'id_proof_type' => 'PAN Card',
            'id_proof_number' => 'ABCDE1234F'
        ],
        [
            'enrollment_no' => 'STU2026004',
            'full_name' => 'Sneha Joshi',
            'father_name' => 'Mohan Joshi',
            'mobile' => '6543210987',
            'email' => 'sneha.joshi@email.com',
            'address' => 'Surat, Gujarat',
            'id_proof_type' => 'Aadhar Card',
            'id_proof_number' => '3456 7890 1234'
        ]
    ];
    
    // Add students
    foreach($centers as $index => $center) {
        for($i = 0; $i < 2 && isset($demo_students[$index * 2 + $i]); $i++) {
            $student = $demo_students[$index * 2 + $i];
            
            $insert_query = "INSERT INTO students 
                            (enrollment_no, full_name, father_name, mobile, email, address, 
                             id_proof_type, id_proof_number, center_id, status, admission_date, created_by, created_at) 
                            VALUES (
                                '{$student['enrollment_no']}',
                                '{$student['full_name']}',
                                '{$student['father_name']}',
                                '{$student['mobile']}',
                                '{$student['email']}',
                                '{$student['address']}',
                                '{$student['id_proof_type']}',
                                '{$student['id_proof_number']}',
                                {$center['id']},
                                'active',
                                NOW(),
                                '{$_SESSION['user_id']}',
                                NOW()
                            )";
            
            mysqli_query($conn, $insert_query);
            
            $student_id = mysqli_insert_id($conn);
            
            // Assign random course
            $courses_query = "SELECT id, fees FROM courses WHERE status='active' ORDER BY RAND() LIMIT 1";
            $courses_result = mysqli_query($conn, $courses_query);
            if(mysqli_num_rows($courses_result) > 0) {
                $course = mysqli_fetch_assoc($courses_result);
                
                $discount = rand(0, 20);
                $net_fee = $course['fees'] - ($course['fees'] * $discount / 100);
                
                $enroll_query = "INSERT INTO student_courses 
                                (student_id, course_id, fee, discount, net_fee, status, enrollment_date) 
                                VALUES (
                                    $student_id,
                                    {$course['id']},
                                    {$course['fees']},
                                    $discount,
                                    $net_fee,
                                    'active',
                                    NOW()
                                )";
                mysqli_query($conn, $enroll_query);
            }
        }
    }
    
    // Refresh query
    $students_result = mysqli_query($conn, $students_query);
    $student_count = mysqli_num_rows($students_result);
}

// Calculate statistics
$month = date('m');
$year = date('Y');
$new_this_month_query = "SELECT COUNT(*) as count FROM students 
                         WHERE status='active' 
                         AND MONTH(admission_date) = $month 
                         AND YEAR(admission_date) = $year";
$new_month_result = mysqli_query($conn, $new_this_month_query);
$new_month_data = mysqli_fetch_assoc($new_month_result);
$new_this_month = $new_month_data['count'] ?? 0;

// Get average course fees
$avg_fees_query = "SELECT AVG(net_fee) as avg_fees FROM student_courses WHERE status='active'";
$avg_fees_result = mysqli_query($conn, $avg_fees_query);
$avg_fees_data = mysqli_fetch_assoc($avg_fees_result);
$avg_fees = $avg_fees_data['avg_fees'] ?? 0;

// Calculate total pending fees
$pending_fees = 0;

// Get all active centers for filter
$centers_query = "SELECT * FROM centers WHERE status='active' ORDER BY center_name";
$centers_result = mysqli_query($conn, $centers_query);

// Display messages
if (isset($_SESSION['success'])) {
    $success = $_SESSION['success'];
    unset($_SESSION['success']);
}

if (isset($_SESSION['error'])) {
    $error = $_SESSION['error'];
    unset($_SESSION['error']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Students - Student Management</title>
    <style>
        /* Reset and Base Styles */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f8f9fa;
            color: #333;
            line-height: 1.6;
        }
        
        /* Header */
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 18px 35px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
            position: sticky;
            top: 0;
            z-index: 1000;
        }
        
        .header h1 {
            font-size: 24px;
            font-weight: 600;
            letter-spacing: 0.5px;
        }
        
        .header-actions {
            display: flex;
            gap: 15px;
            align-items: center;
        }
        
        .back-btn, .logout-btn {
            padding: 10px 18px;
            border-radius: 6px;
            text-decoration: none;
            font-weight: 600;
            font-size: 14px;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .back-btn {
            background: #4CAF50;
            color: white;
            border: 2px solid #4CAF50;
        }
        
        .back-btn:hover {
            background: #45a049;
            border-color: #45a049;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(76, 175, 80, 0.3);
        }
        
        .logout-btn {
            background: #ff4757;
            color: white;
            border: 2px solid #ff4757;
        }
        
        .logout-btn:hover {
            background: #ff3742;
            border-color: #ff3742;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(255, 71, 87, 0.3);
        }
        
        /* Navigation Menu */
        .nav-menu {
            background: white;
            padding: 0 35px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.08);
            border-bottom: 1px solid #eaeaea;
            display: flex;
            gap: 5px;
        }
        
        .nav-item {
            padding: 16px 24px;
            text-decoration: none;
            color: #555;
            font-weight: 600;
            font-size: 15px;
            border-bottom: 3px solid transparent;
            transition: all 0.3s ease;
            position: relative;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .nav-item:hover {
            color: #667eea;
            background: #f8f9ff;
        }
        
        .nav-item.active {
            color: #667eea;
            border-bottom: 3px solid #667eea;
            background: linear-gradient(to bottom, rgba(102, 126, 234, 0.05), transparent);
        }
        
        /* Main Container */
        .container {
            padding: 35px;
            max-width: 1400px;
            margin: 0 auto;
        }
        
        /* Page Title */
        .page-title {
            color: #2c3e50;
            margin-bottom: 30px;
            padding-bottom: 18px;
            border-bottom: 2px solid #667eea;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 20px;
        }
        
        .page-title h2 {
            font-size: 28px;
            font-weight: 700;
            display: flex;
            align-items: center;
            gap: 12px;
        }
        
        .page-title h2:before {
            content: "👥";
            font-size: 24px;
        }
        
        .add-btn {
            background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
            color: white;
            padding: 12px 25px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            text-decoration: none;
            font-weight: 600;
            font-size: 15px;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 10px;
            box-shadow: 0 4px 12px rgba(76, 175, 80, 0.2);
        }
        
        .add-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 16px rgba(76, 175, 80, 0.3);
            background: linear-gradient(135deg, #45a049 0%, #3d8b40 100%);
        }
        
        /* Stats Cards */
        .stats-container {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .stat-card {
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.08);
            text-align: center;
            transition: all 0.3s ease;
            border-top: 4px solid #667eea;
        }
        
        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 20px rgba(0,0,0,0.12);
        }
        
        .stat-card h3 {
            font-size: 14px;
            color: #6c757d;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 10px;
        }
        
        .stat-card .stat-number {
            font-size: 32px;
            font-weight: 700;
            color: #2c3e50;
            margin-bottom: 5px;
        }
        
        .stat-card .stat-subtitle {
            font-size: 13px;
            color: #28a745;
            font-weight: 600;
        }
        
        /* Alert Messages */
        .alert {
            padding: 16px 20px;
            margin-bottom: 25px;
            border-radius: 8px;
            font-weight: 600;
            font-size: 15px;
            display: flex;
            align-items: center;
            gap: 12px;
            animation: slideIn 0.5s ease;
            border-left: 5px solid;
        }
        
        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .alert.success {
            background: #d4edda;
            color: #155724;
            border-left-color: #28a745;
        }
        
        .alert.error {
            background: #f8d7da;
            color: #721c24;
            border-left-color: #dc3545;
        }
        
        /* Search and Filter */
        .filter-container {
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.08);
            margin-bottom: 25px;
            display: flex;
            gap: 20px;
            flex-wrap: wrap;
            align-items: flex-end;
        }
        
        .filter-group {
            flex: 1;
            min-width: 200px;
        }
        
        .filter-group label {
            display: block;
            margin-bottom: 8px;
            color: #495057;
            font-weight: 600;
            font-size: 13px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .filter-group input, .filter-group select {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 6px;
            font-size: 14px;
            transition: all 0.3s ease;
            background: #fcfcfc;
        }
        
        .filter-group input:focus, .filter-group select:focus {
            border-color: #667eea;
            outline: none;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .search-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 12px 25px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-weight: 600;
            font-size: 14px;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        
        .search-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
        }
        
        /* Table Container */
        .table-container {
            background: white;
            padding: 35px;
            border-radius: 12px;
            box-shadow: 0 6px 20px rgba(0,0,0,0.1);
            margin-bottom: 40px;
            border: 1px solid #eaeaea;
        }
        
        .table-title {
            color: #2c3e50;
            margin-bottom: 25px;
            font-size: 22px;
            font-weight: 700;
            display: flex;
            align-items: center;
            gap: 12px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f1f1f1;
        }
        
        .table-title:before {
            content: "📋";
            font-size: 20px;
        }
        
        table {
            width: 100%;
            border-collapse: separate;
            border-spacing: 0;
            margin-top: 20px;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 4px 12px rgba(0,0,0,0.05);
        }
        
        thead {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        th {
            padding: 18px 20px;
            text-align: left;
            font-weight: 700;
            font-size: 14px;
            color: white;
            text-transform: uppercase;
            letter-spacing: 0.8px;
            border-right: 1px solid rgba(255,255,255,0.1);
        }
        
        th:last-child {
            border-right: none;
        }
        
        tbody tr {
            transition: all 0.3s ease;
            border-bottom: 1px solid #f1f1f1;
        }
        
        tbody tr:hover {
            background: #f8f9ff;
            transform: scale(1.002);
            box-shadow: 0 4px 12px rgba(0,0,0,0.08);
        }
        
        td {
            padding: 20px;
            color: #495057;
            font-size: 14px;
            border-bottom: 1px solid #f1f1f1;
        }
        
        tbody tr:last-child td {
            border-bottom: none;
        }
        
        /* Enrollment No */
        .enrollment-no {
            background: #e9ecef;
            padding: 6px 12px;
            border-radius: 6px;
            font-weight: 700;
            color: #495057;
            font-family: 'Courier New', monospace;
            font-size: 12px;
            letter-spacing: 0.5px;
            border: 1px solid #dee2e6;
        }
        
        /* Status Badges */
        .status-badge {
            padding: 6px 14px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            display: inline-block;
            min-width: 80px;
            text-align: center;
        }
        
        .status-active { 
            background: linear-gradient(135deg, #28a745 0%, #1e7e34 100%);
            color: white;
            border: 1px solid #1e7e34;
        }
        
        .status-inactive { 
            background: linear-gradient(135deg, #6c757d 0%, #5a6268 100%);
            color: white;
            border: 1px solid #5a6268;
        }
        
        /* Center Badge */
        .center-badge {
            background: linear-gradient(135deg, #17a2b8 0%, #138496 100%);
            color: white;
            padding: 6px 14px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            display: inline-block;
            min-width: 100px;
            text-align: center;
        }
        
        /* Action Buttons */
        .action-btns {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }
        
        .view-btn, .edit-btn, .delete-btn, .enroll-btn {
            padding: 8px 16px;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            text-decoration: none;
            font-size: 13px;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            gap: 6px;
            transition: all 0.3s ease;
            min-width: 70px;
            justify-content: center;
        }
        
        .view-btn {
            background: linear-gradient(135deg, #17a2b8 0%, #138496 100%);
            color: white;
            border: 1px solid #138496;
        }
        
        .view-btn:hover {
            background: linear-gradient(135deg, #138496 0%, #117a8b 100%);
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(23, 162, 184, 0.3);
        }
        
        .edit-btn {
            background: linear-gradient(135deg, #ffa502 0%, #e59400 100%);
            color: white;
            border: 1px solid #e59400;
        }
        
        .edit-btn:hover {
            background: linear-gradient(135deg, #e59400 0%, #cc8400 100%);
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(229, 148, 0, 0.3);
        }
        
        .delete-btn {
            background: linear-gradient(135deg, #ff4757 0%, #ff3742 100%);
            color: white;
            border: 1px solid #ff3742;
        }
        
        .delete-btn:hover {
            background: linear-gradient(135deg, #ff3742 0%, #ff1e2b 100%);
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(255, 71, 87, 0.3);
        }
        
        /* Enroll Button - NEW */
        .enroll-btn {
            background: linear-gradient(135deg, #9C27B0 0%, #7B1FA2 100%);
            color: white;
            border: 1px solid #7B1FA2;
        }
        
        .enroll-btn:hover {
            background: linear-gradient(135deg, #7B1FA2 0%, #6A1B9A 100%);
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(156, 39, 176, 0.3);
        }
        
        /* No Data */
        .no-data {
            text-align: center;
            padding: 60px 40px;
            color: #6c757d;
            font-size: 18px;
            font-weight: 500;
            background: #f8f9fa;
            border-radius: 10px;
            border: 2px dashed #dee2e6;
            margin: 20px 0;
        }
        
        .no-data:before {
            content: "👤";
            font-size: 40px;
            display: block;
            margin-bottom: 15px;
            opacity: 0.5;
        }
        
        /* Bottom Navigation */
        .bottom-nav {
            margin-top: 40px;
            text-align: center;
            display: flex;
            justify-content: center;
            gap: 15px;
            flex-wrap: wrap;
        }
        
        .bottom-nav a {
            padding: 14px 28px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            text-decoration: none;
            border-radius: 8px;
            font-weight: 600;
            font-size: 15px;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 10px;
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
        }
        
        .bottom-nav a:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(102, 126, 234, 0.3);
        }
        
        /* Responsive Design */
        @media (max-width: 992px) {
            .header, .nav-menu, .container {
                padding: 15px;
            }
            
            .nav-menu {
                overflow-x: auto;
                padding-bottom: 5px;
            }
            
            table {
                display: block;
                overflow-x: auto;
            }
            
            .action-btns {
                flex-direction: column;
                min-width: 120px;
            }
        }
        
        @media (max-width: 768px) {
            .header {
                flex-direction: column;
                gap: 15px;
                text-align: center;
            }
            
            .page-title {
                flex-direction: column;
                text-align: center;
            }
            
            .filter-container {
                flex-direction: column;
            }
            
            .filter-group {
                min-width: 100%;
            }
            
            .bottom-nav {
                flex-direction: column;
            }
            
            .bottom-nav a {
                width: 100%;
                justify-content: center;
            }
        }
    </style>
</head>
<body>
    <!-- Header -->
    <div class="header">
        <h1>STUDENT MANAGEMENT SYSTEM - SUPER ADMIN</h1>
        <div class="header-actions">
            <a href="dashboard.php" class="back-btn">
                <span>←</span> Back to Dashboard
            </a>
            <a href="../logout.php" class="logout-btn" 
               onclick="return confirm('Are you sure you want to logout?')">
                <span>🚪</span> Logout
            </a>
        </div>
    </div>
    
    <!-- Navigation Menu -->
    <div class="nav-menu">
        <a href="dashboard.php" class="nav-item">
            <span>📊</span> Dashboard
        </a>
        <a href="centers.php" class="nav-item">
            <span>🏢</span> Centers
        </a>
        <a href="courses.php" class="nav-item">
            <span>📚</span> Courses
        </a>
        <a href="students.php" class="nav-item active">
            <span>👥</span> Students
        </a>
        <a href="reports.php" class="nav-item">
            <span>📈</span> Reports
        </a>
        <a href="enquiries.php" class="nav-item">
            <span>📞</span> Enquiries
        </a>
    </div>
    
    <!-- Main Content -->
    <div class="container">
        <!-- Page Title -->
        <div class="page-title">
            <h2>Manage Students</h2>
            <a href="add_student.php" class="add-btn">
                <span>➕</span> Add New Student
            </a>
        </div>
        
        <!-- Success/Error Messages -->
        <?php if(isset($success)): ?>
            <div class="alert success">
                <span>✅</span> <?php echo $success; ?>
            </div>
        <?php endif; ?>
        
        <?php if(isset($error)): ?>
            <div class="alert error">
                <span>❌</span> <?php echo $error; ?>
            </div>
        <?php endif; ?>
        
        <!-- Stats Cards -->
        <div class="stats-container">
            <div class="stat-card">
                <h3>Total Students</h3>
                <div class="stat-number"><?php echo $student_count; ?></div>
                <div class="stat-subtitle">Active Students</div>
            </div>
            
            <div class="stat-card">
                <h3>New This Month</h3>
                <div class="stat-number"><?php echo $new_this_month; ?></div>
                <div class="stat-subtitle"><?php echo date('F Y'); ?></div>
            </div>
            
            <div class="stat-card">
                <h3>Pending Fees</h3>
                <div class="stat-number">₹<?php echo number_format($pending_fees, 0); ?></div>
                <div class="stat-subtitle">Total Due</div>
            </div>
            
            <div class="stat-card">
                <h3>Avg. Course Fees</h3>
                <div class="stat-number">₹<?php echo number_format($avg_fees, 0); ?></div>
                <div class="stat-subtitle">Per Student</div>
            </div>
        </div>
        
        <!-- Search and Filter -->
        <div class="filter-container">
            <div class="filter-group">
                <label for="search">Search Student</label>
                <input type="text" id="search" name="search" 
                       placeholder="Search by name, enrollment no, phone...">
            </div>
            
            <div class="filter-group">
                <label for="center">Filter by Center</label>
                <select id="center" name="center">
                    <option value="">All Centers</option>
                    <?php 
                    // Reset centers result pointer
                    mysqli_data_seek($centers_result, 0);
                    while($center = mysqli_fetch_assoc($centers_result)): 
                    ?>
                    <option value="<?php echo $center['id']; ?>"><?php echo htmlspecialchars($center['center_name']); ?></option>
                    <?php endwhile; ?>
                </select>
            </div>
            
            <div class="filter-group">
                <label for="status">Filter by Status</label>
                <select id="status" name="status">
                    <option value="">All Status</option>
                    <option value="active">Active</option>
                    <option value="inactive">Inactive</option>
                    <option value="pending">Pending</option>
                </select>
            </div>
            
            <button type="button" class="search-btn">
                <span>🔍</span> Search
            </button>
            
            <a href="students.php" class="search-btn" style="background: linear-gradient(135deg, #6c757d 0%, #5a6268 100%);">
                <span>🔄</span> Reset
            </a>
        </div>
        
        <!-- Students List -->
        <div class="table-container">
            <h3 class="table-title">All Students (<?php echo $student_count; ?> Students)</h3>
            
            <?php if($student_count > 0): ?>
            <table id="students-table">
                <thead>
                    <tr>
                        <th>Enrollment No</th>
                        <th>Student Details</th>
                        <th>Center</th>
                        <th>Contact Info</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    // Reset students result pointer
                    mysqli_data_seek($students_result, 0);
                    
                    while($student = mysqli_fetch_assoc($students_result)): 
                        // Format phone number
                        $phone = $student['mobile'] ?? 'N/A';
                        if($phone != 'N/A' && strlen($phone) >= 10) {
                            $phone = substr($phone, 0, 5) . ' ' . substr($phone, 5);
                        }
                        
                        // Student courses count
                        $course_query = "SELECT COUNT(*) as course_count FROM student_courses WHERE student_id = {$student['id']} AND status='active'";
                        $course_result = mysqli_query($conn, $course_query);
                        $course_data = mysqli_fetch_assoc($course_result);
                        $course_count = $course_data['course_count'] ?? 0;
                    ?>
                    <tr>
                        <td>
                            <span class="enrollment-no"><?php echo htmlspecialchars($student['enrollment_no']); ?></span>
                        </td>
                        <td>
                            <div style="margin-bottom: 8px;">
                                <strong style="font-size: 15px; color: #2c3e50;"><?php echo htmlspecialchars($student['full_name']); ?></strong>
                            </div>
                            <div style="font-size: 13px; color: #6c757d;">
                                <?php echo $course_count; ?> Courses • 
                                Admission: <?php echo date('d M Y', strtotime($student['admission_date'])); ?>
                            </div>
                        </td>
                        <td>
                            <?php if($student['center_name']): ?>
                                <span class="center-badge"><?php echo $student['center_name']; ?></span>
                            <?php else: ?>
                                <span style="color: #6c757d; font-style: italic;">Not Assigned</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <div style="margin-bottom: 5px;">
                                <span style="font-weight: 500;">📱 <?php echo $phone; ?></span>
                            </div>
                            <div>
                                <span style="font-weight: 500;">📧 <?php echo htmlspecialchars($student['email'] ?? 'N/A'); ?></span>
                            </div>
                        </td>
                        <td>
                            <span class="status-badge status-<?php echo $student['status']; ?>">
                                <?php echo ucfirst($student['status']); ?>
                            </span>
                        </td>
                        <td>
                            <div class="action-btns">
                                <a href="view_student.php?id=<?php echo $student['id']; ?>" 
                                   class="view-btn" title="View Student">
                                    <span>👁️</span> View
                                </a>
                                <a href="edit_student.php?id=<?php echo $student['id']; ?>" 
                                   class="edit-btn" title="Edit Student">
                                    <span>✏️</span> Edit
                                </a>
                                <a href="?delete=<?php echo $student['id']; ?>" 
                                   class="delete-btn"
                                   onclick="return confirm('Are you sure you want to PERMANENTLY delete <?php echo addslashes($student['full_name']); ?>?\\n\\n⚠️ This will delete from database permanently!')"
                                   title="Delete Student">
                                    <span>🗑️</span> Delete
                                </a>
                                <!-- ✅ NEW ENROLL COURSE BUTTON -->
                                <a href="enroll_course.php?student_id=<?php echo $student['id']; ?>" 
                                   class="enroll-btn" title="Enroll Course">
                                    <span>📚</span> Enroll
                                </a>
                            </div>
                        </td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
            <?php else: ?>
                <div class="no-data">
                    No students found. Add your first student using the button above.
                </div>
            <?php endif; ?>
        </div>
        
        <!-- Bottom Navigation -->
        <div class="bottom-nav">
            <a href="courses.php">
                <span>←</span> Back to Courses
            </a>
            <a href="dashboard.php">
                <span>📊</span> Dashboard
            </a>
            <a href="reports.php">
                Go to Reports <span>→</span>
            </a>
        </div>
    </div>
    
    <script>
        // Search functionality
        document.querySelector('.search-btn').addEventListener('click', function() {
            const search = document.getElementById('search').value;
            const center = document.getElementById('center').value;
            const status = document.getElementById('status').value;
            
            // Build query string
            let query = [];
            if (search) query.push(`search=${encodeURIComponent(search)}`);
            if (center) query.push(`center=${center}`);
            if (status) query.push(`status=${status}`);
            
            // Redirect with filters
            if (query.length > 0) {
                window.location.href = `students.php?${query.join('&')}`;
            } else {
                alert('Please enter search criteria');
            }
        });
        
        // Enter key search
        document.getElementById('search').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                document.querySelector('.search-btn').click();
            }
        });
        
        // Add ripple effect to buttons
        document.querySelectorAll('.back-btn, .logout-btn, .add-btn, .search-btn, .view-btn, .edit-btn, .delete-btn, .enroll-btn, .nav-item, .bottom-nav a').forEach(btn => {
            btn.addEventListener('click', function(e) {
                // Create ripple element
                const ripple = document.createElement('span');
                const rect = this.getBoundingClientRect();
                const size = Math.max(rect.width, rect.height);
                const x = e.clientX - rect.left - size / 2;
                const y = e.clientY - rect.top - size / 2;
                
                ripple.style.cssText = `
                    position: absolute;
                    border-radius: 50%;
                    background: rgba(255, 255, 255, 0.5);
                    transform: scale(0);
                    animation: ripple-animation 0.6s linear;
                    width: ${size}px;
                    height: ${size}px;
                    left: ${x}px;
                    top: ${y}px;
                    pointer-events: none;
                `;
                
                this.style.position = 'relative';
                this.style.overflow = 'hidden';
                this.appendChild(ripple);
                
                // Remove ripple after animation
                setTimeout(() => {
                    ripple.remove();
                }, 600);
            });
        });
        
        // Add CSS for ripple animation
        const style = document.createElement('style');
        style.textContent = `
            @keyframes ripple-animation {
                to {
                    transform: scale(4);
                    opacity: 0;
                }
            }
        `;
        document.head.appendChild(style);
    </script>
</body>
</html>