<?php
session_start();
error_reporting(0);
include('../../includes/config.php');
require_once("../../includes/dbcontroller.php");
$db_handle = new DBController();

$role= $_SESSION['role'];
if($role!='Admin'){
	header("location:$website_url/");
	exit();
}
$uid=$_GET['id'];
if((!isset($_GET['id']))){
	header("location:$website_url$listusers_url");
	exit();
}

$account_query="SELECT * FROM users where userid ='$uid'";
$account_data = current($db_handle->runQuery($account_query));

if(isset($_POST['update'])){
	$email=$_POST['email'];
	$admin=$_POST['admin'];
	$active=$_POST['active'];
	
	$update_status = "update users set Useremail ='$email',admin='$admin',active='$active' where userid='$uid'";
	$result = $db_handle->executeQuery($update_status);
	if($result==1){
		echo "<script>alert('User Updated Succesfully')</script>";
		echo "<script>location.href = '$website_url$listusers_url';</script>";
	}
	else{
		echo "<script>alert('There is an issue on updating user')</script>";
		echo "<script>location.href = '$website_url$listusers_url';</script>";
	}	
}
include('../../includes/menu.php');
?>

<div class="container main-cont single-edit-user">
	<h2 class="page-title">Edit User <?php echo $account_data['Useremail']; ?></h2>
	<div class="form-group">
		<form  enctype="multipart/form-data" method="post" accept-charset="utf-8">
			<div class="col-lg-6 col-xs-12 col-sm-6  col-md-6 outer-cell">
				<div class="inner-cell">
					<div class="frm-item">
						<label for="email">User Email</label>            
						<input type="email" name="email" value="<?php echo $account_data['Useremail']; ?>" class="form-control" required>
					</div>
				</div>
			</div>
			<div class="clearfix"></div> 
			<div class="form-group col-md-6">
				<label for="admin">Admin</label>
				<select name="admin" id="admin" class="form-control" >
					<option value="0" <?php echo $account_data['Admin'] == 0 ? 'selected':'' ?>>No</option>
					<option value="1" <?php echo $account_data['Admin'] == 1 ? 'selected':'' ?>>Yes</option>
				</select>
			</div>
			<div class="clearfix"></div>
			<div class="form-group col-md-6">
				<label for="active">Active</label>
				<select name="active" id="active" class="form-control" >
					<option value="1" <?php echo $account_data['Active'] == 0 ? 'selected':'' ?>>Yes</option>
					<option value="0" <?php echo $account_data['Active'] == 0 ? 'selected':'' ?>>No</option>
				</select>
			</div>
			<div class="clearfix"></div> 
			<div class="frm-btn">
				<input type="submit" name="update" value="Update" class="btn btn-primary btn-add">
			</div>
		</form>
	</div> 
</div>

<?php
include('../../includes/footer.php');
include('../../includes/webfooter.php');
?>