<?php
require "../../config/koneksi.php";
require "../../vendor/autoload.php";
require("../../config/function.php");
cek_session_admin();
$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();
        $sukses = $gagal = 0;
        $exec = mysqli_query($koneksi, "TRUNCATE mapel_proto");
		
        for ($i = 4; $i < count($sheetData); $i++) {
                 $nomor = addslashes($sheetData[$i]['0']);
                $soal = addslashes($sheetData[$i]['1']);
                $pilA = addslashes($sheetData[$i]['2']);
                $pilB = addslashes($sheetData[$i]['3']);
                $pilC = addslashes($sheetData[$i]['4']);
                $pilD = addslashes($sheetData[$i]['5']);
                $pilE = addslashes($sheetData[$i]['6']);
                $jawaban = $sheetData[$i]['7'];
                $jenis = $sheetData[$i]['8'];
                $file1 = $sheetData[$i]['9'];
                $file2 = $sheetData[$i]['10'];
                $fileA = $sheetData[$i]['11'];
                $fileB = $sheetData[$i]['12'];
                $fileC = $sheetData[$i]['13'];
                $fileD = $sheetData[$i]['14'];
                $fileE = $sheetData[$i]['15'];
                $id_mapel = $_POST['id_bank'];
		   
           if ($soal <> '' and $jenis <> '') {
                    $exec = mysqli_query($koneksi, "INSERT INTO soal (id_bank,nomor,soal,pilA,pilB,pilC,pilD,pilE,jawaban,jenis,file,file1,fileA,fileB, fileC,fileD,fileE) VALUES ('$id_mapel','$nomor','$soal','$pilA','$pilB','$pilC','$pilD','$pilE','$jawaban','$jenis','$file1','$file2','$fileA','$fileB','$fileC','$fileD','$fileE')");
                    if ($file1 <> '') {
                        $sql = mysqli_query($koneksi, "insert into file_pendukung (nama_file,id_mapel) values ('$file1','$id_mapel')");
                    }
                    if ($file2 <> '') {
                        $sql = mysqli_query($koneksi, "insert into file_pendukung (nama_file,id_mapel) values ('$file2','$id_mapel')");
                    }
                    if ($fileA <> '') {
                        $sql = mysqli_query($koneksi, "insert into file_pendukung (nama_file,id_mapel) values ('$fileA','$id_mapel')");
                    }
                    if ($fileB <> '') {
                        $sql = mysqli_query($koneksi, "insert into file_pendukung (nama_file,id_mapel) values ('$fileB','$id_mapel')");
                    }
                    if ($fileC <> '') {
                        $sql = mysqli_query($koneksi, "insert into file_pendukung (nama_file,id_mapel) values ('$fileC','$id_mapel')");
                    }
                    if ($fileD <> '') {
                        $sql = mysqli_query($koneksi, "insert into file_pendukung (nama_file,id_mapel) values ('$fileD','$id_mapel')");
                    }
                    if ($fileE <> '') {
                        $sql = mysqli_query($koneksi, "insert into file_pendukung (nama_file,id_mapel) values ('$fileE','$id_mapel')");
                    }
					($exec) ? $sukses++ : $gagal++;
              } echo "Berhasil: $sukses | Gagal: $gagal ";
    } 
  }
}