<?php
// meevakeel/lawyer/login.php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/auth.php';
if (isLawyerLoggedIn()) { 
    redirect(SITE_URL . '/lawyer/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 lawyers WHERE email=? AND status=1 LIMIT 1");
            $stmt->execute([$email]);
            $lawyer = $stmt->fetch();
            if ($lawyer && ($password === $lawyer['password'] || verifyPassword($password, $lawyer['password']))) {
                loginLawyer($lawyer);
                $db->prepare("UPDATE lawyers SET last_login=NOW() WHERE id=?")->execute([$lawyer['id']]);
                redirect(SITE_URL . '/lawyer/dashboard.php');
            } else {
                $error = 'Invalid email or password.';
            }
        }
    }
}

$pageTitle = 'Lawyer 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">
  <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,.7);margin:8px 0 0;font-size:.9rem;">Lawyer Portal — Sign In</p>
    </div>
    <div style="padding:36px;">
      <?php if ($flash = getFlash()): ?>
      <div class="alert alert-<?= $flash['type']==='success'?'success':'danger' ?> rounded-3 border-0" style="font-size:.88rem;"><?= $flash['message'] ?></div>
      <?php endif; ?>
      <?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="your@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';"
                  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 as Lawyer
        </button>
      </form>
      <div style="text-align:center;margin-top:20px;">
        <span style="color:var(--text-light);font-size:.88rem;">Not registered yet?</span>
        <a href="<?= SITE_URL ?>/register-lawyer.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 ?>/login.php" style="color:var(--text-light);font-size:.82rem;">Are you a Client? Login here →</a>
      </div>
    </div>
  </div>
</div>
</div>
</div>
</section>
<?php require_once __DIR__ . '/../includes/footer.php'; ?>