<?php
/**
 * شريط الإحصاءات الحي الموحّد.
 *
 * المتغيرات الاختيارية قبل الاستدعاء:
 * - $ua_live_stats_context: shell|dashboard
 * - $ua_live_stats_company
 * - $ua_live_stats_user_name
 * - $ua_live_stats_user_avatar
 * - $ua_live_stats_user_initial
 * - $ua_live_stats_show_counts
 * - $ua_live_stats_show_alerts
 */
$uaLiveContext = (($ua_live_stats_context ?? 'shell') === 'dashboard') ? 'dashboard' : 'shell';
$uaLiveIsDashboard = ($uaLiveContext === 'dashboard');
$uaLiveCompany = trim((string)($ua_live_stats_company ?? $companyName ?? ''));
if ($uaLiveCompany === '') $uaLiveCompany = 'اسم الشركة';
$uaLiveUserName = trim((string)($ua_live_stats_user_name ?? $adminDisplay ?? 'Admin'));
if ($uaLiveUserName === '') $uaLiveUserName = 'Admin';
$uaLiveUserAvatar = trim((string)($ua_live_stats_user_avatar ?? $adminAvatar ?? ''));
$uaLiveUserInitial = trim((string)($ua_live_stats_user_initial ?? ''));
if ($uaLiveUserInitial === '') {
  $uaLiveUserInitial = function_exists('mb_substr') ? mb_substr($uaLiveUserName, 0, 1, 'UTF-8') : substr($uaLiveUserName, 0, 1);
}
if ($uaLiveUserInitial === '') $uaLiveUserInitial = 'A';
$uaLiveShowCounts = array_key_exists('ua_live_stats_show_counts', get_defined_vars()) ? !empty($ua_live_stats_show_counts) : true;
$uaLiveShowAlerts = array_key_exists('ua_live_stats_show_alerts', get_defined_vars()) ? !empty($ua_live_stats_show_alerts) : true;
$uaLiveBarId = $uaLiveIsDashboard ? 'nocDashboardStatsBar' : 'uaShellStatsBar';
$uaLiveSystemId = $uaLiveIsDashboard ? 'nocSystemState' : 'uaShellSystemState';
$uaLiveTrafficId = $uaLiveIsDashboard ? 'nocStatTrafficTotal' : 'uaShellTrafficTotal';
$uaLiveTotalId = $uaLiveIsDashboard ? 'nocStatTotal' : 'uaShellTotal';
$uaLiveOnlineId = $uaLiveIsDashboard ? 'nocStatOnline' : 'uaShellOnline';
$uaLiveOfflineId = $uaLiveIsDashboard ? 'nocStatOffline' : 'uaShellOffline';
$uaLiveDisabledId = $uaLiveIsDashboard ? 'nocStatDisabled' : 'uaShellDisabled';
$uaLiveUnsafeId = $uaLiveIsDashboard ? 'nocStatUnsafe' : 'uaShellUnsafe';
$uaLiveAlertDetailsId = $uaLiveIsDashboard ? 'nocAlertDetails' : 'uaShellAlertDetails';
$uaLiveAlertCountId = $uaLiveIsDashboard ? 'nocAlertCount' : 'uaShellAlertCount';
$uaLiveAlertFeedId = $uaLiveIsDashboard ? 'alertFeed' : 'uaShellAlertFeed';
$uaLiveOuterClass = 'ua-livebar ua-livebar--' . $uaLiveContext . ($uaLiveIsDashboard ? '' : ' ua-unified-shell-block');
$uaLiveAvatarExists = ($uaLiveUserAvatar !== '' && is_file(__DIR__ . '/../' . ltrim($uaLiveUserAvatar, '/\\')));
?>
<section class="<?= h($uaLiveOuterClass) ?>" id="<?= h($uaLiveBarId) ?>" data-ua-livebar data-ua-livebar-context="<?= h($uaLiveContext) ?>"<?= $uaLiveIsDashboard ? '' : ' data-ua-shell-region="stats"' ?> aria-label="إحصاءات مباشرة">
  <div class="ua-livebar__chip ua-livebar__brand" title="اسم الشركة">
    <span class="ua-livebar__icon" aria-hidden="true"><i class="fa fa-sitemap"></i></span>
    <span class="ua-livebar__label ua-livebar__company"><?= h($uaLiveCompany) ?></span>
  </div>
  <?php if ($uaLiveShowCounts): ?>
    <div class="ua-livebar__chip ua-livebar__system is-pending" id="<?= h($uaLiveSystemId) ?>" data-ua-system-state title="جارٍ التحقق من اتصال الراوتر">
      <span class="ua-livebar__icon" aria-hidden="true"><i class="fa fa-heartbeat"></i></span>
      <span class="ua-livebar__label">حالة الشبكة</span>
      <strong data-ua-system-label data-ua-counter="off">جارٍ التحقق</strong>
    </div>
    <div class="ua-livebar__chip ua-livebar__traffic" title="إجمالي الترافيك الحي من مصادر WAN" aria-label="إجمالي الترافيك">
      <span class="ua-livebar__icon" aria-hidden="true"><i class="fa fa-exchange"></i></span>
      <strong id="<?= h($uaLiveTrafficId) ?>" data-ua-stat-value="traffic" data-ua-counter="off">—</strong>
    </div>
    <div class="ua-livebar__chip">
      <span class="ua-livebar__icon" aria-hidden="true"><i class="fa fa-users"></i></span>
      <span class="ua-livebar__label">إجمالي المشتركين</span>
      <strong id="<?= h($uaLiveTotalId) ?>" data-ua-stat-value="total" data-ua-counter="1">—</strong>
    </div>
    <div class="ua-livebar__chip is-green">
      <span class="ua-livebar__icon" aria-hidden="true"><i class="fa fa-wifi"></i></span>
      <span class="ua-livebar__label">المتصلون</span>
      <strong id="<?= h($uaLiveOnlineId) ?>" data-ua-stat-value="online" data-ua-counter="1">—</strong>
    </div>
    <div class="ua-livebar__chip">
      <span class="ua-livebar__icon" aria-hidden="true"><i class="fa fa-user-times"></i></span>
      <span class="ua-livebar__label">غير المتصلين</span>
      <strong id="<?= h($uaLiveOfflineId) ?>" data-ua-stat-value="offline" data-ua-counter="1">—</strong>
    </div>
    <div class="ua-livebar__chip is-red" title="الحسابات المفصولة ماليًا">
      <span class="ua-livebar__icon" aria-hidden="true"><i class="fa fa-ban"></i></span>
      <span class="ua-livebar__label">المفصولون</span>
      <strong id="<?= h($uaLiveDisabledId) ?>" data-ua-stat-value="disabled" data-ua-counter="1">—</strong>
    </div>
    <div class="ua-livebar__chip is-purple" data-ua-unsafe-chip title="الحسابات العاملة بلا تقييد سرعة فعلي">
      <span class="ua-livebar__icon" aria-hidden="true"><i class="fa fa-bolt"></i></span>
      <span class="ua-livebar__label">بلا تقييد</span>
      <strong id="<?= h($uaLiveUnsafeId) ?>" data-ua-stat-value="unsafe" data-ua-counter="1">—</strong>
    </div>
  <?php endif; ?>
  <?php if ($uaLiveShowAlerts): ?>
    <details class="ua-livebar__alerts" id="<?= h($uaLiveAlertDetailsId) ?>" data-ua-alert-details>
      <summary class="ua-livebar__chip ua-livebar__alert-trigger" title="عرض التنبيهات" aria-label="عرض التنبيهات">
        <span class="ua-livebar__icon ua-livebar__icon--bell" aria-hidden="true"><i class="fa fa-bell"></i><em class="nocx-alert-badge is-zero" id="<?= h($uaLiveAlertCountId) ?>" data-ua-alert-count data-ua-counter="off"></em></span>
        <span class="ua-livebar__label">التنبيهات</span>
      </summary>
      <div class="nocx-alert-popover" role="dialog" aria-label="الإنذارات">
        <div class="nocx-alert-popover__head">
          <strong><i class="fa fa-bell" aria-hidden="true"></i> الإنذارات</strong>
          <small>أعطال وأحداث مهمة</small>
        </div>
        <div class="nocx-alert-feed" id="<?= h($uaLiveAlertFeedId) ?>" data-ua-alert-feed><div class="nocx-empty">لا يوجد إنذارات حالياً.</div></div>
      </div>
    </details>
  <?php endif; ?>
  <details class="ua-livebar__user">
    <summary title="المستخدم النشط" aria-label="المستخدم النشط">
      <span class="ua-livebar__avatar"><?php if ($uaLiveAvatarExists): ?><img src="<?= h($uaLiveUserAvatar) ?>?v=<?= (int)@filemtime(__DIR__ . '/../' . ltrim($uaLiveUserAvatar, '/\\')) ?>" alt=""><?php else: ?><?= h($uaLiveUserInitial) ?><?php endif; ?></span>
      <span class="ua-livebar__username"><?= h($uaLiveUserName) ?></span>
      <span class="ua-livebar__caret" aria-hidden="true">▾</span>
    </summary>
    <div class="nocx-user-menu-panel" role="menu">
      <a href="user_profile.php" role="menuitem"><i class="fa fa-id-card-o"></i><span>ملفي الشخصي</span></a>
      <?php if (function_exists('has_perm') && has_perm('can_manage_admins')): ?>
        <a href="users_permissions.php" role="menuitem"><i class="fa fa-users"></i><span>المستخدمون والصلاحيات</span></a>
      <?php endif; ?>
      <a href="logout.php?switch_user=1" class="js-switch-user" data-ua-switch-user="1" role="menuitem"><i class="fa fa-exchange"></i><span>تبديل المستخدم</span></a>
      <a href="logout.php" class="js-hard-logout" data-ua-hard-logout="1" role="menuitem"><i class="fa fa-sign-out"></i><span>تسجيل الخروج</span></a>
    </div>
  </details>
</section>
