<?php
$tt_page_info = array(
  'Page_Info' =>   array(
    'Title' => 'পাসওয়ার্ড পরিবর্তন করুন',
    'Description' => '',
    'Keyword' => '',
  ),
  'Social_og' => array(
    'Image' => '',
    'Type' => 'website',
    'URL' => 'account/security/',
  ),
);
$error = false;

// Update Information
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // Check if CSRF token is valid
    $csrf_token = $_POST['csrf_token'] ?? '';

    if (!verifyCSRFToken($csrf_token)) {
      $error = true;
    } else {

      // Check Pwd Length
      if (strlen($_POST['current_password']) >= 8 && strlen($_POST['current_password']) <= 32 &&
          strlen($_POST['new_password']) >= 8 && strlen($_POST['new_password']) <= 32) {

          // check curent Password Currect or not
          if (password_verify($_POST['current_password'], $the_writer['UserPassword'])) {

            $mahamudfutureit_key = md5(randomString().time());

            // Curent Password Okey , save new password
            $update_pwd_user = UpdateData('tt_accounts', 'UserID', $the_writer['UserID'], array(
              "UserLoginKey" =>$mahamudfutureit_key,
              "UserPassword" => password_hash($_POST['new_password'], PASSWORD_BCRYPT),
              "UserPwdTemp" => NULL,
            ));

            if ($update_pwd_user) {
              //  Now Login
              setcookie("lsp_cpa", $mahamudfutureit_key, time() + 36000, "/");
              header("Location:".$tt_site_domain."account/security/");
              exit;
            }

          }else {
            $error = true;
          }

      } else {
          $error = true;
      }
    }
}

require_once('./header.php'); // Include Footer
?>
<section id="account_mahamudfutureit" class="sectionbg pt-4 pb-5">
      <div class="container">
          <div class="row mt-4 justify-content-center">
            <div class="col-md-8">
              <div class="acc-info-head round-15 mahamudfutureit-shadow border-0 card p-3 p-md-4">
                  <h1 class="m-0 fs-4"><img height="35px" src="<?php echo $site_domain ?>assets/image/icons/lock.png"> <?php echo $tt_page_info['Page_Info']['Title'] ?></h1>
                  <hr>
                  <nav style="--s-breadcrumb-divider: '/';" aria-label="breadcrumb">
                      <ol class="breadcrumb m-0">
                        <li class="breadcrumb-item"><a href="<?php echo $site_domain ?>account/">অ্যাকাউন্ট</a></li>
                        <li class="breadcrumb-item"><a href="#"> <?php echo $tt_page_info['Page_Info']['Title'] ?></a></li>
                      </ol>
                  </nav>
              </div>

              <?php
              if ($error == true) {
                ?>
                <div class="col-12 mt-4">
                  <div class="border-0 rounded-4 alert alert-danger alert-dismissible fade show" role="alert">
                    পাসওয়ার্ড সঠিক নয় ! আবার চেষ্টা করুন
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                  </div>
                </div>
                <?php
              }
              ?>
              <div class="round-15 mahamudfutureit-shadow border-0 card mt-3 p-3 p-md-4">
                  <h5>পাসওয়ার্ড ও লগইন</h5>
                  <form action="" method="post" class="needs-validation" novalidate>
                    <div class="form-floating mb-3">
                        <input class="form-control rounded-0" autocomplete="off" name="current_password" type="password" id="currentPassword" placeholder="********" minlength="8" maxlength="32" required>
                        <label for="currentPassword">বর্তমান পাসওয়ার্ড</label>
                        <span class="pwd_icon toggle-password" style="cursor: pointer;">
                            <i class="bi bi-eye"></i>
                        </span>
                        <div class="invalid-feedback">
                            বর্তমান পাসওয়ার্ড সঠিক ভাবে লিখুন!
                        </div>
                    </div>

                    <div class="form-floating mb-3">
                        <input class="form-control rounded-0" autocomplete="off" name="new_password" type="password" id="newPassword" placeholder="********" minlength="8" maxlength="32" required>
                        <label for="newPassword">নতুন পাসওয়ার্ড</label>
                        <span class="pwd_icon toggle-password" style="cursor: pointer;">
                            <i class="bi bi-eye"></i>
                        </span>
                        <div class="invalid-feedback">
                            নতুন পাসওয়ার্ড সঠিক ভাবে লিখুন!
                        </div>
                    </div>
                    <div class="alert text-center alert-warning mt-4" role="alert">
                        পাসওয়ার্ড ভুলে গেলে নতুন পাসওয়ার্ড এর জন্য কতৃপক্ষের সাথে যোগাযোগ করুণ </a> ।
                    </div>
                      <div class="text-end">
                        <button name="changepwd" type="submit" href="#" class="  btn btn-primary rounded-pill">পাসওয়ার্ড পরিবর্তন করুণ</button>
                      </div>
                        <?php echo csrfField() ?>
                  </form>
              </div>
            </div>
          </div>
      </div>
  </section>
  <script>
  document.querySelectorAll('.toggle-password').forEach(function (togglePassword) {
      togglePassword.addEventListener('click', function () {
          const passwordInput = this.previousElementSibling.previousElementSibling; // Select the corresponding input
          const isPassword = passwordInput.type === 'password';
          passwordInput.type = isPassword ? 'text' : 'password';

          // Toggle the icon class
          const icon = this.querySelector('i');
          icon.classList.toggle('bi-eye');
          icon.classList.toggle('bi-eye-slash');
      });
  });
  </script>
<?php
require_once('./footer.php'); // Include Footer
?>
