<?php

include 'load.php';

if(isset($_GET['user_id'])){
	
	$user_id=mysqli_real_escape_string($conn,$_GET['user_id']);
	$view_qury=mysqli_query($conn,"SELECT * FROM mobileusers WHERE user_id='$user_id'");
	$view_result=mysqli_fetch_array($view_qury);
	
}

if(isset($_GET['user_id'])){
	
	$user_id=mysqli_real_escape_string($conn,$_GET['user_id']);
	$view_qury2=mysqli_query($conn,"SELECT * FROM tbl_user_branch WHERE user_id='$user_id'");
	$view_result2=mysqli_fetch_array($view_qury2);
	
}

else{
	
	echo "<script>window.location='userlists.php';</script>";
	
}


	if(isset($_GET['user_id']) && !empty($_GET['user_id']))

	{

		$id = $_GET['user_id'];

		$stmt_edit = $DB_con->prepare('SELECT * FROM mobileusers WHERE user_id =:user_id');

		$stmt_edit->execute(array(':user_id'=>$id));

		$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);

		extract($edit_row);

	}

	else

	{

	echo "<script type='text/javascript'>window.location.href = 'userlists.php';</script>";

	}	

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

	{

		$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($imgFile)

			{

			$upload_dir = 'images/admin/'; // upload directory	

			$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension

			$valid_extensions = array('jpeg', 'jpg', 'png', 'gif' ,'docx' , 'pdf' , 'video' , 'mp3'); // valid extensions

			$userpic = rand(1,1000000).".".$imgExt;

			if(in_array($imgExt, $valid_extensions))

			{			

				if($imgSize < 5000000)

				{

				unlink($upload_dir.$view_result['image']);

				 move_uploaded_file($tmp_dir,$upload_dir.$userpic);

			}
				
			else

			{

			$errMSG = "Sorry, your file is too large it investould be less then 5MB";

			}

			}

			else

			{

			$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";		

			}	

			}

			else

			{

			// if no image selected the old image remain as it is.

			$userpic = $view_result['image']; // old image from database

			}

		if(!isset($errMSG))

		{

			$stmt = $DB_con->prepare('UPDATE mobileusers

									     SET image=:upic,
										 
											 fullname=:fullname,
											 
											 phonenumber=:phonenumber,
											 
											 address=:address,
										 
											 user_name=:user_name,
											 
											 user_email=:user_email,
											 
											 user_pass=:user_pass,
											 
											 admintype=:admintype,
											 
											 ucomission=:ucomission,

											 status=:status,
											 
											 branch_id=:branch_id,
											 
											 basicsalary=:basicsalary,
											 
											 othourrate=:othourrate

								       WHERE user_id=:user_id');
									   
			$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);

			$stmt->bindParam(':user_id',$id);

			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 = "UPDATE tbl_user_branch SET branch_id='$branch_id' WHERE user_id='$LastID'";

					if($conn->query($sql) === TRUE){
					  	$successMSG = "New Users Account Successfully Updated ....";

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

            }

			}else{

				$errMSG = "Sorry Data Could Not Updated !";

			}

		

		}

		

						

	}

	

?>

<!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>Edit 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>Edit 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" value="<?php echo $view_result['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" value="<?php echo $view_result['phonenumber']; ?>" 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" value="<?php echo $view_result['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" value="<?php echo $view_result['user_email']; ?>" placeholder="Enter Email" required>
</div>
</div>
<div class="col-lg-3 col-sm-3 col-12">
<div class="form-group">
<label>User Name</label>
<input type="text" class="form-control" name="user_name" value="<?php echo $view_result['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" value="" placeholder="Enter Password">
<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" id="inputadmintype">
<?php $group = mysqli_query($conn, "SELECT * FROM user_groups"); 
	if ($group->num_rows > 0) {
		while($row = $group->fetch_assoc()) { 
			if($row['group_id']==$view_result['admintype']){ ?>
				<option value="<?php echo $row['group_id']; ?>" selected><?php echo $row['group_name']; ?></option>
			<?php }else{ ?>
				<option value="<?php echo $row['group_id']; ?>"><?php echo $row['group_name']; ?></option>
			<?php } ?>
			
	<?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="<?php

																					$id = $view_result2['branch_id'];

																					$query = $DB_con->prepare('SELECT branch_id FROM branch WHERE branch_id='.$id);

																					$query->execute();

																					$result = $query->fetch();

																					echo $result['branch_id'];

																					?>"><?php

														$id =  $view_result2['branch_id'];

														$query = $DB_con->prepare('SELECT branch_name FROM branch WHERE branch_id='.$id);

														$query->execute();

														$result = $query->fetch();

														echo $result['branch_name'];

														?></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="fullName">Comission(%)</label>
<input type="text" name="ucomission" class="form-control" placeholder="Enter Comission" value="<?php echo $view_result['ucomission']; ?>">
</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" value="<?php echo $view_result['basicsalary']; ?>">
</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" value="<?php echo $view_result['othourrate']; ?>">
</div>
</div>
<div class="col-lg-3 col-sm-3 col-12">
<div class="form-group">
<label>Status</label>
<select class="form-control" name="status" required>
<option value="1" <?php if($view_result['status']=="1"){echo "selected";} ?>>Active</option>
<option value="0" <?php if($view_result['status']=="0"){echo "selected";} ?>>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="images/admin/<?php echo $view_result['image']; ?>" 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="update" class="btn btn-submit me-2">Update</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>