<?php
require_once __DIR__ . '/../config.php';
if (session_status() === PHP_SESSION_NONE) session_start();
if (isset($_SESSION['dealer_id'])) redirect(SITE_URL . '/dealer/index.php');

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $pass  = trim($_POST['password'] ?? '');
    if ($email && $pass) {
        $db = getDB();
        $stmt = $db->prepare("SELECT * FROM dealers WHERE email=?");
        $stmt->execute([$email]);
        $dealer = $stmt->fetch();
        if ($dealer && password_verify($pass, $dealer['password'])) {
            if ($dealer['status'] === 'approved') {
                $_SESSION['dealer_id'] = $dealer['id'];
                $_SESSION['dealer_name'] = $dealer['name'];
                redirect(SITE_URL . '/dealer/index.php');
            } else {
                $error = 'Your account status is: ' . ucfirst($dealer['status']) . '. Please wait for admin approval.';
            }
        } else {
            $error = 'Invalid email or password.';
        }
    } else { $error = 'Please enter email and password.'; }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Dealer Portal Login | Balimeen</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Outfit:wght@700;800;900&display=swap" rel="stylesheet">
<style>
body { font-family: 'Inter', sans-serif; margin: 0; padding: 0; background: #fff; display: flex; height: 100vh; }
.split-left { width: 45%; background: url('../assets/images/hero-bg.jpg') center/cover no-repeat; position: relative; }
.split-left::after { content: ''; position: absolute; inset: 0; background: linear-gradient(to bottom, rgba(10,31,78,0.2), rgba(10,31,78,0.8)); }
.split-right { width: 55%; display: flex; align-items: center; justify-content: center; background: #f8fafc; }
.login-box { background: #fff; padding: 48px; border-radius: 16px; width: 100%; max-width: 440px; box-shadow: 0 10px 40px rgba(0,0,0,.05); border: 1px solid #e2e8f0; }
.logo-area { text-align: center; margin-bottom: 32px; }
.logo-area img { width: 120px; margin-bottom: 16px; }
.logo-area h2 { font-family: 'Outfit', sans-serif; font-size: 1.5rem; color: #0f172a; margin: 0 0 8px; }
.logo-area p { color: #64748b; font-size: .9rem; margin: 0; }
.form-group { margin-bottom: 20px; }
.form-group label { display: block; font-size: .85rem; font-weight: 600; color: #334155; margin-bottom: 8px; }
.form-group input { width: 100%; padding: 12px 16px; border: 1.5px solid #cbd5e1; border-radius: 8px; font-size: .95rem; outline: none; transition: border-color 0.2s; box-sizing: border-box;}
.form-group input:focus { border-color: #2557d6; }
.btn { width: 100%; padding: 14px; background: #2557d6; color: #fff; border: none; border-radius: 8px; font-weight: 600; font-size: 1rem; cursor: pointer; transition: background 0.2s; }
.btn:hover { background: #1d4ed8; }
.alert { padding: 12px; border-radius: 8px; margin-bottom: 20px; font-size: .85rem; font-weight: 500; }
.alert-error { background: #fef2f2; color: #991b1b; border: 1px solid #fca5a5; }
.options { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; font-size: .85rem; }
@media (max-width: 768px) {
    .split-left { display: none; }
    .split-right { width: 100%; padding: 20px; }
}
</style>
</head>
<body>
<div class="split-left"></div>
<div class="split-right">
  <div class="login-box">
    <div class="logo-area">
      <img src="<?= SITE_URL ?>/assets/images/logo.jpg" alt="Balimeen" style="width:64px;height:64px;border-radius:12px;object-fit:contain;">
      <h2>Dealer Portal Login</h2>
      <p>Welcome back! Please login to your account.</p>
    </div>
    <?php if ($error): ?><div class="alert alert-error"><?= h($error) ?></div><?php endif; ?>
    <form method="POST">
      <div class="form-group">
        <label>Dealer Code / Email</label>
        <input type="text" name="email" placeholder="Enter your email" required autofocus>
      </div>
      <div class="form-group">
        <label>Password</label>
        <input type="password" name="password" placeholder="Enter your password" required>
      </div>
      <div class="options">
        <label style="display:flex;align-items:center;gap:6px;cursor:pointer;"><input type="checkbox"> Remember me</label>
        <a href="../pages/forgot_password.php" style="color:#2557d6;text-decoration:none;font-weight:600;">Forgot Password?</a>
      </div>
      <button type="submit" class="btn">Login</button>
    </form>
    <div style="text-align:center;margin-top:24px;font-size:.9rem;color:#64748b;">
      New Dealer? <a href="register.php" style="color:#2557d6;font-weight:600;text-decoration:none;">Apply Now</a>
    </div>
  </div>
</div>
</body>
</html>
