<?php $title = 'لوحة التحكم'; include __DIR__.'/../layout/header.php'; ?>

<?php
$statusMap = [
    'draft' => ['label' => 'مسودة', 'class' => 'st-draft'],
    'confirmed' => ['label' => 'مؤكدة', 'class' => 'st-confirmed'],
    'awaiting_return' => ['label' => 'بانتظار الإرجاع', 'class' => 'st-awaiting-return'],
    'awaiting_settlement' => ['label' => 'بانتظار التصفية', 'class' => 'st-awaiting-settlement'],
    'closed' => ['label' => 'مغلقة', 'class' => 'st-closed'],
    'cancelled' => ['label' => 'ملغية', 'class' => 'st-cancelled'],
];

$openInvoices = 0;
$closedInvoices = 0;
foreach (($recentInvoices ?? []) as $inv) {
    if (($inv['invoice_status'] ?? '') === 'closed') $closedInvoices++;
    else $openInvoices++;
}
?>

<style>
.dashboard-wrap{display:grid;gap:18px}
.dashboard-hero{background:linear-gradient(135deg,#fff,#fbf7f2);border:1px solid #eadfd5}
.dashboard-sub{color:#8b7a6b;line-height:1.8;margin-top:6px}
.kpi-grid{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:14px}
.kpi-card{background:#fff;border:1px solid #eadfd5;border-radius:22px;padding:18px;box-shadow:0 10px 24px rgba(17,24,39,.05)}
.kpi-card .kpi-label{font-size:13px;color:#8b7a6b;margin-bottom:8px}
.kpi-card .kpi-value{font-size:32px;font-weight:800;color:#5e3b23;line-height:1}
.dashboard-panels{display:grid;grid-template-columns:1.2fr .8fr;gap:16px}
.panel-card{background:#fff;border:1px solid #eadfd5;border-radius:22px;padding:18px;box-shadow:0 10px 24px rgba(17,24,39,.05)}
.panel-card h3{margin:0 0 12px}
.invoice-table-wrap{overflow:auto}
.invoice-status{display:inline-flex;align-items:center;gap:6px;padding:7px 12px;border-radius:999px;font-size:12px;font-weight:800;white-space:nowrap}
.st-draft{background:#f3f4f6;color:#374151}
.st-confirmed{background:#dbeafe;color:#1d4ed8}
.st-awaiting-return{background:#fef3c7;color:#92400e}
.st-awaiting-settlement{background:#fde68a;color:#92400e}
.st-closed{background:#dcfce7;color:#166534}
.st-cancelled{background:#fee2e2;color:#991b1b}
.invoice-row-closed td{background:#f8fff9}
.closed-block{background:linear-gradient(135deg,#ecfdf5,#f0fdf4);border:1px solid #bbf7d0}
.closed-split{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px}
.closed-box{background:#fff;border:1px solid #d1fae5;border-radius:18px;padding:14px}
.closed-box .label{font-size:13px;color:#6b7280}
.closed-box .value{font-size:28px;font-weight:800;color:#166534;margin-top:6px}
.invoice-no{font-weight:800;color:#2d1f16}
.empty-note{padding:18px;text-align:center;color:#8b7a6b}
@media (max-width: 960px){
  .kpi-grid{grid-template-columns:repeat(2,minmax(0,1fr))}
  .dashboard-panels{grid-template-columns:1fr}
}
@media (max-width: 560px){
  .kpi-grid{grid-template-columns:1fr}
  .closed-split{grid-template-columns:1fr}
}
</style>

<div class="dashboard-wrap">
  <div class="card dashboard-hero">
    <div class="page-head">
      <div>
        <h2 style="margin:0">لوحة التحكم</h2>
        <div class="dashboard-sub">عرض سريع ومختصر لأهم أرقام النظام، مع تحسين شكل الحالات وفصل الفواتير المغلقة بشكل أوضح وأسهل.</div>
      </div>
      <span class="pill">Dashboard</span>
    </div>
  </div>

  <div class="kpi-grid">
    <div class="kpi-card">
      <div class="kpi-label">العملاء</div>
      <div class="kpi-value"><?= e($stats['customers']) ?></div>
    </div>
    <div class="kpi-card">
      <div class="kpi-label">القطع</div>
      <div class="kpi-value"><?= e($stats['items']) ?></div>
    </div>
    <div class="kpi-card">
      <div class="kpi-label">الفواتير</div>
      <div class="kpi-value"><?= e($stats['invoices']) ?></div>
    </div>
    <div class="kpi-card">
      <div class="kpi-label">تذاكر الانتظار</div>
      <div class="kpi-value"><?= e($stats['queue_pending']) ?></div>
    </div>
  </div>

  <div class="dashboard-panels">
    <div class="panel-card">
      <div class="page-head">
        <div>
          <h3>آخر الفواتير</h3>
          <div class="muted">الحالات مترجمة ومميزة بالألوان لتسهيل القراءة السريعة.</div>
        </div>
      </div>

      <div class="invoice-table-wrap">
        <table class="table">
          <thead>
            <tr>
              <th>رقم الفاتورة</th>
              <th>العميل</th>
              <th>الحالة</th>
              <th>الإجمالي</th>
            </tr>
          </thead>
          <tbody>
            <?php if(!empty($recentInvoices)): ?>
              <?php foreach($recentInvoices as $row): ?>
                <?php
                  $statusKey = (string)($row['invoice_status'] ?? '');
                  $statusInfo = $statusMap[$statusKey] ?? ['label' => $statusKey ?: '-', 'class' => 'st-draft'];
                  $rowClass = $statusKey === 'closed' ? 'invoice-row-closed' : '';
                ?>
                <tr class="<?= e($rowClass) ?>">
                  <td>
                    <a class="invoice-no" href="<?= e(base_url('?page=invoice_show&id='.$row['id'])) ?>">
                      <?= e($row['invoice_no']) ?>
                    </a>
                  </td>
                  <td><?= e($row['customer_name']) ?></td>
                  <td>
                    <span class="invoice-status <?= e($statusInfo['class']) ?>">
                      <?= e($statusInfo['label']) ?>
                    </span>
                  </td>
                  <td><?= e(number_format((float)$row['grand_total'],3)) ?></td>
                </tr>
              <?php endforeach; ?>
            <?php else: ?>
              <tr><td colspan="4" class="empty-note">لا توجد فواتير حديثة حالياً.</td></tr>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>

    <div class="panel-card closed-block">
      <div class="page-head">
        <div>
          <h3>ملخص الحالات</h3>
          <div class="muted">فصل الفواتير المغلقة عن بقية الحالات لعرض أوضح.</div>
        </div>
      </div>

      <div class="closed-split">
        <div class="closed-box">
          <div class="label">الفواتير المفتوحة / غير المغلقة</div>
          <div class="value"><?= e((string)$openInvoices) ?></div>
        </div>
        <div class="closed-box">
          <div class="label">الفواتير المغلقة</div>
          <div class="value"><?= e((string)$closedInvoices) ?></div>
        </div>
      </div>

      <div style="margin-top:14px" class="muted">
        الحالات المعروضة:
        <span class="invoice-status st-confirmed">مؤكدة</span>
        <span class="invoice-status st-awaiting-return">بانتظار الإرجاع</span>
        <span class="invoice-status st-awaiting-settlement">بانتظار التصفية</span>
        <span class="invoice-status st-closed">مغلقة</span>
      </div>
    </div>
  </div>
</div>

<?php include __DIR__.'/../layout/footer.php'; ?>
