<?php
require("../../config/koneksi.php");
require("../../config/function.php");
require("../../config/crud.php");
cek_session_admin();
(isset($_GET['pg'])) ? $pg = $_GET['pg'] : $pg = '';
if ($pg == 'setting_app') {
    $alamat = nl2br($_POST['alamat']);
	$smt = $_POST['semester'];
    $data = [
        'token_api' => $_POST['token_api'],
        'sekolah' => $_POST['sekolah'],
        'kode_sekolah' => $_POST['kode'],
        'jenjang' => $_POST['jenjang'],
		 'npsn' => $_POST['npsn'],
        'kepsek' => $_POST['kepsek'],
        'nip' => $_POST['nip'],
		 'url_host' => $_POST['url'],
        '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'],
        'id_server' => $_POST['id_server'],
        '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'];
        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 = 'dist/img/logo' . rand(1, 100) . '.' . $ext;
                $upload = move_uploaded_file($temp, '../../' . $dest);
                if ($upload) {
                    $exec = update($koneksi, 'aplikasi', ['logo' => $dest], ['id_aplikasi' => 1]);
                } else {
                    echo "gagal";
                }
            }
        }
        if ($_FILES['ttd']['name'] <> '') {
            $logo = $_FILES['ttd']['name'];
            $temp = $_FILES['ttd']['tmp_name'];
            $ext = explode('.', $logo);
            $ext = end($ext);
            if (in_array($ext, $ektensi)) {
                $dest = 'dist/img/ttd' . '.' . $ext;
                $upload = move_uploaded_file($temp, '../../' . $dest);
            }
        }
        if ($_FILES['bk']['name'] <> '') {
            $logo = $_FILES['bk']['name'];
            $temp = $_FILES['bk']['tmp_name'];
            $ext = explode('.', $logo);
            $ext = end($ext);
            if (in_array($ext, $ektensi)) {
                $dest = 'dist/img/bk' . '.' . $ext;
                $upload = move_uploaded_file($temp, '../../' . $dest);
                if ($upload) {
                    $exec = update($koneksi, 'aplikasi', ['background' => $dest], ['id_aplikasi' => 1]);
                } else {
                    echo "gagal";
                }
            }
        }
		 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 = 'dist/img/stempel' . rand(1, 1000) . '.' . $ext;
                $upload = move_uploaded_file($temp, '../../' . $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 == 'setting_proktor') {
     $datax = [
        'tekhnisi' => $_POST['tekhnisi'],
		 'proktor' => $_POST['proktor']
		 
		   
		];
    $exec = update($koneksi, 'aplikasi', $datax, ['id_aplikasi' => '1']);
}


