<?php

include("../../../_init/init.php");
include("../../../_support/class.db.php");


$db = new db;
$db->SetDatabaseConnection(DATABASE_USER,DATABASE_PASS,DATABASE_NAME);

if ($_POST['action'] == 'add') {
	
	$sql = "INSERT INTO `".DATABASE_NAME."`.administration  SET ime = 'novi uporabnik'";
	$db->SQLexecute($sql);
	echo "/* ".$sql." */";
	//$id = $db->GetLastInsertId();
	
	

} elseif ($_POST['action'] == 'update') {
	if ($_POST['field'] == "password") $_POST['value'] = md5($_POST['value']);
	
	$sql = "UPDATE `".DATABASE_NAME."`.administration 
				SET ".$_POST['field']." = '".$_POST['value']."' WHERE id = ".$_POST['id'];
	echo "/* ".$sql." */";
	$result = $db->SQLexecute($sql);
	
} elseif ($_POST['action'] == 'delete') {
	

	$sql = "UPDATE  `".DATABASE_NAME."`.administration  SET deleted = 1  WHERE id = ".$_POST['id'];
	$result = $db->SQLexecute($sql);

	
} else {
	$data = array();
	
	
	$sql = " SELECT * ";	
	$sql .= " FROM `".DATABASE_NAME."`.administration as a  WHERE deleted = 0 ";
	
	if ($_POST['action'] == "search") {
		$sql .= "  AND	(		ime 		LIKE '%".$_POST['searchWord']."%' 
								OR 	priimek 	LIKE '%".$_POST['searchWord']."%' 
								OR	username	LIKE '%".$_POST['searchWord']."%' 
							) ";
	}
	$sql .= " ORDER BY priimek DESC ";
	
	$result = $db->SQLexecute($sql);
	while ($dat = $db->fetchAssoc($result)) {
		
		$data[] = $dat;
	}
	echo "/*".$sql."*/";
	?>
	
	{
	    results: <?php if ($t = count($data)) echo $t; else echo "0"; ?>, 
	    rows: <?php echo json_encode($data); ?>        
	    
	}
<?php 
}
?>