<?php
/**
 * =====================================================
 * SIMPAS v2.0
 * PROSES IMPORT DATA SISWA
 * =====================================================
 */

error_reporting(E_ALL);
ini_set('display_errors',1);

date_default_timezone_set("Asia/Jakarta");

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

include "../../libraries/phpqrcode/qrlib.php";

/*
|--------------------------------------------------------------------------
| Cek Session Preview
|--------------------------------------------------------------------------
*/

if(
    !isset($_SESSION['import_siswa']) ||
    empty($_SESSION['import_siswa'])
){

    echo "

    <script>

    alert('Silakan upload file Excel terlebih dahulu.');

    window.location='import_excel.php';

    </script>

    ";

    exit;

}

$rows = $_SESSION['import_siswa'];

/*
|--------------------------------------------------------------------------
| Statistik
|--------------------------------------------------------------------------
*/

$total      = 0;
$berhasil   = 0;
$duplikat   = 0;
$gagal      = 0;
$qrBerhasil = 0;

/*
|--------------------------------------------------------------------------
| Folder QR
|--------------------------------------------------------------------------
*/

$folderQR = "../../uploads/qrcode/";

if(!file_exists($folderQR)){

    mkdir($folderQR,0777,true);

}

/*
|--------------------------------------------------------------------------
| Mulai Import
|--------------------------------------------------------------------------
*/

foreach($rows as $row){

    $total++;

    $nis = mysqli_real_escape_string(
        $conn,
        trim($row['nis'])
    );

    $nisn = mysqli_real_escape_string(
        $conn,
        trim($row['nisn'])
    );

    $nama_siswa = mysqli_real_escape_string(
        $conn,
        trim($row['nama_siswa'])
    );

    $jk = strtoupper(
        mysqli_real_escape_string(
            $conn,
            trim($row['jk'])
        )
    );

    $nama_kelas = mysqli_real_escape_string(
        $conn,
        trim($row['kelas'])
    );

    $status = mysqli_real_escape_string(
        $conn,
        trim($row['status'])
    );

    if($status==""){

        $status="Aktif";

    }

    if(
        $nis=="" ||
        $nama_siswa==""
    ){

        $gagal++;

        continue;

    }
        /*
    |--------------------------------------------------------------------------
    | Cek NIS Duplikat
    |--------------------------------------------------------------------------
    */

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

    if(mysqli_num_rows($cek)>0){

        $duplikat++;
        continue;

    }

    /*
    |--------------------------------------------------------------------------
    | Cari ID Kelas
    |--------------------------------------------------------------------------
    */

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

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

        $gagal++;
        continue;

    }

    $kelas = mysqli_fetch_assoc($qKelas);

    $id_kelas = $kelas['id_kelas'];

    /*
    |--------------------------------------------------------------------------
    | Validasi Jenis Kelamin
    |--------------------------------------------------------------------------
    */

    if($jk!="L" && $jk!="P"){

        $jk="L";

    }

    /*
    |--------------------------------------------------------------------------
    | Simpan Database
    |--------------------------------------------------------------------------
    */

    $foto = "default.png";

    $qr_code = $nis;

    $insert = mysqli_query($conn,"
        INSERT INTO siswa
        (
            nis,
            nisn,
            nama_siswa,
            id_kelas,
            jk,
            foto,
            qr_code,
            status
        )
        VALUES
        (
            '$nis',
            '$nisn',
            '$nama_siswa',
            '$id_kelas',
            '$jk',
            '$foto',
            '$qr_code',
            '$status'
        )
    ");

    if(!$insert){

        $gagal++;
        continue;

    }

    $id_siswa = mysqli_insert_id($conn);

    $isiQR = "SIMPAS|".$nis;

    $namaQR = $nis.".png";

    $fileQR = $folderQR.$namaQR;

    if(file_exists($fileQR)){
        unlink($fileQR);
    }

    QRcode::png(
        $isiQR,
        $fileQR,
        QR_ECLEVEL_H,
        8,
        2
    );

    if(file_exists($fileQR)){

        $qrBerhasil++;

    }

    $berhasil++;

}
?>
<!DOCTYPE html>
<html lang="id">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Hasil Import Data Siswa</title>

<link rel="stylesheet"
href="../../assets/plugins/bootstrap/css/bootstrap.min.css">

<link rel="stylesheet"
href="../../assets/plugins/fontawesome-free/css/all.min.css">

<style>

body{
    background:#f4f6f9;
}

.card{
    margin-top:40px;
}

.table td,
.table th{
    vertical-align:middle;
}

</style>

</head>

<body>

<div class="container">

<div class="row justify-content-center">

<div class="col-lg-8">

<div class="card shadow">

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

<h4 class="mb-0">

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

HASIL IMPORT DATA SISWA

</h4>

</div>

<div class="card-body">

<table class="table table-bordered">

<tr>
<th width="250">Total Data Excel</th>
<td><?= $total; ?></td>
</tr>

<tr>
<th>Berhasil Diimport</th>
<td class="text-success">
<strong><?= $berhasil; ?></strong>
</td>
</tr>

<tr>
<th>QR Code Berhasil Dibuat</th>
<td class="text-primary">
<strong><?= $qrBerhasil; ?></strong>
</td>
</tr>

<tr>
<th>Data Duplikat</th>
<td class="text-warning">
<strong><?= $duplikat; ?></strong>
</td>
</tr>

<tr>
<th>Data Gagal</th>
<td class="text-danger">
<strong><?= $gagal; ?></strong>
</td>
</tr>

</table>

<div class="alert alert-success">

<b>Import selesai.</b><br>

Semua siswa yang berhasil diimport telah dibuatkan QR Code secara otomatis.

</div>

<div class="text-center">

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

<i class="fas fa-check-circle"></i>

Kembali ke Data Siswa

</a>

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

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

Import Lagi

</a>

</div>

</div>

</div>

</div>

</div>

</div>

</body>

</html>