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

$error = ''; $success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $company = trim($_POST['company_name'] ?? '');
    $name    = trim($_POST['name'] ?? '');
    $email   = trim($_POST['email'] ?? '');
    $phone   = trim($_POST['phone'] ?? '');
    $gstin   = trim($_POST['gstin'] ?? '');
    $city    = trim($_POST['city'] ?? '');
    $state   = trim($_POST['state'] ?? '');
    $pass    = trim($_POST['password'] ?? '');

    if ($company && $name && $email && $pass) {
        $db = getDB();
        $chk = $db->prepare("SELECT id FROM dealers WHERE email=?");
        $chk->execute([$email]);
        if ($chk->fetch()) {
            $error = 'Email already registered.';
        } else {
            $hash = password_hash($pass, PASSWORD_BCRYPT);
            $db->prepare("INSERT INTO dealers (company_name,name,email,phone,gstin,city,state,password,status) VALUES (?,?,?,?,?,?,?,?,'pending')")
               ->execute([$company,$name,$email,$phone,$gstin,$city,$state,$hash]);
            $success = 'Application submitted! Our team will review your details and activate your account shortly.';
        }
    } else { $error = 'Please fill all required fields.'; }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Dealer Application | 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 { background: #f1f5f9; display: flex; align-items: center; justify-content: center; min-height: 100vh; font-family: 'Inter', sans-serif; margin: 0; padding:20px; }
.login-box { background: #fff; padding: 40px; border-radius: 16px; width: 100%; max-width: 500px; box-shadow: 0 10px 40px rgba(0,0,0,.08); }
.logo-area { text-align: center; margin-bottom: 24px; }
.logo-area h2 { font-family: 'Outfit', sans-serif; font-size: 1.6rem; color: #0f172a; margin-top: 10px; margin-bottom:4px;}
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: .85rem; font-weight: 600; color: #334155; margin-bottom: 6px; }
.form-group input { width: 100%; padding: 12px; border: 1.5px solid #cbd5e1; border-radius: 8px; font-size: .95rem; outline: none; box-sizing: border-box; }
.form-group input:focus { border-color: #2557d6; }
.btn { width: 100%; padding: 12px; background: #2557d6; color: #fff; border: none; border-radius: 8px; font-weight: 700; font-size: 1rem; cursor: pointer; }
.btn:hover { background: #1d4ed8; }
.alert { padding: 12px; border-radius: 8px; margin-bottom: 16px; font-size: .85rem; font-weight: 500; }
.alert-error { background: #fef2f2; color: #991b1b; border: 1px solid #fca5a5; }
.alert-success { background: #f0fdf4; color: #166534; border: 1px solid #86efac; }
</style>
</head>
<body>
<div class="login-box">
  <div class="logo-area">
    <h2>Apply as Dealer</h2>
    <p style="font-size:.85rem;color:#64748b;margin:0;">Partner with Balimeen for factory-direct wholesale prices.</p>
  </div>
  <?php if ($error): ?><div class="alert alert-error"><?= h($error) ?></div><?php endif; ?>
  <?php if ($success): ?>
    <div class="alert alert-success"><?= h($success) ?></div>
    <div style="text-align:center;margin-top:20px;"><a href="login.php" class="btn" style="text-decoration:none;display:inline-block;width:auto;padding:10px 20px;">Return to Login</a></div>
  <?php else: ?>
  <form method="POST">
    <div class="form-group"><label>Company / Firm Name *</label><input type="text" name="company_name" required autofocus></div>
    <div class="form-group"><label>Contact Person *</label><input type="text" name="name" required></div>
    <div class="form-row">
      <div class="form-group"><label>Email *</label><input type="email" name="email" required></div>
      <div class="form-group"><label>Phone *</label><input type="text" name="phone" required></div>
    </div>
    <div class="form-group"><label>GSTIN</label><input type="text" name="gstin"></div>
    <div class="form-row">
      <div class="form-group"><label>City</label><input type="text" name="city"></div>
      <div class="form-group"><label>State</label><input type="text" name="state"></div>
    </div>
    <div class="form-group"><label>Password *</label><input type="password" name="password" required></div>
    <button type="submit" class="btn">Submit Application</button>
  </form>
  <div style="text-align:center;margin-top:20px;font-size:.85rem;">
    <a href="login.php" style="color:#2557d6;font-weight:600;">Already a dealer? Login</a>
  </div>
  <?php endif; ?>
</div>
</body>
</html>
