<?php
$pageName  = "Pincode";
include($_SERVER['DOCUMENT_ROOT'] . "/auth/header.php");


if (@!$_SESSION['login']) {
  header("Location:./login.php");
}
if (@$_SESSION['acct_email']) {
  header("Location:./account/dashboard.php");
}

$viesConn = "SELECT * FROM users WHERE acct_email =:acct_email";
$stmt = $conn->prepare($viesConn);
$stmt->execute([
  'acct_email' => $_SESSION['login']
]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);

$acct_email = $row['acct_email'];

$user_email = $row['acct_email'];


if (isset($_POST['pincode_submit'])) {
  $pincodeVerified = $_POST['input'];
  $old_otp = $row['acct_otp'];

  if ($pincodeVerified !== $old_otp) {
    toast_alert('error', 'Invalid OTP');
    // notify_alert('Invalid Account Pincode','danger','2000','Close');
  }
  if (empty($pincodeVerified)) {
    toast_alert('error', 'OTP code required');
    // notify_alert('Please Enter Your Pincode','danger','2000','Close');

  }
  if ($pincodeVerified == $old_otp) {
      
      
                    //       $full_name = $user['firstname'];
                    // $APP_NAME = $page['url_name'];
                    // $APP_URL = WEB_URL;
                    // $SITE_ADDRESS = $page['url_address'];
                    // $APP_NUMBER = $page['url_tel'];
                    // $APP_EMAIL = $page['url_email'];
                    // $user_email = $user['acct_email'];
                    // $message = $sendMail->LoginMsg($full_name, $user_acctno, $APP_NAME, $APP_NUMBER, $APP_EMAIL, $APP_URL, $SITE_ADDRESS);
                    // // User Email
                    // $subject = "Login Notification" . "-" . $APP_NAME;
                    // $email_message->send_mail($user_email, $message, $subject);
                    
                        //IP LOGIN DETAILS
            
            $device = $_SERVER['HTTP_USER_AGENT'];
            $ipAddress = $_SERVER['REMOTE_ADDR'];
            $nowDate = date('Y-m-d H:i:s');
             
            $stmt221 = $conn->prepare("INSERT INTO audit_logs (user_email,device,ipAddress,datenow) VALUES(:user_email,:device,:ipAddress,:datenow)");
            $stmt221->execute([
                'user_email' => $user_email,
                'device' => $device,
                'ipAddress' => $ipAddress,
                'datenow' => $nowDate
                ]);
                    
    //session_start();
    $_SESSION['acct_email'] = $acct_email;
    $_COOKIE['firstVisit'] = $acct_email;
    header("Location:./account/dashboard.php");
  }
}
?>








<div class="container-fluid h-100 awrp anuphan">





  <form method="POST" class="bg-sm-none px-lg-10 mt-5 p-0 mx-auto shadow-none anuphan post-form mx-auto border-none">
    <h3 class="anuphan-3 mt-4">
      Security verification
    </h3>
    <label class="xsmall">Email Verification Code</label>
    <div class="form-wrapper m-0">
      <input type="text" class="anuphan-2 has-icon" autocomplete="off" name="input" placeholder="••••••" maxlength="6">
      <span class="input-icon-wrapper n-top-1">
        <span class="input-icon anuphan-2 pointer xsmall primary-color gc-txt">6 digits</span>
      </span>
    </div>
    <small class="grey-color-3 lc-err-msg mt-2" style="display: none;"></small>
    <div class="mt-2 d-flex">
      <i class="fa fa-square-o black-text top-3" onclick="trustDevice(this)"></i>
      <p class="xsmall black-text m-0 ml-2">Trust this device for 14 days</p>
    </div>
    <button type="submit" class="mt-4 anuphan-2" name="pincode_submit">Submit</button>

    <div class="mt-4 row">
      <div class="col-md-8">
        <p class="xsmall2 black-text">
          Not <?= $row['acct_email'] ?>?
          <a href="./login.php" class="primary-color xsmall2">
            Try another email
          </a>
        </p>
      </div>
    </div>
  </form>

  <!-- - End Modal -->
</div>


<?php

include_once($_SERVER['DOCUMENT_ROOT'] . "/auth/footer.php");

?>