<?php
// nyaya-guru/client-login.php
// Unified entry point — same as login.php
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. Please try again.';
    } else {
        $email    = sanitizeEmail($_POST['email'] ?? '');
        $password = $_POST['password'] ?? '';

        if (!$email || !$password) {
            $error = 'Please enter your 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();

            // Support both plain-text (test accounts) and hashed passwords
            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'] ?? '/client/dashboard.php';
                redirect($redirect);
            } else {
                $error = 'Invalid email or password. Please try again.';
            }
        }
    }
}

$pageTitle = 'Client Login — MeeVakeel';
require_once __DIR__ . '/includes/header.php';
?>

<section style="min-height:82vh;background:var(--off-white);display:flex;align-items:center;padding:60px 0;">
<div class="container">
<div class="row justify-content-center align-items-center g-5">

  <!-- Left: Info panel -->
  <div class="col-lg-5 d-none d-lg-block">
    <div style="padding:20px;">
      <div style="font-family:var(--font-heading);font-size:2rem;font-weight:800;color:var(--primary);margin-bottom:8px;">
        <span style="color:var(--gold);">Nyaya</span> Guru
      </div>
      <h2 style="font-size:1.5rem;color:var(--primary);margin-bottom:16px;line-height:1.35;">
        Your Legal Partner,<br>Available 24/7
      </h2>
      <p style="color:var(--text-light);font-size:.9rem;line-height:1.85;margin-bottom:28px;">
        Sign in to access your consultations, chat with lawyers, view your case history, and book new appointments.
      </p>
      <?php foreach ([
        ['fas fa-user-tie',    'Verified Lawyers',      '500+ verified advocates across India'],
        ['fas fa-lock',        'Secure & Private',      'All consultations fully confidential'],
        ['fas fa-rupee-sign',  'Transparent Pricing',   'No hidden charges, fees shown upfront'],
        ['fas fa-bolt',        'Instant Booking',       'Same-day appointments available'],
      ] as [$icon,$title,$desc]): ?>
      <div style="display:flex;gap:14px;margin-bottom:18px;align-items:flex-start;">
        <div style="width:40px;height:40px;min-width:40px;background:linear-gradient(135deg,var(--gold),var(--gold-dark));border-radius:10px;display:flex;align-items:center;justify-content:center;color:var(--primary);font-size:.9rem;">
          <i class="<?= $icon ?>"></i>
        </div>
        <div>
          <div style="font-weight:700;font-size:.88rem;color:var(--primary);"><?= $title ?></div>
          <div style="font-size:.8rem;color:var(--text-light);"><?= $desc ?></div>
        </div>
      </div>
      <?php endforeach; ?>
    </div>
  </div>

  <!-- Right: Login card -->
  <div class="col-lg-5 col-md-8">
    <div style="background:white;border-radius:20px;overflow:hidden;box-shadow:var(--shadow-lg);">

      <!-- Card header -->
      <div style="background:linear-gradient(135deg,var(--primary),#1a3a6b);padding:34px;text-align:center;">
        <div style="width:56px;height:56px;background:rgba(212,175,55,.15);border:2px solid rgba(212,175,55,.4);border-radius:14px;display:flex;align-items:center;justify-content:center;margin:0 auto 14px;font-size:1.3rem;color:var(--gold);">
          <i class="fas fa-user-circle"></i>
        </div>
        <h4 style="color:white;margin:0 0 4px;font-size:1.1rem;">Welcome Back</h4>
        <p style="color:rgba(255,255,255,.6);font-size:.82rem;margin:0;">Sign in to your client account</p>
      </div>

      <div style="padding:32px;">

        <?php if ($error): ?>
        <div style="background:rgba(220,53,69,.07);border:1px solid rgba(220,53,69,.25);border-radius:10px;padding:12px 16px;margin-bottom:20px;font-size:.85rem;color:#c0392b;display:flex;align-items:center;gap:10px;">
          <i class="fas fa-exclamation-circle"></i> <?= $error ?>
        </div>
        <?php endif; ?>

        <!-- Flash message -->
        <?php if ($flash = getFlash()): ?>
        <div style="background:rgba(40,167,69,.07);border:1px solid rgba(40,167,69,.25);border-radius:10px;padding:12px 16px;margin-bottom:20px;font-size:.85rem;color:#1a7a34;">
          <i class="fas fa-check-circle me-2"></i><?= $flash['message'] ?>
        </div>
        <?php endif; ?>

        <form method="POST" action="">
          <input type="hidden" name="csrf_token" value="<?= generateCSRF() ?>">

          <div class="mb-3">
            <label class="form-label" style="font-weight:600;font-size:.85rem;">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:.82rem;pointer-events:none;"></i>
              <input type="email" name="email" class="form-control" style="padding-left:38px;"
                     placeholder="you@email.com"
                     value="<?= htmlspecialchars($_POST['email'] ?? '') ?>" required autocomplete="email">
            </div>
          </div>

          <div class="mb-4">
            <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:6px;">
              <label class="form-label mb-0" style="font-weight:600;font-size:.85rem;">Password</label>
            </div>
            <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:.82rem;pointer-events:none;"></i>
              <input type="password" name="password" id="clientPwd" class="form-control"
                     style="padding-left:38px;padding-right:44px;"
                     placeholder="Your password" required autocomplete="current-password">
              <button type="button" onclick="togglePwd('clientPwd',this)"
                      style="position:absolute;right:12px;top:50%;transform:translateY(-50%);background:none;border:none;color:var(--text-light);cursor:pointer;padding:0;font-size:.82rem;">
                <i class="fas fa-eye"></i>
              </button>
            </div>
          </div>

          <button type="submit" class="btn btn-gold w-100 py-3" style="font-weight:700;border-radius:12px;font-size:.95rem;">
            <i class="fas fa-sign-in-alt me-2"></i>Login to My Account
          </button>
        </form>

        <div style="text-align:center;margin-top:22px;padding-top:18px;border-top:1px solid var(--border);">
          <p style="color:var(--text-light);font-size:.85rem;margin-bottom:10px;">
            Don't have an account?
            <a href="register.php" style="color:var(--primary);font-weight:700;margin-left:4px;">Register Free →</a>
          </p>
          <a href="lawyer/login.php" style="color:var(--text-light);font-size:.8rem;">
            <i class="fas fa-user-tie me-1"></i>Are you a Lawyer? Login here
          </a>
        </div>

      </div>
    </div>

    <!-- Test credentials note (remove in production) -->
    <div style="background:rgba(212,175,55,.08);border:1px solid rgba(212,175,55,.3);border-radius:12px;padding:14px 18px;margin-top:16px;">
      <p style="font-size:.78rem;color:var(--text-light);margin:0;">
        <i class="fas fa-info-circle me-1" style="color:var(--gold);"></i>
        <strong>Demo client:</strong> mobin@ugra.in &nbsp;·&nbsp; <strong>Password:</strong> (set during registration)
      </p>
    </div>
  </div>

</div>
</div>
</section>

<script>
function togglePwd(id, btn) {
  var inp = document.getElementById(id);
  var isPass = inp.type === 'password';
  inp.type = isPass ? 'text' : 'password';
  btn.innerHTML = isPass ? '<i class="fas fa-eye-slash"></i>' : '<i class="fas fa-eye"></i>';
}
</script>

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