<?php
// admin/users/all_students.php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once "../../config/database.php";

if (!isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../../auth/login.php"); exit();
}

// --- SEARCH LOGIC ---
$search = $_GET['search'] ?? '';
$where_clause = "";
$params = [];

if (!empty($search)) {
    $where_clause = " WHERE s.fullname LIKE ? OR s.reg_number LIKE ? ";
    $params = ["%$search%", "%$search%"];
}

// --- FETCH ALL STUDENTS ---
try {
    $sql = "SELECT s.*, c.class_name 
            FROM students s 
            JOIN classes c ON s.class_id = c.id 
            $where_clause 
            ORDER BY s.fullname ASC";
    $stmt = $pdo->prepare($sql);
    $stmt->execute($params);
    $all_students = $stmt->fetchAll();
} catch (PDOException $e) {
    die("Error fetching students: " . $e->getMessage());
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>All Students List | Admin</title>
    <style>
        body { font-family: 'Segoe UI', Tahoma, sans-serif; background: #f0f2f5; padding: 20px; }
        .container { max-width: 1200px; margin: auto; }
        .card { background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
        
        /* Header & Search */
        .header-flex { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
        .search-box { display: flex; gap: 10px; margin-bottom: 20px; }
        .search-input { flex: 1; padding: 12px; border: 1px solid #ddd; border-radius: 8px; outline: none; }
        .btn-search { background: #3498db; color: white; border: none; padding: 10px 20px; border-radius: 8px; cursor: pointer; font-weight: bold; }
        
        /* Table Styling */
        table { width: 100%; border-collapse: collapse; background: white; }
        th { background: #2c3e50; color: white; padding: 15px; text-align: left; position: sticky; top: 0; }
        td { padding: 12px; border-bottom: 1px solid #eee; vertical-align: middle; }
        tr:hover { background: #f9f9f9; }
        
        .student-img { width: 45px; height: 45px; border-radius: 50%; object-fit: cover; border: 2px solid #3498db; }
        .badge { background: #e8f4fd; color: #2980b9; padding: 4px 10px; border-radius: 20px; font-size: 12px; font-weight: bold; }
        
        /* Action Buttons */
        .btn-edit { 
            background: #3498db; color: white; padding: 6px 12px; 
            text-decoration: none; border-radius: 5px; font-size: 12px; font-weight: bold; 
        }
        .btn-delete { 
            background: #e74c3c; color: white; padding: 6px 12px; 
            text-decoration: none; border-radius: 5px; font-size: 12px; font-weight: bold; 
        }
        .btn-edit:hover { background: #2980b9; }
        .btn-delete:hover { background: #c0392b; }

        .back-btn { text-decoration: none; color: #3498db; font-weight: bold; display: inline-block; margin-bottom: 15px; }
    </style>
</head>
<body>

<div class="container">
    <a href="students.php" class="back-btn">← Back to Registration</a>
    
    <div class="card">
        <div class="header-flex">
            <h2>Full Student Directory</h2>
            <p style="color: #666;"><?= count($all_students) ?> Students Enrolled</p>
        </div>
        
        <form method="GET" class="search-box">
            <input type="text" name="search" class="search-input" 
                   placeholder="Search by Name or Registration Number..." 
                   value="<?= htmlspecialchars($search) ?>">
            <button type="submit" class="btn-search">Search</button>
            <?php if(!empty($search)): ?>
                <a href="all_students.php" style="padding: 10px; color: #e74c3c; text-decoration: none;">Clear Search</a>
            <?php endif; ?>
        </form>

        <table>
            <thead>
                <tr>
                    <th>Photo</th>
                    <th>Full Name</th>
                    <th>Reg No</th>
                    <th>Class</th>
                    <th style="text-align: center;">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if (count($all_students) > 0): ?>
                    <?php foreach ($all_students as $s): ?>
                    <tr>
                        <td>
                            <img src="../../uploads/students/<?= $s['photo'] ?>" 
                                 class="student-img" 
                                 onerror="this.src='../../uploads/students/default.png'">
                        </td>
                        <td><strong><?= htmlspecialchars($s['fullname']) ?></strong></td>
                        <td><code><?= htmlspecialchars($s['reg_number']) ?></code></td>
                        <td><span class="badge"><?= htmlspecialchars($s['class_name']) ?></span></td>
                        <td>
                            <div style="display: flex; gap: 10px; justify-content: center;">
                                <a href="edit_student.php?id=<?= $s['id'] ?>" class="btn-edit">Edit</a>
                                
                                <a href="students.php?delete_id=<?= $s['id'] ?>" 
                                   class="btn-delete" 
                                   onclick="return confirm('WARNING: Are you sure you want to delete this student permanently?')">
                                   Delete
                                </a>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="5" style="text-align: center; padding: 50px; color: #999;">
                            No students found matching your search criteria.
                        </td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

</body>
</html>