<?php
require_once __DIR__ . '/../helpers/auth.php';

if (is_logged_in()) {
    $user = current_user();
    $role = $user['role_name'] ?? '';
    if ($role === 'staff') {
        header('Location: staff_dashboard.php');
        exit;
    }
    if ($role === 'driver') {
        header('Location: driver_app.php');
        exit;
    }
    header('Location: dashboard.php');
    exit;
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim((string)($_POST['username'] ?? ''));
    $password = (string)($_POST['password'] ?? '');

    if (!login_attempt($username, $password)) {
        $error = 'Invalid username or password.';
    } else {
        $user = current_user();
        $role = $user['role_name'] ?? '';
        if ($role === 'staff') {
            header('Location: staff_dashboard.php');
            exit;
        }
        if ($role === 'driver') {
            header('Location: driver_app.php');
            exit;
        }
        header('Location: dashboard.php');
        exit;
    }
}
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Login - Caerus Tech IMS</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" href="assets/app.css">
</head>
<body class="app-auth">
<div class="container py-5">
  <div class="row justify-content-center align-items-center" style="min-height: 92vh;">
    <div class="col-12 col-md-8 col-lg-5 col-xl-4">
      <div class="login-card p-4 p-md-5">
        <div class="login-brand mb-3">
          <span class="login-brand-mark">CT</span>
          <div>
            Caerus Tech IMS
            <div class="auth-note mt-1">Minimal inventory and delivery operations workspace.</div>
          </div>
        </div>

        <div class="soft-panel mb-4">
          <div class="fw-semibold mb-1">Sign in</div>
          <div class="auth-note">Use your admin, staff, or driver account to access barcode stock controls and live delivery tracking.</div>
        </div>

        <?php if ($error !== ''): ?>
          <div class="alert alert-danger mb-3"><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></div>
        <?php endif; ?>

        <form method="post" autocomplete="off" class="d-grid gap-3">
          <div>
            <label class="form-label" for="username">Username</label>
            <input class="form-control" id="username" name="username" type="text" value="<?= htmlspecialchars((string)($_POST['username'] ?? ''), ENT_QUOTES, 'UTF-8') ?>" required autofocus>
          </div>

          <div>
            <label class="form-label" for="password">Password</label>
            <input class="form-control" id="password" name="password" type="password" required>
          </div>

          <button class="btn btn-dark w-100" type="submit">Continue</button>
        </form>
      </div>
    </div>
  </div>
</div>
</body>
</html>
