<?php
session_start();
require("php_con.php");
require("check_user.php");

$success_msg=0;

if(isset($_POST['add_bt'])){
	
	$user_username=mysqli_real_escape_string($connation,$_POST['user_username']);
	$user_password=md5(mysqli_real_escape_string($connation,$_POST['user_password']));
	$user_fname=mysqli_real_escape_string($connation,$_POST['user_fname']);
	$user_lname=mysqli_real_escape_string($connation,$_POST['user_lname']); 
	$user_type=mysqli_real_escape_string($connation,$_POST['user_type']);
	
	if(mysqli_query($connation,"INSERT INTO
	user_details (user_id, user_username, user_password, user_fname, user_lname, user_type, user_reg_time)
	VALUES (NULL, '$user_username', '$user_password', '$user_fname', '$user_lname', '$user_type', '$current_date $current_time')")){
		$success_msg=1;
	}
	else{
		$success_msg=2;
	}
}

if(isset($_GET['user_id'])){	
	$user_id=mysqli_real_escape_string($connation,$_GET['user_id']);
	$view_qury=mysqli_query($connation,"SELECT * FROM user_details WHERE user_id='$user_id'");
	$view_resalt=mysqli_fetch_assoc($view_qury);
}

if(isset($_POST['update_bt'])){
	$user_id=mysqli_real_escape_string($connation,$_GET['user_id']);
	$user_fname=mysqli_real_escape_string($connation,$_POST['user_fname']);
	$user_lname=mysqli_real_escape_string($connation,$_POST['user_lname']);
	$user_type=mysqli_real_escape_string($connation,$_POST['user_type']);
	
	if(empty($_POST['user_password'])){$user_password=$view_resalt['user_password'];}else{$user_password=md5(mysqli_real_escape_string($connation,$_POST['user_password']));}
	
	if(mysqli_query($connation,"UPDATE user_details SET user_password='$user_password',user_fname='$user_fname',user_lname='$user_lname',user_type='$user_type' WHERE user_id='$user_id'")){
		echo "<script>window.location='edit-user?user_id=$user_id&updated';</script>";
	}
	else{
		$success_msg=4;
	}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title><?php echo $system_name; ?></title>
<link rel="stylesheet" href="main_css.css">
	
<script src="data_table/js/jquery-3.5.1.js"></script>
<script src="data_table/js/jquery.dataTables.min.js"></script>
<script src="data_table/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="data_table/css/bootstrap.css">
<link rel="stylesheet" href="data_table/css/dataTables.bootstrap4.min.css">
	
<link rel="stylesheet" href="alert/sweetalert2.min.css">
<script src="alert/sweetalert2.min.js"></script>
	
<link rel="stylesheet" href="icon/css/font-awesome.css">
<link rel="stylesheet" href="icon/css/font-awesome.min.css">
</head>
<style>
	table tr td{
		font-size: 14px;
		padding: 5px;
	}
</style>
<body>
<script>
window.addEventListener("load",function(){
	var load_screen=document.getElementById('load_screen');
	document.body.removeChild(load_screen);
});
</script>

<div id="load_screen"><div id="loading"><img src="image/loading.gif" width="80px;"></div></div>
	
<?php require("header.php"); ?>
	
<div class="contaner">
	
<div class="row">
	
<form method="post" class="input-form col-lg-6 col-sm-12 m-auto" autocomplete="off">
	
<div style="margin-bottom: 10px;"><a href="user" class="btn btn-dark btn-sm"><i class="fa fa-backward"></i> Back to User</a></div>
	
<table style="width: 100%;">
  <tbody>
	  
    
<tr>
	<td> User Type *<br>
		<select name="user_type" required="required" class="form-control form-control-sm" id="user_type">
			<option value="" hidden="yes">Select User Type</option>
		  <option <?php if(isset($_GET['user_id'])){if($view_resalt['user_type']==1){echo "selected";}} ?> value="1">Admin User</option>
		  <option <?php if(isset($_GET['user_id'])){if($view_resalt['user_type']==2){echo "selected";}} ?> value="2">Cashier User</option>
        </select>
		</td>
		<td>&nbsp;</td>
</tr>
  <tr>
    <td> First Name *<br>
      <input name="user_fname" type="text" required class="form-control form-control-sm" id="user_fname" value="<?php if(isset($_GET['user_id'])){echo $view_resalt['user_fname'];} ?>">
      </td>
    <td> Last Name *<br>
      <input name="user_lname" type="text" required="required" class="form-control form-control-sm" id="user_lname" value="<?php if(isset($_GET['user_id'])){echo $view_resalt['user_lname'];} ?>">
      </td>
  </tr>
    
  <tr>
    <td> User Name *<br>
      <input name="user_username" type="text" required class="form-control form-control-sm" id="user_username" value="<?php if(isset($_GET['user_id'])){echo $view_resalt['user_username'];} ?>" <?php if(isset($_GET['user_id'])){echo "disabled";} ?>></td>
    <td> Password *<br>
      <input name="user_password" type="password" class="form-control form-control-sm" id="user_password" <?php if(isset($_GET['user_id'])){}else{echo "required";} ?>></td>
  </tr>
  <tr>
    <td colspan="2" align="right">
      <button name="<?php if(isset($_GET['user_id'])){echo "update_bt";}else{echo "add_bt";} ?>" type="submit" class="btn btn-success btn-sm mt-2"><?php if(isset($_GET['user_id'])){echo "Update User";}else{echo "Add User";} ?></button>
      </td>
  </tr>
    
</tbody>
</table>
	
</form>
		
</div>
</div>	
	


<?php if($success_msg==2){ ?>
<script>
    swal({
	title: "Error",
       html: "<br> Your entered details something is wrong, Please try again.",
       type: "error",
	   titleColor: "text-primary",
       showCancelButton: false,
       confirmButtonText: 'OK',
       showLoaderOnConfirm:false,
       buttonsStyling: false,
       confirmButtonClass: 'btn btn-success',
       cancelButtonClass: '',    
     
    });
</script>
<?php } ?>

<?php if($success_msg==1){ ?>
<script>
    swal({
	title: "Congratulation",
       html: "<br> Your entered details has been saved successfully.",
       type: "success",
	   titleColor: "text-primary",
       showCancelButton: false,
       confirmButtonText: 'OK',
       showLoaderOnConfirm:false,
       buttonsStyling: false,
       confirmButtonClass: 'btn btn-success',
       cancelButtonClass: '',     
    });
</script>
<?php } ?>
	
<?php if($success_msg==4){ ?>
<script>
    swal({
	title: "Error",
       html: "<br> Your entered details something is wrong, Please try again.",
       type: "error",
	   titleColor: "text-primary",
       showCancelButton: false,
       confirmButtonText: 'OK',
       showLoaderOnConfirm:false,
       buttonsStyling: false,
       confirmButtonClass: 'btn btn-success',
       cancelButtonClass: '',    
     
    });
</script>
<?php } ?>

<?php if(isset($_GET['updated'])){ ?>
<script>
    swal({
	title: "Congratulation",
       html: "<br> Your customer details has been update successfully.",
       type: "success",
	   titleColor: "text-primary",
       showCancelButton: false,
       confirmButtonText: 'OK',
       showLoaderOnConfirm:false,
       buttonsStyling: false,
       confirmButtonClass: 'btn btn-success',
       cancelButtonClass: '',     
    });
</script>
<?php } ?>
	
</body>
</html>
<?php @mysqli_close($connation); ?>