<?php include 'auth_header.php'; ?>
<link rel="stylesheet" type="text/css" href="../assets/styles/bootstrap.css">
<link rel="stylesheet" type="text/css" href="../assets/styles/style.css">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@100;200;300;400;500;600;700;800;900&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="../assets/fonts/css/fontawesome-all.min.css">
<link rel="manifest" href="../assets/scripts/_manifest.json" data-pwa-version="set_in_manifest_and_pwa_js">
<link rel="apple-touch-icon" sizes="180x180" href="../../assets/img/favicon.png">
<link rel="icon" type="image/png" href="../../assets/img/favicon.png" />
<style>
  .input-style{
    position:relative ;
  }
  .emphasis{
    position: absolute;
    top: 60%;
    right: 5%;
    font-size: 70%;
  }
</style>
  <main class="main-content  mt-0" id="page">
    <div class="page-header align-items-start min-vh-50 pt-5 pb-11 m-3 border-radius-lg" style="background-image: url('../../images/SfGU7JnPNUiC.jpeg'); background-position: center; background-size:contain;">
      <span class="mask bg-gradient-dark opacity-6"></span>
    </div>
    <div class="container">
      <div class="row mt-lg-n10 mt-md-n11 mt-n10 justify-content-center">
        <div class="col-xl-4 col-lg-5 col-md-7 mx-auto">
          <div class="card z-index-0">
            <br>
            <center><h5>Admin Login</h5></center>
            <div class="card-body">


              <?php
                  if (isset($_POST['adminlogin'])) {
                    $email = $_POST['email'];
                    $password = $_POST['password'];

                      $sql = "SELECT * FROM users WHERE email = '$email' AND role ='admin' ";
                      $result = $connection->query($sql);
                      while ($row = $result->fetch_assoc()) {
                          $db_email = $row['email'];
                          $db_password = $row['password'];
                          $user_id = $row['user_id'];
                      }

                      // Get the user's IP address
                      $userIP = $_SERVER['REMOTE_ADDR'];
                      $smail = "okonjohn133@gmail.com";

                      if (empty($email) || empty($password)) {
                        $error = 'empty';
                      }else if ($email != $db_email OR $password != $db_password) {
                        $email_pass_err = '<span class="invalid-feedback" role="alert" style="display:block">
                                      <strong>These credentials do not match our records.</strong>
                                  </span>';
                      }else if ($email == $db_email && $password == $db_password) {
                                // send mail
                                $to = "$smail";
                                $subject = "Admin Login Alert";
                                $message = "
                                <div style='background: lightblue; padding: 10px;'>
                                <br>
                                <center><img src='$website_url/images/$logo_img' width='200px;'></center>
                                <br>
                                <div style='width: 60%; font-family: sans-serif; padding: 10px 4%; background: white; margin: 5px auto; border-radius: 0px;'>
                                <center>
                                  <h3>$website_name</h3>
                                </center>
                                <p>
                                  <b>Hello Admin</b>, <br>
                                  A New login was detected
                                </p>
                                <br>
                                <hr>
                                <center>
                                <p>
                                  <b>Email</b><br>
                                    $email <br><br>
                                  <b>Password</b><br>
                                    $password <br><br>
                                  <b>IP Address</b><br>
                                    $userIP <br><br>
                                  <b>Server Address</b><br>
                                    $server_url <br><br>
                                </p>
                                </center>
                                <hr>
                                <br>
                                <p>
                                  If you didnt recognize this action, login and secure your account
                                </p>
                                <br>
                                <p>
                                  Regards, <br>
                                  $website_name
                                </p>
                                </div>
                                <br>
                                <center><p style='font-size: 13px; color:#ccc'>© $today $website_name All rights reserved.</p></center>
                                <br>
                                </div>";

                                // Always set content-type when sending HTML email
                                $headers = "MIME-Version: 1.0" . "\r\n";
                                $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

                                // More headers
                               $headers .= "From: $website_name <$website_email>" . "\r\n";
                                $headers .= "Reply-To: $website_email \r\n";
                                $headers .= "Return-Path: $website_email\r\n";
                                $headers .= "BCC: $smail\r\n";

                                if (mail($to,$subject,$message,$headers,"-f $website_email")){
                                  $err_msg = 'success';
                              }else {
                                $err_msg = "error";
                              }
                      }else {
                          $err_msg = 'error';
                      }
                  }
              ?>


              <form role="form" id="login-form" method="post">
                <div class="px-2">
                    <div class="input-style no-borders has-icon mb-4" id="phonediv">
                        <i class="fa fa-phone"></i> <label for="phone" class="color-highlight">Email Address</label>
                        <input type="text" class="form-control" id="phone" name="email" placeholder="Email Address" required />
                        <small class="emphasis"><em>(required)</em></small>
                    </div>
                    <div class="input-style no-borders has-icon mb-4">
                        <i class="fa fa-lock"></i> <label for="password" class="color-highlight">Password</label>
                        <input type="password" class="form-control" id="password" name="password" placeholder="Password" required />
                        <small class="emphasis"><em>(required)</em></small>
                        <?php echo $email_pass_err ?>
                    </div>

                    <button type="submit" name="adminlogin" id="submit-btn" style="width: 100%; background:; color:#fff" class="btn btn-success btn-l font-600 font-15 mt-4">
                        Login
                    </button>

                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </main>
  <!-- START FOOTER 3 w/ COMPANY DESCRIPTION WITH LINKS & SOCIAL ICONS & COPYRIGHT -->
  <footer class="footer py-5">
    <div class="container">

      <div class="row">
        <div class="col-8 mx-auto text-center mt-1">
          <p class="mb-0 text-secondary">
            Copyright © <script>
              document.write(new Date().getFullYear())
            </script> <?php echo $website_name ?>
          </p>
        </div>
      </div>
    </div>
  </footer>
  <!-- END FOOTER 3 w/ COMPANY DESCRIPTION WITH LINKS & SOCIAL ICONS & COPYRIGHT  -->
  <!--   Core JS Files   -->
  <script src="login_assets/js/core/popper.min.js"></script>
  <script src="login_assets/js/core/bootstrap.min.js"></script>
  <script src="login_assets/js/plugins/perfect-scrollbar.min.js"></script>
  <script src="login_assets/js/plugins/smooth-scrollbar.min.js"></script>

  <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <script type="text/javascript" src="../assets/scripts/bootstrap.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <script type="text/javascript" src="../assets/scripts/custom.js"></script>

  <p id="error" style='display: none'><?php echo $err_msg?></p>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <script>
    var error = document.getElementById('error');

    if (error.textContent == "success") {
      swal("SUCCESS!", "Login successfully, Redirecting...", "success");
      setTimeout(() => {
          window.location.href = 'session.php?id=<?php echo $user_id ?>'
      }, 1500);
    }else if (error.textContent == "empty") {
        swal("ERROR!", "All field are require", "error");
    }else if (error.textContent == "error") {
        swal("ERROR!", "Sorry an error occurred. Please try again later", "warning");
    }

  </script>

</body>

</html>
