<?php
$account = current_account();
$user = current_user();
$invoices = $invoices ?? [];
$signs = $signs ?? [];
$orders = $orders ?? [];
$pages = $pages ?? [];
$hour = (int) date('G');
$hi = $hour < 12 ? 'Good morning' : ($hour < 17 ? 'Good afternoon' : 'Good evening');
$kpis = [];
if (can_see('invoices')) {
    $kpis[] = [(string) (int) ($invoiceCount ?? count($invoices)), 'Invoices', 'bi-receipt', '#2563eb', '#eff6ff', url('invoices')];
    $kpis[] = [(string) (int) ($unpaidCount ?? 0), 'Still due', 'bi-exclamation-circle-fill', '#ef4444', '#fef2f2', url('invoices')];
}
if (can_see('sign')) {
    $kpis[] = [(string) count($signs), 'To sign', 'bi-pen-fill', '#ea580c', '#fff7ed', url('sign')];
}
if (can_see('orders')) {
    $kpis[] = [(string) (int) ($orderCount ?? count($orders)), 'Orders', 'bi-bag-check-fill', '#0f766e', '#f0fdfa', url('orders')];
}
$jumpMeta = [
    'invoices' => ['Invoices', 'See bills and what is still due', 'bi-receipt', '#2563eb', '#eff6ff', 'invoices'],
    'documents' => ['Documents', 'Quotes, contracts, and files', 'bi-folder2-open', '#0f766e', '#f0fdfa', 'documents'],
    'sign' => ['Sign a paper', 'Finish a quote or contract', 'bi-pen', '#ea580c', '#fff7ed', 'sign'],
    'orders' => ['My orders', 'What your company bought', 'bi-bag-check', '#2563eb', '#eff6ff', 'orders'],
    'contract' => ['My contract', 'Dates and renewal', 'bi-file-earmark-ruled', '#7c3aed', '#f5f3ff', 'contract'],
    'service_request' => ['Service request', 'Ask for a visit or a fix', 'bi-tools', '#0f766e', '#f0fdfa', 'service'],
    'equipment' => ['My equipment', 'Machines at your site', 'bi-cpu', '#ea580c', '#fff7ed', 'equipment'],
];
?>
<div class="cpt-head">
  <div>
    <h1 class="cpt-title"><?= e($hi) ?>, <?= e($user['first_name'] ?? '') ?> <span aria-hidden="true">👋</span></h1>
    <p class="cpt-lead mb-0">You only see <?= e($account['name'] ?? 'your company') ?>. Staff deals and other customers stay hidden.</p>
  </div>
</div>

<?php if ($kpis): ?>
<div class="cpt-kpis<?= count($kpis) === 3 ? ' is-3' : '' ?>">
  <?php foreach ($kpis as $k): ?>
    <a class="cpt-kpi" href="<?= e($k[5]) ?>" style="--kpi-accent: <?= e($k[3]) ?>; --kpi-tint: <?= e($k[4]) ?>">
      <span class="cpt-kpi-ico"><i class="bi <?= e($k[2]) ?>"></i></span>
      <span>
        <span class="cpt-kpi-n"><?= e($k[0]) ?></span>
        <span class="cpt-kpi-l d-block"><?= e($k[1]) ?></span>
      </span>
    </a>
  <?php endforeach; ?>
</div>
<?php endif; ?>

<?php if ($pages): ?>
<div class="cpt-jumps">
  <?php foreach ($pages as $key):
      $j = $jumpMeta[$key] ?? null;
      if (!$j) {
          continue;
      }
  ?>
    <a class="cpt-jump" href="<?= e(url($j[5])) ?>" style="--kpi-accent: <?= e($j[3]) ?>; --kpi-tint: <?= e($j[4]) ?>">
      <span class="cpt-jump-ico"><i class="bi <?= e($j[2]) ?>"></i></span>
      <span><strong><?= e($j[0]) ?></strong><span><?= e($j[1]) ?></span></span>
    </a>
  <?php endforeach; ?>
</div>
<?php endif; ?>

