
<?php include('../conn.php');
if (isset($_GET['id'])) {
$id=mysqli_real_escape_string($conn, $_GET['id']);
$id2=base64_decode($id); }
?>

<!DOCTYPE html>
<html lang="en">
<?php include("../common/head.php")?>
<style>

 
    label{
        font-size:20px;

    }
    .edit-body{
        margin:100px;
    }

    input,select{
        background-color:rgba(0, 0, 0, 0) !important;
}
</style>
<body>
                 <!-- Modal -->
                 <?php 
$sql = "SELECT * FROM users WHERE id='$id2' ";
$result=mysqli_query($conn,$sql);
    $row=mysqli_fetch_array($result);
?>
        <div class="edit-header">
          <h3 class="edit-tittle text-center " id="exampleModalLabel">Edit User</h3>
        </div>
        <div class="edit-body ">
            <form method="post" class=" ">


              <div class="row no-gutters">
              <div class="form-group col-6">
                  <label for="exampleFormControlInput1">User FirstName</label>
                  <input type="text" class="form-control" name="fname" id="exampleFormControlInput1" value="<?php echo $row['fname']?>">
                </div>
                <div class="form-group col-6">
                  <label for="exampleFormControlInput1">User LastName</label>
                  <input type="text" class="form-control" name="sname" id="exampleFormControlInput1" value="<?php echo $row['sname']?>">
                </div>
                <div class="form-group col-6 ">
                  <label for="exampleFormControlInput1">Phone Number</label>
                  <input type="number" class="form-control" name="number" id="exampleFormControlInput1" placeholder=" User Phone number"   pattern="^\d{10}$" title="Phone number must be upto 10 values" value="<?php echo $row['number']?>">
                </div>
                <div class="form-group col-6">
                  <label for="exampleFormControlInput1">Email Address</label>
                  <input type="email" class="form-control" name="email" id="exampleFormControlInput1" placeholder=" Email" value="<?php echo $row['email']?>">
                </div>

                <div class="form-group col-6">
                  <label for="exampleFormControlSelect1">Select Role</label>
                  <select class="form-control" name="role" id="exampleFormControlSelect1">
                  <option value="<?php echo $row['role']?>"><?php echo $row['role']?></option>
                  <option value="Admin">Admin</option>
                    <option value="cashier">cashier</option>
                    <option value="Manager">Manager</option>
                    <option value="Cheif">Cheif</option>
                    <option value="Pole Attendance">Pole Attendance</option>
                  </select>
                </div>
              </div>
             

                <input type="hidden" name="date" value="<?php echo date("F j, Y") ?>">
                
                  <button class="btn btn-primary "  name="submit">Submit</button>
              </form>
        </div>
        <div class="modal-footer">
        </div>

        <?php 





if(isset($_POST['submit'])){
 
    $fname=addslashes($_POST['fname']);
     $sname=addslashes($_POST['sname']);
     $email=$_POST['email'];
     $number=$_POST['number'];
    $password=$_POST['password'];
     $role=$_POST['role'];
     $date=$_POST['date'];


     $sql = "SELECT `email`, `fname` FROM `users` WHERE `email`='".$email."' AND  `fname`='".$fname."'";
     $result = $conn->query($sql);
     
     if($result->num_rows >= 1) {
      // Image db insert sql
		$sql = "UPDATE `users` SET `fname`='$fname',`sname`='$sname',`number`='$number',`email`='$email',`role`='$role' WHERE id='$id2'";
		if(mysqli_query($conn,$sql)){

      echo"<script>alert('User edited succefully'); window.location.href='../user'</script>";
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}
     } else {
		

      echo"<script>alert('User email allready Exist'); window.location.href='../user'</script>";
	
 
     }

}

?>

    </body>
</html>