<?php
require_once '../core/conn.php';
session_start();
date_default_timezone_set('Africa/Lagos');

$username = "";
$alert = "";

// If login form submitted
if (isset($_POST['submit'])) {
    $username = trim((string)$_POST['username']);
    $password = trim((string)$_POST['password']);

    // Secure query using prepared statement
    $stmt = $con->prepare("SELECT id, username, password, status FROM admin WHERE username = ? LIMIT 1");
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $result = $stmt->get_result();
    $user = $result->fetch_assoc();

    if ($user) {
        // Plain text comparison (since DB is not hashed)
        if ($password === (string)$user['password']) {

            if ((int)$user['status'] === 1) {
                $_SESSION['admin'] = (int)$user['id'];
                $_SESSION['admin_username'] = (string)$user['username'];
                $_SESSION['admin_password'] = (string)$user['password'];

                $alert = "success|Login successful! Redirecting...";
            } elseif ((int)$user['status'] === 0) {
                $alert = "warning|Sorry $username, your account is not active. Contact the admin.";
            } elseif ((int)$user['status'] === 2) {
                $alert = "error|Sorry $username, your account has been banned. Contact Admin to unban your account.";
            } else {
                $alert = "error|Account status is invalid. Contact admin.";
            }

        } else {
            $alert = "error|Invalid username or password. Please try again.";
        }
    } else {
        $alert = "error|Invalid username or password. Please try again.";
    }

    $stmt->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <title>Admin Login</title>

  <!-- Font Awesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

  <!-- SweetAlert2 -->
  <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

  <style>
    :root{
      --blue:#1a73e8;
      --blue2:#0d47a1;
      --blueSoft:#eaf2ff;
      --text:#0f172a;
      --muted:#64748b;
      --line:#e2e8f0;
      --shadow: 0 18px 45px rgba(2,6,23,.16);
      --shadow2: 0 10px 26px rgba(2,6,23,.10);
      --radius:22px;
    }

    *{ box-sizing:border-box; }
    html,body{ height:100%; }
    body{
      margin:0;
      font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Arial;
      color:var(--text);
      background: linear-gradient(135deg, #0d47a1, #1a73e8);
      overflow-x:hidden;
    }

    /* Background effects */
    .bg{
      position:fixed; inset:0;
      z-index:0;
    }
    .grid{
      position:absolute; inset:0;
      background-image:
        linear-gradient(to right, rgba(255,255,255,.06) 1px, transparent 1px),
        linear-gradient(to bottom, rgba(255,255,255,.06) 1px, transparent 1px);
      background-size: 36px 36px;
      opacity:.35;
    }
    .blob{
      position:absolute;
      width:520px; height:520px;
      border-radius:999px;
      filter: blur(40px);
      opacity:.35;
    }
    .blob-1{
      top:-180px; left:-160px;
      background: radial-gradient(circle at 30% 30%, #ffffff, rgba(255,255,255,0));
    }
    .blob-2{
      bottom:-220px; right:-180px;
      background: radial-gradient(circle at 70% 70%, #cce0ff, rgba(255,255,255,0));
    }

    /* Layout */
    .wrap{
      position:relative;
      z-index:1;
      min-height:100%;
      display:grid;
      grid-template-columns: 420px 320px;
      gap:20px;
      align-items:center;
      justify-content:center;
      padding:26px;
    }

    .card{
      background: rgba(255,255,255,.92);
      border: 1px solid rgba(255,255,255,.22);
      backdrop-filter: blur(8px);
      border-radius: var(--radius);
      box-shadow: var(--shadow);
      overflow:hidden;
    }

    .card__header{
      padding:22px 22px 16px;
      background: linear-gradient(135deg, rgba(26,115,232,.18), rgba(13,71,161,.08));
      border-bottom: 1px solid rgba(2,6,23,.06);
    }

    .brand{
      display:flex;
      gap:14px;
      align-items:center;
    }
    .brand__mark{
      width:52px; height:52px;
      border-radius:16px;
      display:grid; place-items:center;
      background: var(--blueSoft);
      border: 1px solid rgba(26,115,232,.18);
      color: var(--blue);
      box-shadow: 0 10px 18px rgba(26,115,232,.12);
    }
    .brand__mark i{ font-size:20px; }
    .brand__text h1{
      margin:0;
      font-size:20px;
      letter-spacing:.2px;
    }
    .brand__text p{
      margin:5px 0 0;
      color: var(--muted);
      font-size:13px;
    }

    .trust{
      margin-top:14px;
      display:flex;
      align-items:center;
      gap:10px;
      color: var(--muted);
      font-size:12px;
    }
    .trust__dot{
      width:10px;height:10px;border-radius:50%;
      background: var(--blue);
      box-shadow: 0 0 0 6px rgba(26,115,232,.15);
    }

    .card__body{
      padding:22px;
    }

    .hero h2{
      margin:0;
      font-size:22px;
      color:#0b2a66;
    }
    .hero p{
      margin:8px 0 0;
      color: var(--muted);
      line-height:1.5;
      font-size:13.5px;
    }

    .form{
      margin-top:18px;
      display:grid;
      gap:14px;
    }

    .field label{
      display:block;
      font-size:13px;
      color:#28406b;
      margin-bottom:7px;
      font-weight:600;
    }

    .input{
      position:relative;
      display:flex;
      align-items:center;
      background:#fff;
      border:1px solid var(--line);
      border-radius: 14px;
      overflow:hidden;
      box-shadow: 0 8px 18px rgba(2,6,23,.06);
    }
    .input:focus-within{
      border-color: rgba(26,115,232,.55);
      box-shadow: 0 0 0 4px rgba(26,115,232,.14), 0 10px 20px rgba(2,6,23,.08);
    }

    .input__icon{
      width:44px;
      display:grid;
      place-items:center;
      color: rgba(13,71,161,.85);
    }
    .input input{
      border:none;
      outline:none;
      padding:14px 10px;
      width:100%;
      font-size:15px;
      color: var(--text);
    }
    .input input::placeholder{
      color:#94a3b8;
    }

    .input__action{
      border:none;
      background:transparent;
      cursor:pointer;
      padding:0 14px;
      height:100%;
      color:#64748b;
    }
    .input__action:hover{ color: var(--blue2); }

    .row{
      display:flex;
      justify-content:space-between;
      align-items:center;
      gap:12px;
      margin-top:2px;
    }

    .check{
      display:flex;
      align-items:center;
      gap:8px;
      font-size:13px;
      color: var(--muted);
      user-select:none;
    }
    .check input{
      width:16px;height:16px;
      accent-color: var(--blue);
    }

    .link{
      font-size:13px;
      color: var(--blue);
      text-decoration:none;
      font-weight:700;
    }
    .link:hover{ text-decoration:underline; }

    .btn{
      margin-top:4px;
      border:none;
      border-radius: 14px;
      padding:14px 14px;
      width:100%;
      cursor:pointer;
      display:flex;
      align-items:center;
      justify-content:center;
      gap:10px;
      color:#fff;
      font-weight:800;
      letter-spacing:.2px;
      background: linear-gradient(135deg, var(--blue), var(--blue2));
      box-shadow: 0 14px 30px rgba(26,115,232,.28);
      transition: transform .15s ease, box-shadow .15s ease;
    }
    .btn:hover{
      box-shadow: 0 18px 38px rgba(26,115,232,.34);
    }
    .btn:active{
      transform: translateY(2px);
      box-shadow: 0 10px 22px rgba(26,115,232,.28);
    }

    .btn__icon{
      width:34px;height:34px;
      border-radius:12px;
      background: rgba(255,255,255,.18);
      display:grid;place-items:center;
    }

    .note{
      margin-top:12px;
      display:flex;
      gap:10px;
      align-items:flex-start;
      padding:12px 12px;
      border-radius: 14px;
      border:1px solid rgba(26,115,232,.18);
      background: rgba(26,115,232,.07);
      color:#0b2a66;
      font-size:12.5px;
      line-height:1.45;
    }
    .note i{
      margin-top:2px;
      color: var(--blue);
    }

    .card__footer{
      display:flex;
      justify-content:space-between;
      align-items:center;
      padding:14px 18px;
      border-top: 1px solid rgba(2,6,23,.06);
      background: rgba(248,250,252,.7);
      color: var(--muted);
      font-size:12px;
    }

    .pill{
      padding:6px 10px;
      border-radius:999px;
      border:1px solid rgba(26,115,232,.18);
      background: rgba(26,115,232,.08);
      color:#0b2a66;
      font-weight:700;
    }

    /* Right side */
    .side{
      display:flex;
      flex-direction:column;
      gap:14px;
    }
    .side__card{
      background: rgba(255,255,255,.90);
      border: 1px solid rgba(255,255,255,.20);
      border-radius: var(--radius);
      box-shadow: var(--shadow2);
      padding:16px;
    }
    .side__card h3{
      margin:0 0 10px;
      font-size:14px;
      color:#0b2a66;
      letter-spacing:.2px;
    }
    .side__card ul{
      margin:0;
      padding:0;
      list-style:none;
      display:grid;
      gap:10px;
    }
    .side__card li{
      display:flex;
      gap:10px;
      align-items:flex-start;
      color: var(--muted);
      font-size:13px;
    }
    .side__card i{
      color: var(--blue);
      margin-top:2px;
    }

    .side__mini{
      background: rgba(255,255,255,.90);
      border: 1px solid rgba(255,255,255,.20);
      border-radius: var(--radius);
      box-shadow: var(--shadow2);
      padding:14px;
      display:flex;
      gap:12px;
      align-items:flex-start;
    }
    .mini__icon{
      width:44px;height:44px;
      border-radius:16px;
      display:grid;place-items:center;
      background: var(--blueSoft);
      color: var(--blue);
      border: 1px solid rgba(26,115,232,.18);
    }
    .side__mini strong{
      display:block;
      font-size:14px;
      color:#0b2a66;
    }
    .side__mini p{
      margin:4px 0 0;
      font-size:12.5px;
      color: var(--muted);
      line-height:1.45;
    }

    /* Responsive */
    @media (max-width: 980px){
      .wrap{
        grid-template-columns: 1fr;
        max-width: 520px;
      }
      .side{ display:none; }
    }

    @media (max-width: 420px){
      .wrap{ padding:18px; }
      .card__body{ padding:18px; }
      .card__header{ padding:18px; }
    }
  </style>
</head>
<body>

  <div class="bg">
    <div class="blob blob-1"></div>
    <div class="blob blob-2"></div>
    <div class="grid"></div>
  </div>

  <main class="wrap">
    <section class="card">
      <header class="card__header">
        <div class="brand">
          <div class="brand__mark">
            <i class="fa-solid fa-user-shield"></i>
          </div>
          <div class="brand__text">
            <h1>Admin Portal</h1>
            <p>Secure access to your control panel</p>
          </div>
        </div>

        <div class="trust">
          <span class="trust__dot"></span>
          <span>Encrypted session • Role-based access</span>
        </div>
      </header>

      <div class="card__body">
        <div class="hero">
          <h2>Welcome back</h2>
          <p>Sign in to manage branches, units, pumps, deposits and reports.</p>
        </div>

        <form method="POST" class="form" autocomplete="on">
          <div class="field">
            <label for="username">Username</label>
            <div class="input">
              <span class="input__icon"><i class="fa-regular fa-user"></i></span>
              <input
                type="text"
                id="username"
                name="username"
                placeholder="Enter your username"
                required
                value="<?php echo htmlspecialchars($username, ENT_QUOTES, 'UTF-8'); ?>"
              >
            </div>
          </div>

          <div class="field">
            <label for="password">Password</label>
            <div class="input">
              <span class="input__icon"><i class="fa-solid fa-lock"></i></span>
              <input
                type="password"
                id="password"
                name="password"
                placeholder="Enter your password"
                required
              >
              <button type="button" class="input__action" id="togglePassword" aria-label="Toggle password">
                <i class="fa-regular fa-eye"></i>
              </button>
            </div>
          </div>

          <div class="row">
            <label class="check">
              <input type="checkbox" id="remember">
              <span>Remember me</span>
            </label>

            <a class="link" href="https://wa.me/2347066620622">Forgot password?</a>
          </div>

          <button type="submit" name="submit" class="btn">
            <span class="btn__icon"><i class="fa-solid fa-right-to-bracket"></i></span>
            <span>Login to Dashboard</span>
          </button>

          <div class="note">
            <i class="fa-solid fa-shield-halved"></i>
            <span>For security, always log out after use and never share your login details.</span>
          </div>
        </form>
      </div>

      <footer class="card__footer">
        <span>© <?php echo date('Y'); ?> • Admin System</span>
        <span class="pill">Secure Portal v1</span>
      </footer>
    </section>

    <aside class="side">
      <div class="side__card">
        <h3>Quick Tips</h3>
        <ul>
          <li><i class="fa-solid fa-check"></i> Use a strong password</li>
          <li><i class="fa-solid fa-check"></i> Don’t login on public Wi-Fi</li>
          <li><i class="fa-solid fa-check"></i> Logout after every session</li>
        </ul>
      </div>

      <div class="side__mini">
        <div class="mini__icon"><i class="fa-solid fa-gauge-high"></i></div>
        <div>
          <strong>Fast & Clean UI</strong>
          <p>Built for daily operations and approvals.</p>
        </div>
      </div>
    </aside>
  </main>

  <script>
    // Toggle password visibility
    document.getElementById('togglePassword').addEventListener('click', function () {
      const passwordInput = document.getElementById('password');
      const icon = this.querySelector('i');
      const isPwd = passwordInput.getAttribute('type') === 'password';
      passwordInput.setAttribute('type', isPwd ? 'text' : 'password');
      icon.className = isPwd ? 'fa-regular fa-eye-slash' : 'fa-regular fa-eye';
    });

    // SweetAlert messages
    <?php if (!empty($alert)): ?>
      <?php
        $parts = explode('|', $alert, 2);
        $type = $parts[0] ?? 'error';
        $message = $parts[1] ?? 'Something went wrong';
        $title = $type === 'success' ? 'Success' : ($type === 'warning' ? 'Warning' : 'Error');
      ?>
      setTimeout(() => {
        Swal.fire({
          icon: '<?php echo $type; ?>',
          title: '<?php echo $title; ?>',
          text: '<?php echo addslashes($message); ?>',
          confirmButtonColor: '#1a73e8',
          background: '#ffffff',
          backdrop: 'rgba(26, 115, 232, 0.10)',
          allowOutsideClick: false
        });

        <?php if ($type === 'success'): ?>
          setTimeout(function() {
            window.location.href = 'dashboard.php';
          }, 1200);
        <?php endif; ?>
      }, 250);
    <?php endif; ?>

    // Inactivity timeout (10 minutes)
    let inactivityTimer;
    function startInactivityTimer() {
      clearTimeout(inactivityTimer);
      inactivityTimer = setTimeout(() => {
        Swal.fire({
          icon: 'warning',
          title: 'Session Expired',
          text: 'You have been logged out due to inactivity',
          confirmButtonColor: '#1a73e8',
          background: '#ffffff'
        }).then(() => {
          window.location.href = 'logout.php';
        });
      }, 600000);
    }

    startInactivityTimer();
    ['mousemove','keypress','click','scroll','touchstart'].forEach(evt =>
      document.addEventListener(evt, startInactivityTimer)
    );
  </script>

</body>
</html>
