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

if ($_SESSION['login'] != '') {
  $_SESSION['login'] = '';
}

if (isset($_POST['login'])) {
  $email = $_POST['emailid'];
  $password = $_POST['password']; // plaintext input

  // Fetch student by email only
  $sql = "SELECT EmailId, Password, StudentId, id, FullName, Status 
            FROM tblstudents 
            WHERE StudentId = :email 
            LIMIT 1";
  $query = $dbh->prepare($sql);
  $query->bindParam(':email', $email, PDO::PARAM_STR);
  $query->execute();
  $result = $query->fetch(PDO::FETCH_OBJ);

  if ($result) {
    // Verify password
    if (password_verify($password, $result->Password)) {

      if ($result->Status == 1) {
        $_SESSION['stdid'] = $result->StudentId;
        $_SESSION['login'] = $email;
        $_SESSION['primaryID'] = $result->id;
        $_SESSION['Full_Name'] = $result->FullName;

        // Last login
        $sqlLastLogin = "SELECT loginTime FROM tblstudentlogs WHERE StudentId = :studentId ORDER BY loginTime DESC LIMIT 1";
        $queryLastLogin = $dbh->prepare($sqlLastLogin);
        $queryLastLogin->bindParam(':studentId', $result->id, PDO::PARAM_INT);
        $queryLastLogin->execute();
        $lastLoginObj = $queryLastLogin->fetch(PDO::FETCH_OBJ);
        $_SESSION['last_login'] = $lastLoginObj ? $lastLoginObj->loginTime : 'First login';

        // Log login
        $logintime = date('Y-m-d H:i:s');
        $ip = $_SERVER['REMOTE_ADDR'];
        $sqlLog = "INSERT INTO tblstudentlogs (StudentId, loginTime, ipAddress) 
                           VALUES (:studentId, :logintime, :ip)";
        $queryLog = $dbh->prepare($sqlLog);
        $queryLog->bindParam(':studentId', $result->id, PDO::PARAM_INT);
        $queryLog->bindParam(':ip', $ip, PDO::PARAM_STR);
        $queryLog->bindParam(':logintime', $logintime);

        $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>document.location = 'dashboard.php';</script>";

      } elseif ($result->Status == 0) {
        echo "<script>alert('Your account has been blocked. Please contact admin');</script>";
      } else {
        echo "<script>alert('Your account is pending for approval. Please contact admin');</script>";
      }

    } else {
      echo "<script>alert('Invalid login details');</script>";
    }

  } else {
    echo "<script>alert('Invalid login details');</script>";
  }
}
?>


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

<head>
  <meta charset="UTF-8">
  <title>WisdomStack | Student 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: #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 #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;
      text-align: center;
    }

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

    .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: #ff5ca8;
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      margin: 8px 0 0 0;
      letter-spacing: 0.5px;
    }

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

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

    .form-side h3 {
      color: #e95ea1;
      font-weight: bold;
      margin-bottom: 25px;
    }

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

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

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

    /* Pulse animation */
    @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_img7.avif" alt="Library Image">
      </div>

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

        <form method="post" class="text-start">
          <div class="mb-3">
            <label for="emailid" class="form-label">Student ID</label>
            <input type="text" class="form-control" name="emailid" required autocomplete="off">
          </div>

          <div class="mb-3">
            <label for="password" class="form-label">Password</label>
            <input type="password" class="form-control" name="password" required autocomplete="off">
          </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>
</body>

</html>