<?php include 'koneksi.php'; ?>

<?php
// Handle GET parameters
$per_page = isset($_GET['per_page']) ? (int)$_GET['per_page'] : 10;
$search = isset($_GET['search']) ? mysqli_real_escape_string($host, $_GET['search']) : '';
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $per_page;

// Build WHERE clause for search
$where_clause = '';
if (!empty($search)) {
    $where_clause = "WHERE nama_siswa LIKE '%$search%' OR kelas LIKE '%$search%'";
}

// Get total count for pagination
$total_query = "SELECT COUNT(*) as total FROM tb_siswa $where_clause";
$total_result = mysqli_query($host, $total_query);
$total_row = mysqli_fetch_assoc($total_result);
$total_records = $total_row['total'];
$total_pages = ceil($total_records / $per_page);

// Build main query with LIMIT and OFFSET
$query = "SELECT * FROM tb_siswa $where_clause ORDER BY kelas ASC LIMIT $per_page OFFSET $offset";
$results = mysqli_query($host, $query);
?>
<!DOCTYPE html>
<html lang="en">
<head><link rel="icon" href="upload/bn2.png" type="image/png">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Data siswa | Smk Budiniah 2</title>
    <style>
        body{
            margin: 0;
            font-family: 'Times New Roman', Times, serif;
        }
        body a{
            cursor: pointer;
        }
        .hero-caption{
            width: 100%;
            background: #eef5ff;
            justify-content: center;
            text-align: center;
            padding: 60px 0 120px;
            padding-left: 5%;
            padding-right: 5%
        }
        .hero-caption h1{
            margin-bottom: 20px;
            font-size: 50px;
            font-weight: bold;
            text-transform: capitalize;
            color: #131a33;
        }
        .child-caption{
            margin-bottom: 20px;
            text-align: center;
            border-bottom: 2px #131a33 solid;
            padding-bottom: 20px;
        }
        .child-caption h2{
            font-size: 20px;
            font-weight: bold;
            justify-content: left;
            text-align: left;
            color: #131a33;
            text-transform: capitalize;
        }
        .fitur-tambahan{
            display: flex;
            flex-direction: row;
            justify-content: space-around;
            align-items: center;
            margin-bottom: 20px;
            gap: 20px;
        }
        .card-siswa{
            margin: -80px auto 40px auto;
            background: #fff;
            padding: 30px;
            border-radius: 15px;
            max-width: 1100px;
            display: grid;
            gap: 20px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            position: relative;
            z-index: 10;
        }
        .siswa-card{
            background:white ;
            border-radius:10px;
            padding: 20px;
            max-width: 1100px;
            display: flex;
            flex-direction: column;
            align-items: center;
            margin: 10px;
            text-transform: capitalize;
        }
        .siswa-card table{
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }
        .siswa-card table th, .siswa-card table td{
            padding: 8px;
            text-transform: capitalize;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        .siswa-card table th{
            background: #f8f9fa;
            color: #333;
            padding: 15px;
            text-align: left;
            font-weight: bold;
            text-transform: uppercase;
            font-size: 12px;
            letter-spacing: 1px;
            border-bottom: 2px solid #dee2e6;
        }
        .siswa-card table td{
            background-color: #fff;
        }
        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 20px;
            gap: 10px;
        }
        .pagination a, .pagination span {
            padding: 8px 12px;
            text-decoration: none;
            border: 1px solid #ddd;
            color: #131a33;
            background-color: #fff;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        .pagination a:hover {
            background-color: #eef5ff;
        }
        .pagination .active {
            background-color: #131a33;
            color: white;
            border-color: #131a33;
        }
        .pagination .disabled {
            color: #ccc;
            cursor: not-allowed;
        }

        /* Responsive Design */
        @media (max-width: 768px) {
            .hero-caption {
                padding: 40px 5% 80px;
            }
            .hero-caption h1 {
                font-size: 30px;
            }
            .card-siswa {
                margin: -60px auto 20px auto;
                padding: 20px;
                max-width: 95%;
            }
            .siswa-card {
                padding: 15px;
                margin: 5px;
            }
            .child-caption h2 {
                font-size: 18px;
            }
            .fitur-tambahan {
                flex-direction: column;
                gap: 15px;
                align-items: stretch;
            }
            .jumlah-paginasi, .pencarian {
                display: flex;
                flex-direction: column;
                align-items: flex-start;
            }
            .jumlah-paginasi p, .pencarian label {
                margin-bottom: 5px;
                font-weight: bold;
            }
            .siswa-card table {
                font-size: 14px;
                overflow-x: auto;
                display: block;
                white-space: nowrap;
            }
            .siswa-card table th, .siswa-card table td {
                padding: 6px;
                min-width: 100px;
            }
            .pagination {
                flex-wrap: wrap;
                gap: 5px;
            }
            .pagination a, .pagination span {
                padding: 6px 10px;
                font-size: 14px;
            }
        }

        @media (max-width: 480px) {
            .hero-caption h1 {
                font-size: 24px;
            }
            .card-siswa {
                padding: 15px;
            }
            .siswa-card table th, .siswa-card table td {
                padding: 4px;
                font-size: 12px;
            }
        }
    </style>
