<?php

require __dir__ . '/init.php';


## Edit User Info;

if(isset($_POST['edit'])){
	$_POST = help::sanitize($_POST);
	unset($_POST['edit']);
	$SQL = sQuery::update( 'tbl_users', $_POST, "email='{$email}'" );
	$update = $link->query( $SQL );
	$msg = $update ? "Account Details Edited Successfully!" : "Cannot Edit Account! ";
}


## Update User Image;

if( !empty($_FILES) ) {
	$update = false;
	$image = help::process_image( $_FILES['fileToUpload'] );
	if( $image['error_msg'] ) $img_msg = $image['error_msg'];
	else {
		$img_name = md5($image['name']) . ".{$image['extension']}";
		$target = UPLOADS_DIR . "/profile/{$img_name}";
		$SQL = sQuery::update( 'tbl_users', array("pics" => $img_name), "email='{$email}'" );
		$moved = move_uploaded_file($image['tmp_name'], $target);
		if( $moved && $link->query( $SQL ) ) {
			$update = !!($img_msg = "Profile image updated Successfully");
		} else $img_msg = "Profile image not updated";
	};
};


template::get_header();

$user = help::get_user( $email );

?>

	<?php template::title("Edit User"); ?>
	
	<div class='mb-3 text-right'>
		<a href='statement.php?email=<?php echo $email; ?>' class='btn btn-success'>
			View statement
		</a>
	</div>
	
	<div class="row">
	
		<div class="col-lg-4">
			<div class='card'>
				<div class='card-body'>
				
					<?php template::alert( $img_msg ?? null, $update ?? null ); ?>
			
					<form action="user-edit.php?email=<?php echo $user['email']; ?>" method="POST" enctype="multipart/form-data"  class="form-horizontal form-label-left" >
						<div class="preview mb-3">
							<img src='<?php echo $userImgURL . "/{$user['pics']}"; ?>' width="150px" class='img-thumbnail'>
						</div>
						<div class="item form-group">
							<label class="form-label" for="name">
								User Image <span class="required"></span>
							</label>
							<div class="">
								<input type="file" name="fileToUpload" id="fileToUpload" class="form-control" required >
							</div>
						</div>
						<div class="">
							<button type="submit"  class="btn btn-primary" value="Upload Image" name="submit">Update Image</button>
						</div>
					</form>
				
				</div>
			</div>
		</div>
		
		<!--- SCOPE ---->
		
		<div class="col-lg-8">
			
			<form action="user-edit.php?email=<?php echo $user['email']; ?>" method="POST">
				<div class="card">
					<div class="card-body">
								
						<?php template::alert( $msg, $update ?? null ); ?>
			
						<div class="table-responsive">
							
							<?php
								$userdata = array(
								
									"First Name" => "fname",
									"Last Name" => "lname",
									"Account Type" => 'utype',
									"Account Number" => 'acctno',
									
									"Balance" => "balance",
									"Email" => ['email', 'email'],
									"Phone" => ['number', 'phone'],
									
									"Gender" => ['custom', function() { 
										$gender = function($name){
											global $user;
											if( strtolower($user['gender']) == strtolower($name) ) echo 'selected';
										};
								?>
										<select name='gender' class='form-control'>
											<option value='male' <?php $gender('male'); ?>>Male</option>
											<option value='femail' <?php $gender('female'); ?>>Female</option>
										</select>
								<?php 
									}],
									
									"Password" => 'pwd',
									
									"PIN Number" => 'pin'
									
								);
							?>
								
								<?php 
									foreach( $userdata as $label => $input ): 
										if( is_string($input) ) $input = ['text', $input];
								?>
									<div class='form-group'>
										<label><strong><?php echo $label; ?>:</strong></label>
										<div>
											<?php if( $input[0] != 'custom' ) { ?>
												<input type="<?php echo $input[0]; ?>" name="<?php echo $input[1]; ?>" class='form-control' value="<?php echo $user[($input[1])]; ?>" />
											<?php } else $input[1](); ?>
										</div>
									</div>
								<?php endforeach; ?>

						</div>
						
						<div class='border-top pt-3'>
							<button  type="submit" name="edit" class="btn btn-success btn-icon icon-left">
								<i class="fas fa-save"></i> Save Changes
							</button>
						</div>
						
					</div>
				</div>
			</form>
			
		</div>
	
	</div>

<?php template::get_footer(); ?>  