<?php
require_once('config.php');


if (isset($_COOKIE['Google_SLKA'])) {
  goInside();
}


//
// if (!isset($_SESSION['login_attempts'])) {
//     $_SESSION['login_attempts'] = 0;
// }
//
// if ($_SESSION['login_attempts'] >= 5) {
//     echo 'Too many failed login attempts. Try again later.';
// }
//
// echo $_SESSION['login_attempts'];


if ($_SERVER["REQUEST_METHOD"] === "POST") {
  $user_mail = $_POST['email'];

  // Get Data From Database
  $get_user_data = showAllttdata("admin_user", "WHERE admin_email =  '$user_mail' and admin_status ='active' ");

  // Check if data was found
  if (count($get_user_data) > 0) {
    foreach ($get_user_data as $now_user) {
       // Verifiy password with database with this email
       if (password_verify($_POST['password'], $now_user['admin_password'])) {
          $user_admin_id = $now_user['admin_id'];
          $mahamudfutureit_key = md5(time());
          // if number and password both verifyed then check security key available or not
          if ($now_user['admin_security_key'] == '') {

            // if not set create securithy key
            $entry_sql = "UPDATE admin_user SET admin_security_key = '$mahamudfutureit_key' WHERE admin_id = '$user_admin_id'";
            $entry_doc = $conn->prepare($entry_sql);
            if (  $entry_doc->execute()) {
              setcookie("Google_SLKA", $mahamudfutureit_key, time() + 36000, "/");
              goInside();
            }

          }else {
            setcookie("Google_SLKA", $now_user['admin_security_key'], time() + 36000, "/");
            goInside();
          }

       } else {
         $_SESSION['login_attempts']++;
         $show_msg = array('type' => 'danger', 'content' => 'Wrong account information !' );
       }
    }
  } else {
    $_SESSION['login_attempts']++;
    $show_msg = array('type' => 'danger', 'content' => 'Wrong account information !' );
    // wrong email
  }

}

function goInside() {
  header("Location:index.php");
  exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">
  <title>Login <?php echo $cms_Data['TitleSeparetor'].$cms_Data['AppName'] ?></title>

  <!-- Favicons -->
  <link href="assets/img/logo.png" rel="icon">
  <link href="assets/img/logo.png" rel="apple-touch-icon"> 
  <meta name="robots" content="noindex, nofollow">
  <!-- Files  -->
  <link href="https://fonts.gstatic.com" rel="preconnect">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Nunito:300,300i,400,400i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">
  <link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
  <body>

    <main>
      <div class="container">

        <section class="section register min-vh-100 d-flex flex-column align-items-center justify-content-center py-4">
          <div class="container">
            <div class="row justify-content-center">
              <div class="col-lg-4 col-md-6 d-flex flex-column align-items-center justify-content-center">

                <div class="d-flex justify-content-center py-4">
                  <a href="https://mf1t.com/" class="logo d-flex align-items-center w-auto">
                    <img src="assets/img/logo.png" alt="">
                    <span class="d-none d-lg-block">mf1t.com</span>
                  </a>
                </div><!-- End Logo -->

                <div class="card mb-3">

                  <div class="card-body">

                    <div class="pt-4 pb-2">
                      <h5 class="card-title text-center pb-0 fs-4">Login to Your Account</h5>
                    </div>
                    <?php

                    if(isset($show_msg)){
                      ?>
                      <div class="alert mt-3 py-2 alert-<?php echo $show_msg['type'] ?>" role="alert">
                        <?php echo $show_msg['content'] ?>
                      </div>
                      <?php
                    }
                     ?>
                    <form class="row g-3" method="post">

                      <div class="col-12">
                        <label for="yourEmail" class="form-label">Email</label>
                        <input type="email" value="" name="email" class="form-control" id="yourEmail" required>
                      </div>

                      <div class="col-12">
                        <label for="yourPassword" class="form-label">Password</label>
                        <input type="password" value="" minlength="8" name="password" class="form-control" id="yourPassword" required>
                      </div>

                      <div class="col-12">
                        <button class="btn btn-primary w-100" type="submit" >Login</button>
                      </div>
                      <div class="col-12">
                        <p class="small mb-0">Don't have account? <a href="<?php echo $site_domain ?>">Back to Website</a></p>
                      </div>
                    </form>
                  </div>
                </div>
                <div class="credits">
                  Powered by <a href="https://mf1t.com/">mf1t.com</a>
                </div>
              </div>
            </div>
          </div>
        </section>
      </div>
    </main>
   <!-- Vendor JS Files -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
  <!-- Template Main JS File -->
  <script src="assets/js/main.js"></script>
</body>
</html>