<div class="row g-3">
  <?php if (can_see('invoices')): ?>
  <div class="col-lg-6">
    <div class="cpt-panel">
      <div class="cpt-panel-head">
        <div class="cpt-panel-ico"><i class="bi bi-receipt"></i></div>
        <div class="min-w-0 flex-grow-1">
          <h2>Invoices</h2>
          <p>Bills for your company</p>
        </div>
        <a href="<?= e(url('invoices')) ?>">View all</a>
      </div>
      <div class="cpt-panel-body">
        <?php if (!$invoices): ?>
          <p class="text-muted mb-0">No bills yet.</p>
        <?php else: foreach ($invoices as $r):
            $homeLeft = max(0, (float) $r['amount'] - (float) $r['paid_amount']);
            $st = cpt_status_label((string) $r['status']);
        ?>
          <div class="cpt-item cpt-tone-<?= e($st['tone']) ?>">
            <div class="cpt-item-main">
              <div class="cpt-item-ico"><i class="bi bi-receipt"></i></div>
              <div class="min-w-0 flex-grow-1">
                <div class="cpt-item-name"><?= e($r['number']) ?></div>
                <div class="cpt-item-sub">
                  <span>Invoice <?= e(money_fmt($r['amount'])) ?></span>
                  <span>Paid <?= e(money_fmt($r['paid_amount'])) ?></span>
                  <?php if ($homeLeft > 0.009): ?><span>Pending <?= e(money_fmt($homeLeft)) ?></span><?php endif; ?>
                </div>
              </div>
              <span class="<?= e($st['pill']) ?>"><?= e($st['text']) ?></span>
            </div>
          </div>
        <?php endforeach; endif; ?>
      </div>
    </div>
  </div>
  <?php endif; ?>
  <?php if (can_see('sign')): ?>
  <div class="col-lg-6">
    <div class="cpt-panel">
      <div class="cpt-panel-head is-orange">
        <div class="cpt-panel-ico"><i class="bi bi-pen"></i></div>
        <div class="min-w-0 flex-grow-1">
          <h2>Waiting for your signature</h2>
          <p><?= count($signs) ? count($signs) . ' paper' . (count($signs) === 1 ? '' : 's') . ' to finish' : 'Nothing to sign right now' ?></p>
        </div>
        <a href="<?= e(url('sign')) ?>">Sign</a>
      </div>
      <div class="cpt-panel-body">
        <?php if (!$signs): ?>
          <p class="text-muted mb-0">Nothing to sign.</p>
        <?php else: foreach ($signs as $r): ?>
          <div class="cpt-item cpt-tone-progress">
            <div class="cpt-item-main">
              <div class="cpt-item-ico"><i class="bi bi-pen"></i></div>
              <div class="min-w-0 flex-grow-1">
                <div class="cpt-item-name"><?= e($r['title']) ?></div>
              </div>
              <span class="cpt-pill is-pending">Waiting</span>
            </div>
          </div>
        <?php endforeach; endif; ?>
      </div>
    </div>
  </div>
  <?php endif; ?>
  <?php if (can_see('orders')): ?>
  <div class="col-lg-6">
    <div class="cpt-panel">
      <div class="cpt-panel-head is-green">
        <div class="cpt-panel-ico"><i class="bi bi-bag-check"></i></div>
        <div class="min-w-0 flex-grow-1">
          <h2>Recent orders</h2>
          <p>What your company bought</p>
        </div>
        <a href="<?= e(url('orders')) ?>">My orders</a>
      </div>
      <div class="cpt-panel-body">
        <?php if (!$orders): ?>
          <p class="text-muted mb-0">No orders yet.</p>
        <?php else: foreach ($orders as $r):
            $st = cpt_status_label((string) $r['status']);
        ?>
          <div class="cpt-item cpt-tone-<?= e($st['tone']) ?>">
            <div class="cpt-item-main">
              <div class="cpt-item-ico"><i class="bi bi-bag-check"></i></div>
              <div class="min-w-0 flex-grow-1">
                <div class="cpt-item-name"><?= e($r['title']) ?></div>
                <div class="cpt-item-sub">
                  <?php if (!empty($r['number'])): ?><span><?= e($r['number']) ?></span><?php endif; ?>
                  <?php if (!empty($r['ordered_at'])): ?><span><?= e(dt_fmt($r['ordered_at'])) ?></span><?php endif; ?>
                </div>
              </div>
              <div class="cpt-item-amt"><strong><?= e(money_fmt($r['amount'])) ?></strong></div>
              <span class="<?= e($st['pill']) ?>"><?= e($st['text']) ?></span>
            </div>
          </div>
        <?php endforeach; endif; ?>
      </div>
    </div>
  </div>
  <?php endif; ?>
</div>
