<?php
// view: pages/dashboard
$items_count = (int)($items_count ?? 0);
$low_count = (int)($low_count ?? 0);
$today_sales = is_array($today_sales ?? null) ? $today_sales : ['c'=>0,'s'=>0];
?>
<div class="dashboard-cards-grid mb-4">
  <?php if (can('alerts.view') && is_array($alerts_summary ?? null)): ?>
    <div class="dashboard-card dashboard-card--danger">
      <div class="dashboard-card__icon"><i class="fa-solid fa-bell"></i></div>
      <div class="dashboard-card__title">تنبيهات تحتاج متابعة</div>
      <div class="dashboard-card__value"><?= (int)($alerts_summary['total'] ?? 0) ?></div>
      <div class="dashboard-card__desc">افتح لوحة التنبيهات بسرعة</div>
      <a href="<?= e(url('/alerts')) ?>" class="dashboard-card__action">لوحة التنبيهات <i class="fa-solid fa-arrow-left-long"></i></a>
    </div>
  <?php endif; ?>

  <div class="dashboard-card dashboard-card--info">
    <div class="dashboard-card__icon"><i class="fa-solid fa-boxes-stacked"></i></div>
    <div class="dashboard-card__title">عدد الأصناف</div>
    <div class="dashboard-card__value"><?= (int)$items_count ?></div>
    <div class="dashboard-card__desc">الوصول السريع إلى الأصناف</div>
    <a href="<?= e(url('/inventory/items')) ?>" class="dashboard-card__action">إدارة الأصناف <i class="fa-solid fa-arrow-left-long"></i></a>
  </div>

  <?php if (can('sales.invoices.view')): ?>
    <div class="dashboard-card dashboard-card--success">
      <div class="dashboard-card__icon"><i class="fa-solid fa-receipt"></i></div>
      <div class="dashboard-card__title">عدد فواتير اليوم</div>
      <div class="dashboard-card__value"><?= (int)($today_sales['c'] ?? 0) ?></div>
      <div class="dashboard-card__desc">الدخول إلى شاشة المبيعات</div>
      <a href="<?= e(url('/sales')) ?>" class="dashboard-card__action">لوحة المبيعات <i class="fa-solid fa-arrow-left-long"></i></a>
    </div>

    <div class="dashboard-card dashboard-card--primary">
      <div class="dashboard-card__icon"><i class="fa-solid fa-sack-dollar"></i></div>
      <div class="dashboard-card__title">مبيعات اليوم</div>
      <div class="dashboard-card__value"><?= format_money(($today_sales['s'] ?? 0)) ?></div>
      <div class="dashboard-card__desc">عرض فواتير المبيعات اليومية</div>
      <a href="<?= e(url('/sales/invoices')) ?>" class="dashboard-card__action">عرض فواتير المبيعات <i class="fa-solid fa-arrow-left-long"></i></a>
    </div>
  <?php endif; ?>

  <div class="dashboard-card dashboard-card--warning">
    <div class="dashboard-card__icon"><i class="fa-solid fa-triangle-exclamation"></i></div>
    <div class="dashboard-card__title">تنبيه نقص مخزون</div>
    <div class="dashboard-card__value"><?= (int)$low_count ?></div>
    <div class="dashboard-card__desc">أصناف تحتاج إعادة طلب أو متابعة</div>
    <a href="<?= e(url('/reports/inventory/stock')) ?>" class="dashboard-card__action">تقرير المخزون <i class="fa-solid fa-arrow-left-long"></i></a>
  </div>
</div>

<?php if (can('alerts.view') && is_array($alerts_summary ?? null)): ?>
<div class="content-card">
  <div class="card-header d-flex justify-content-between align-items-center">
    <h3 class="card-title mb-0">ملخص التنبيهات</h3>
    <a class="btn btn-sm btn-outline-primary" href="<?= e(url('/alerts')) ?>">فتح لوحة التنبيهات</a>
  </div>
  <div class="card-body">
    <div class="alerts-summary-grid">
      <div class="alerts-summary-item"><span>الشيكات</span><strong><?= (int)($alerts_summary['checks']['total'] ?? 0) ?></strong></div>
      <div class="alerts-summary-item"><span>المخزون</span><strong><?= (int)($alerts_summary['inventory']['total'] ?? 0) ?></strong></div>
      <div class="alerts-summary-item"><span>الأطراف</span><strong><?= (int)($alerts_summary['parties']['total'] ?? 0) ?></strong></div>
      <div class="alerts-summary-item"><span>الخزن والبنوك</span><strong><?= (int)($alerts_summary['treasury']['total'] ?? 0) ?></strong></div>
      <div class="alerts-summary-item"><span>المسودات</span><strong><?= (int)($alerts_summary['drafts']['total'] ?? 0) ?></strong></div>
      <div class="alerts-summary-item"><span>إجمالي التنبيهات</span><strong><?= (int)($alerts_summary['total'] ?? 0) ?></strong></div>
    </div>
  </div>
</div>
<?php endif; ?>
