<?php
/**
 * =====================================================
 * SIMPAS v1.0
 * Modul : Upload Foto Siswa
 * File   : upload_foto.php
 * =====================================================
 */

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

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

if(!isset($_GET['id'])){

    echo "<script>
            alert('ID Siswa tidak ditemukan');
            window.location='index.php';
          </script>";
    exit;
}

$id = intval($_GET['id']);

$query = mysqli_query($conn,"
SELECT
s.*,
k.nama_kelas
FROM siswa s
LEFT JOIN kelas k
ON s.id_kelas=k.id_kelas
WHERE s.id_siswa='$id'
LIMIT 1
");

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

    echo "<script>
            alert('Data siswa tidak ditemukan');
            window.location='index.php';
          </script>";
    exit;
}

$siswa = mysqli_fetch_assoc($query);

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

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

}else{

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

}
?>

<div class="content-wrapper">

<section class="content-header">

<div class="container-fluid">

<h1>

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

Upload Foto Siswa

</h1>

</div>

</section>

<section class="content">

<div class="container-fluid">

<div class="card shadow">

<div class="card-body">

<form
method="POST"
enctype="multipart/form-data">

<div class="row">

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

<img
id="preview"
src="<?= $foto; ?>"
class="img-thumbnail"
style="width:220px;height:260px;object-fit:cover;">

</div>

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

<div class="form-group">

<label>NIS</label>

<input
type="text"
class="form-control"
value="<?= $siswa['nis']; ?>"
readonly>

</div>

<div class="form-group">

<label>Nama Siswa</label>

<input
type="text"
class="form-control"
value="<?= $siswa['nama_siswa']; ?>"
readonly>

</div>

<div class="form-group">

<label>Kelas</label>

<input
type="text"
class="form-control"
value="<?= $siswa['nama_kelas']; ?>"
readonly>

</div>

<div class="form-group">

<label>Pilih Foto</label>

<input
type="file"
name="foto"
id="foto"
class="form-control"
accept=".jpg,.jpeg,.png"
required>

<small class="text-muted">

Format JPG / JPEG / PNG

Maksimal 2 MB

</small>

</div>

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

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

Upload Foto

</button>

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

Kembali

</a>

</div>

</div>

</form>

</div>

</div>

</div>

</section>

</div>
<?php

/*
|--------------------------------------------------------------------------
| Proses Upload
|--------------------------------------------------------------------------
*/

if(isset($_POST['upload'])){

    if(!isset($_FILES['foto'])){

        echo "<script>alert('File tidak ditemukan.');</script>";

    }else{

        $namaFile = $_FILES['foto']['name'];
        $tmpFile  = $_FILES['foto']['tmp_name'];
        $ukuran   = $_FILES['foto']['size'];
        $error    = $_FILES['foto']['error'];

        if($error!=0){

            echo "<script>alert('Upload gagal.');</script>";

        }else{

            $ext = strtolower(pathinfo($namaFile,PATHINFO_EXTENSION));

            $allow = array('jpg','jpeg','png');

            if(!in_array($ext,$allow)){

                echo "<script>

                alert('Format foto harus JPG, JPEG atau PNG.');

                </script>";

            }elseif($ukuran > 2*1024*1024){

                echo "<script>

                alert('Ukuran foto maksimal 2 MB.');

                </script>";

            }else{

                $folder="../../uploads/foto/";

                if(!file_exists($folder)){

                    mkdir($folder,0777,true);

                }

                /*
                |--------------------------------------------------------------------------
                | Hapus Foto Lama
                |--------------------------------------------------------------------------
                */

                if(
                    !empty($siswa['foto']) &&
                    file_exists($folder.$siswa['foto']) &&
                    $siswa['foto']!="default.png"
                ){

                    unlink($folder.$siswa['foto']);

                }

                /*
                |--------------------------------------------------------------------------
                | Nama File Baru
                |--------------------------------------------------------------------------
                */

                $namaBaru =
                    $siswa['nis']."_".time().".".$ext;

                if(move_uploaded_file($tmpFile,$folder.$namaBaru)){

                    mysqli_query($conn,"
                    UPDATE siswa
                    SET foto='$namaBaru'
                    WHERE id_siswa='".$siswa['id_siswa']."'
                    ");

                    echo "

                    <script>

                    alert('Foto berhasil diupload.');

                    window.location='index.php';

                    </script>

                    ";

                    exit;

                }else{

                    echo "<script>

                    alert('Gagal menyimpan file.');

                    </script>";

                }

            }

        }

    }

}

?>

<script>

document.getElementById("foto").onchange=function(){

const file=this.files[0];

if(file){

document.getElementById("preview").src=
URL.createObjectURL(file);

}

}

</script>

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