<?php
require_once("includes/initialize.php");
if (!$session->is_logged_in()){redirect_to("index.php");}
$message="";

$username="";
$password="";
$surname="";
$othernames="";
$email="";
$upload="";
$errors = array();
$upload_errors = array(

UPLOAD_ERR_OK	=>"No errors",
UPLOAD_ERR_INI_SIZE =>"Larger than upload_max_filesize",
UPLOAD_ERR_FORM_SIZE=>"Larger than form MAX_FILE_SIZE",
UPLOAD_ERR_PARTIAL=>"Partial upload",
UPLOAD_ERR_NO_FILE=>"No file",
UPLOAD_ERR_NO_TMP_DIR=>"No temporary directory",
UPLOAD_ERR_CANT_WRITE=>"Can't write to disk",
UPLOAD_ERR_EXTENSION=>"File upload stopped by extension"

);
// if (!isset($_POST['submit'])){
//     header("Location: index.php");
// }else
if (isset($_POST['submit'])){
	$id = $session->user_id;

$email = mysql_real_escape_string($_POST['email']);	
$password =  mysql_real_escape_string($_POST['password']);
$surname = mysql_real_escape_string($_POST['surname']);	
$othernames =  mysql_real_escape_string($_POST['othernames']);

$tmp_file = $_FILES['file_upload']['tmp_name'];
$target_file = basename($_FILES['file_upload']['name']);
$upload_dir = "pictures/".$target_file;
// if (empty($target_file)){

//     $message = "No picture file to upload";
    
// }
// if (){
// 	$erro = $_FILES['file_upload']['error'];
// 	$message=$upload_errors[$erro];
 if ((empty($target_file)) && $email!='' && $password!='' && $surname !='' && $othernames!='') {
     $message = "Welcome";
	    	$up =ADMINACCOUNT::update2($email,$password,$surname,$othernames,$id);
	    	echo $up;
	    header("Location: dashboard.php");
// 	}else if ($email =='' || $password =='' || $surname =='' || $othernames ==''){
// 	    $message ="One or more fields is missing";
	    
	    
// 	}
}else if ((!empty($target_file)) && (file_exists($upload_dir))){
    
    $erro = $_FILES['file_upload']['error'];
 	$message=$upload_errors[$erro];
}else if (move_uploaded_file($tmp_file, $upload_dir)){
	
	$up =ADMINACCOUNT::update($email,$password,$surname,$othernames,$target_file,$id);
	if ($up){
header("Location: dashboard.php");
	} 
}else {
	$erro = $_FILES['file_upload']['error'];
	$message=$upload_errors[$erro];
}
//echo "Done";
// }
if (empty($email)){
	array_push($errors, "Email is required");
}if (empty($password)){
	array_push($errors, "Password is required");
}
if (empty($surname)){
	array_push($errors, "Surname is required");
}
if (empty($othernames)){
	array_push($errors, "Othernames is required");
}

}

 ?>	
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OSCOTECHADMIN:index</title>
<link href="stylesheet/newuser.css" type="text/css" rel="stylesheet" />
</head>

<body>
<div class="setimage">
<img src="images/icon.png" />

</div>
<div class="header">
<h2>Account Details</h2>
</div>
<form method="post" class="formup" action="account_updata.php" enctype="multipart/form-data">
<?php if (count($errors)>0){?>
<div class="error">

<?php foreach ($errors as $error){?>
<p><?php echo $error; ?></p>
<?php } ?>

</div>
<?php }?>	
<?php if ($message !=''){
	 echo output_message($message);
}?>

<div class="input-group">

<label>Email</label>
<input type="text" name="email" required="required" style="margin-left:43px" value="<?php echo htmlentities($email);?>" />

</div>
<div id="input-group2">
<label>Password</label>
<input type="password" required="required" name="password" style="margin-left:20px" value="<?php echo htmlentities($password)?>";/>
</div>

<div class="input-group">

<label>Surname</label>
<input type="text"  required="required" name="surname" style="margin-left:20px" value="<?php echo htmlentities($surname);?>" />

</div>
<div id="input-group2">
<label>Othernames</label>
<input type="othernames"  required="required" name="othernames" value="<?php echo htmlentities($othernames)?>";/>
</div>

<div >
<label>Passport</label>
<input type="file" style="margin-left:30px" name="file_upload" maxsize="100000" ;/>
</div>

<div class="input-group2">
<center><button type="submit" name="submit" style="width:30%; font-stretch:extra-expanded; font-weight:bolder" class="btn">Update</button></center>
</div>
</form>
</body>
</html>
