<?php

include 'load.php';

if(isset($_POST['save']))

{

		$fullname = $_POST['fullname'];
		
		$phonenumber = $_POST['phonenumber'];
		
		$address = $_POST['address'];
		
		$user_name = $_POST['user_name'];
		
		$user_email = $_POST['user_email'];
		
		$user_pass = $_POST['user_pass'];

        $user_pass = password_hash($user_pass, PASSWORD_DEFAULT);
		
		$admintype = $_POST['admintype'];
		
		$ucomission = $_POST['ucomission'];

		$status = $_POST['status'];

		$branch_id = $_POST['branch_id'];
		
		$basicsalary = $_POST['basicsalary'];
		
		$othourrate = $_POST['othourrate'];
		
		$imgFile = $_FILES['user_image']['name'];
		$tmp_dir = $_FILES['user_image']['tmp_name'];
		$imgSize = $_FILES['user_image']['size'];
		
		if(empty($fullname)){
			$errMSG = "Please Enter Full Name.";
		}
		else if(empty($user_name)){
			$errMSG = "Please Enter User Name.";
		}
		else if(empty($user_email)){
			$errMSG = "Please Enter Email.";
		}
		else if(empty($status)){
			$errMSG = "Please Select Active Or Deactive.";
		}
		
		{
			$upload_dir = 'images/admin/'; // upload directory
	
			$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
		
			// valid image extensions
			$valid_extensions = array('jpeg', 'jpg', 'png', 'gif' ,'docx' , 'pdf' , 'video' , 'mp3' ); // valid extensions
		
			// rename uploading image
			$userpic = rand(1,1000000).".".$imgExt;
				
			// allow valid image file formats
			if(in_array($imgExt, $valid_extensions)){			
				// investck file size '100MB'
				if($imgSize < 100000000)				{
					move_uploaded_file($tmp_dir,$upload_dir.$userpic);
				}
				else{
					$errMSG = "Sorry, your file is too large.";
				}
			}
			else{
				$errMSG = "Sorry, only JPG, JPEG, PNG & GIF , DOCX & PDF files are allowed.";		
			}
		}
		// if no error occured, continue ....
		

		if(!isset($errMSG))

		{

			$stmt = $DB_con->prepare('INSERT INTO mobileusers(image,fullname,phonenumber,address,user_name,user_email,user_pass,admintype,ucomission,status,branch_id,basicsalary,othourrate) VALUES(:upic,:fullname,:phonenumber,:address,:user_name,:user_email,:user_pass,:admintype,:ucomission,:status,:branch_id,:basicsalary,:othourrate)');

			$stmt->bindParam(':upic',$userpic);
			
			$stmt->bindParam(':fullname',$fullname);
			
			$stmt->bindParam(':phonenumber',$phonenumber);
			
			$stmt->bindParam(':address',$address);
			
			$stmt->bindParam(':user_name',$user_name);
			
			$stmt->bindParam(':user_email',$user_email);

			$stmt->bindParam(':user_pass',$user_pass);
			
			$stmt->bindParam(':admintype',$admintype);
			
			$stmt->bindParam(':ucomission',$ucomission);

			$stmt->bindParam(':status',$status);
			
			$stmt->bindParam(':branch_id',$branch_id);
			
			$stmt->bindParam(':basicsalary',$basicsalary);
			
			$stmt->bindParam(':othourrate',$othourrate);
			
			if($stmt->execute())

			{

			$LastID ='';
            $GetLastIdQuery=$conn->query("SELECT user_id FROM mobileusers ORDER BY user_id DESC LIMIT 1");
            if($GLIDrow=$GetLastIdQuery->fetch_array()) {
            	$LastID =$GLIDrow[0];


            	$sql = "INSERT INTO `tbl_user_branch`(`user_branch_id`, `branch_id`, `user_id`) VALUES (null,'$branch_id','$LastID')";

					if($conn->query($sql) === TRUE){
					  	$successMSG = "Successfully! Created New User Account....";

						header("refresh:2;userlists.php"); // redirects image view page after 5 seconds.
					}else{
					  	$errMSG = "error while inserting....";
					}

            }
       


			}else

			{

				$errMSG = "error while inserting....";

			}

		}

	}

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
<meta name="description" content="POS - Payroll | Pos & Invetory System | Dashboard

">
<meta name="keywords" content="Payroll | Pos & Invetory System | Dashboard

">
<meta name="author" content="Payroll | Pos & Invetory System | Dashboard

">
<meta name="robots" content="noindex, nofollow">

<title>Add Users | Payroll | Pos & Invetory System | Dashboard

</title>


<?php
require_once 'headercss.php';
?>
</head>
<body>
<div id="global-loader">
<div class="whirly-loader"> </div>
</div>

<div class="main-wrapper">
<?php
require_once 'header.php';
?>

<?php
require_once 'navbar.php';
?>

<div class="page-wrapper">
<div class="content">
<div class="page-header">
<div class="page-title">
<h4>User Management</h4>
<h6>Add User</h6>
</div>
</div>

