<?php
$forecast = $forecast ?? [];
$byStage = $byStage ?? [];
$byRep = $byRep ?? [];
$panel = $industryPanel ?? null;
$openDeals = 0;
foreach ($byStage as $s) {
    $openDeals += (int) ($s['deal_count'] ?? 0);
}
?>
<div class="svc-page">
  <div class="mb-3">
    <h1 class="page-title">Reports</h1>
    <p class="page-sub mb-0">Pipeline health and a simple forecast. No extra report to run.</p>
  </div>

  <div class="task-summary mb-3">
    <?php foreach ([
        [money_fmt($forecast['pipeline_value'] ?? 0), 'Open pipeline', 'bi-diagram-3-fill', '#2563eb', '#eff6ff'],
        [money_fmt($forecast['weighted_value'] ?? 0), 'Weighted forecast', 'bi-graph-up-arrow', '#7c3aed', '#f5f3ff'],
        [money_fmt($forecast['won_this_month'] ?? 0), 'Won this month', 'bi-trophy-fill', '#16a34a', '#f0fdf4'],
        [(string) $openDeals, opportunity_label(), 'bi-briefcase-fill', '#ea580c', '#fff7ed'],
    ] as $s): ?>
      <div class="task-summary-card" style="--task-accent: <?= e($s[3]) ?>; --task-tint: <?= e($s[4]) ?>">
        <div class="task-summary-icon"><i class="bi <?= e($s[2]) ?>"></i></div>
        <div>
          <div class="task-summary-n"><?= e((string) $s[0]) ?></div>
          <div class="task-summary-l"><?= e($s[1]) ?></div>
        </div>
      </div>
    <?php endforeach; ?>
  </div>

  <div class="row g-3">
    <div class="col-lg-6">
      <div class="card-soft rec-card p-3">
        <?php \App\Core\View::partial('record_section_head', [
            'icon' => 'bi-layers',
            'title' => 'By stage',
            'hint' => 'Open ' . strtolower(opportunity_label()) . ' in the pipeline.',
        ]); ?>
        <table class="table table-soft mb-0">
          <thead><tr><th>Stage</th><th><?= e(opportunity_label()) ?></th><th>Value</th></tr></thead>
          <tbody>
          <?php if (!$byStage): ?>
            <tr><td colspan="3" class="text-muted">Nothing in the pipeline yet.</td></tr>
          <?php endif; ?>
          <?php foreach ($byStage as $s): ?>
            <tr><td><?= e($s['name']) ?></td><td><?= (int) $s['deal_count'] ?></td><td><?= money_fmt($s['deal_value']) ?></td></tr>
          <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
    <div class="col-lg-6">
      <div class="card-soft rec-card p-3">
        <?php \App\Core\View::partial('record_section_head', [
            'icon' => 'bi-people',
            'title' => 'By salesperson',
            'hint' => 'Who owns the pipeline.',
        ]); ?>
        <table class="table table-soft mb-0">
          <thead><tr><th>Person</th><th><?= e(opportunity_label()) ?></th><th>Pipeline</th><th>Won</th></tr></thead>
          <tbody>
          <?php if (!$byRep): ?>
            <tr><td colspan="4" class="text-muted">No salesperson totals yet.</td></tr>
          <?php endif; ?>
          <?php foreach ($byRep as $r): ?>
            <tr><td><?= e($r['owner_name']) ?></td><td><?= (int) $r['deal_count'] ?></td><td><?= money_fmt($r['deal_value']) ?></td><td><?= money_fmt($r['won_value']) ?></td></tr>
          <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>

  <?php if ($panel): ?>
  <div class="card-soft rec-card p-3 mt-3">
    <?php \App\Core\View::partial('record_section_head', [
        'icon' => 'bi-clipboard-data',
        'title' => (string) ($panel['title'] ?? 'Industry'),
        'hint' => (string) ($panel['hint'] ?? ''),
    ]); ?>
    <?php if (empty($panel['rows'])): ?>
      <p class="rec-section-hint mb-0">Nothing to show for this setup yet.</p>
    <?php elseif (($panel['code'] ?? '') === 'distribution'): ?>
      <table class="table table-soft mb-0"><thead><tr><th>Account</th><th>Last order</th><th>Cycle</th><th>Owner</th></tr></thead><tbody>
      <?php foreach ($panel['rows'] as $r): ?>
        <tr><td><a href="<?= e(url('accounts/'.$r['id'])) ?>"><?= e($r['name']) ?></a></td><td><?= e($r['last_order_at']) ?></td><td><?= (int)$r['reorder_cycle_days'] ?> days</td><td><?= e($r['owner_name']) ?></td></tr>
      <?php endforeach; ?>
      </tbody></table>
    <?php elseif (($panel['code'] ?? '') === 'software'): ?>
      <table class="table table-soft mb-0"><thead><tr><th>Account</th><th>Renewal</th><th>Owner</th></tr></thead><tbody>
      <?php foreach ($panel['rows'] as $r): ?>
        <tr><td><a href="<?= e(url('accounts/'.$r['id'])) ?>"><?= e($r['name']) ?></a></td><td><?= e($r['renewal_date']) ?></td><td><?= e($r['owner_name']) ?></td></tr>
      <?php endforeach; ?>
      </tbody></table>
    <?php elseif (($panel['code'] ?? '') === 'contract'): ?>
      <table class="table table-soft mb-0"><thead><tr><th>Bid</th><th>Account</th><th>Stage</th><th>Amount</th></tr></thead><tbody>
      <?php foreach ($panel['rows'] as $r): ?>
        <tr><td><a href="<?= e(url('opportunities/'.$r['id'])) ?>"><?= e($r['name']) ?></a></td><td><?= e($r['account_name']) ?></td><td><?= e($r['stage_name']) ?></td><td><?= money_fmt($r['amount']) ?></td></tr>
      <?php endforeach; ?>
      </tbody></table>
    <?php else: ?>
      <table class="table table-soft mb-0"><thead><tr><th>Account</th><th>Status</th><th>Assigned</th></tr></thead><tbody>
      <?php foreach ($panel['rows'] as $r):
          $st = visit_status_label($r);
      ?>
        <tr><td><a href="<?= e(url('service/'.$r['id'])) ?>"><?= e($r['name']) ?></a></td><td><?= e($st['text']) ?></td><td><?= e($r['assignee_name'] ?: '—') ?></td></tr>
      <?php endforeach; ?>
      </tbody></table>
    <?php endif; ?>
  </div>
  <?php endif; ?>
</div>
