<?php 
include("inc/db.php");

// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Initialize the search term
$searchTerm = isset($_GET['search']) ? mysqli_real_escape_string($db, $_GET['search']) : '';

// Modify the query to include a search filter
$query = "SELECT * FROM staff_reg";
if ($searchTerm) {
    $query .= " WHERE fname LIKE '%$searchTerm%' OR dept LIKE '%$searchTerm%' OR cate LIKE '%$searchTerm%' OR design LIKE '%$searchTerm%' OR academic LIKE '%$searchTerm%' OR publi LIKE '%$searchTerm%'";
}
$result = mysqli_query($db, $query);
$serialNumber = 1;
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Staff Registration</title>
    <link rel="stylesheet" href="bootstrap-4.6.2-dist/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h2 style="padding:70px;" align="center"><b>STAFF LIST</b></h2>
    
    <!-- Search Bar -->
    <div class="row form-group">
        <div class="col-md-12">
            <form action="" method="GET">
                <input type="text" name="search" class="form-control" placeholder="Search for names, department, etc." value="<?php echo htmlspecialchars($searchTerm); ?>">
                <button type="submit" class="btn btn-primary mt-2">Search</button>
            </form>
        </div>
    </div>
    
    <!-- Staff Data Table -->
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Serial Number</th>
                <th>Image</th>
                <th>Full Name</th>
                <th>Department</th>
                <th>Category</th>
                <th>Designation</th>
                <th>Academic Qualification</th>
                <th>Publication</th>
                <th>View</th>
            </tr>
        </thead>
        <tbody>
            <?php
            if (mysqli_num_rows($result) > 0) {
                while ($row = mysqli_fetch_assoc($result)) {
                    echo "<tr>
                        <td>{$serialNumber}</td>
                        <td><img src='uploads/{$row['good']}' alt='Passport' width='50' height='50'></td>
                        <td>{$row['fname']}</td>
                        <td>{$row['dept']}</td>
                        <td>{$row['cate']}</td>
                        <td>{$row['design']}</td>
                        <td>{$row['academic']}</td>
                        <td>{$row['publi']}</td>
                        <td><a href='staff_profi.php?id={$row['id']}' class='btn btn-info btn-sm'>View Profile</a></td>
                    </tr>";
                    $serialNumber++;
                }
            } else {
                echo "<tr><td colspan='9' align='center'>No data found</td></tr>";
            }
            ?>
        </tbody>
    </table>
</div>
</body>
</html>
