<?php



$errors = array();
$info = array();
$info_success = array();


//check the id passed
if(isset($_POST['staff']))
{
	$id =  $_POST['staff'];
}





//check if action is save_users
//validate the first name
if(!empty($_POST['first_name']))
{
   $first_name = mysqli_real_escape_string($dbc, $_POST['first_name']);
   
   //collect first name to fill the static form
   $_SESSION['fn'] = $first_name;
 
}
else
{
   $errors[] = '<p> * </p>';
   
   
}



//validate the other names
if(!empty($_POST['other_names']))
{
   $other_names = mysqli_real_escape_string($dbc, $_POST['other_names']);
   
   $_SESSION['on'] = $other_names;
 
}
else
{
   $errors[] = '<p> * </p>';
}

//collect the gender
$gender = mysqli_real_escape_string($dbc, $_POST['gender']);
$_SESSION['gender'] = $gender;


//validate the email
if(!empty($_POST['email']))
{
   $email = mysqli_real_escape_string($dbc, $_POST['email']);
   
   $_SESSION['e'] = $email;
 
}
else
{
   $errors[] = '<p> * </p>';
}

//validate the username
if(!empty($_POST['username']))
{
   $username = mysqli_real_escape_string($dbc, $_POST['username']);
   
   $_SESSION['u'] = $username;
 
}
else
{
   $errors[] = '<p> * </p>';
}



//validate the group
if($_POST['group'] == 0)
{
	 $errors[] = '<p> * </p>';
   
 
}
else
{
	$group = mysqli_real_escape_string($dbc, $_POST['group']);
	$_SESSION['g'] = $group;
  
}

// //validate optional birthdate
//  $birthdate= (!empty($_POST['birthdate']))? (mysqli_real_escape_string($dbc,$_POST['birthdate'])): 
//  NULL;
//  $_SESSION['birthdate'] = $birthdate;
 
 
 //check image
//check for image using my own custom script
	// if (array_key_exists('upload', $_POST))
	// {
	// 	//define the path
	// 	define('UPLOAD_DIRECTORY', 'uploads/pictures/');
		
	// 	//getting my caption now 
	// 	//replace any space with an underscore 
	// 	$file = $_FILES['image']['name'];
	// 	$image_r = str_replace(' ', '_',$file);
		
		
	// 	//prevent overwritten
	// 	$now = date('Y-m-d-His');
	// 	$image_r = $now.$image_r;
			
		
	// 	//convert the maximum size to kilobyte
	// 	$maximum_size = number_format(MAX_FILE_SIZE/1024 ,1 ).'KB';
		
	// 	//create an array of permitted files
	// 	$permitted_images_type = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
		
	// 	//begin by assume type is not ok
	// 	$type_ok = false;
		
	// 	//begin by assuming the filesize aint aceptable
	// 	$size_ok = false;
		
	// 	//check if he filoe is of permitted type
	// 	foreach($permitted_images_type as $type)
	// 	{
	// 		if ($type == $_FILES['image']['type'] )
	// 		{
	// 			$type_ok = true;
				
	// 			break;
	// 		}
			
	// 	}
		
	// 	//check file if is within the permitted size
	// 	if (($_FILES['image']['size']) > 0  && ($_FILES['image']['size'] <= MAX_FILE_SIZE))
	// 	{
	// 		$size_ok = true;
	// 	}
		
		
	// 	if($size_ok and $type_ok)
	// 	{
	// 		switch($_FILES['image']['error'])
	// 		{
	// 			case 0: 
	// 			{
				
	// 				//move the file to the upload folder and rename it
	// 				$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIRECTORY.$image_r);
					
	// 				if($success)
	// 				{
	// 					$image = $image_r;
	// 					$info['image_result_message'] = "$file uploaded successfully";
	// 				}
	// 				else
	// 				{
	// 					$errors['image_result_message'] = "Error uploading $file";
	// 				}
	// 			break;	
	// 			}
				
	// 			case 3:
	// 			{
	// 				$errors['image_result_message'] = "Error Uploading $file. Please try again";
	// 			}
	// 			default:
	// 			{
	// 				$errors['image_result_message'] = "System error uploading $file. Contact Webmaster.";
	// 			}
				
				
	// 		}//end of switch
	// 	}//end of size ok
	// 	else if($_FILES['image']['error'] == 4)
	// 	{
	// 		//collect the former name of the image
	// 		$query = "SELECT image FROM users WHERE '$id' = user_id";
	// 		$result = mysqli_query($dbc,$query);
			
	// 		if(mysqli_num_rows($result) > 0)
	// 		{
	// 			while($row_i = mysqli_fetch_array($result, MYSQLI_ASSOC))
	// 			{
	// 				$image = $row_i['image']; 
	// 			}
				
	// 		}
	// 		//end of checking if file exist 

	// 	}
		
	// 	else
	// 	{
	// 		$errors['image_result_message'] = "$file cannot be uploaded. Maximum size: $maximum_size. Types: 
	// 		gif, jpg, 	png.";
	// 	}
		
										
	
	// }//end of if upload

 
 
 
 
 
