<?php
require_once __DIR__ . '/../config.php';
if (isCustomer()) redirect(SITE_URL . '/pages/account.php');
$error = ''; $success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name    = trim($_POST['name'] ?? '');
    $email   = trim($_POST['email'] ?? '');
    $phone   = trim($_POST['phone'] ?? '');
    $pass    = trim($_POST['password'] ?? '');
    $confirm = trim($_POST['confirm'] ?? '');
    if (!$name || !$email || !$pass) { $error = 'Name, email and password are required.'; }
    elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $error = 'Please enter a valid email address.'; }
    elseif (strlen($pass) < 6) { $error = 'Password must be at least 6 characters.'; }
    elseif ($pass !== $confirm) { $error = 'Passwords do not match.'; }
    else {
        $db = getDB();
        $check = $db->prepare("SELECT id FROM customers WHERE email=?");
        $check->execute([$email]);
        if ($check->fetch()) { $error = 'This email is already registered. <a href="login.php">Sign in instead</a>'; }
        else {
            $hash = password_hash($pass, PASSWORD_BCRYPT);
            $db->prepare("INSERT INTO customers (name,email,phone,password) VALUES (?,?,?,?)")->execute([$name,$email,$phone,$hash]);
            $id = $db->lastInsertId();
            $_SESSION['customer_id']    = $id;
            $_SESSION['customer_name']  = $name;
            $_SESSION['customer_email'] = $email;
            redirect(SITE_URL . '/pages/account.php?welcome=1');
        }
    }
}
$page_title = 'Create Account | Balimeen Private Limited';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0">
<title><?= h($page_title) ?></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">
<link rel="stylesheet" href="<?= SITE_URL ?>/assets/css/style.css">
<link rel="stylesheet" href="<?= SITE_URL ?>/assets/css/shop.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
</head>
<body>
<div class="auth-page">
  <!-- Left -->
  <div class="auth-left">
    <div class="auth-left-inner">
      <div class="auth-brand" style="background:transparent;border:none;">
        <img src="<?= SITE_URL ?>/assets/images/logo.jpg" alt="Balimeen" style="width:100%;height:100%;object-fit:contain;border-radius:12px;">
      </div>
      <h2>Join <span>Balimeen</span><br>Family</h2>
      <p>Create your free account and get access to exclusive factory-direct prices on premium stainless steel kitchen sinks.</p>
      <div class="auth-perks">
        <div class="auth-perk"><div class="auth-perk-icon">🎯</div><div class="auth-perk-text"><h4>Easy Online Ordering</h4><p>Browse, add to cart, and checkout in minutes</p></div></div>
        <div class="auth-perk"><div class="auth-perk-icon">📋</div><div class="auth-perk-text"><h4>Order History</h4><p>View all your past purchases anytime</p></div></div>
        <div class="auth-perk"><div class="auth-perk-icon">🔔</div><div class="auth-perk-text"><h4>Order Updates</h4><p>Get notified on every stage of delivery</p></div></div>
      </div>
    </div>
  </div>
  <!-- Right -->
  <div class="auth-right">
    <div class="auth-card">
      <h3>Create Account</h3>
      <p class="sub-head">Already have an account? <a href="login.php">Sign in →</a></p>
      <?php if ($error): ?><div class="alert alert-error" style="margin-bottom:16px;border-radius:9px;padding:11px 14px;background:#fef2f2;border:1px solid #fca5a5;color:#991b1b;font-size:.87rem;font-weight:500;"><?= $error ?></div><?php endif; ?>
      <form method="POST">
        <label class="auth-form-label">Full Name *</label>
        <div class="auth-input-wrap"><i class="fas fa-user ico"></i><input type="text" name="name" placeholder="Your full name" value="<?= h($_POST['name']??'') ?>" required autofocus></div>
        <div class="auth-row">
          <div>
            <label class="auth-form-label">Email Address *</label>
            <div class="auth-input-wrap"><i class="fas fa-envelope ico"></i><input type="email" name="email" placeholder="you@email.com" value="<?= h($_POST['email']??'') ?>" required></div>
          </div>
          <div>
            <label class="auth-form-label">Phone Number</label>
            <div class="auth-input-wrap"><i class="fas fa-phone ico"></i><input type="tel" name="phone" placeholder="+91 XXXXX XXXXX" value="<?= h($_POST['phone']??'') ?>"></div>
          </div>
        </div>
        <div class="auth-row">
          <div>
            <label class="auth-form-label">Password *</label>
            <div class="auth-input-wrap"><i class="fas fa-lock ico"></i><input type="password" name="password" placeholder="Min 6 characters" id="passA" required></div>
          </div>
          <div>
            <label class="auth-form-label">Confirm Password *</label>
            <div class="auth-input-wrap"><i class="fas fa-lock ico"></i><input type="password" name="confirm" placeholder="Repeat password" id="passB" required></div>
          </div>
        </div>
        <button type="submit" class="auth-btn"><i class="fas fa-user-plus"></i> Create My Account</button>
      </form>
      <div class="auth-divider">or</div>
      <div style="text-align:center;">
        <a href="<?= SITE_URL ?>" style="font-size:.83rem;color:#64748b;"><i class="fas fa-arrow-left"></i> Back to Website</a>
      </div>
    </div>
  </div>
</div>
</body></html>
