<?php
/**
 * /app/register.php — User Registration Page
 * Bridges to existing /api/auth/register.php endpoint
 */
require_once __DIR__ . '/../config.php';

// Already logged in? Redirect to dashboard
if (current_user()) {
    header('Location: ' . url('app/dashboard.php'));
    exit;
}

$csrf = $_SESSION['reg_csrf'] ?? ($_SESSION['reg_csrf'] = bin2hex(random_bytes(16)));
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>سجّل في سودانا</title>
<link rel="manifest" href="/manifest.json">
<style>
  *{box-sizing:border-box;margin:0;padding:0;font-family:'Cairo',system-ui,sans-serif}
  body{background:linear-gradient(135deg,#073328,#0a1628);color:#e2e8f0;min-height:100vh;
       display:flex;align-items:center;justify-content:center;padding:20px}
  .reg{background:#0d2137;border:1px solid #1e3a2a;border-radius:16px;padding:32px 28px;
       width:100%;max-width:420px;box-shadow:0 10px 40px rgba(0,0,0,.5)}
  .logo{text-align:center;font-size:1.6rem;font-weight:900;color:#00d97e;margin-bottom:6px}
  .sub{text-align:center;color:#64748b;font-size:.85rem;margin-bottom:24px}
  label{display:block;font-size:.78rem;color:#94a3b8;margin:14px 0 6px;font-weight:600}
  input{width:100%;background:#060d1a;border:1px solid #1e3a2a;border-radius:10px;
        padding:12px 14px;color:#e2e8f0;font-size:.95rem;font-family:inherit;outline:none;
        transition:border-color .15s}
  input:focus{border-color:#00d97e}
  .btn{width:100%;background:#00d97e;color:#000;border:none;border-radius:10px;
       padding:14px;font-weight:900;font-size:1rem;cursor:pointer;margin-top:24px;
       font-family:inherit;transition:filter .15s}
  .btn:hover{filter:brightness(1.1)}
  .btn:disabled{background:#475569;cursor:not-allowed}
  .alt{text-align:center;font-size:.82rem;color:#64748b;margin-top:18px}
  .alt a{color:#00d97e;text-decoration:none;font-weight:700}
  .err{background:#3a1010;color:#fca5a5;padding:10px 14px;border-radius:8px;
       font-size:.82rem;margin-top:12px;display:none}
  .err.show{display:block}
</style>
</head>
<body>
<form class="reg" id="regForm">
  <div class="logo">🇸🇩 سودانا</div>
  <div class="sub">سجّل حسابك المجاني — 30 ثانية</div>

  <input type="hidden" name="csrf" value="<?= htmlspecialchars($csrf) ?>">

  <label>الاسم الكامل</label>
  <input type="text" name="name" required minlength="2" maxlength="100" placeholder="أحمد محمد">

  <label>رقم الهاتف</label>
  <input type="tel" name="phone" required pattern="[0-9+]{9,15}" placeholder="0912345678">

  <label>البريد الإلكتروني (اختياري)</label>
  <input type="email" name="email" placeholder="ahmed@example.com">

  <label>كلمة المرور (6 أحرف على الأقل)</label>
  <input type="password" name="password" required minlength="6" placeholder="••••••••">

  <button type="submit" class="btn" id="submitBtn">سجّل الآن</button>

  <div class="err" id="errBox"></div>

  <div class="alt">
    عندك حساب؟ <a href="/?login=1">سجّل دخولك</a>
  </div>
</form>

<script>
const form = document.getElementById('regForm');
const btn  = document.getElementById('submitBtn');
const err  = document.getElementById('errBox');

form.addEventListener('submit', async (e) => {
  e.preventDefault();
  err.classList.remove('show');
  btn.disabled = true;
  btn.textContent = 'جاري التسجيل...';

  const data = Object.fromEntries(new FormData(form));

  try {
    const r = await fetch('/api/auth/register.php', {
      method:'POST',
      headers:{'Content-Type':'application/json'},
      body: JSON.stringify(data),
    });
    const j = await r.json();
    if (j.ok || j.success) {
      window.location.href = '/app/dashboard.php';
    } else {
      err.textContent = j.message || j.error || 'فشل التسجيل — حاول مجدداً';
      err.classList.add('show');
      btn.disabled = false;
      btn.textContent = 'سجّل الآن';
    }
  } catch(ex) {
    err.textContent = 'خطأ في الاتصال — تحقق من الإنترنت';
    err.classList.add('show');
    btn.disabled = false;
    btn.textContent = 'سجّل الآن';
  }
});
</script>
</body>
</html>
