<?php
$tt_page_info = array(
  'Page_Info' =>   array(
    'Title' => 'Login',
    'Description' => '',
  ),
  'Social_og' => array(
    'Image' => '',
    'Type' => 'website',
    'URL' => 'login/',
  ),
);


$error = false;

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

    $mahamudfutureit_key = md5(randomString().time());
    // Initialize an array to store validation errors

    // Check if CSRF token is valid
    $csrf_token = $_POST['csrf_token'] ?? '';
    if (!verifyCSRFToken($csrf_token)) {
      $error =  true;
    }else{

      // Get Mobile and email from user
      $acc_mobile = $_POST['mobile'];
      $acc_pwd = $_POST['password'];

      // all field have data then check in database
      if ($acc_mobile !='' && $acc_pwd !='') {

        $user_data = getUser($acc_mobile, 'UserMobile');
        if ($user_data) {
          $user_acc_id = $user_data['UserID'];
          $user_acc_status = $user_data['AccountStatus'];
          $user_acc_pwd = $user_data['UserPassword'];
          $user_acc_lgkey = $user_data['UserLoginKey'];

          if (password_verify($acc_pwd, $user_data['UserPassword']) && $user_acc_status == 'active') {

            userActivity($user_acc_id, "Security", "Login Successful", "IP Address :".$userIP);

            // if access key empty then create and store a access key
            if ($user_acc_lgkey != '') {
              $user_access_key = $user_acc_lgkey;
            }else {
              $user_access_key = $mahamudfutureit_key;
              UpdateData('tt_accounts', 'UserID', $user_acc_id, array("UserLoginKey" => $user_access_key));
            }

            //  Now Login
            setcookie("lsp_cpa", $user_access_key, time() + (86400 * 7), "/");
            header("Location:".$tt_site_domain."account/");
            exit;

          }else {
            $error =  true;
          }

        } else {
            $error =  true;
        }

      }else {
        $error =  true;
      }

      // Regenerate the token to avoid reuse
      unset($_SESSION['csrf_token']);
    }
}

require_once('./header.php'); // Include Footer
?>
<section id="join_mahamudfutureit" class="pt-4 pb-4">
    <div class="container">
        <div class="row">
            <section class="col-md-6 align-self-center">
                <div class="w300 text-center">
                    <img width="230" src="<?php echo $advancenews['Website_Logo'] ?>" alt="<?php echo $advancenews['Website_title'] ?>">
                    <h6 class="mt-3"><?php echo $advancenews['Login_stitle'] ?></h6><h6> <?php echo $advancenews['Website_Description'] ?> </h6>
                </div>
            </section>
            <section class="col-md-5 align-self-center">
                <section class="login-page pt-5 pb-5">
                    <div class="attraiit-card card p-4 p-md-4 ml-auto mr-auto border-0">
                      <h4 class="text-center">লগইন</h4>
                      <p class="text-center fw-bold"><?php echo $advancenews['Login_stitle'] ?></p>
                      <?php
                      if ($advancenews['Writer_login_status'] != 'active') {
                        ?>
                        <div class="alert alert-warning text-center">
                          নিবন্ধন প্রক্রিয়া সাময়িক বন্ধ রয়েছে !
                        </div>
                        <?php
                      }else {
                        ?>
                        <form action="" method="post">
                           <?php
                           if ($error == true) {
                             ?>
                             <div class="col-12 mt-4">
                               <div class="border-0 rounded-0 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="form-floating mb-3">
                                <input type="text" type="text"  inputmode="numeric"  pattern="^0[1][3-9][0-9]{8}$"  name="mobile" class="form-control  rounded-0  " id="u-name"
                                    placeholder=" মোবাইল নাম্বার">
                                <label for="u-name"> মোবাইল নাম্বার</label>
                            </div>
                            <div class="form-floating mb-3">
                                <input class="form-control rounded-0 "  autocomplete="off" name="password" type="password" id="passwordInput" placeholder="********" minlength="8" maxlength="32" required>
                                <label for="password">পাসওয়ার্ড</label>
                                <span class="pwd_icon" id="togglePassword" style="cursor: pointer;">
                                    <i class="bi bi-eye"></i>
                                </span>
                            </div>
                            <div class="d-grid mt-3">
                                <button class="btn btn-success" name="login" type="submit">লগইন</button>
                            </div>
                            <?php echo csrfField() ?>
                            <p class="text-center mt-3">অ্যাকাউন্ট না থাকলে নতুন অ্যাকাউন্ট <u><a href="<?php echo $site_domain;?>register/">নিবন্ধন করুণ</a></u>
                            <a href="<?php echo $site_domain;?>turms-of-use/" class="text-center d-block mt-3"><b> Terms & Conditions</b></a>
                        </form>
                        <?php
                      }
                      ?>

                    </div>
                </section>
            </section>
        </div>
    </div>
</section>
<script>
    const passwordInput = document.getElementById('passwordInput');
    const togglePassword = document.getElementById('togglePassword');
    togglePassword.addEventListener('click', function () {
        const isPassword = passwordInput.type === 'password';
        passwordInput.type = isPassword ? 'text' : 'password';
        const icon = togglePassword.querySelector('i');
        icon.classList.toggle('bi-eye');
        icon.classList.toggle('bi-eye-slash');
    });
</script>
<?php
require_once('./footer.php'); // Include Footer
?>
