<?php
ini_set('max_execution_time', 300); 
require "../../config/koneksi.php";
require "../../config/function.php";

$datetime = date('Y-m-d H:i:s');
if ($koneksi) {
    
    $token = $setting['token_api'];
	
	$data[]='siswa';
   $data[]='users';
	
    if ($token <> '' and $data <> '') {
       
        foreach ($data as $data) {
           
            if ($data == 'siswa') {
                $datax = http_request($setting['url_host'] . "/syncsiswa.php?token=" . $token .  "&npsn=" . $setting['npsn']);
                $r = json_decode($datax, TRUE);
                if ($r <> null) {
                  
                    $sql = mysqli_query($koneksi, "truncate table siswa");
                   
                    foreach ($r['siswa'] as $r) {
                        $sql = mysqli_query($koneksi, "insert into siswa
                            (id_siswa,kelas,pk,nis,no_peserta,nama,level,ruang,sesi,username,password,agama,jk,server,npsn,foto) values 			
                            ('$r[id_siswa]','$r[kelas]','$r[pk]','$r[nis]','$r[no_peserta]','" . addslashes($r['nama']) . "','$r[level]','$r[ruang]','$r[sesi]','$r[username]','$r[password]','$r[agama]','$r[jk]','$r[server]','$r[npsn]','$r[foto]')");

                        $qkelas = mysqli_query($koneksi, "SELECT kelas FROM kelas WHERE kelas='$r[kelas]'");
                        $cekkelas = mysqli_num_rows($qkelas);
                        if (!$cekkelas <> 0) {
                            $exec = mysqli_query($koneksi, "INSERT INTO kelas (kelas,level)VALUES('$r[kelas]','$r[level]')");
                        }
						$qlevel = mysqli_query($koneksi, "SELECT level FROM level WHERE level='$r[level]'");
                        $ceklevel = mysqli_num_rows($qlevel);
                        if (!$ceklevel <> 0) {
                            $exec = mysqli_query($koneksi, "INSERT INTO level (level)VALUES('$r[level]')");
                        }
						$qruang = mysqli_query($koneksi, "SELECT kode_ruang FROM ruang WHERE kode_ruang='$r[ruang]'");
                        $cekruang = mysqli_num_rows($qruang);
                        if (!$cekruang <> 0) {
                            $exec = mysqli_query($koneksi, "INSERT INTO ruang (kode_ruang,keterangan)VALUES('$r[ruang]','$r[ruang]')");
                        }
						 $qpk = mysqli_query($koneksi, "SELECT id_pk FROM pk WHERE id_pk='$r[pk]'");
                            $cekpk = mysqli_num_rows($qpk);
                            if (!$cekpk <> 0) {
                                $exec = mysqli_query($koneksi, "INSERT INTO pk (id_pk,pk)VALUES('$r[pk]','$r[pk]')");
                            }
						 $qsesi = mysqli_query($koneksi, "SELECT kode_sesi FROM sesi WHERE kode_sesi='$r[sesi]'");
                        $ceksesi = mysqli_num_rows($qsesi);
                        if (!$ceksesi <> 0) {
                            $exec = mysqli_query($koneksi, "INSERT INTO sesi (kode_sesi,nama_sesi)VALUES('$r[sesi]','$r[sesi]')");
                        }
                       
                    }

              }
			
            }
			  
		 if ($data == 'users') {
                $syncbank = http_request($setting['url_host'] . "/syncdata.php?token=" . $token);
               $sync = json_decode($syncbank, TRUE);

                if ($sync <> null) {
                
                    $sql = mysqli_query($koneksi, "truncate table users");
                    foreach ($sync['users'] as $u) {                    
                        
						$sqlbank = mysqli_query($koneksi, "insert into users
                                (id_user,nip,nama,walas,username,password,level) values 			
                                ('$u[id_user]','$u[nip]','$u[nama]','$u[walas]','$u[username]','$u[password]','$u[level]')");
			      
				  }	
			 }
			}
		
		
		
		}
	}	
}