<?php
// Database connection parameters
$servername = "localhost";
    $username = "nifzdonk_user";
    $password = "s1w&afHHJnu3";
    $dbname = "nifzdonk_carnival";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Filter by unique id (if provided)
$filterById = "";
if (isset($_GET['filter_id'])) {
    $filterById = $_GET['filter_id'];
}

$sql = "SELECT * FROM registrations";
if (!empty($filterById)) {
    $sql .= " WHERE unique_id LIKE '%$filterById%'";
}

$result = $conn->query($sql);

// Close database connection
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Carnival Registration Information</title>
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #dddddd;
            text-align: left;
            padding: 8px;
        }
        th {
            background-color: #f2f2f2;
        }
        input[type=text] {
            padding: 5px;
        }
        button {
            padding: 5px 10px;
        }
    </style>
</head>
<body>
    <h2>Carnival Registration Information</h2>

    <!-- Filter by id form -->
    <form method="get" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
        <label for="filterById">Filter by Unique Id:</label>
        <input type="text" id="filterById" name="filter_id" value="<?php echo $filterById; ?>">
        <button type="submit">Filter</button>
    </form>

    <!-- Display data in table format -->
    <?php if ($result->num_rows > 0): ?>
        <h3>Registered Participants</h3>
        <table>
            <tr>
                <th>Unique Id</th>
                <th>Student's Name</th>
                <th>Standard/Class</th>
                <th>Parent's Name</th>
                <th>Number of Members</th>
                <th>Vehicle Type</th>
                <th>Parking Allotment</th>
                <th>Area</th>
                <th>Day</th>
            </tr>
            <?php while ($row = $result->fetch_assoc()): ?>
                <tr>
                    <td><?php echo $row["unique_id"]; ?></td>
                    <td><?php echo $row["student_name"]; ?></td>
                    <td><?php echo $row["std"]; ?></td>
                    <td><?php echo $row["parent_name"]; ?></td>
                    <td><?php echo $row["num_members"]; ?></td>
                    <td><?php echo $row["vehicle_type"]; ?></td>
                    <td><?php echo $row["parking"]; ?></td>
                    <td><?php echo $row["area"]; ?></td>
                    <td><?php echo $row["day"]; ?></td>
                </tr>
            <?php endwhile; ?>
        </table>
    <?php else: ?>
        <p>No records found.</p>
    <?php endif; ?>
</body>
</html>
