<?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 == 'edit_ujian') {
 $data= [
      
		'soal' => $_POST['jenissoal'],
		'opsi' => $_POST['jenisopsi'],
		'kirim' => $_POST['kirim']
		
		  ];
		  
		   $exec = update($koneksi, 'menu', $data, ['id_menu' => '1']);
		   $exec = mysqli_query($koneksi, "truncate banksoal");
		   $exec = mysqli_query($koneksi, "truncate soal");
		   $exec = mysqli_query($koneksi, "truncate ujian");
		   $exec = mysqli_query($koneksi, "truncate nilai");
		   $exec = mysqli_query($koneksi, "truncate jawaban");
		   $exec = mysqli_query($koneksi, "truncate jodoh");
		 
}
if ($pg == 'edit_sinkron') {
 $data= [
      
		'sinkron' => $_POST['sinkron'],
		'ujian'=>$_POST['ujian']
		
		  ];
		  
		   $exec = update($koneksi, 'menu', $data, ['id_menu' => '1']);
		  
		 
}
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 == 'tambahujian') {
  $kode = str_replace(' ', '', $_POST['idujian']);
    $nama = addslashes($_POST['nama']);
    $datax = [
                'id_jenis' => $kode,
				'nama'=>$nama,
				'status'=>'tidak'
    ];
    $cekujian = rowcount($koneksi, 'jenis', ['id_jenis' => $kode]);
    if ($cekujian > 1) {
        echo "ujian sudah ada";
    
        } else {
           $exec = insert($koneksi, 'jenis', $datax);
            echo $exec;
        }
    }
if ($pg == 'hapusujian') {
    $id = $_POST['id'];
    delete($koneksi, 'jenis', ['id_jenis' => $id]);
}
if ($pg == 'ubahujian') {
  $kode = $_POST['idujian'];
    $nama = $_POST['nama'];
	$status = $_POST['status'];
    $datax = [
              
				'nama'=>$nama,
				'status'=>$status
    ];
	
   $dataQ = [
   'nama_ujian' =>$nama
   ];
           
		   $exec = update($koneksi, 'jenis', $datax,['id_jenis' => $kode]);
		  update($koneksi,'aplikasi', $dataQ,['id_aplikasi' => '1']);
            echo "OK";
			
        
    }
	if ($pg == 'ubahopsi') {
		$opsi = $_POST['opsi'];
    $exec = mysqli_query($koneksi, "UPDATE banksoal SET opsi='$opsi'");
	 $exec = mysqli_query($koneksi, "UPDATE soal SET opsi='$opsi'");
}
if ($pg == 'edit_aplikasi') {
 $data= [
		'aplikasi' => $_POST['apk']
		
		  ];
		  $exec = update($koneksi, 'menu', $data, ['id_menu' => '1']);
}