if ($pg == 'setting_api') {
$url = $_POST['url'];	
 $data = [
        'url_api' => $url		
		  ];

		   $exec = update($koneksi, 'aplikasi', $data, ['id_aplikasi' => '1']);
		   
}
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 == 'setting_clear') {
    $user = fetch($koneksi, 'users', ['id_user' => $_SESSION['id_user']]);
    $password = $_POST['password'];
    if (!password_verify($password, $user['password'])) {
        echo "password salah";
    } else {
        if (!empty($_POST['data'])) {
            $data = $_POST['data'];
            if ($data <> '') {
                foreach ($data as $table) {
                    if ($table <> 'users') {
                        mysqli_query($koneksi, "TRUNCATE $table");
                    } else {
                        mysqli_query($koneksi, "DELETE FROM $table WHERE level!='admin'");
                    }
                }
                echo "ok";
            }
        }
    }
}
if ($pg == 'reset') {
	$exec = mysqli_query($koneksi, "truncate absensi");
$exec = mysqli_query($koneksi, "truncate absen_mapel");
$exec = mysqli_query($koneksi, "truncate absen_daring");
$exec = mysqli_query($koneksi, "truncate absen_daringmapel");
$exec = mysqli_query($koneksi, "truncate agenda");
$exec = mysqli_query($koneksi, "truncate backup");
$exec = mysqli_query($koneksi, "truncate barusikap");
$exec = mysqli_query($koneksi, "truncate bayar");
$exec = mysqli_query($koneksi, "truncate berita");
$exec = mysqli_query($koneksi, "truncate banksoal");
$exec = mysqli_query($koneksi, "truncate bel");
$exec = mysqli_query($koneksi, "truncate biaya");
$exec = mysqli_query($koneksi, "truncate binduk_absen");
$exec = mysqli_query($koneksi, "truncate binduk_nilai");
$exec = mysqli_query($koneksi, "truncate binduk_ortu");
$exec = mysqli_query($koneksi, "truncate binduk_siswa");
$exec = mysqli_query($koneksi, "truncate bk_siswa");
$exec = mysqli_query($koneksi, "truncate bk_sp");
$exec = mysqli_query($koneksi, "truncate bk_surat");
$exec = mysqli_query($koneksi, "truncate buku");
$exec = mysqli_query($koneksi, "truncate buku_kategori");
$exec = mysqli_query($koneksi, "truncate buku_pinjam");
$exec = mysqli_query($koneksi, "truncate buku_rak");
$exec = mysqli_query($koneksi, "truncate daftar");
$exec = mysqli_query($koneksi, "truncate datarfid");
$exec = mysqli_query($koneksi, "truncate deskripsi");
$exec = mysqli_query($koneksi, "truncate  file_pendukung");	
 $exec = mysqli_query($koneksi, "truncate jawaban_dup");
  $exec = mysqli_query($koneksi, "truncate jadwal_mapel");
   $exec = mysqli_query($koneksi, "truncate jawaban_quiz");
    $exec = mysqli_query($koneksi, "truncate jawaban_tugas");
	 $exec = mysqli_query($koneksi, "truncate jenjang");
 $exec = mysqli_query($koneksi, "truncate jodoh");
 $exec = mysqli_query($koneksi, "truncate kelas");
 $exec = mysqli_query($koneksi, "truncate kontakme");
  $exec = mysqli_query($koneksi, "truncate komentar");
 $exec = mysqli_query($koneksi, "truncate level");
 $exec = mysqli_query($koneksi, "truncate log");
 $exec = mysqli_query($koneksi, "truncate log_guru");
 $exec = mysqli_query($koneksi, "truncate lingkup");
 $exec = mysqli_query($koneksi, "truncate mata_pelajaran");
 $exec = mysqli_query($koneksi, "truncate mapel_ijazah");
 $exec = mysqli_query($koneksi, "truncate materi");
 $exec = mysqli_query($koneksi, "truncate m_bayar");
 $exec = mysqli_query($koneksi, "truncate m_eskul");
 $exec = mysqli_query($koneksi, "truncate m_produktif");
 $exec = mysqli_query($koneksi, "truncate m_proyek");
 $exec = mysqli_query($koneksi, "truncate m_rek");
 $exec = mysqli_query($koneksi, "truncate nilai");
 $exec = mysqli_query($koneksi, "truncate nilai_formatif");
 $exec = mysqli_query($koneksi, "truncate nilai_harian");
 $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 nilai_skl");
 $exec = mysqli_query($koneksi, "truncate nilai_sku");
 $exec = mysqli_query($koneksi, "truncate pengumuman");
 $exec = mysqli_query($koneksi, "truncate penabung");
 $exec = mysqli_query($koneksi, "truncate pk");
 $exec = mysqli_query($koneksi, "truncate proyek");
 $exec = mysqli_query($koneksi, "truncate pesan");
 $exec = mysqli_query($koneksi, "truncate ruang");
 $exec = mysqli_query($koneksi, "truncate server");
 $exec = mysqli_query($koneksi, "truncate sesi");
 $exec = mysqli_query($koneksi, "truncate siswa");
  $exec = mysqli_query($koneksi, "truncate siswa_eskul");
 $exec = mysqli_query($koneksi, "truncate soal");
  $exec = mysqli_query($koneksi, "truncate soal_quiz");
 $exec = mysqli_query($koneksi, "truncate ujian");
 $exec = mysqli_query($koneksi, "truncate jurusan");
 $exec = mysqli_query($koneksi, "truncate trx");
  $exec = mysqli_query($koneksi, "truncate tugas");
   $exec = mysqli_query($koneksi, "truncate tujuan");
$exec = mysqli_query($koneksi, "truncate sapras");
$exec = mysqli_query($koneksi, "truncate sapras_reg");
$exec = mysqli_query($koneksi, "truncate sarana");
 	
 $files = glob('../../files/*'); 
foreach ($files as $file) {
    if (is_file($file))
        unlink($file); 
}
$foto = glob('../../foto/fotosiswa/*'); 
foreach ($foto as $file) {
    if (is_file($file))
        unlink($file); 
}
$bayar = glob('../../dist/img/pembayaran/*'); 
foreach ($bayar as $file) {
    if (is_file($file))
        unlink($file); 
}
}