<?php
// nyaya-guru/login.php — Client Login
require_once __DIR__ . '/includes/config.php';
require_once __DIR__ . '/includes/auth.php';

if (isUserLoggedIn())  { redirect(SITE_URL . '/client/dashboard.php'); }

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!validateCSRF($_POST['csrf_token'] ?? '')) { $error = 'Invalid request.'; }
    else {
        $email    = sanitizeEmail($_POST['email'] ?? '');
        $password = $_POST['password'] ?? '';
        if (!$email || !$password) {
            $error = 'Please enter email and password.';
        } else {
            $db   = getDB();
            $stmt = $db->prepare("SELECT * FROM users WHERE email=? AND status=1 LIMIT 1");
            $stmt->execute([$email]);
            $user = $stmt->fetch();
            if ($user && ($password === $user['password'] || verifyPassword($password, $user['password']))) {
              loginUser($user);


$db->prepare("UPDATE users SET last_login=NOW() WHERE id=?")->execute([$user['id']]);
              $redirect = $_GET['redirect'] ?? '';

if ($redirect) {
    header("Location: " . $redirect);
    exit;
} else {
    redirect(SITE_URL . '/client/dashboard.php');
}
            } else {
                $error = 'Invalid email or password.';
            }
        }
    }
}

$pageTitle = 'Client Login — MeeVakeel';
require_once __DIR__ . '/includes/header.php';
?>
<section style="min-height:80vh;background:var(--off-white);display:flex;align-items:center;padding:60px 0;">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-5">

  <!-- Card -->
  <div style="background:white;border-radius:20px;overflow:hidden;box-shadow:var(--shadow-lg);">
    <div style="background:linear-gradient(135deg,var(--primary),#1a3a6b);padding:36px;text-align:center;">
      <div style="font-family:var(--font-heading);font-size:1.8rem;font-weight:800;color:white;">
        <span style="color:var(--gold);">Mee</span> Vakeel
      </div>
      <p style="color:rgba(255,255,255,0.7);margin:8px 0 0;font-size:0.9rem;">Sign in to your client account</p>
    </div>
    <div style="padding:36px;">
      <?php if ($error): ?>
      <div class="alert alert-danger rounded-3 border-0" style="font-size:.88rem;"><i class="fas fa-exclamation-circle me-2"></i><?= $error ?></div>
      <?php endif; ?>

      <form method="POST">
        <input type="hidden" name="csrf_token" value="<?= generateCSRF() ?>">
        <div class="mb-3">
          <label class="form-label">Email Address</label>
          <div style="position:relative;">
            <i class="fas fa-envelope" style="position:absolute;left:13px;top:50%;transform:translateY(-50%);color:var(--text-light);font-size:.85rem;"></i>
            <input type="email" name="email" class="form-control" style="padding-left:38px;" placeholder="you@email.com" value="<?= htmlspecialchars($_POST['email']??'') ?>" required>
          </div>
        </div>
        <div class="mb-4">
          <label class="form-label">Password</label>
          <div style="position:relative;">
            <i class="fas fa-lock" style="position:absolute;left:13px;top:50%;transform:translateY(-50%);color:var(--text-light);font-size:.85rem;"></i>
            <input type="password" name="password" id="pwd" class="form-control" style="padding-left:38px;padding-right:44px;" placeholder="Your password" required>
            <span onclick="var p=document.getElementById('pwd');p.type=p.type==='password'?'text':'password';this.className=p.type==='password'?'fas fa-eye':'fas fa-eye-slash';"
                  class="fas fa-eye" style="position:absolute;right:13px;top:50%;transform:translateY(-50%);color:var(--text-light);cursor:pointer;font-size:.85rem;"></span>
          </div>
        </div>
        <button type="submit" class="btn btn-gold w-100 py-3" style="font-weight:700;border-radius:12px;">
          <i class="fas fa-sign-in-alt me-2"></i>Login
        </button>
      </form>

      <div style="text-align:center;margin-top:20px;">
        <span style="color:var(--text-light);font-size:.88rem;">Don't have an account?</span>
        <a href="<?= SITE_URL ?>/register.php" style="color:var(--primary);font-weight:700;font-size:.88rem;margin-left:6px;">Register Free</a>
      </div>
      <div style="text-align:center;margin-top:10px;">
        <a href="<?= SITE_URL ?>/lawyer/login.php" style="color:var(--text-light);font-size:.82rem;">Are you a Lawyer? Login here →</a>
      </div>
    </div>
  </div>

</div>
</div>
</div>
</section>
<?php require_once __DIR__ . '/includes/footer.php'; ?>