<?php
/**
 * ==========================================================
 * SIMPAS
 * Modul : Cetak Kartu Siswa
 * File  : cetak.php
 * ==========================================================
 */

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

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

/* ==========================================================
   FILTER KELAS
========================================================== */

$id_kelas = "";

if(isset($_GET['kelas'])){
    $id_kelas = mysqli_real_escape_string($conn,$_GET['kelas']);
}

/* ==========================================================
   DATA KELAS
========================================================== */

$qKelas = mysqli_query($conn,"
SELECT *
FROM kelas
ORDER BY nama_kelas ASC
");

/* ==========================================================
   DATA SISWA
========================================================== */

$sql = "
SELECT
    s.*,
    k.nama_kelas
FROM siswa s
LEFT JOIN kelas k
ON s.id_kelas=k.id_kelas
";

if($id_kelas!=""){

    $sql .= " WHERE s.id_kelas='$id_kelas' ";

}

$sql .= "
ORDER BY
k.nama_kelas,
s.nama_siswa ASC
";

$qSiswa = mysqli_query($conn,$sql);

?>

<div class="content-wrapper">

<section class="content-header">

<div class="container-fluid">

<div class="row mb-2">

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

<h1>

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

Cetak Kartu Siswa

</h1>

</div>

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

<a href="index.php" class="btn btn-secondary">

<i class="fas fa-arrow-left"></i>

Kembali

</a>

</div>

</div>

</div>

</section>

<section class="content">

<div class="container-fluid">

<div class="card shadow">

<div class="card-header bg-primary">

<h3 class="card-title">

Filter Data Siswa

</h3>

</div>

<div class="card-body">

<form method="GET">

<div class="row">

<div class="col-md-4">

<label>Kelas</label>

<select
name="kelas"
class="form-control">

<option value="">Semua Kelas</option>

<?php
while($kelas=mysqli_fetch_assoc($qKelas)){
?>

<option
value="<?= $kelas['id_kelas']; ?>"
<?= ($id_kelas==$kelas['id_kelas'])?'selected':''; ?>>

<?= $kelas['nama_kelas']; ?>

</option>

<?php
}
?>

</select>

</div>

<div class="col-md-2">

<label>&nbsp;</label>

<button
type="submit"
class="btn btn-primary btn-block">

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

Tampilkan

</button>

</div>

</div>

</form>

<hr>
<!-- ==========================================
     PILIH SEMUA
========================================== -->

<div class="row mb-3">

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

        <div class="custom-control custom-checkbox">

            <input
                type="checkbox"
                class="custom-control-input"
                id="pilihSemua">

            <label
                class="custom-control-label"
                for="pilihSemua">

                <strong>Pilih Semua Siswa</strong>

            </label>

        </div>

    </div>

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

        <span class="badge badge-success p-2">

            Jumlah dipilih :
            <span id="jumlahDipilih">0</span>

            siswa

        </span>

    </div>

</div>

<form
method="POST"
action="proses_cetak.php"
target="_blank">

<div class="table-responsive">

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

<thead class="bg-primary">

<tr>

<th width="50">

Pilih

</th>

<th width="70">

Foto

</th>

<th>NIS</th>

<th>Nama Siswa</th>

<th>Kelas</th>

<th width="120">

QR

</th>

</tr>

</thead>

<tbody>

<?php

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

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

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

    }else{

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

    }

    $qr="../../uploads/qrcode/".$row['nis'].".png";

?>

<tr>

<td class="text-center">

<input
type="checkbox"
name="id_siswa[]"
class="cekSiswa"
value="<?= $row['id_siswa']; ?>">

</td>

<td class="text-center">

<img

src="<?= $foto; ?>"

width="55"

height="55"

class="img-circle elevation-2"

style="object-fit:cover;">

</td>

<td>

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

</td>

<td>

<strong>

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

</strong>

</td>

<td>

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

</td>

<td class="text-center">

<?php

if(file_exists($qr)){

?>

<img

src="<?= $qr; ?>"

width="45"

class="img-thumbnail">

<?php

}else{

?>

<span class="badge badge-danger">

Belum Ada

</span>

<?php

}

?>

</td>

</tr>

<?php

}

?>

</tbody>

</table>

</div>

<hr>

<div class="text-right">

<button

type="submit"

class="btn btn-success btn-lg">

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

Cetak Kartu

</button>

</div>

</form>
</div>
</div>
</section>
</div>

<?php
include "../../templates/footer.php";
?>

<!-- DataTables -->
<script>

$(document).ready(function () {

    // ===========================
    // DATATABLE
    // ===========================

    $('#tabelCetak').DataTable({

        responsive:true,

        autoWidth:false,

        pageLength:25,

        order:[[3,'asc']],

        language:{

            search:"Cari Siswa :",

            lengthMenu:"Tampilkan _MENU_ siswa",

            zeroRecords:"Data tidak ditemukan",

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

            infoEmpty:"Belum ada data",

            paginate:{

                first:"Awal",

                last:"Akhir",

                next:"›",

                previous:"‹"

            }

        },

        columnDefs:[

            {

                orderable:false,

                targets:[0,1,5]

            }

        ]

    });


    // ===========================
    // PILIH SEMUA
    // ===========================

    $("#pilihSemua").click(function(){

        $(".cekSiswa").prop(
            "checked",
            $(this).prop("checked")
        );

        hitungDipilih();

    });


    // ===========================
    // CHECKBOX SATUAN
    // ===========================

    $(".cekSiswa").change(function(){

        hitungDipilih();

        if(

            $(".cekSiswa:checked").length
            ==
            $(".cekSiswa").length

        ){

            $("#pilihSemua").prop("checked",true);

        }else{

            $("#pilihSemua").prop("checked",false);

        }

    });


    // ===========================
    // HITUNG
    // ===========================

    function hitungDipilih(){

        let jumlah = $(".cekSiswa:checked").length;

        $("#jumlahDipilih").text(jumlah);

    }


    hitungDipilih();

});

</script>
