<?php
ob_start();
?>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<?php
//var_dump($_POST);
include '../db_con.php';

	        
	@$name = mysqli_real_escape_string($con,$_REQUEST['user']['name']);
	@$u_name = mysqli_real_escape_string($con,$_REQUEST['user']['u_name']);
	@$u_email = mysqli_real_escape_string($con,$_REQUEST['user']['u_email']);
	@$u_password = sha1(mysqli_real_escape_string($con,$_REQUEST['user']['u_password']));
	@$u_status = mysqli_real_escape_string($con,$_REQUEST['user']['u_status']);
	@$u_dcenter = mysqli_real_escape_string($con,$_REQUEST['user']['delcenter']);


	$rule=$_POST["rule"];
	$role='';
	for ($row = 0; $row < count($rule); $row++) {

		$role.=$rule[$row].","; 
	}

	//var_dump($_POST) or die();

	if($_REQUEST['actionid']=='insert'){

		if(isset($u_name) && !empty($u_name)){

			$rs = @mysqli_query($con,"select u_name from adminusers where u_name='$u_name'");
			$rowno=mysqli_num_rows($rs);
			if($rowno>0){

				header('Location: adduser.php?msg=2');
				die();

			}else{

			$sql = "INSERT INTO `adminusers`(`name`, `u_name`, `u_email`, `u_password`, `u_status`, `u_type`, restrictions, delcenter) VALUES ('$name','$u_name','$u_email','$u_password','$u_status','2','$role','$u_dcenter')";
			//echo $sql;

			//var_dump($sql) or die();
			$result = @mysqli_query($con,$sql);
				if ($result){
					header('Location: user.php');
				} else {
					
					header('Location: user.php?msg=2');
					//echo "Some errors occured.";
				}
			}

		}
	}
		


	if($_REQUEST['actionid']=='update'){
		
		
		$pid=$_REQUEST['id'];

		if(isset($u_name) && !empty($u_name)){

			$rs = @mysqli_query($con,"SELECT u_name from adminusers where u_name='$u_name' AND u_id!='$pid'");
			//echo "SELECT u_name from user where u_name='$u_name' AND u_id!='$pid'";
			//$prow=mysqli_fetch_array($rs);
			$rowno=mysqli_num_rows($rs);
			if($rowno>0){

				//header("Location:update_user.php?msg=3&pid=".$pid."'");
				die();

			}else{
				//$sql = "update menu set mname= N'$name', mdesc= N'$desc',  mimage=N'$image' where mid=$pid";

				// $sql = "UPDATE `user` SET `name`='$name',`u_email`='$u_email',`u_status`='$u_status', `u_password`='$u_password' WHERE u_id = $pid";

				$sql = "UPDATE `adminusers` SET `name`='$name',`u_name`='$u_name',`u_email`='$u_email',`u_status`='$u_status', restrictions='$role', delcenter='$u_dcenter' WHERE u_id = $pid";

				//var_dump($sql) or die();

				$result = @mysqli_query($con,$sql);
					if ($result){
						
						header('Location: user.php');
						} 
					else {
						//echo $sql;
						echo "Some errors occured.";
					}
			}
		}
	}




	if($_REQUEST['actionid']=='remove'){

		$id = intval($_REQUEST['id']);
				
		$sql = "delete from adminusers where u_id=$id";
		$result = @mysqli_query($con,$sql);
		if ($result){
			
			header("Location: user.php");
			//echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Some errors occured.'));
		}
	}

	if($_REQUEST['actionid']=='change'){

		$id = intval($_REQUEST['id']);

		$sql = "select nat from item where itemid='$id'";
		//echo $sql;
		$rs = @mysqli_query($con,$sql);
		$row=mysqli_fetch_array($rs);
		$status=$row['nat'];
						
		if($status==1) $sql = "update item set nat=0 where itemid=$id";
		if($status==0) $sql = "update item set nat=1 where itemid=$id";
		$result = @mysqli_query($con,$sql);
		if ($result){
			
			header("Location: nat.php");
			//echo "Location: viewcoffee.php?product='".$product."'";
			//echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Some errors occured.'));
		}


	}
?>