<?php
require("../../../config/koneksi.php");
require("../../../config/function.php");
require("../../../config/crud.php");
(isset($_GET['pg'])) ? $pg = $_GET['pg'] : $pg = '';

if ($pg == 'tambah') {
	if($_POST['id']<>''):
  $where = [
  'idpeg' => $_POST['id'],		
    ];
    $data = [
'idpeg' => $_POST['id'],
'nokartu' => $_POST['nokartu'],
'nama' => $_POST['nama'],
'level' =>'pegawai'
    ];
	
 $cek = rowcount($koneksi, 'datareg', $where);
            if ($cek == 0) {
				mysqli_query($koneksi, "update users SET sts_jari='1' WHERE id_user='$_POST[id]'");
            $exec = insert($koneksi, 'datareg', $data);
            echo"OK";
if ($exec){
mysqli_query($koneksi, "delete from tmpreg");
			}
		}
	
endif;
}

if ($pg == 'hapus') {
 $id = $_POST['id'];
  $reg = mysqli_fetch_array(mysqli_query($koneksi, "SELECT * FROM datareg  WHERE id='$id'"));
  $data = [
		'idjari' => $reg['idjari'],
		'nama' => $reg['nama'],
		'serial' => $reg['serial']
    ];
	$exec = insert($koneksi, 'temp_finger', $data);
	
	if($reg['level']=='pegawai'):				          
    mysqli_query($koneksi, "update users SET sts_jari='0',idjari=NULL WHERE idjari='$reg[idjari]'");
	elseif($reg['level']=='siswa'):
	 mysqli_query($koneksi, "update siswa SET sts_jari='0',idjari=NULL WHERE idjari='$reg[idjari]'");
	endif;
    delete($koneksi, 'datareg', ['id' => $id]);
	
}

if ($pg == 'siswa') {
	if($_POST['id']<>''):
  
	$idjari = $_POST['idjari'];
    $data = [
'idsiswa' => $_POST['id'],
'idjari' => $_POST['idjari'],
'serial' => $_POST['serial'],
'nama' => $_POST['nama'],
'level' =>'siswa'
    ];

            $exec = insert($koneksi, 'datareg', $data);
            echo"OK";
if ($exec){

mysqli_query($koneksi, "update siswa SET sts_jari='1',idjari='$idjari' WHERE id_siswa='$_POST[id]'");
			}
		
	
endif;
}