<?php include('lib/config.php');?>
<?php		
	//===============================================================
		
	$toDay = date("Y-m-d");
	$cDateTime = date("mdYHis");
	
	$user_id = isset($_POST['user_id'])?$_POST['user_id']:'';
	
	$user_pic = isset($_POST['user_pic'])?$_POST['user_pic']:'';
	$group_id = isset($_POST['group_id'])?$_POST['group_id']:'';
	$first_name = isset($_POST['first_name'])?$_POST['first_name']:'';
	$last_name = isset($_POST['last_name'])?$_POST['last_name']:'';
	$user_name = isset($_POST['user_name'])?$_POST['user_name']:'';
	$user_email = isset($_POST['user_email'])?$_POST['user_email']:'';
	$user_password = isset($_POST['user_password'])?$_POST['user_password']:'';		
	$status = isset($_POST['status'])?$_POST['status']:'';
	
	//===============================================================
	//============== Uploading Files ===========================
	//===============================================================
	$oldDocfile = isset($_POST['old_user_pic'])?$_POST['old_user_pic']:'';
	
	$uploaddir = 'uploads/users/';
	
	if(isset($_FILES['user_pic']['name']) && $_FILES['user_pic']['name'] != '')
	{			
		$docarr = explode(".", $_FILES['user_pic']['name']);						
		$docfile = $docarr[0]."_".$cDateTime.'.'.$docarr[1];
		$upload = $uploaddir . $docfile;
		if(move_uploaded_file($_FILES['user_pic']['tmp_name'], $upload))
		{
			@unlink("uploads/users/$oldImgfile");			
			$oldDocfile = $docfile;			
		}	
	}
	//===============================================================
	//============== Add record ==============================
	//===============================================================
	if(isset($_POST['action']) && $_POST['action'] =='save')
	{	
		$insQry = "INSERT INTO ".TBL_USERS." SET 
		user_pic='".$oldDocfile."',
		group_id='".addslashes($group_id)."',
		first_name='".addslashes($first_name)."',
		last_name='".addslashes($last_name)."',
		user_name='".addslashes($user_name)."',
		user_email='".addslashes($user_email)."',
		user_password='".md5($user_password)."',
		status='".addslashes($status)."',
		created = '".$toDay."' ";
		
		$errNo = $con->insertRecords($insQry, __FILE__,__LINE__);
		
		if($errNo===TRUE )
			$return_arr[] = array("message" => 'ok');
		else
			$return_arr[] = array("message" => 'err');
					
		echo json_encode($return_arr);
	}
	//===============================================================
	//============== Update record ==================================
	//===============================================================
	elseif(isset($_POST['action']) && $_POST['action'] =='update')
	{			
		if(!empty($user_password))
		{		
			$updQry = "UPDATE ".TBL_USERS." SET 
			user_pic='".$oldDocfile."',
			group_id='".addslashes($group_id)."',
			first_name='".addslashes($first_name)."',
			last_name='".addslashes($last_name)."',
			user_email='".addslashes($user_email)."',
			user_password='".md5($user_password)."',
			status='".addslashes($status)."',
			modified = '".$toDay."' 
			WHERE user_id = '".$user_id."' LIMIT 1";
		}
		else
		{
			$updQry = "UPDATE ".TBL_USERS." SET 
			user_pic='".$oldDocfile."',
			group_id='".addslashes($group_id)."',
			first_name='".addslashes($first_name)."',
			last_name='".addslashes($last_name)."',
			user_email='".addslashes($user_email)."',
			status='".addslashes($status)."',
			modified = '".$toDay."' 
			WHERE user_id = '".$user_id."' LIMIT 1";
		}	
		$errNo = $con->updateRecords($updQry, __FILE__,__LINE__);
		
		if($errNo===TRUE )
			$return_arr[] = array("message" => 'ok');
		else
			$return_arr[] = array("message" => 'err');
					
		echo json_encode($return_arr);
	}
	//===============================================================
	//============== Departments Access ==========================
	//===============================================================
	if(isset($_POST['action']) && $_POST['action'] =='departmentAccess')
	{	
		//------------------ Delete Old ---------------
		$delQry = "DELETE FROM ".TBL_USERS_DEPARTMENTS_PERMISSIONS." WHERE user_id = '".$user_id."'";
		$errNo = $con->deleteRecords($delQry, __FILE__,__LINE__);
		//---------------------------------------------
		
		if(!empty($_POST['departments']))
		{    
			foreach($_POST['departments'] as $dept)
			{
				$insQry = "INSERT INTO ".TBL_USERS_DEPARTMENTS_PERMISSIONS." SET user_id='".addslashes($user_id)."',dept_id='".addslashes($dept)."' ";				
				$errNo = $con->insertRecords($insQry, __FILE__,__LINE__);
			}
		}
		
		if($errNo===TRUE )
			$return_arr[] = array("message" => 'ok');
		else
			$return_arr[] = array("message" => 'err');
					
		echo json_encode($return_arr);
	}
	//===============================================================
	//============== Delete record ==================================
	//===============================================================
	elseif(isset($_POST['action']) && $_POST['action'] =='delete')
	{			
		$delQry = "DELETE FROM ".TBL_USERS." WHERE user_id = '".$user_id."' LIMIT 1";
		$errNo = $con->deleteRecords($delQry, __FILE__,__LINE__);
		
		if($errNo===TRUE )
			$return_arr[] = array("message" => 'ok');
		else
			$return_arr[] = array("message" => 'err');
					
		echo json_encode($return_arr);
	}
?>