<?php
// admin/setup_first_admin.php
// ✅ شاشة إعداد أول Admin + اسم الشركة/المالك (بدون تكرار دوال helpers)

if (session_status() === PHP_SESSION_NONE) session_start();

// Stage 2: keep optional parent config if the deployment uses it, then load local DB bootstrap.
$__ua_parent_config = dirname(__DIR__) . '/config.php';
if (is_file($__ua_parent_config)) {
  require_once $__ua_parent_config;
}
require_once __DIR__ . '/db_bootstrap.php';
require_once __DIR__ . '/app_runtime.php';
if (function_exists('ensure_db')) {
  ensure_db(true);
}
unset($__ua_parent_config);
require_once __DIR__ . '/helpers.php';
require_once __DIR__ . '/db_schema_helpers.php';
require_once __DIR__ . '/app_assets.php';
require_once __DIR__ . '/media_assets_lib.php';
require_once __DIR__ . '/lang.php';

$conn = $GLOBALS['conn'] ?? ($conn ?? null);
if (!($conn instanceof mysqli)) {
  die("خطأ: اتصال قاعدة البيانات غير جاهز");
}

mysqli_report(MYSQLI_REPORT_OFF);

// ✅ توفير table_exists/col_exists إذا غير موجودة في helpers.php

$msg=""; $err="";

