<?php
include 'config.php';

// Proses tambah siswa
if (isset($_POST['tambah_siswa'])) {
    $nis = $conn->real_escape_string($_POST['nis']);
    $nama = $conn->real_escape_string($_POST['nama']);
    $kelas = $conn->real_escape_string($_POST['kelas']);
    
    $sql = "INSERT INTO siswa (nis, nama, kelas) VALUES ('$nis', '$nama', '$kelas')";
    if ($conn->query($sql)) {
        $success = "Siswa berhasil ditambahkan";
    } else {
        $error = "Gagal menambahkan siswa: " . $conn->error;
    }
}

// Proses hapus siswa
if (isset($_GET['hapus'])) {
    $nis = $conn->real_escape_string($_GET['hapus']);
    $sql = "DELETE FROM siswa WHERE nis = '$nis'";
    if ($conn->query($sql)) {
        $success = "Siswa berhasil dihapus";
    } else {
        $error = "Gagal menghapus siswa: " . $conn->error;
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kelola Siswa - Aplikasi Kehadiran Ujian</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container">
            <a class="navbar-brand" href="index.php">
                <i class="fas fa-qrcode me-2"></i>Kehadiran Ujian
            </a>
            <div class="collapse navbar-collapse">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="index.php">Scan QR</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="data_kehadiran.php">Data Kehadiran</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="kelola_siswa.php">Kelola Siswa</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="generate_qr.php">Generate QR</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        <h2 class="mb-4"><i class="fas fa-users me-2"></i>Kelola Data Siswa</h2>
        
        <?php if (isset($success)): ?>
            <div class="alert alert-success"><?= $success ?></div>
        <?php endif; ?>
        
        <?php if (isset($error)): ?>
            <div class="alert alert-danger"><?= $error ?></div>
        <?php endif; ?>
        
        <div class="row">
            <div class="col-md-4">
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <h5 class="mb-0"><i class="fas fa-user-plus me-2"></i>Tambah Siswa</h5>
                    </div>
                    <div class="card-body">
                        <form method="POST">
                            <div class="mb-3">
                                <label class="form-label">NIS</label>
                                <input type="text" class="form-control" name="nis" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Nama Siswa</label>
                                <input type="text" class="form-control" name="nama" required>
                            </div>
                            <div class="mb-3">
                                <label class="form-label">Kelas</label>
                                <input type="text" class="form-control" name="kelas" required>
                            </div>
                            <button type="submit" name="tambah_siswa" class="btn btn-primary w-100">
                                <i class="fas fa-save me-2"></i>Simpan
                            </button>
                        </form>
                    </div>
                </div>
            </div>
            
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header bg-success text-white">
                        <h5 class="mb-0"><i class="fas fa-list me-2"></i>Daftar Siswa</h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th>No</th>
                                        <th>NIS</th>
                                        <th>Nama</th>
                                        <th>Kelas</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $sql = "SELECT * FROM siswa ORDER BY kelas, nama";
                                    $result = $conn->query($sql);
                                    $no = 1;
                                    
                                    if ($result->num_rows > 0) {
                                        while($row = $result->fetch_assoc()) {
                                            echo "<tr>
                                                    <td>{$no}</td>
                                                    <td>{$row['nis']}</td>
                                                    <td>{$row['nama']}</td>
                                                    <td>{$row['kelas']}</td>
                                                    <td>
                                                        <a href='generate_qr.php?siswa={$row['nis']}' class='btn btn-sm btn-info'>
                                                            <i class='fas fa-qrcode me-1'></i>QR
                                                        </a>
                                                        <a href='kelola_siswa.php?hapus={$row['nis']}' class='btn btn-sm btn-danger' onclick='return confirm(\"Yakin hapus siswa {$row['nama']}?\")'>
                                                            <i class='fas fa-trash me-1'></i>Hapus
                                                        </a>
                                                    </td>
                                                </tr>";
                                            $no++;
                                        }
                                    } else {
                                        echo "<tr><td colspan='5' class='text-center'>Tidak ada data siswa</td></tr>";
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
<?php $conn->close(); ?>