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

(isset($_GET['pg'])) ? $pg = $_GET['pg'] : $pg = '';
if ($pg == 'setting_app') {
    $alamat = nl2br($_POST['alamat']);
	$smt = $_POST['semester'];
    $data = [
        
        'sekolah' => $_POST['sekolah'],
        'id_server' => $_POST['kode'],
        'jenjang' => $_POST['jenjang'],
		 'npsn' => $_POST['npsn'],
        'kepsek' => $_POST['kepsek'],
        'nip' => $_POST['nip'],
		'nowa' => $_POST['nowa'],
        'alamat' => $_POST['alamat'],
		'desa' => $_POST['desa'],
        'kecamatan' => $_POST['kecamatan'],
        'kabupaten' => $_POST['kabupaten'],
		 'propinsi' => $_POST['propinsi'],
        'telp' => $_POST['telp'],
        'fax' => $_POST['fax'],
        'web' => $_POST['web'],
        'email' => $_POST['email'],
		'header' => $_POST['laporan'],
       
        'waktu' => $_POST['waktu'],
		 'semester' => $_POST['semester'],
		  'tp' => $_POST['tp'],
		    'jenis' => $_POST['jenis']
    ];
    $exec = update($koneksi, 'aplikasi', $data, ['id_aplikasi' => 1]);
	
    if ($exec) {
        $ektensi = ['jpg', 'png','svg','PNG', 'JPG', 'JPEG'];
        if ($_FILES['logo']['name'] <> '') {
            $logo = $_FILES['logo']['name'];
            $temp = $_FILES['logo']['tmp_name'];
            $ext = explode('.', $logo);
            $ext = end($ext);
            if (in_array($ext, $ektensi)) {
                $dest = 'logo' . rand(0,100). '.' . $ext;
                $upload = move_uploaded_file($temp, '../../images/' . $dest);
                if ($upload) {
                    $exec = update($koneksi, 'aplikasi', ['logo' => $dest], ['id_aplikasi' => 1]);
                } else {
                    echo "gagal";
                }
            }
        }
         if ($_FILES['ttd']['name'] <> '') {
            $ttd = $_FILES['ttd']['name'];
            $temp = $_FILES['ttd']['tmp_name'];
            $ext = explode('.', $ttd);
            $ext = end($ext);
            if (in_array($ext, $ektensi)) {
                $dest = 'ttd' . '.' . $ext;
               $upload = move_uploaded_file($temp, '../../images/' . $dest);
            }
        }
		 if ($_FILES['stempel']['name'] <> '') {
            $header = $_FILES['stempel']['name'];
            $temp = $_FILES['stempel']['tmp_name'];
            $ext = explode('.', $header);
            $ext = end($ext);
            if (in_array($ext, $ektensi)) {
                $dest = 'stempel' . '.' . $ext;
                $upload = move_uploaded_file($temp, '../../images/' . $dest);
                if ($upload) {
                    $data4 = [
                        'stempel' => $dest
                    ];
                    $exec = update($koneksi, 'aplikasi', $data4, ['id_aplikasi' => 1]);
                } else {
                    echo "gagal";
                }
            }
        }
    } else {
        echo "Gagal menyimpan";
    }
}


if ($pg == 'setting_restore') {
    function restore($file)
    {
        require("../../config/koneksi.php");
        global $rest_dir;
        $nama_file    = $file['name'];
        $ukrn_file    = $file['size'];
        $tmp_file    = $file['tmp_name'];

        if ($nama_file == "") {
            echo "Fatal Error";
        } else {
            $alamatfile    = $rest_dir . $nama_file;
            $templine    = array();

            if (move_uploaded_file($tmp_file, $alamatfile)) {

                $templine    = '';

                $lines    = file($alamatfile);

                foreach ($lines as $line) {
                    if (substr($line, 0, 2) == '--' || $line == '')
                        continue;

                    $templine .= $line;

                    if (substr(trim($line), -1, 1) == ';') {
                        mysqli_query($koneksi, $templine);
                        $templine = '';
                    }
                }
            } else {
                echo "Proses upload gagal, kode error = " . $file['error'];
            }
        }
    }
    restore($_FILES['datafile']);
    if (isset($_FILES['datafile'])) {
        echo "data berhasil di restore";
    }
}


