<?php include 'koneksi.php'; 
session_start();
if (!isset($_SESSION['login_admin'])) {
    header('Location: dashboard.php');
    exit;
}


$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;

$where_clause = '';
if ($search){
    $where_clause = "WHERE nama_siswa LIKE '%$search%' OR kelas LIKE '%$search%'";
}
$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);

$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>kelola data siswa </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;
            text-transform: capitalize;
            color: #131a33;
            justify-content: left;
            text-align: left;
        }
        .fitur-tambahan {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            gap: 20px;
        }
        .card-siswa{
            margin:-80px auto;
            background: #fff;
            padding: auto;
            border-radius: 22px;
            max-width: 900px;
            display: grid;
            gap: 20px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            position: relative;
        }
        .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-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 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
        }
        .tambah_data_siswa a{
            display: inline-block;
            background-color: #0037fd;
            color: white;
            text-decoration: none;
            padding: 10px 20px;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }
        .tambah_data_siswa a:hover{
            background-color: #0026b3;
        }
        .action-buttons {
            display: flex;
            gap: 10px;
        }
        .edit-button, .delete-button {
            display: inline-block;
            padding: 5px 10px;
            text-decoration: none;
            border-radius: 4px;
            font-size: 12px;
            font-weight: bold;
            text-transform: uppercase;
            transition: background-color 0.3s ease;
        }
        .edit-button {
            background-color: #28a745;
            color: white;
        }
        .edit-button:hover {
            background-color: #218838;
        }
        .delete-button {
            background-color: #dc3545;
            color: white;
        }
        .delete-button:hover {
            background-color: #c82333;
        }

        /* Mobile Responsiveness */
        @media (max-width: 768px) {
            .hero-caption {
                padding: 40px 5%;
            }
            .hero-caption h1 {
                font-size: 30px;
            }
            .card-siswa {
                margin: -30px 5% 40px 15%;
                padding: 20px 5% 40px;
                max-width: none;
            }
            .siswa-card {
                padding: 0;
                margin: 5px;
            }
            .siswa-card table {
                font-size: 12px;
            }
            .siswa-card table th, .siswa-card table td {
                padding: 10px;
                font-size: 11px;
            }
            .fitur-tambahan {
                flex-direction: column;
                align-items: stretch;
                gap: 10px;
            }
            .jumlah-paginasi, .pencarian {
                display: flex;
                flex-direction: column;
                align-items: flex-start;
            }
            .pagination {
                display: flex;
                flex-wrap: wrap;
                justify-content: center;
                gap: 5px;
            }
            .pagination a, .pagination span {
                padding: 6px 8px;
                font-size: 12px;
            }
        }
    </style>
</head>
<body>
    <?php require 'navbar_admin.php'; ?>
    <div class="hero-caption">
        <!-- biarin kosong aja gw incer designnya -->
    </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="tambah_data_siswa">
                    <a href="tambahdata_siswa.php">tambah</a>
                    </div>
                    <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="20" <?php if($per_page == 20) echo 'selected'; ?>>20</option>
                            <option value="30" <?php if($per_page == 30) echo 'selected'; ?>>30</option>
                            <option value="50" <?php if($per_page == 50) echo 'selected'; ?>>50</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">seacrh</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>
                        <th>Aksi</th>
                    </tr>
                    <?php
                    $nomor = $offset + 1;
                    while($row = mysqli_fetch_assoc($results)){
                        $nama_siswa = htmlspecialchars($row['nama_siswa']);
                        $kelas = htmlspecialchars($row['kelas']);
                        $id_siswa = $row['id_siswa'];
                        echo "<tr>";
                        echo "<td>" . $nomor . "</td>";
                        echo "<td>" . $nama_siswa . "</td>";
                        echo "<td>" . $kelas . "</td>";
                        echo "<td><div class='action-buttons'><a class='edit-button' href='edit_siswa.php?id=" . $id_siswa . "'>edit</a> <a class='delete-button' href='hapus_siswa.php?id=" . $id_siswa . "'>hapus</a></div></td>";
                        echo "</tr>";
                        $nomor++;
                    }
                        ?>

                </table>
                </div>
                <?php 
                if ($total_pages > 1){
                    echo "<div class='pagination'>";
                    if ($page > 1){
                        $prev_page = $page - 1;
                        echo "<a href='siswa_admin.php?page=$prev_page&per_page=$per_page'>Previous</a>";
                    }else{
                        echo "<span class='disabled'>Previous</span>";
                    }

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

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