<?php
/**
 * ============================================================
 * SIMPAS v1.0
 * Modul  : Data Siswa
 * File   : index.php
 * ============================================================
 */

include "../../config/koneksi.php";
include "../../config/session.php";

include "../../templates/header.php";
include "../../templates/navbar.php";
include "../../templates/sidebar.php";

/*
|--------------------------------------------------------------------------
| Ambil Data Siswa
|--------------------------------------------------------------------------
*/

$query = mysqli_query($conn, "
SELECT
    s.*,
    k.nama_kelas
FROM siswa s
LEFT JOIN kelas k
ON s.id_kelas = k.id_kelas
ORDER BY s.nama_siswa ASC
");

?>

<div class="content-wrapper">

<section class="content-header">

<div class="container-fluid">

<div class="row mb-2">

<div class="col-sm-6">

<h1>Data Siswa</h1>

</div>

<div class="col-sm-6 text-right">

<a href="tambah.php" class="btn btn-success">

<i class="fas fa-user-plus"></i>

Tambah Siswa

</a>

<a href="import_excel.php" class="btn btn-primary">

<i class="fas fa-file-excel"></i>

Import Excel

</a>

<a href="generate_semua_qr.php"
class="btn btn-warning"
onclick="return confirm('Generate QR untuk semua siswa?')">

Generate Semua QR

</a>

<a href="cetak_semua.php" class="btn btn-danger">

<i class="fas fa-id-card"></i>

Cetak Semua Kartu

</a>

</div>

</div>

</div>

</section>

<section class="content">

<div class="container-fluid">

<div class="card shadow">

<div class="card-header">

<h3 class="card-title">

Daftar Data Siswa

</h3>

</div>

<div class="card-body">

<div class="table-responsive">

<table
id="tabelSiswa"
class="table table-bordered table-striped table-hover">

<thead>

<tr>

<th width="50">No</th>

<th width="80">Foto</th>

<th>NIS</th>

<th>NISN</th>

<th>Nama Siswa</th>

<th>Kelas</th>

<th>JK</th>

<th>QR Code</th>

<th>Status</th>

<th width="170">Aksi</th>

</tr>

</thead>

<tbody>

<?php

$no = 1;

while($row = mysqli_fetch_assoc($query)){

?>
<?php

if(empty($row['foto'])){

    $foto = "../../uploads/foto/default.png";

}else{

    $foto = "../../uploads/foto/".$row['foto'];

}

if(empty($row['qr_code'])){

    $qr = '<span class="badge badge-secondary">Belum Dibuat</span>';

}else{

    $qr = '<img src="../../uploads/qrcode/'.$row['qr_code'].'.png"
               width="45"
               class="img-thumbnail">';
}

?>

<tr>

<td class="text-center">

<?= $no++; ?>

</td>

<td class="text-center">

<img
src="<?= $foto; ?>"
class="img-circle elevation-2"
width="55"
height="55"
style="object-fit:cover;">

</td>

<td>

<?= htmlspecialchars($row['nis']); ?>

</td>

<td>

<?= htmlspecialchars($row['nisn']); ?>

</td>

<td>

<strong>

<?= htmlspecialchars($row['nama_siswa']); ?>

</strong>

</td>

<td>

<?= htmlspecialchars($row['nama_kelas']); ?>

</td>

<td class="text-center">

<?php

if($row['jk']=="L"){

    echo "L";

}else{

    echo "P";

}

?>

</td>

<td class="text-center">

<?= $qr; ?>

</td>

<td class="text-center">

<?php

if($row['status']=="Aktif"){

?>

<span class="badge badge-success">

Aktif

</span>

<?php

}elseif($row['status']=="Lulus"){

?>

<span class="badge badge-primary">

Lulus

</span>

<?php

}else{

?>

<span class="badge badge-warning">

Pindah

</span>

<?php

}

?>

</td>

<td class="text-center">

<a
href="kartu.php?id=<?= $row['id_siswa']; ?>"
class="btn btn-info btn-sm"
title="Preview Kartu">

<i class="fas fa-id-card"></i>

</a>

<a
href="upload_foto.php?id=<?= $row['id_siswa']; ?>"
class="btn btn-secondary btn-sm"
title="Upload Foto">

<i class="fas fa-camera"></i>

</a>

<a
href="generate_qr.php?id=<?= $row['id_siswa']; ?>"
class="btn btn-warning btn-sm"
title="Generate QR">

<i class="fas fa-qrcode"></i>

</a>

<a
href="edit.php?id=<?= $row['id_siswa']; ?>"
class="btn btn-primary btn-sm"
title="Edit">

<i class="fas fa-edit"></i>

</a>

<a
href="hapus.php?id=<?= $row['id_siswa']; ?>"
class="btn btn-danger btn-sm"
onclick="return confirm('Hapus data siswa ini?')"
title="Hapus">

<i class="fas fa-trash"></i>

</a>

</td>

</tr>

<?php

}

?>

</tbody>
                </table>

            </div>

        </div>

    </div>

</div>

</section>

</div>

<script>

$(document).ready(function () {

    $('#tabelSiswa').DataTable({

        responsive: true,

        autoWidth: false,

        pageLength: 10,

        order: [[4, 'asc']],

        language: {

            search: "Cari :",

            lengthMenu: "Tampilkan _MENU_ data",

            zeroRecords: "Data tidak ditemukan",

            info: "Menampilkan _START_ - _END_ dari _TOTAL_ siswa",

            infoEmpty: "Belum ada data",

            infoFiltered: "(disaring dari _MAX_ data)",

            paginate: {

                first: "Awal",

                last: "Akhir",

                next: "›",

                previous: "‹"

            }

        },

        columnDefs: [

            {

                orderable: false,

                targets: [1,7,9]

            }

        ]

    });

});

</script>

<?php
include "../../templates/footer.php";
?>
<!-- SweetAlert2 -->
<script src="../../assets/plugins/sweetalert2/sweetalert2.all.min.js"></script>

<script>

$(document).on("click",".btn-hapus",function(e){

    e.preventDefault();

    let url=$(this).attr("href");

    Swal.fire({

        title:'Hapus Data?',

        text:'Data siswa akan dihapus!',

        icon:'warning',

        showCancelButton:true,

        confirmButtonColor:'#d33',

        cancelButtonColor:'#6c757d',

        confirmButtonText:'Ya, Hapus',

        cancelButtonText:'Batal'

    }).then((result)=>{

        if(result.isConfirmed){

            window.location=url;

        }

    });

});

</script>