<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }

        form {
            background-color: #ffffff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            max-width: 600px;
            margin: auto;
        }

        label {
            display: block;
            margin: 10px 0 5px;
        }

        input[type="text"], input[type="file"], input[type="submit"], select,button {
            width: 100%;
            padding: 10px;
            margin: 5px 0 15px;
            border: 1px solid #cccccc;
            border-radius: 4px;
        }

        input[type="submit"],button {
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
        }

        input[type="submit"]:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<?php
include 'connection/config.php';
$pdo = new PDO('mysql:host=localhost;dbname=cbt', 'root', '');

// Initialize search query and students array
$searchQuery = '';
$students = [];

// Check if a search query is submitted
if (isset($_POST['search'])) {
    $searchQuery = $_POST['search_query'];

    // Prepare the statement to search for students
    $stmt = $pdo->prepare("SELECT * FROM students WHERE matric LIKE ? OR fname LIKE ? OR level LIKE ? OR dept LIKE ?");
    $stmt->execute(["%$searchQuery%", "%$searchQuery%", "%$searchQuery%", "%$searchQuery%"]);
    $students = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

// Initialize student variable
$student = null;

// Check if the student ID is provided for editing
if (isset($_POST['id'])) {
    $id = $_POST['id'];

    // Fetch student record based on the provided ID
    $stmt = $pdo->prepare("SELECT * FROM students WHERE id = ?");
    $stmt->execute([$id]);
    $student = $stmt->fetch(PDO::FETCH_ASSOC);

    // Check if the student exists
    if (!$student) {
        echo "Student not found.";
        exit; // Exit if student not found
    }

    // Update student record if form is submitted
    if (isset($_POST['update'])) {
        $matric = $_POST['matric'];
        $fname = $_POST['fname'];
        $level = $_POST['level'];
        $dept = $_POST['dept'];

        // Update student information in the database
        $updateStmt = $pdo->prepare("UPDATE students SET matric = ?, fname = ?, level = ?, dept = ? WHERE id = ?");
        $updateStmt->execute([$matric, $fname, $level, $dept, $id]);

        echo "Student record updated successfully!";
        // Redirect to prevent form resubmission
        header("Location: admincontrolcenter2.php?page=edit_student");
        exit;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student Management</title>
</head>
<body>

<h2>Search Student Records</h2>

<!-- Search Form -->
<form method="POST">
    <input type="text" name="search_query" placeholder="Search by Matric, Name, Level, or Department" required>
    <button type="submit" name="search">Search</button>
</form>

<!-- Display Student Records -->
<?php if ($students): ?>
    <h2>Search Results</h2>
    <table border="1">
        <tr>
            <th>ID</th>
            <th>Matric Number</th>
            <th>First Name</th>
            <th>Level</th>
            <th>Department</th>
            <th>Actions</th>
        </tr>
        <?php foreach ($students as $studentData): ?>
            <tr>
                <td><?php echo htmlspecialchars($studentData['id']); ?></td>
                <td><?php echo htmlspecialchars($studentData['matric']); ?></td>
                <td><?php echo htmlspecialchars($studentData['fname']); ?></td>
                <td><?php echo htmlspecialchars($studentData['level']); ?></td>
                <td><?php echo htmlspecialchars($studentData['dept']); ?></td>
                <td>
                    <form method="POST" style="display:inline;">
                        <input type="hidden" name="id" value="<?php echo htmlspecialchars($studentData['id']); ?>">
                        <button type="submit">Edit</button>
                    </form>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>
<?php elseif (isset($_POST['search'])): ?>
    <p>No student records found.</p>
<?php endif; ?>

<!-- Edit Student Form -->
<?php if ($student): ?>
    <h2>Edit Student Record</h2>
    <form method="POST">
        <input type="hidden" name="id" value="<?php echo htmlspecialchars($student['id']); ?>">

        <label for="matric">Matric Number:</label>
        <input type="text" name="matric" id="matric" value="<?php echo htmlspecialchars($student['matric']); ?>" required><br><br>

        <label for="fname">First Name:</label>
        <input type="text" name="fname" id="fname" value="<?php echo htmlspecialchars($student['fname']); ?>" required><br><br>

        <label for="level">Level:</label>
        <input type="text" name="level" id="level" value="<?php echo htmlspecialchars($student['level']); ?>" required><br><br>

        <label for="dept">Department:</label>
        <input type="text" name="dept" id="dept" value="<?php echo htmlspecialchars($student['dept']); ?>" required><br><br>

        <button type="submit" name="update">Update Student</button>
    </form>
<?php endif; ?>

</body>
</html>