</head>
<body>
    <?php require 'navbar.php'; ?>
    <div class="hero-caption">
        <h1>Data Siswa | Smk Budiniah 2</h1>
    </div>
    <div class="card-siswa">
        <?php
        if ($results && mysqli_num_rows($results) > 0){
            ?>
            <div class="siswa-card">
                <div class="child-caption">
                <h2>data siswa</h2>
                </div>
                <form method="GET" id="filter-form">
                <div class="fitur-tambahan">
                <div class="jumlah-paginasi">
                    <p>show :</p>
                    <select name="per_page" id="per_page" onchange="document.getElementById('filter-form').submit();">
                        <option value="10" <?php if($per_page == 10) echo 'selected'; ?>>10</option>
                        <option value="25" <?php if($per_page == 25) echo 'selected'; ?>>25</option>
                        <option value="50" <?php if($per_page == 50) echo 'selected'; ?>>50</option>
                        <option value="100" <?php if($per_page == 100) echo 'selected'; ?>>100</option>
                    </select>
                </div>
                <div class="pencarian">
                    <label for="search">search</label>
                    <input type="text" name="search" id="search" value="<?php echo htmlspecialchars($search); ?>" onkeyup="document.getElementById('filter-form').submit();">
                </div>
                </div>
                </form>
                <table>
                <tr>
                    <th>No</th>
                    <th>Nama Siswa</th>
                    <th>Kelas</th>
                </tr>
                <?php
                $nomor = $offset + 1;
                while ($row = mysqli_fetch_assoc($results)){
                    $nama_siswa = htmlspecialchars($row['nama_siswa']);
                    $kelas = htmlspecialchars($row['kelas']);
                    echo "<tr>";
                    echo "<td>" . $nomor . "</td>";
                    echo "<td>" . $nama_siswa . "</td>";
                    echo "<td>" . $kelas . "</td>";
                    echo "</tr>";
                    $nomor++;
                }
                ?>
            </table>
            </div>
            <?php
            // Pagination
            if ($total_pages > 1) {
                echo '<div class="pagination">';
                // Previous button
                if ($page > 1) {
                    $prev_page = $page - 1;
                    echo '<a href="?page=' . $prev_page . '&per_page=' . $per_page . '&search=' . urlencode($search) . '">&laquo; Previous</a>';
                } else {
                    echo '<span class="disabled">&laquo; Previous</span>';
                }

                // Page numbers
                for ($i = 1; $i <= $total_pages; $i++) {
                    if ($i == $page) {
                        echo '<span class="active">' . $i . '</span>';
                    } else {
                        echo '<a href="?page=' . $i . '&per_page=' . $per_page . '&search=' . urlencode($search) . '">' . $i . '</a>';
                    }
                }

                // Next button
                if ($page < $total_pages) {
                    $next_page = $page + 1;
                    echo '<a href="?page=' . $next_page . '&per_page=' . $per_page . '&search=' . urlencode($search) . '">Next &raquo;</a>';
                } else {
                    echo '<span class="disabled">Next &raquo;</span>';
                }
                echo '</div>';
            }
            ?>
            <?php
        }else{
            echo "<div class='siswa-card'>";
            echo "<p>Tidak ada data siswa tersedia.</p>";
            echo "</div>";
        }
        ?>
    </div>

    <?php require 'footer.php'; ?>
</body>
</html>
