<?php
require __DIR__ . '/config.php';
if (session_status() === PHP_SESSION_NONE) session_start();

$count = $pdo->query("SELECT COUNT(*) c FROM users")->fetch()['c'];
if ($count > 0) {
    die('تم إعداد حساب المدير بالفعل. من فضلك احذف هذا الملف (create_admin.php) من السيرفر فورًا لأسباب أمنية، وسجّل الدخول من login.php');
}

$error = '';
$done  = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    if (strlen($username) < 3 || strlen($password) < 6) {
        $error = 'اسم المستخدم 3 أحرف على الأقل، وكلمة المرور 6 أحرف على الأقل';
    } else {
        $hash = password_hash($password, PASSWORD_DEFAULT);
        $stmt = $pdo->prepare("INSERT INTO users (username, password_hash) VALUES (?,?)");
        $stmt->execute([$username, $hash]);
        $done = true;
    }
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>إنشاء حساب المدير</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700;900&display=swap" rel="stylesheet">
<style>
  body{ font-family:'Tajawal',sans-serif; background:#12332A; margin:0; min-height:100vh; display:flex; align-items:center; justify-content:center; padding:20px; }
  .box{ background:#FBF7EC; max-width:400px; width:100%; border-radius:16px; padding:28px 24px; }
  h1{ font-size:19px; color:#12332A; margin:0 0 16px; }
  .field{ margin-bottom:14px; }
  .field label{ display:block; font-size:13px; font-weight:700; color:#12332A; margin-bottom:5px; }
  .field input{ width:100%; padding:11px; border-radius:8px; border:1px solid #D8CFB0; font-size:14px; box-sizing:border-box; }
  button{ width:100%; background:#12332A; color:#E6B95C; border:none; padding:12px; border-radius:8px; font-weight:700; font-size:15px; cursor:pointer; }
  .error{ color:#A3402F; font-size:13px; margin-bottom:12px; }
  .success{ color:#2F6B4F; font-size:14px; line-height:1.7; }
  p.warn{ font-size:12px; color:#8b8368; margin-top:16px; }
</style>
</head>
<body>
  <div class="box">
    <?php if ($done): ?>
      <h1>تم إنشاء الحساب بنجاح ✓</h1>
      <p class="success">تقدر دلوقتي تسجّل الدخول من صفحة <strong>login.php</strong> بنفس بيانات الحساب.</p>
      <p class="warn">مهم جدًا: احذف ملف create_admin.php من السيرفر فورًا عشان محدش يقدر يعمل حساب تاني غير مصرح له.</p>
    <?php else: ?>
      <h1>إنشاء حساب المدير الأول</h1>
      <?php if ($error): ?><p class="error"><?php echo htmlspecialchars($error, ENT_QUOTES, 'UTF-8'); ?></p><?php endif; ?>
      <form method="POST">
        <div class="field"><label>اسم المستخدم</label><input type="text" name="username" required></div>
        <div class="field"><label>كلمة المرور</label><input type="password" name="password" required></div>
        <button type="submit">إنشاء الحساب</button>
      </form>
    <?php endif; ?>
  </div>
</body>
</html>
