<?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();
}
if(isset($_POST['add'])){
	$password=$_POST['password'];
	$confirm_password=$_POST['confirm_password'];
	if($password===$confirm_password){
		$email=$_POST['email'];
		$pass=md5($_POST['password']);
		$admin=$_POST['admin'];
		$active=$_POST['active'];

		$exist_user_sql="select * from users WHERE useremail='$email'";
		$count   = $db_handle->numRows($exist_user_sql);
		if($count>0){
			$extra=$_SERVER['REQUEST_URI'];
			header("location:$website_url$extra");
			$_SESSION['usererrmsg']="Email is already registered";
			exit();
		}
		$insert = "INSERT into users(Useremail,password,admin,active) values('$email','$pass',$admin,$active)";
		$result = $db_handle->executeQuery($insert);
		
		echo "<script>alert('User Created Sucessfully.')</script>";
		echo "<script>location.href = '$website_url$listusers_url';</script>";
	}
	else{
		$extra=$_SERVER['REQUEST_URI'];
		header("location:$website_url$extra");
		$_SESSION['passerrmsg']="New Password and Confirm Password should be same";
		exit();
	}
}
include('../../includes/menu.php');
?>

<div class="container main-cont single-add-user"><h2 class="page-title">Add User</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="" class="form-control" required>
					</div>
				</div>
			</div>
			<div class="clearfix"></div>
			<span style="color:red;" >
				<?php
				echo htmlentities($_SESSION['usererrmsg']);
				?>
				<?php
				echo htmlentities($_SESSION['usererrmsg']="");
				?>
			</span>
			<div class="clearfix"></div>
			<div class="col-lg-3 col-xs-12 col-sm-6  col-md-6 outer-cell">
				<div class="inner-cell">
					<div class="frm-item">    
						<label for="password">Password</label>           
						<input type="password" name="password" value="" class="form-control" required>
					</div>
				</div>
			</div>
			<div class="col-lg-3 col-xs-12 col-sm-6  col-md-6 outer-cell">
				<div class="inner-cell">
					<div class="frm-item">    
						<label for="confirm_password">Confirm Password</label>            
						<input type="password" name="confirm_password" value="" 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">No</option>
					<option value="1">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">Yes</option>
					<option value="0">No</option>
				</select>
			</div>
			<div class="clearfix"></div>
			<span style="color:red;" >
				<?php
				echo htmlentities($_SESSION['passerrmsg']);
				?>
				<?php
				echo htmlentities($_SESSION['passerrmsg']="");
				?>
			</span>
			<div class="frm-btn">
				<input type="submit" name="add" value="Add" class="btn btn-primary btn-add">
			</div>
		</form>
	</div> 
</div>
<?php
include('../../includes/footer.php');
include('../../includes/webfooter.php');
?>