if ($pg == 'ambil_jenjang') {
    $jenis = $_POST['jenis'];
    $sql = mysqli_query($koneksi, "SELECT * FROM jenis_sp WHERE jenis='" . $jenis . "'");
    echo "<option value='semua'>Pilih Jenjang</option>";
    while ($data = mysqli_fetch_array($sql)) {
        echo "<option value='$data[jenjang]'>$data[ket]</option>";
    }
}
if ($pg == 'reset_pesan') {
	$exec = mysqli_query($koneksi, "truncate pesan_terkirim");
}
if ($pg == 'apiwa') {
	$data=[
	'url_api'=>$_POST['urlapi']
	];
   $exec = update($koneksi, 'aplikasi', $data, ['id_aplikasi' => 1]);
}


if ($pg == 'reset') {
$exec = mysqli_query($koneksi, "delete from users where level<>'admin'");		
$exec = mysqli_query($koneksi, "truncate absen_daring");	
$exec = mysqli_query($koneksi, "truncate absensi");	
$exec = mysqli_query($koneksi, "truncate banksoal");
$exec = mysqli_query($koneksi, "truncate barusikap");
$exec = mysqli_query($koneksi, "truncate berita");
$exec = mysqli_query($koneksi, "truncate datareg");
$exec = mysqli_query($koneksi, "truncate deskrip");
$exec = mysqli_query($koneksi, "truncate file_pendukung");
$exec = mysqli_query($koneksi, "truncate jadwal_rapor");
$exec = mysqli_query($koneksi, "truncate jadwal_mapel");
$exec = mysqli_query($koneksi, "truncate agenda");
$exec = mysqli_query($koneksi, "truncate jawaban");
$exec = mysqli_query($koneksi, "truncate jawaban_dup");
$exec = mysqli_query($koneksi, "truncate jodoh");
$exec = mysqli_query($koneksi, "truncate kelas");
$exec = mysqli_query($koneksi, "truncate kunci_soal");
$exec = mysqli_query($koneksi, "truncate level");
$exec = mysqli_query($koneksi, "truncate lingkup");
$exec = mysqli_query($koneksi, "truncate log");
$exec = mysqli_query($koneksi, "truncate mapel_rapor");
$exec = mysqli_query($koneksi, "truncate mata_pelajaran");
$exec = mysqli_query($koneksi, "truncate m_eskul");
$exec = mysqli_query($koneksi, "truncate m_proyek");
$exec = mysqli_query($koneksi, "truncate nilai");
$exec = mysqli_query($koneksi, "truncate nilai_formatif");
$exec = mysqli_query($koneksi, "truncate nilai_proses");
$exec = mysqli_query($koneksi, "truncate nilai_proyek");
$exec = mysqli_query($koneksi, "truncate nilai_rapor");
$exec = mysqli_query($koneksi, "truncate nilai_sumatif");
$exec = mysqli_query($koneksi, "truncate pesan_terkirim");
$exec = mysqli_query($koneksi, "truncate pk");
$exec = mysqli_query($koneksi, "truncate prestasi");
$exec = mysqli_query($koneksi, "truncate proyek");
$exec = mysqli_query($koneksi, "truncate reset");
$exec = mysqli_query($koneksi, "truncate reset_andro");
$exec = mysqli_query($koneksi, "truncate ruang");
$exec = mysqli_query($koneksi, "truncate sinkron");
$exec = mysqli_query($koneksi, "truncate siswa");
$exec = mysqli_query($koneksi, "truncate siswa_eskul");
$exec = mysqli_query($koneksi, "truncate soal");
$exec = mysqli_query($koneksi, "truncate sosial");
$exec = mysqli_query($koneksi, "truncate spiritual");
$exec = mysqli_query($koneksi, "truncate temp_finger");
$exec = mysqli_query($koneksi, "truncate tmpreg");
$exec = mysqli_query($koneksi, "truncate tujuan");
$exec = mysqli_query($koneksi, "truncate ujian");
$exec = mysqli_query($koneksi, "truncate materi");
$exec = mysqli_query($koneksi, "truncate tugas");
$exec = mysqli_query($koneksi, "truncate jawaban_tugas");
$exec = mysqli_query($koneksi, "truncate chating");
$exec = mysqli_query($koneksi, "truncate nilai_harian");
$datafile = glob('../../datafile/*'); 
foreach ($datafile as $file) {
    if (is_file($file))
        unlink($file); 
}
$datafoto = glob('../../images/fotosiswa/*'); 
foreach ($datafoto as $file) {
    if (is_file($file))
        unlink($file); 
}
$foto = glob('../../files/*'); 
foreach ($foto as $file) {
    if (is_file($file))
        unlink($file); 
}
    $filezip = '../../files.zip';
     unlink($filezip); 
}