<?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') {
    if ($_POST['password'] <> "") {
        $data = [
            'username'     => $_POST['username'],
            'nama'   => $_POST['nama'],
			'nowa'   => $_POST['nowa'],
            'password'      => password_hash($_POST['password'], PASSWORD_DEFAULT)
        ];
    } else {
        $data = [
            'username'     => $_POST['username'],
            'nama'   => $_POST['nama'],
			 'nowa'   => $_POST['nowa']
        ];
    }
    $id_user = $_POST['id'];
    $exec = update($koneksi, 'users', $data, ['id_user' => $id_user]);
    echo $exec;
}
if ($pg == 'tambah') {
    $data = [
        'nip'          => $_POST['nip'],
        'username'     => $_POST['username'],
        'nama'   => $_POST['nama'],
		 'nowa'   => $_POST['nowa'],
        'level'         => 'admin',
        'password'      => password_hash($_POST['password'], PASSWORD_DEFAULT)

    ];
    $exec = insert($koneksi, 'users', $data);
    echo $exec;
}
if ($pg == 'hapus') {
    $id = $_POST['id'];
    delete($koneksi, 'users', ['id_user' => $id]);
}


if ($pg == 'tambah_guru') {     
     $nama = $_POST['nama'];
     $nama = str_replace("'", "&#39;", $nama); 
	 $data = [
        'nip' => $_POST['nip'],
        'nama'   => $nama,
        'username'  => $_POST['username'],
        'password'  => $_POST['password'],
		 'level'  => 'guru',       
         'jabatan'  => $_POST['walas'],
		 'nowa'  => $_POST['nowa'] 
    ];
	 $where = [
        'username'  => $_POST['username']  
    ];
	 $cek = rowcount($koneksi, 'users', $where);
            if ($cek == 0) {
    $exec = insert($koneksi, 'users', $data);
    echo "OK";
	}else{
		echo"gagal";
	}						
}
if ($pg == 'tambah_staff') {     
     $nama = $_POST['nama'];
     $nama = str_replace("'", "&#39;", $nama); 
	 $data = [
        'nip' => $_POST['nip'],
        'nama'   => $nama,
        'username'  => $_POST['username'],
        'password'  => $_POST['password'],
		 'level'  => 'staff',       
		 'nowa'  => $_POST['nowa'] 
    ];
	 $where = [
        'username'  => $_POST['username']  
    ];
	 $cek = rowcount($koneksi, 'users', $where);
            if ($cek == 0) {
    $exec = insert($koneksi, 'users', $data);
    echo "OK";
	}else{
		echo"gagal";
	}						
}	
if ($pg == 'edit_guru') { 
     $id = $_POST['id'];    
     $nama = $_POST['nama'];
     $nama = str_replace("'", "&#39;", $nama); 
	 $data = [
        'nip' => $_POST['nip'],
        'nama'   => $nama,
        'password'  => $_POST['password'],      
         'jabatan'  => $_POST['walas'],
		 'nowa'  => $_POST['nowa'] 
    ];
	
    $exec = update($koneksi, 'users', $data,['id_user'=>$id]);
    echo "OK";
						
}	
if ($pg == 'edit_staff') { 
     $id = $_POST['id'];    
     $nama = $_POST['nama'];
     $nama = str_replace("'", "&#39;", $nama); 
	 $data = [
        'nip' => $_POST['nip'],
        'nama'   => $nama,
        'password'  => $_POST['password'],      
		 'nowa'  => $_POST['nowa'] 
    ];
	
    $exec = update($koneksi, 'users', $data,['id_user'=>$id]);
    echo "OK";
						
}		
if ($pg == 'hapusakun') {
	
    mysqli_query($koneksi, "UPDATE setting SET nogateway=NULL, apikey=NULL WHERE id_setting='1'");
	$files = glob('../../baileys_auth_info/*'); 
foreach ($files as $file) {
    if (is_file($file))
        unlink($file); 
}
}
if ($pg == 'ubahnilai') {
	$kirim = $_POST['kirim'];
	
    mysqli_query($koneksi, "UPDATE setting SET kirimnilai='$kirim' WHERE id_setting='1'");
}
if ($pg == 'ubahabsen') {
	$kirim = $_POST['kirim'];
	
    mysqli_query($koneksi, "UPDATE setting SET kirimabsen='$kirim' WHERE id_setting='1'");
}
if ($pg == 'profil') {
	$idu = $_POST['idu'];
	 $ektensi = ['jpg', 'png','jpeg'];
        if ($_FILES['gambar']['name'] <> '') {
            $gambar = $_FILES['gambar']['name'];
            $temp = $_FILES['gambar']['tmp_name'];
            $ext = explode('.', $gambar);
            $ext = end($ext);
            if (in_array($ext, $ektensi)) {
                $dest = 'foto/fotoguru/gambar' . rand(1, 100) . '.' . $ext;
                $upload = move_uploaded_file($temp, '../../' . $dest);
                if ($upload) {
                    $exec = update($koneksi, 'users', ['foto' => $dest], ['id_user' => $idu]);
                } else {
                    echo "gagal";
                }
            }
        }
}