<?php
/**
 * ==========================================================
 * SIMPAS v1.0
 * Preview Import Excel
 * ==========================================================
 */

date_default_timezone_set("Asia/Jakarta");

include "../../config/koneksi.php";
include "../../config/session.php";
$_SESSION['import_siswa'] = [];

require "../../libraries/SimpleXLSX.php";

use Shuchkin\SimpleXLSX;

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

if (!isset($_FILES['file_excel'])) {
    die("File tidak ditemukan.");
}

$tmpFile = $_FILES['file_excel']['tmp_name'];

if (!$xlsx = SimpleXLSX::parse($tmpFile)) {

    die(SimpleXLSX::parseError());

}

$rows = $xlsx->rows();
?>

<div class="content-wrapper">

<section class="content-header">

<div class="container-fluid">

<div class="row mb-2">

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

<h1>

Preview Import Data

</h1>

</div>

</div>

</div>

</section>

<section class="content">

<div class="container-fluid">

<div class="card shadow">

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

<h3 class="card-title">

Preview Excel

</h3>

</div>

<div class="card-body">

<form action="proses_import.php" method="post">

<table class="table table-bordered table-hover">

<thead class="bg-success">

<tr>

<th>No</th>

<th>NIS</th>

<th>NISN</th>

<th>Nama</th>

<th>JK</th>

<th>Kelas</th>

<th>Status</th>

<th>Validasi</th>

</tr>

</thead>

<tbody>

<?php

$no=1;

unset($rows[0]);

foreach($rows as $row){
    // Pastikan minimal ada 6 kolom
    if(count($row) < 6){
        continue;
    }

    $nis         = trim($row[0]);
    $nisn        = trim($row[1]);
    $nama_siswa  = trim($row[2]);
    $jk          = strtoupper(trim($row[3]));
    $nama_kelas  = trim($row[4]);
    $status      = trim($row[5]);

    $warna = "table-success";
    $pesan = "Siap Diimport";
    if($warna=="table-success"){

    $_SESSION['import_siswa'][] = [

        'nis'=>$nis,
        'nisn'=>$nisn,
        'nama_siswa'=>$nama_siswa,
        'jk'=>$jk,
        'kelas'=>$nama_kelas,
        'status'=>$status

    ];

}

    // Validasi NIS
    if($nis==""){

        $warna="table-danger";
        $pesan="NIS kosong";

    }

    // Validasi Nama
    elseif($nama_siswa==""){

        $warna="table-danger";
        $pesan="Nama kosong";

    }

    // Validasi JK
    elseif($jk!="L" && $jk!="P"){

        $warna="table-warning";
        $pesan="JK harus L atau P";

    }

    // Validasi Kelas
    else{

        $qkelas=mysqli_query($conn,"
            SELECT id_kelas
            FROM kelas
            WHERE nama_kelas='$nama_kelas'
            LIMIT 1
        ");

        if(mysqli_num_rows($qkelas)==0){

            $warna="table-danger";
            $pesan="Kelas tidak ditemukan";

        }

    }

    // Validasi NIS Duplikat
    if($warna=="table-success"){

        $cek=mysqli_query($conn,"
            SELECT id_siswa
            FROM siswa
            WHERE nis='$nis'
            LIMIT 1
        ");

        if(mysqli_num_rows($cek)>0){

            $warna="table-warning";
            $pesan="NIS sudah ada";

        }

    }

?>

<tr class="<?= $warna ?>">

<td><?= $no++; ?></td>

<td><?= htmlspecialchars($nis); ?></td>

<td><?= htmlspecialchars($nisn); ?></td>

<td><?= htmlspecialchars($nama_siswa); ?></td>

<td><?= htmlspecialchars($jk); ?></td>

<td><?= htmlspecialchars($nama_kelas); ?></td>

<td><?= htmlspecialchars($status); ?></td>

<td>

<?php

if($warna=="table-success"){

    echo '<span class="badge badge-success">'.$pesan.'</span>';

}elseif($warna=="table-warning"){

    echo '<span class="badge badge-warning">'.$pesan.'</span>';

}else{

    echo '<span class="badge badge-danger">'.$pesan.'</span>';

}

?>

</td>

</tr>

<?php

}
?>

</tbody>

</table>

<div class="mt-3">

<button
type="submit"
class="btn btn-success">

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

Import Data

</button>

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

Kembali

</a>

</div>

</form>

</div>

</div>

</div>

</section>

</div>

<?php

include "../../templates/footer.php";

?>