//validate the address
// if(!empty($_POST['address']))
// {
//    $address = mysqli_real_escape_string($dbc, $_POST['address']);
//    $_SESSION['address'] = $address;
 
// }
// else
// {
//    $errors[] = '<p> * </p>';
// }

$department = mysqli_real_escape_string($dbc, $_POST['department']);
$_SESSION['department'] = $department;

$address= (!empty($_POST['address']))? (mysqli_real_escape_string($dbc,$_POST['address'])): 
NULL;

$_SESSION['address'] =$address;

 
 
 
//validate the phone_number
if(!empty($_POST['phone_number']) && is_numeric($_POST['phone_number']))
{
   $phone_number = mysqli_real_escape_string($dbc, $_POST['phone_number']);
   
   $_SESSION['phone_number'] =$phone_number;
 
}
else
{
   $errors[] = '<p> * </p>';
}
 
						  
	
//validate optional user_status
$user_status= (!empty($_POST['user_status']))? (mysqli_real_escape_string($dbc,$_POST['user_status'])): 
NULL;

$_SESSION['user_status'] =$user_status;

//collect the status
$status = (!empty($_POST['status']))? (mysqli_real_escape_string($dbc,$_POST['status'])): 
 NULL;
$_SESSION['status'] = $status;



//modified_by 
$user_id = get_user_id();
$sid = get_security_id($user_id);





if(empty($errors))
{
	//destroy session if there is no error
	unset($_SESSION['fn']);
	unset($_SESSION['on']);
	unset($_SESSION['g']);
	unset($_SESSION['e']);
	unset($_SESSION['un']);
	unset($_SESSION['g']);
	unset($_SESSION['department']);
	unset($_SESSION['address']);
	unset($_SESSION['phone_number']);
	unset($_SESSION['user_status']);
	unset($_SESSION['status']);
	
	
	 //check if the query is for adding den add
	
	$q = "UPDATE staff SET first_name ='$first_name', other_names= '$other_names', gender ='$gender', email ='$email', username = '$username', number = '$username', group_id = '$group', department_id = '$department', address = '$address',phone_number ='$phone_number', user_status = '$user_status', date_modified=NOW(), modified_by = '$sid', status = '$status'
							
				WHERE id = '$id'";
							
							
	$r = mysqli_query($dbc,$q);
	
	if($r)
	{
		$info_success['success'] = 'Updated successfully!';
		header("Location: gateway.php?act=view_staffs&is=".urlencode($info_success['success']).""); //is - info success
		
	}
	else
	{
		$errors['error'] =  'Staff not updated due to a system error. We apologize for any inconvenience.';
										
		header("Location: gateway.php?act=view_staffs&error=".urlencode($errors['error'])."");
	}
	
}
else
{//stay back in add_users form


	$errors['error'] =  'Staff not updated. Re-select user and Fill the form appropriately, take note of <span class="ast">*</span>.';
										
	header("Location: gateway.php?act=view_staffs&error=".urlencode($errors['error'])."");

}



?>