<div class="card">
<div class="card-body">
<form method="POST" enctype="multipart/form-data">
<div class="row">
<?php

									if(isset($errMSG)){

									?>
									
									<div class="alert alert-danger border-0 bg-danger alert-dismissible fade show py-2">
									<div class="d-flex align-items-center">
										<div class="font-35 text-white"><i class="bx bxs-message-square-x"></i>
										</div>
										<div class="ms-3">
											<h6 class="mb-0 text-white">Error Alerts</h6>
											<div class="text-white"> <?php echo $errMSG; ?>!</div>
										</div>
									</div>
									<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
									</div>
									
									<?php

									}
	
									else if(isset($successMSG)){

									?>
									
									<div class="alert alert-success border-0 bg-success alert-dismissible fade show py-2">
									<div class="d-flex align-items-center">
										<div class="font-35 text-white"><i class="bx bxs-check-circle"></i>
										</div>
										<div class="ms-3">
											<h6 class="mb-0 text-white">Success Alerts</h6>
											<div class="text-white"><?php echo $successMSG; ?>!</div>
										</div>
									</div>
									<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
									</div>
									
									<?php

									}

									?>	
<div class="col-lg-4 col-sm-4 col-12">
<div class="form-group">
<label>Full Name</label>
<input type="text" class="form-control" name="fullname" placeholder="Enter Full Name" required>
</div>
</div>
<div class="col-lg-4 col-sm-4 col-12">
<div class="form-group">
<label>Phone Number</label>
<input type="text" class="form-control" name="phonenumber" placeholder="Enter Phone Number" required>
</div>
</div>
<div class="col-lg-4 col-sm-4 col-12">
<div class="form-group">
<label>Address</label>
<input type="text" class="form-control" name="address" placeholder="Enter Address" required>
</div>
</div>
<div class="col-lg-3 col-sm-3 col-12">
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" name="user_email" placeholder="Enter Email" required>
</div>
</div>
<div class="col-lg-3 col-sm-3 col-12">
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" name="user_name" placeholder="Enter Username" required>
</div>
</div>
<div class="col-lg-3 col-sm-3 col-12">
<div class="form-group">
<label>Password</label>
<div class="pass-group">
<input type="password" class="pass-input" name="user_pass" placeholder="Enter Password" value="" required>
<span class="fas toggle-password fa-eye-slash"></span>
</div>
</div>
</div>
<div class="col-lg-3 col-sm-3 col-12">
<div class="form-group">
<label>Role</label>
<select name="admintype" class="select" required>
	<option value="" selected>Select User Role</option>
	<?php $group = mysqli_query($conn, "SELECT * FROM user_groups"); 
	if ($group->num_rows > 0) {
		while($row = $group->fetch_assoc()) { ?>
			<option value="<?php echo $row['group_id']; ?>"><?php echo $row['group_name']; ?></option>
	<?php }
	}
	
	
	?>								
</select>
</div>
</div>
<div class="col-lg-3 col-sm-6 col-12">
	<div class="form-group">
	<label> Branch</label>
	<select class="select" name="branch_id" required>
		<option value="0">Main Branch</option>
		<?php
            $BranchListQuery=$conn->query("SELECT * FROM branch");
            while ($row=$BranchListQuery->fetch_array()) {
        ?>
            <option value="<?php echo $row[0];?>"><?php echo $row[1];?></option>
        <?php } ?>
	</select>
	</div>
</div>
<div class="col-lg-3 col-sm-3 col-12">
<div class="form-group">
<label for="comission">Comission(%)</label>
<input type="text" name="ucomission" class="form-control" placeholder="Enter Comission">
</div>
</div>
<div class="col-lg-3 col-sm-3 col-12">
<div class="form-group">
<label for="basicsalary">Basic Salary</label>
<input type="text" name="basicsalary" class="form-control" placeholder="Enter Basic Salary">
</div>
</div>
<div class="col-lg-3 col-sm-3 col-12">
<div class="form-group">
<label for="othourrate">OT Rate</label>
<input type="text" name="othourrate" class="form-control" placeholder="Enter OT Rate">
</div>
</div>
<div class="col-lg-3 col-sm-3 col-12">
<div class="form-group">
<label> Status</label>
<select class="select" name="status" required>
<option value="1">Active</option>
<option value="0">Deactive</option>
</select>
</div>
</div>
<div class="col-lg-3 col-sm-3 col-12">
<div class="form-group">
<label> Profile Picture</label>
<label for="fileName"><img src="assets/img/empty.png" id="yourImgTag" class="pro_pick" style="width:80px;height:80px;"></label>
<input type="file" name="user_image" id="fileName" hidden="yes" onChange="dis_name();">

<script>
function dis_name(){
var input = document.getElementById("fileName");
var fReader = new FileReader();
fReader.readAsDataURL(input.files[0]);
fReader.onloadend = function(event){
var img = document.getElementById("yourImgTag");
img.src = event.target.result;
}
}
</script>
</div>
</div>
<div class="col-lg-12">
<a href="userlists.php" class="btn btn-cancel">Cancel</a>
<button type="submit" name="save" class="btn btn-submit me-2">Save</button>
</div>
</div>
</form>
</div>
</div>

</div>
</div>
</div>


<script src="assets/js/jquery-3.6.0.min.js"></script>

<script src="assets/js/feather.min.js"></script>

<script src="assets/js/jquery.slimscroll.min.js"></script>

<script src="assets/js/jquery.dataTables.min.js"></script>
<script src="assets/js/dataTables.bootstrap4.min.js"></script>

<script src="assets/js/bootstrap.bundle.min.js"></script>

<script src="assets/plugins/select2/js/select2.min.js"></script>

<script src="assets/plugins/sweetalert/sweetalert2.all.min.js"></script>
<script src="assets/plugins/sweetalert/sweetalerts.min.js"></script>

<script src="assets/js/script.js"></script>
</body>
</html>