<?php
require "../../config/koneksi.php";
require "../../vendor/autoload.php";
require("../../config/function.php");

$file_mimes = array('application/vnd.ms-excel', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
if (isset($_FILES['file']['name'])) {
    $ext = ['xls', 'xlsx'];
    $arr_file = explode('.', $_FILES['file']['name']);
    $extension = end($arr_file);
    if (in_array($extension, $ext)) {
        if ('xls' == $extension) {
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
        } else {
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
        }

        $spreadsheet = $reader->load($_FILES['file']['tmp_name']);

        $sheetData = $spreadsheet->getActiveSheet()->toArray();
        
        for ($i = 4; $i < count($sheetData); $i++) {
            $id_siswa = $sheetData[$i]['0'];
			$nis = $sheetData[$i]['1'];
			$nama = $sheetData[$i]['2'];
			$nama = addslashes($nama);
			$level = $sheetData[$i]['3'];
			$kelas = $sheetData[$i]['4'];
			$pk = $sheetData[$i]['5'];
			if ($pk == "") {
                $pk = "semua";
            }
			$jk = $sheetData[$i]['6'];
			$agama = $sheetData[$i]['7'];
			$nowa = $sheetData[$i]['8'];
			$foto = $sheetData[$i]['9'];
			$npsn = $setting['npsn'];
			if($level=="1" OR $level=="2"){
				$fase="A";
			}elseif($level=="3" OR $level=="4"){
				$fase="B";
			}elseif($level=="5" OR $level=="6"){
				$fase="C";
			}elseif($level=="7" OR $level=="8" OR $level=="9"){
				$fase="D";
			}elseif($level=="10"){
				$fase="E";
			}elseif($level=="11" OR $level=="12"){
				$fase="F";
			}
			$qpk = mysqli_query($koneksi, "SELECT id_pk FROM pk WHERE id_pk='$pk'");
                $cekpk = mysqli_num_rows($qpk);
                if (!$cekpk <> 0) {
                    $exec = mysqli_query($koneksi, "INSERT INTO pk (id_pk,pk)VALUES('$pk','$pk')");
                }
			 $qlevel = mysqli_query($koneksi, "SELECT id_level FROM level WHERE id_level='$level'");
            $ceklevel = mysqli_num_rows($qlevel);
            if (!$ceklevel <> 0) {
                $exec = mysqli_query($koneksi, "INSERT INTO level (id_level,level)VALUES('$level','$level')");
            }
			$qkelas = mysqli_query($koneksi, "SELECT kelas FROM kelas WHERE kelas='$kelas'");
            $cekkelas = mysqli_num_rows($qkelas);
            if (!$cekkelas <> 0) {
                $exec = mysqli_query($koneksi, "INSERT INTO kelas (level,kelas,pk)VALUES('$level','$kelas','$pk')");
            }
           $qus = mysqli_query($koneksi, "SELECT nis FROM siswa WHERE nis='$nis'");
            $cekuser = mysqli_num_rows($qus);
            if (!$cekuser <> 0) {
			
                $exec = mysqli_query($koneksi, "INSERT INTO siswa (nis,nama,level,kelas,fase,pk,jk,agama,nowa,npsn,foto) 
				VALUES ('$nis','$nama','$level','$kelas','$fase','$pk','$jk','$agama','$nowa','$npsn','$foto')");
             
            }
        }
       
    } else {
        echo "Pilih file yang bertipe xlsx or xls";
    }
}
