<?php
require_once('config.php');
require_once('inc/header.php');
$mahamudfutureit_key = md5(time());
if (isset($_POST['newpassword']) && isset($_POST['oldpassword'])) {
  if (password_verify($_POST['oldpassword'], $the_user['admin_password'])) {

    $data_for_update = array(
      "admin_password"=> password_hash($_POST['newpassword'], PASSWORD_BCRYPT),
    );
    if (UpdateData('admin_user', 'admin_id', $the_user['admin_id'], $data_for_update)) {
      $tt_alert = ttAlert('success', 'Password Changed Successfully !');
    }else {
      $tt_alert = ttAlert('danger', 'Something went wrong to change password !');
    }

  }else {
    $tt_alert = ttAlert('danger', 'Wrong old password !');
  }
}
?>
<main id="main" class="main ">
  <div class="row">
    <div class="col-6 align-items-center">
      <div class="pagetitle mb-2">
        <h1><?php echo $page_title ?></h1>
      </div>
    </div>
  </div>
  <div class="container">
     <?php echo $tt_alert ?? ''; ?>
      <section class="section">
          <div class="row justify-content-center">
            <div class="col-md-5">
              <form class="" action="" method="post" id="passwordForm">
                 <div class="p-3 border rounded-3 bg-white">
                   <label class="form-label" for="OldPassword">Old Password</label>
                   <input class="form-control" id="OldPassword" type="password" name="oldpassword">
                   <label class="form-label mt-3" for="newPassword">New Password</label>
                   <div class="input-group mb-3">
                     <input type="password" class="form-control" id="newPassword" name="newpassword" minlength="8" maxlength="16"  required>
                     <a href="#" class="input-group-text" id="ShowPasswordTT"><i class="bi bi-eye" id="toggleIcon"></i></a>
                   </div>
                   <button type="submit" class="w-100 btn btn-success" name="changePassword">Change Password</button>
                 </div>
               </form>

               <script>
                 document.getElementById('ShowPasswordTT').addEventListener('click', function (event) {
                   event.preventDefault();
                   const passwordInput = document.getElementById('newPassword');
                   const toggleIcon = document.getElementById('toggleIcon');

                   if (passwordInput.type === 'password') {
                     passwordInput.type = 'text';
                     toggleIcon.classList.remove('bi-eye');
                     toggleIcon.classList.add('bi-eye-slash');
                   } else {
                     passwordInput.type = 'password';
                     toggleIcon.classList.remove('bi-eye-slash');
                     toggleIcon.classList.add('bi-eye');
                   }
                 }); 
               </script>
            </div>
          </div>
      </section>
    </div>
</main>
<?php require_once('inc/footer.php'); ?>
