<?php
session_start();
date_default_timezone_set('Asia/Kolkata');
include('includes/config.php');

if (isset($_POST['login'])) {
  $username = $_POST['username'];
  $password = $_POST['password'];

  $sql = "SELECT UserName, FullName, Password, id FROM admin WHERE UserName=:username LIMIT 1";
  $query = $dbh->prepare($sql);
  $query->bindParam(':username', $username, PDO::PARAM_STR);
  $query->execute();
  $result = $query->fetch(PDO::FETCH_OBJ);

  if ($result) {
    if (password_verify($password, $result->Password)) {
      $_SESSION['alogin'] = $result->UserName;
      $_SESSION['adminID'] = $result->id;
      $_SESSION['Full_Name'] = $result->FullName;

      if ($_SESSION['alogin'] != "adminbluebase") {

        $sqlLastLogin = "SELECT loginTime FROM tbladminlogs WHERE adminId = :adminId AND applogin=0 ORDER BY loginTime DESC LIMIT 1";
        $queryLastLogin = $dbh->prepare($sqlLastLogin);
        $queryLastLogin->bindParam(':adminId', $result->id, PDO::PARAM_INT);
        $queryLastLogin->execute();
        $lastLoginObj = $queryLastLogin->fetch(PDO::FETCH_OBJ);

        $_SESSION['last_login'] = $lastLoginObj ? $lastLoginObj->loginTime : 'First login';


        // Insert login log
        $ip = $_SERVER['REMOTE_ADDR'];
        $loginTime = date('Y-m-d H:i:s'); // Current time in Asia/Kolkata
        $sqlLog = "INSERT INTO tbladminlogs (AdminId, loginTime, ipAddress) 
           VALUES (:adminId, :loginTime, :ip)";
        $queryLog = $dbh->prepare($sqlLog);
        $queryLog->bindParam(':adminId', $result->id, PDO::PARAM_INT);
        $queryLog->bindParam(':loginTime', $loginTime, PDO::PARAM_STR);
        $queryLog->bindParam(':ip', $ip, PDO::PARAM_STR);
        $queryLog->execute();

      }

      // Get theme logo
      $themequery = "SELECT logo, name, color FROM theme WHERE id = 1";
      $theme = $dbh->prepare($themequery);
      $theme->execute();
      $themelogo = $theme->fetch(PDO::FETCH_OBJ);

      $_SESSION['logo'] = $themelogo->logo;
      $_SESSION['theme_name'] = $themelogo->name;
      $_SESSION['theme_color'] = $themelogo->color;

      echo "<script type='text/javascript'> document.location ='dashboard.php'; </script>";
    } else {
      echo "<script>alert('Invalid password');</script>";
    }
  } else {
    echo "<script>alert('Invalid username');</script>";
  }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>WisdomStack Admin Login</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="../assets/img/wisdomstack_logo.jpeg">

  <!-- Bootstrap 5 -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <!-- Font Awesome -->
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">

  <style>
    body {
      /* background: #ffffff; */
      background: #fffcffff;
      font-family: 'Segoe UI', sans-serif;
    }

    .login-wrapper {
      min-height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 20px;
    }

    .login-box {
      background: #fff;
      border-radius: 15px;
      /* box-shadow: 0 8px 20px rgba(0,0,0,0.1); */
      box-shadow: 0 8px 20px #b63f78ff;
      overflow: hidden;
      width: 100%;
      max-width: 700px;
      display: flex;
      flex-direction: row;
    }

    .promo-side {
      flex: 1;
      overflow: hidden;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .promo-side img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      display: block;
    }

    .form-side {
      padding: 40px 30px;
      flex: 1;
    }

    .form-side h3 {
      font-weight: bold;
      /* color: #0d6efd; */
      color: #e95ea1;
      margin-bottom: 25px;
      text-align: center;
    }

    .btn-login {
      width: 100%;
      border-radius: 25px;
    }

    @media (max-width: 768px) {
      .login-box {
        flex-direction: column;
        max-width: 90%;
      }

      .promo-side {
        padding: 20px;
      }

      .form-side {
        padding: 25px;
      }
    }

    /* Branding Section */
    .brand-section {
      display: flex;
      flex-direction: column;
      align-items: center;
    }

    .brand-logo {
      width: 90px;
      height: 90px;
      object-fit: contain;
      border-radius: 50%;
      box-shadow: 0 0 10px rgba(230, 74, 138, 0.4);
      animation: pulse 2s infinite ease-in-out;
    }

    .brand-title {
      font-size: 26px;
      font-weight: 700;
      background: linear-gradient(90deg, #b63f78, #ff5ca8);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      letter-spacing: 0.5px;
      margin: 8px 0 0 0;
    }

    .brand-title span {
      font-weight: 600;
      color: #b63f78;
    }

    .brand-subtitle {
      font-size: 15px;
      color: #555;
      letter-spacing: 1px;
    }

    /* Gentle logo glow */
    @keyframes pulse {
      0% {
        box-shadow: 0 0 10px rgba(230, 74, 138, 0.4);
      }

      50% {
        box-shadow: 0 0 20px rgba(230, 74, 138, 0.7);
      }

      100% {
        box-shadow: 0 0 10px rgba(230, 74, 138, 0.4);
      }
    }
  </style>
</head>

<body>
  <div class="container-fluid login-wrapper">
    <div class="login-box">
      <!-- Left Side Image -->
      <div class="promo-side p-0">
        <img src="../assets/img/lms_img5.jpg" alt="Admin Panel">
      </div>

      <!-- Right Side Login Form -->
      <div class="form-side text-center">
        <!-- Branding Section -->
        <div class="brand-section mb-4">
          <img src="../assets/img/wisdomstack_logo.jpeg" alt="WisdomStack Logo" class="brand-logo mb-2">
          <h2 class="brand-title">Wisdom<span>Stack</span></h2>
          <p class="brand-subtitle">Admin Login</p>
        </div>

        <!-- Login Form -->
        <form method="post" class="text-start">
          <div class="mb-3">
            <label for="username" class="form-label">Username</label>
            <input type="text" class="form-control" name="username" required autocomplete="off">
          </div>

          <div class="mb-3 position-relative">
            <label for="password" class="form-label">Password</label>

            <div class="input-group">
              <input type="password" class="form-control" id="password" name="password" required autocomplete="off">

              <span class="input-group-text" id="togglePassword" style="cursor:pointer;">
                <i class="fa-solid fa-eye-slash"></i>
              </span>
            </div>
          </div>


          <button type="submit" name="login" class="btn btn-primary btn-login mt-3">
            Login <i class="fas fa-sign-in-alt ms-2"></i>
          </button>
        </form>
      </div>

    </div>
  </div>

  <!-- Bootstrap JS -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  <script>
    document.getElementById("togglePassword").addEventListener("click", function () {
      const passwordField = document.getElementById("password");
      const icon = this.querySelector("i");

      if (passwordField.type === "password") {
        passwordField.type = "text";
        icon.classList.remove("fa-eye-slash");
        icon.classList.add("fa-eye");
      } else {
        passwordField.type = "password";
        icon.classList.remove("fa-eye");
        icon.classList.add("fa-eye-slash");
      }
    });
  </script>

</body>

</html>