// ✅ تأكد من جدول admin_users
if(!table_exists($conn,'admin_users')){
  $conn->query("
    CREATE TABLE admin_users (
      id INT AUTO_INCREMENT PRIMARY KEY,
      username VARCHAR(100) NOT NULL UNIQUE,
      password VARCHAR(255) NULL,
      password_hash VARCHAR(255) NULL,
      full_name VARCHAR(150) NULL,
      permissions LONGTEXT NULL,
      is_active TINYINT(1) NOT NULL DEFAULT 1,
      created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
  ");
}

// ✅ تأكد من جدول company_info
if(!table_exists($conn,'company_info')){
  $conn->query("
    CREATE TABLE company_info (
      id INT AUTO_INCREMENT PRIMARY KEY,
      company_name VARCHAR(200) NULL,
      owner_name VARCHAR(200) NULL,
      phone VARCHAR(50) NULL,
      address VARCHAR(255) NULL,
      logo_path VARCHAR(255) NULL,
      created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
      updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
  ");
}
$r = $conn->query("SELECT id FROM company_info WHERE id=1");
if(!$r || !$r->num_rows){
  $conn->query("INSERT INTO company_info(id,company_name,owner_name) VALUES (1,'اسم الشركة','عماد خميس')");
}

// ✅ إذا يوجد Admin بالفعل => تحويل لتسجيل الدخول
$chk = $conn->query("SELECT id FROM admin_users LIMIT 1");
if($chk && $chk->num_rows){
  if (function_exists('ua_install_link_required') && ua_install_link_required() && function_exists('ua_install_link_approved') && !ua_install_link_approved()) {
    header("Location: install_link.php");
    exit;
  }
  header("Location: login.php");
  exit;
}

if(isset($_POST['save'])){
  $company_name = trim($_POST['company_name'] ?? '');
  $owner_name   = trim($_POST['owner_name'] ?? '');

  $username = trim($_POST['username'] ?? '');
  $pass1    = (string)($_POST['password'] ?? '');
  $pass2    = (string)($_POST['password2'] ?? '');

  if($company_name === '' || $owner_name === '' || $username === '' || $pass1 === ''){
    $err = "يرجى تعبئة جميع الحقول";
  } elseif($pass1 !== $pass2){
    $err = "كلمة المرور وتأكيدها غير متطابقين";
  } else {
    // تحديث بيانات الشركة
    $cn = $conn->real_escape_string($company_name);
    $on = $conn->real_escape_string($owner_name);
    $conn->query("UPDATE company_info SET company_name='$cn', owner_name='$on' WHERE id=1");

    // إنشاء Admin بصلاحيات كاملة
    $u = $conn->real_escape_string($username);
    $hash = $conn->real_escape_string(password_hash($pass1, PASSWORD_DEFAULT));

    $permJson = $conn->real_escape_string('{"all":true}');
    $conn->query("
      INSERT INTO admin_users (username,password,password_hash,full_name,permissions,is_active)
      VALUES ('$u','$hash','$hash','Super Admin','$permJson',1)
    ");

    if (function_exists('ua_install_link_required') && ua_install_link_required() && function_exists('ua_install_link_approved') && !ua_install_link_approved()) {
      header("Location: install_link.php");
      exit;
    }
    header("Location: login.php");
    exit;
  }
}

$ci = $conn->query("SELECT * FROM company_info WHERE id=1")->fetch_assoc();
$company_name = trim((string)($ci['company_name'] ?? ''));
$owner_name   = trim((string)($ci['owner_name'] ?? ''));
if (in_array($company_name, ['اسم الشركة', 'Company Name', 'company name', 'اسم المنشأة'], true)) $company_name = '';
if (in_array($owner_name, ['عماد خميس', 'اسم المالك', 'Owner Name'], true)) $owner_name = '';
$programName = 'إدارة الشبكة والعملاء';
$programVersion = function_exists('app_version') ? trim((string)app_version()) : '';
$programLogo = function_exists('ua_media_url') ? ua_media_url('sidebar_program_logo', 'assets/img/sidebar_program_logo.png') : 'assets/img/sidebar_program_logo.png';
?>
<!doctype html>
<?php $uaLang = function_exists('current_lang') ? current_lang() : 'ar'; $uaDir = ($uaLang === 'en') ? 'ltr' : 'rtl'; ?>
<html lang="<?= h($uaLang) ?>" dir="<?= h($uaDir) ?>">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>إعداد أول تشغيل - <?= h($programName) ?></title>
  <?= ua_asset_tag_css('bootstrap_462_css') ?>
  <?php $ua_setup_css_v = @filemtime(__DIR__ . '/assets/css/ua_program_unified.css') ?: 1; ?>
  <link rel="stylesheet" href="assets/css/ua_program_unified.css?v=<?= (int)$ua_setup_css_v ?>">
  <style>
    :root{
      --ua-setup-bg:#eef4f8;
      --ua-setup-panel:#ffffff;
      --ua-setup-ink:#0f172a;
      --ua-setup-muted:#64748b;
      --ua-setup-line:#dbe5ee;
      --ua-setup-primary:#0f7fd8;
      --ua-setup-primary-dark:#0967b3;
      --ua-setup-side:#102033;
      --ua-setup-soft:#f7fafc;
    }
    *{box-sizing:border-box}
    body.ua-first-run{
      min-height:100vh;
      margin:0;
      overflow:hidden;
      background:
        radial-gradient(circle at 50% 44%, rgba(15,127,216,.10), transparent 30%),
        linear-gradient(135deg, #f7fbfd 0%, var(--ua-setup-bg) 100%);
      color:var(--ua-setup-ink);
      font-family:Tahoma, Arial, sans-serif;
    }
    .ua-setup-page{
      min-height:100vh;
      display:grid;
      place-items:center;
      padding:18px;
      background:rgba(15,23,42,.10);
    }
    .ua-setup-window{
      width:min(560px, calc(100vw - 36px));
      min-height:0;
      display:block;
      overflow:hidden;
      background:var(--ua-setup-panel);
      border:1px solid rgba(148,163,184,.28);
      border-radius:16px;
      box-shadow:0 22px 70px rgba(15,23,42,.18);
    }
    .ua-setup-side{
      position:relative;
      min-height:0;
      padding:16px 20px;
      background:
        linear-gradient(150deg, rgba(9,103,179,.24), transparent 62%),
        var(--ua-setup-side);
      color:#fff;
      display:flex;
      align-items:center;
      justify-content:space-between;
      gap:14px;
    }
    .ua-setup-brand{
      display:flex;
      align-items:center;
      gap:12px;
    }
    .ua-setup-logo{
      width:40px;
      height:40px;
      border-radius:9px;
      background:#fff;
      padding:7px;
      object-fit:contain;
      box-shadow:0 10px 24px rgba(0,0,0,.18);
    }
    .ua-setup-app{
      display:block;
      font-size:16px;
      line-height:1.35;
      font-weight:800;
    }
    .ua-setup-version{
      display:block;
      margin-top:3px;
      color:#b9c9d8;
      font-size:12px;
    }
    .ua-setup-side-copy h1{
      margin:0;
      font-size:16px;
      line-height:1.35;
      font-weight:800;
    }
    .ua-setup-side-copy p{
      display:none;
    }
    .ua-setup-badge{
      width:max-content;
      max-width:100%;
      padding:6px 9px;
      border:1px solid rgba(255,255,255,.18);
      border-radius:999px;
      color:#dbeafe;
      font-size:12px;
      background:rgba(255,255,255,.08);
    }
    .ua-setup-main{
      padding:20px 24px 22px;
      display:flex;
      flex-direction:column;
      justify-content:center;
    }
    .ua-setup-kicker{
      color:var(--ua-setup-primary);
      font-size:12px;
      font-weight:800;
      margin-bottom:6px;
    }
    .ua-setup-title{
      margin:0;
      font-size:21px;
      font-weight:900;
      line-height:1.35;
    }
    .ua-setup-subtitle{
      margin:6px 0 16px;
      color:var(--ua-setup-muted);
      font-size:13px;
      line-height:1.6;
    }
    .ua-setup-alert{
      margin:0 0 12px;
      padding:9px 11px;
      border-radius:10px;
      border:1px solid #fecaca;
      background:#fff1f2;
      color:#991b1b;
      font-size:13px;
    }
    .ua-setup-grid{
      display:grid;
      grid-template-columns:1fr 1fr;
      gap:12px;
    }
    .ua-setup-field{
      min-width:0;
    }
    .ua-setup-field.ua-span{
      grid-column:1 / -1;
    }
    .ua-setup-field label{
      display:block;
      margin-bottom:6px;
      color:#243244;
      font-size:13px;
      font-weight:800;
    }
    .ua-setup-input{
      width:100%;
      height:38px;
      padding:0 12px;
      border:1px solid var(--ua-setup-line);
      border-radius:10px;
      background:var(--ua-setup-soft);
      color:var(--ua-setup-ink);
      font-size:13px;
      outline:none;
      transition:border-color .15s ease, box-shadow .15s ease, background .15s ease;
    }
    .ua-setup-input:focus{
      border-color:rgba(15,127,216,.72);
      background:#fff;
      box-shadow:0 0 0 3px rgba(15,127,216,.12);
    }
    .ua-setup-divider{
      height:1px;
      margin:14px 0 13px;
      background:linear-gradient(90deg, transparent, var(--ua-setup-line), transparent);
    }
    .ua-setup-actions{
      display:flex;
      align-items:center;
      gap:12px;
      margin-top:15px;
    }
    .ua-setup-submit{
      height:40px;
      min-width:156px;
      border:0;
      border-radius:10px;
      background:linear-gradient(180deg, var(--ua-setup-primary), var(--ua-setup-primary-dark));
      color:#fff;
      font-weight:900;
      font-size:13px;
      cursor:pointer;
      box-shadow:0 12px 22px rgba(15,127,216,.24);
    }
    .ua-setup-note{
      color:var(--ua-setup-muted);
      font-size:12px;
      line-height:1.7;
    }
    @media (max-width:760px){
      body.ua-first-run{overflow:auto}
      .ua-setup-page{padding:12px}
      .ua-setup-window{
        width:100%;
        min-height:auto;
        grid-template-columns:1fr;
      }
      .ua-setup-side{align-items:flex-start; flex-direction:column;}
      .ua-setup-badge{display:none}
      .ua-setup-main{padding:20px 16px}
      .ua-setup-grid{grid-template-columns:1fr}
      .ua-setup-actions{align-items:stretch; flex-direction:column}
      .ua-setup-submit{width:100%}
    }

    /* First-run setup uses the same local EXE chrome as the login screen. */
    body.ua-first-run{
      background:#eef8fb !important;
      font-family:"UA Cairo Login", Tahoma, Arial, sans-serif !important;
    }
    .ua-setup-page{
      min-height:100vh !important;
      height:100vh !important;
      display:flex !important;
      align-items:center !important;
      justify-content:center !important;
      padding:0 !important;
      background:transparent !important;
      direction:ltr !important;
      overflow:hidden !important;
    }
    .ua-setup-window{
      width:960px !important;
      height:600px !important;
      max-width:calc(100vw - 24px) !important;
      max-height:calc(100vh - 24px) !important;
      display:grid !important;
      grid-template-columns:50% 50% !important;
      overflow:hidden !important;
      border:1px solid rgba(14,83,108,.16) !important;
      border-radius:0 !important;
      background:#f6fbfd !important;
      box-shadow:0 26px 80px rgba(3,10,20,.35) !important;
      direction:ltr !important;
    }
    .ua-setup-side{
      position:relative !important;
      min-width:0 !important;
      height:100% !important;
      padding:0 !important;
      display:block !important;
      overflow:hidden !important;
      background-image:url('assets/img/login_network_light_bg.png') !important;
      background-size:cover !important;
      background-position:left center !important;
      background-repeat:no-repeat !important;
      background-color:#dceff6 !important;
      color:#083a53 !important;
    }
    .ua-setup-side::after{
      content:"" !important;
      position:absolute !important;
      inset:0 !important;
      display:block !important;
      pointer-events:none !important;
      background:
        linear-gradient(90deg, rgba(255,255,255,.02) 0%, rgba(255,255,255,.16) 60%, rgba(255,255,255,.72) 100%),
        linear-gradient(180deg, rgba(5,44,68,.06), rgba(5,44,68,.14)) !important;
      z-index:2 !important;
    }
    .ua-setup-brand{
      display:none !important;
    }
    .ua-setup-logo{
      width:34px !important;
      height:34px !important;
      border-radius:10px !important;
      padding:0 !important;
      background:#fff !important;
      box-shadow:none !important;
      object-fit:cover !important;
    }
    .ua-setup-app{
      color:#083a53 !important;
      font-size:13px !important;
      font-weight:900 !important;
      line-height:1.25 !important;
      white-space:nowrap !important;
    }
    .ua-setup-version{
      color:#60758a !important;
      font-size:11px !important;
      font-weight:800 !important;
    }
    .ua-setup-side-copy,
    .ua-setup-badge{
      display:none !important;
    }
    .ua-setup-side .exa-visual-lock{
      display:block !important;
      position:absolute !important;
      left:50% !important;
      top:50% !important;
      width:156px !important;
      height:176px !important;
      transform:translate(-50%, -50%) !important;
      z-index:4 !important;
      pointer-events:none !important;
      opacity:.99 !important;
      animation:exaLockFloat 4.8s ease-in-out infinite, exaLockMaterialize 5.8s ease-in-out infinite !important;
      filter:
        drop-shadow(0 20px 30px rgba(6,48,72,.28))
        drop-shadow(0 0 24px rgba(255,255,255,.34))
        drop-shadow(0 0 10px rgba(226,182,71,.20)) !important;
    }
    .ua-setup-side .exa-visual-lock::before{
      content:"" !important;
      display:block !important;
      position:absolute !important;
      inset:-26px !important;
      border-radius:38px !important;
      background:
        radial-gradient(circle at 50% 48%, rgba(255,255,255,.34), transparent 46%),
        conic-gradient(from 40deg, transparent 0 26%, rgba(255,255,255,.42) 36%, rgba(226,182,71,.36) 43%, transparent 58% 100%) !important;
      opacity:.30 !important;
      mix-blend-mode:screen !important;
      animation:exaLockAssemble 5.8s ease-in-out infinite !important;
    }
    .ua-setup-side .exa-visual-lock::after{
      content:"" !important;
      display:block !important;
      position:absolute !important;
      left:8px !important;
      right:8px !important;
      top:0 !important;
      height:3px !important;
      border-radius:99px !important;
      background:linear-gradient(90deg, transparent, rgba(255,255,255,.9), rgba(226,182,71,.82), transparent) !important;
      opacity:0 !important;
      transform:translateY(0) !important;
      animation:exaLockScan 5.8s ease-in-out infinite !important;
    }
    .ua-setup-side .exa-lock-shackle{
      position:absolute !important;
      left:34px !important;
      top:8px !important;
      width:88px !important;
      height:82px !important;
      display:block !important;
      border:13px solid rgba(255,255,255,.64) !important;
      border-bottom:0 !important;
      border-radius:50px 50px 0 0 !important;
      box-shadow:
        inset 0 0 0 1px rgba(12,95,128,.12),
        0 0 24px rgba(59,190,225,.16) !important;
    }
    .ua-setup-side .exa-lock-body{
      position:absolute !important;
      left:19px !important;
      top:76px !important;
      width:118px !important;
      height:98px !important;
      display:block !important;
      border-radius:25px !important;
      background:linear-gradient(145deg, rgba(255,255,255,.68), rgba(197,232,243,.38)) !important;
      border:1px solid rgba(255,255,255,.58) !important;
      box-shadow:
        inset 0 1px 0 rgba(255,255,255,.72),
        inset 0 -18px 28px rgba(14,96,132,.14),
        0 18px 52px rgba(5,48,74,.20) !important;
      backdrop-filter:blur(2px) !important;
    }
    .ua-setup-side .exa-lock-body i{
      position:absolute !important;
      left:50% !important;
      top:48% !important;
      width:18px !important;
      height:34px !important;
      display:block !important;
      transform:translate(-50%, -50%) !important;
      border-radius:12px !important;
      background:linear-gradient(180deg, rgba(9,79,112,.62), rgba(9,54,82,.76)) !important;
      box-shadow:0 0 18px rgba(28,171,217,.18) !important;
    }
    .ua-setup-side .exa-lock-body i::before{
      content:"" !important;
      position:absolute !important;
      left:50% !important;
      top:-17px !important;
      width:27px !important;
      height:27px !important;
      transform:translateX(-50%) !important;
      border-radius:50% !important;
      background:rgba(9,79,112,.68) !important;
    }
    .ua-setup-side .exa-lock-ring{
      position:absolute !important;
      inset:36px 20px 0 !important;
      display:block !important;
      border-radius:42px !important;
      border:1px solid rgba(255,255,255,.46) !important;
      opacity:0 !important;
      animation:exaLockPulse 3.2s ease-out infinite !important;
    }
    .ua-setup-side .exa-lock-ring-2{
      animation-delay:1.3s !important;
    }
    .ua-setup-side .exa-lock-spark{
      position:absolute !important;
      width:10px !important;
      height:10px !important;
      display:block !important;
      border-radius:50% !important;
      background:#f0c24f !important;
      box-shadow:0 0 16px rgba(240,194,79,.8) !important;
      opacity:.9 !important;
      animation:exaSparkDrift 3.8s ease-in-out infinite !important;
    }
    .ua-setup-side .exa-lock-spark-1{
      right:12px !important;
      top:76px !important;
    }
    .ua-setup-side .exa-lock-spark-2{
      left:8px !important;
      bottom:42px !important;
      animation-delay:1.6s !important;
    }
    .ua-setup-main{
      position:relative !important;
      min-width:0 !important;
      width:calc(100% + 42px) !important;
      height:100% !important;
      margin-left:-42px !important;
      padding:30px 46px 28px 78px !important;
      display:flex !important;
      flex-direction:column !important;
      justify-content:center !important;
      overflow:hidden !important;
      direction:rtl !important;
      background:linear-gradient(90deg,
        rgba(255,255,255,0) 0%,
        rgba(238,248,251,.88) 36px,
        #eef8fb 84px,
        #f6fbfd 100%) !important;
      z-index:3 !important;
    }
    .ua-setup-kicker{
      display:none !important;
    }
    .ua-setup-title{
      margin:0 !important;
      color:#083a53 !important;
      font-size:31px !important;
      font-weight:900 !important;
      line-height:1.18 !important;
      text-align:center !important;
      letter-spacing:0 !important;
    }
    .ua-setup-subtitle{
      max-width:430px !important;
      margin:8px auto 16px !important;
      color:#60758a !important;
      font-size:13.5px !important;
      font-weight:700 !important;
      line-height:1.55 !important;
      text-align:center !important;
    }
    .ua-setup-subtitle::after{
      content:"" !important;
      display:block !important;
      width:72px !important;
      height:3px !important;
      margin:13px auto 0 !important;
      border-radius:99px !important;
      background:linear-gradient(90deg, #19a8d5, #e2b647) !important;
      opacity:.82 !important;
    }
    .ua-setup-alert{
      margin:0 0 10px !important;
      padding:9px 12px !important;
      border-radius:10px !important;
      background:#fef3f2 !important;
      border-color:#fecdca !important;
      color:#b42318 !important;
      font-size:13px !important;
      font-weight:700 !important;
    }
    .ua-setup-grid{
      display:grid !important;
      grid-template-columns:1fr 1fr !important;
      gap:10px !important;
    }
    .ua-setup-field label{
      margin-bottom:5px !important;
      color:#344054 !important;
      font-size:12.5px !important;
      font-weight:700 !important;
      line-height:1.35 !important;
    }
    .ua-setup-input{
      width:100% !important;
      height:42px !important;
      border-radius:10px !important;
      border:1px solid #ccd5df !important;
      background:#ffffff !important;
      color:#101828 !important;
      font-family:"UA Cairo Login", Tahoma, Arial, sans-serif !important;
      font-size:13.5px !important;
      font-weight:700 !important;
      line-height:1.4 !important;
      padding:0 12px !important;
      box-shadow:0 1px 2px rgba(16,24,40,.05) !important;
    }
    .ua-setup-input::placeholder{
      color:#98a2b3 !important;
      opacity:1 !important;
    }
    .ua-setup-input:focus{
      border-color:#d6a935 !important;
      background:#ffffff !important;
      box-shadow:0 0 0 3px rgba(214,169,53,.14) !important;
    }
    .ua-setup-divider{
      height:1px !important;
      margin:12px 0 !important;
      background:linear-gradient(90deg, transparent, #d7e2ea, transparent) !important;
    }
    .ua-setup-actions{
      display:flex !important;
      align-items:center !important;
      gap:12px !important;
      margin-top:14px !important;
    }
    .ua-setup-submit{
      height:44px !important;
      min-width:168px !important;
      border:1px solid #cfa43b !important;
      border-radius:10px !important;
      background:linear-gradient(180deg, #e5bd4f, #cfa43b) !important;
      color:#111827 !important;
      font-family:"UA Cairo Login", Tahoma, Arial, sans-serif !important;
      font-size:14px !important;
      font-weight:900 !important;
      box-shadow:none !important;
    }
    .ua-setup-note{
      color:#60758a !important;
      font-size:12px !important;
      font-weight:700 !important;
      line-height:1.55 !important;
    }

    @media (max-width:760px){
      body.ua-first-run{overflow:auto !important}
      .ua-setup-page{
        height:auto !important;
        min-height:100vh !important;
        padding:12px !important;
        overflow:auto !important;
      }
      .ua-setup-window{
        width:100% !important;
        height:auto !important;
        min-height:0 !important;
        max-height:none !important;
        grid-template-columns:1fr !important;
      }
      .ua-setup-side{
        display:none !important;
      }
      .ua-setup-main{
        width:100% !important;
        margin-left:0 !important;
        padding:22px !important;
      }
      .ua-setup-grid{
        grid-template-columns:1fr !important;
      }
      .ua-setup-actions{
        align-items:stretch !important;
        flex-direction:column !important;
      }
      .ua-setup-submit{
        width:100% !important;
      }
    }
  </style>
</head>
<body class="ua-first-run public-entry-page ua-public-special-page ua-first-admin-page">

<main class="ua-setup-page" aria-label="إعداد أول تشغيل">
  <section class="ua-setup-window" aria-labelledby="uaSetupTitle">
    <aside class="ua-setup-side">
      <div class="ua-setup-brand">
        <img class="ua-setup-logo" src="<?= h($programLogo) ?>" alt="">
        <span>
          <span class="ua-setup-app"><?= h($programName) ?></span>
          <span class="ua-setup-version">تهيئة النسخة الجديدة</span>
        </span>
      </div>

      <div class="ua-setup-side-copy">
        <h1>أول تشغيل للبرنامج</h1>
        <p>أنشئ بيانات المدير الأساسي مرة واحدة. هذه البيانات ستكون بدل أي كلمة مرور افتراضية.</p>
      </div>

      <div class="ua-setup-badge">نسخة Server محلية</div>
      <div class="exa-visual-lock" aria-hidden="true">
        <span class="exa-lock-ring exa-lock-ring-1"></span>
        <span class="exa-lock-ring exa-lock-ring-2"></span>
        <span class="exa-lock-spark exa-lock-spark-1"></span>
        <span class="exa-lock-spark exa-lock-spark-2"></span>
        <span class="exa-lock-shackle"></span>
        <span class="exa-lock-body"><i></i></span>
      </div>
    </aside>

    <section class="ua-setup-main">
      <div class="ua-setup-kicker">إعداد آمن</div>
      <h2 class="ua-setup-title" id="uaSetupTitle">إنشاء المدير الأساسي</h2>
      <p class="ua-setup-subtitle">أدخل بيانات الشركة ثم اختر اسم المستخدم وكلمة المرور الجديدة للمدير.</p>

      <?php if($err){ ?><div class="ua-setup-alert" role="alert"><?= h($err) ?></div><?php } ?>

      <form method="post" autocomplete="on">
        <div class="ua-setup-grid">
          <div class="ua-setup-field">
            <label for="companyName">اسم الشركة</label>
            <input id="companyName" class="ua-setup-input" name="company_name" value="<?= h($company_name) ?>" required>
          </div>
          <div class="ua-setup-field">
            <label for="ownerName">اسم المالك</label>
            <input id="ownerName" class="ua-setup-input" name="owner_name" value="<?= h($owner_name) ?>" required>
          </div>
        </div>

        <div class="ua-setup-divider" aria-hidden="true"></div>

        <div class="ua-setup-grid">
          <div class="ua-setup-field ua-span">
            <label for="adminUser">اسم المستخدم</label>
            <input id="adminUser" class="ua-setup-input" name="username" value="admin" autocomplete="username" required>
          </div>
          <div class="ua-setup-field">
            <label for="adminPass">كلمة المرور الجديدة</label>
            <input id="adminPass" class="ua-setup-input" type="password" name="password" autocomplete="new-password" required>
          </div>
          <div class="ua-setup-field">
            <label for="adminPass2">تأكيد كلمة المرور</label>
            <input id="adminPass2" class="ua-setup-input" type="password" name="password2" autocomplete="new-password" required>
          </div>
        </div>

        <div class="ua-setup-actions">
          <button class="ua-setup-submit" name="save" type="submit">حفظ وفتح البرنامج</button>
          <div class="ua-setup-note">بعد الحفظ سيتم تحويلك إلى صفحة تسجيل الدخول.</div>
        </div>
      </form>
    </section>
  </section>
</main>


<?php if (function_exists('ua_i18n_json_payload')): ?>
<script>window.UA_I18N = <?= ua_i18n_json_payload() ?: '{}' ?>;</script>
<?php $ua_v_i18n_js = @filemtime(__DIR__ . '/assets/js/ua_i18n.js') ?: time(); ?>
<script src="assets/js/ua_i18n.js?v=<?= (int)$ua_v_i18n_js ?>"></script>
<?php endif; ?>
</body>
</html>
