<?php
include('includes/header.php');
include('includes/navbar.php');
?>


<?php
if (isset($_POST['update_profile'])) {
    $full_name = $_POST['full_name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $username = $_POST['username'];

    if (empty($full_name) || empty($email) || empty($password) || empty($username)) {
      $error = "empty";
    }else {
      $sql_userupdate = "UPDATE users SET full_name  = '$full_name', email = '$email', password = '$password', username = '$username' WHERE id = 1";
      if ($connection->query($sql_userupdate)===TRUE) {
          $error = "update_success";
          }else {
            $error = "error";
          }
    }
}
 ?>

<div class="container-fluid">

    <!-- DataTales Example -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Edit Admin Profile </h6>
        </div>
        <div class="card-body">
          <form action="" method="POST">
              <div class="form-group">
                  <label> Full Name </label>
                  <input type="text" name="full_name" value="<?php echo $full_name ?>" class="form-control">
              </div>

              <div class="form-group">
                  <label> Email</label>
                  <input type="email" name="email" value="<?php echo $email ?>" class="form-control">
              </div>
              <div class="form-group">
                  <label>Admin Password</label>
                  <input type="password" name="password" value="<?php echo $password ?>" class="form-control">
              </div>
              <div class="form-group">
                  <label>Username</label>
                  <input type="text" name="username" value="<?php echo $username ?>" class="form-control">
              </div>

              <button type="submit" name="update_profile" class="btn btn-success"> Update Profile </button>
          </form>
        </div>
    </div>
</div>

</div>

<p id="error" style='display: none'><?php echo $error?></p>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script>
    var error = document.getElementById('error');

    if (error.textContent == 'empty') {
         swal("ERROR!", "All fields are required!", "warning");
    }else if (error.textContent == "update_success") {
        swal("SUCCESS!", "Profile Updated Successfully", "success");
        setTimeout(() => {
            window.location.href = 'admin_profile.php'
        }, 2000);
    }else if (error.textContent == "error") {
        swal("ERROR!", "Sorry an error occurred. Please try again later", "warning");
    }

</script>
<?php
include('includes/scripts.php');
include('includes/footer.php');
?>
