<div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-3">
  <ul class="nav ab-tabs" id="abReportTabs">
    <?php foreach (['overview'=>'Overview','leads'=>'Leads','customers'=>'Customers','revenue'=>'Revenue','marketing'=>'Marketing','appointments'=>'Appointments','ai'=>'AI','staff'=>'Staff Performance','system_logs'=>'System Logs'] as $key => $label): ?>
    <li class="nav-item"><a class="nav-link <?= $type===$key?'active':'' ?>" href="<?= base_url('reports') ?>?type=<?= $key ?>&days=<?= $days ?>"><?= e($label) ?></a></li>
    <?php endforeach; ?>
  </ul>
  <div class="d-flex gap-2">
    <select class="form-select form-select-sm" id="abReportDays" style="width:auto;">
      <option value="7" <?= $days==7?'selected':'' ?>>Last 7 days</option>
      <option value="30" <?= $days==30?'selected':'' ?>>Last 30 days</option>
      <option value="90" <?= $days==90?'selected':'' ?>>Last 90 days</option>
    </select>
    <a href="<?= base_url('reports/' . $type . '/export/csv') ?>?days=<?= $days ?>" class="btn btn-light-soft btn-sm"><i class="bi bi-filetype-csv me-1"></i>CSV</a>
    <a href="<?= base_url('reports/' . $type . '/export/excel') ?>?days=<?= $days ?>" class="btn btn-light-soft btn-sm"><i class="bi bi-file-earmark-excel me-1"></i>Excel</a>
    <a href="<?= base_url('reports/' . $type . '/export/pdf') ?>?days=<?= $days ?>" class="btn btn-light-soft btn-sm"><i class="bi bi-file-earmark-pdf me-1"></i>PDF</a>
    <button class="btn btn-light-soft btn-sm" onclick="window.print()"><i class="bi bi-printer me-1"></i>Print</button>
  </div>
</div>

<div id="abReportContent">
<?php if ($type === 'overview'): ?>
  <div class="row g-3">
    <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Revenue</div><div class="ab-stat-value"><?= e(money($report['revenue']['total_revenue'])) ?></div></div></div>
    <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">New Leads</div><div class="ab-stat-value"><?= array_sum(array_column($report['leads_pipeline']['stages'], 'count')) ?></div></div></div>
    <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Appointments</div><div class="ab-stat-value"><?= array_sum(array_column($report['appointments']['by_status'], 'count')) ?></div></div></div>
    <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Conversations</div><div class="ab-stat-value"><?= array_sum(array_column($report['conversations']['by_channel'], 'total')) ?></div></div></div>
  </div>

<?php elseif ($type === 'leads'): ?>
  <div class="ab-table-card"><div class="table-responsive"><table class="table ab-table">
    <thead><tr><th>Stage</th><th>Count</th><th>Value</th></tr></thead>
    <tbody><?php foreach ($report['stages'] as $s): ?><tr><td><?= e($s['stage']) ?></td><td><?= (int) $s['count'] ?></td><td class="ab-mono"><?= e(money($s['total_value'])) ?></td></tr><?php endforeach; ?></tbody>
  </table></div></div>

<?php elseif ($type === 'customers'): ?>
  <div class="row g-3 mb-3"><div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">New Customers</div><div class="ab-stat-value"><?= (int) $report['new_customers'] ?></div></div></div></div>
  <div class="ab-table-card"><div class="table-responsive"><table class="table ab-table">
    <thead><tr><th>Status</th><th>Count</th><th>Lifetime Value</th></tr></thead>
    <tbody><?php foreach ($report['by_status'] as $s): ?><tr><td><?= e(ucfirst($s['status'])) ?></td><td><?= (int) $s['count'] ?></td><td class="ab-mono"><?= e(money($s['total_lifetime_value'])) ?></td></tr><?php endforeach; ?>
    <?php if (empty($report['by_status'])): ?><tr><td colspan="3" class="text-center p-3 ab-text-muted">No customers yet.</td></tr><?php endif; ?></tbody>
  </table></div></div>

<?php elseif ($type === 'revenue'): ?>
  <div class="row g-3"><div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Total Revenue</div><div class="ab-stat-value"><?= e(money($report['total_revenue'])) ?></div></div></div>
  <div class="col-md-3"><div class="ab-stat-card"><div class="ab-stat-label">Invoices Paid</div><div class="ab-stat-value"><?= (int) $report['invoices_paid'] ?></div></div></div></div>

<?php elseif ($type === 'marketing'): ?>
  <div class="row g-3 mb-3">
    <div class="col-md-2"><div class="ab-stat-card"><div class="ab-stat-label">Campaigns</div><div class="ab-stat-value"><?= (int) $report['totals']['total_campaigns'] ?></div></div></div>
    <div class="col-md-2"><div class="ab-stat-card"><div class="ab-stat-label">Completed</div><div class="ab-stat-value"><?= (int) $report['totals']['completed_campaigns'] ?></div></div></div>
    <div class="col-md-2"><div class="ab-stat-card"><div class="ab-stat-label">Recipients</div><div class="ab-stat-value"><?= (int) $report['totals']['total_recipients'] ?></div></div></div>
    <div class="col-md-2"><div class="ab-stat-card"><div class="ab-stat-label">Opens</div><div class="ab-stat-value"><?= (int) $report['totals']['total_opens'] ?></div></div></div>
    <div class="col-md-2"><div class="ab-stat-card"><div class="ab-stat-label">Clicks</div><div class="ab-stat-value"><?= (int) $report['totals']['total_clicks'] ?></div></div></div>
    <div class="col-md-2"><div class="ab-stat-card"><div class="ab-stat-label">Unsubscribes</div><div class="ab-stat-value"><?= (int) $report['totals']['total_unsubscribes'] ?></div></div></div>
  </div>
  <div class="ab-table-card"><div class="table-responsive"><table class="table ab-table">
    <thead><tr><th>Campaign</th><th>Recipients</th><th>Opens</th><th>Clicks</th><th>Sent</th></tr></thead>
    <tbody><?php foreach ($report['recent_campaigns'] as $c): ?><tr><td><?= e($c['name']) ?></td><td><?= (int) $c['recipients_count'] ?></td><td><?= (int) $c['opens_count'] ?></td><td><?= (int) $c['clicks_count'] ?></td><td class="ab-cell-sub"><?= e(date('M j, Y', strtotime($c['sent_at']))) ?></td></tr><?php endforeach; ?>
    <?php if (empty($report['recent_campaigns'])): ?><tr><td colspan="5" class="text-center p-3 ab-text-muted">No campaigns sent yet.</td></tr><?php endif; ?></tbody>
  </table></div></div>

<?php elseif ($type === 'appointments'): ?>
  <div class="ab-table-card"><div class="table-responsive"><table class="table ab-table">
    <thead><tr><th>Status</th><th>Count</th></tr></thead>
    <tbody><?php foreach ($report['by_status'] as $a): ?><tr><td><?= e(ucfirst(str_replace('_',' ',$a['status']))) ?></td><td><?= (int) $a['count'] ?></td></tr><?php endforeach; ?>
    <?php if (empty($report['by_status'])): ?><tr><td colspan="2" class="text-center p-3 ab-text-muted">No appointments in this period.</td></tr><?php endif; ?></tbody>
  </table></div></div>

<?php elseif ($type === 'ai'): ?>
  <div class="row g-3 mb-3">
    <div class="col-md-4"><div class="ab-stat-card"><div class="ab-stat-label">Automation Runs</div><div class="ab-stat-value"><?= (int) $report['automation_runs']['total_runs'] ?></div></div></div>
    <div class="col-md-4"><div class="ab-stat-card"><div class="ab-stat-label">Completed</div><div class="ab-stat-value"><?= (int) $report['automation_runs']['completed'] ?></div></div></div>
    <div class="col-md-4"><div class="ab-stat-card"><div class="ab-stat-label">Failed</div><div class="ab-stat-value"><?= (int) $report['automation_runs']['failed'] ?></div></div></div>
  </div>
  <div class="ab-table-card"><div class="table-responsive"><table class="table ab-table">
    <thead><tr><th>Channel</th><th>Total</th><th>Leads Captured</th><th>Booked</th><th>Escalated</th></tr></thead>
    <tbody><?php foreach ($report['conversations'] as $c): ?><tr><td><?= e(ucwords(str_replace('_',' ',$c['channel']))) ?></td><td><?= (int) $c['total'] ?></td><td><?= (int) $c['leads_captured'] ?></td><td><?= (int) $c['appointments_booked'] ?></td><td><?= (int) $c['escalated_to_human'] ?></td></tr><?php endforeach; ?>
    <?php if (empty($report['conversations'])): ?><tr><td colspan="5" class="text-center p-3 ab-text-muted">No conversations in this period.</td></tr><?php endif; ?></tbody>
  </table></div></div>

<?php elseif ($type === 'staff'): ?>
  <div class="ab-table-card"><div class="table-responsive"><table class="table ab-table">
    <thead><tr><th>Staff</th><th>Leads Owned</th><th>Leads Won</th><th>Appointments Completed</th><th>Conversations Handled</th></tr></thead>
    <tbody><?php foreach ($report['staff'] as $s): ?><tr><td class="ab-cell-primary"><?= e($s['name']) ?></td><td><?= (int) $s['leads_owned'] ?></td><td><?= (int) $s['leads_won'] ?></td><td><?= (int) $s['appointments_completed'] ?></td><td><?= (int) $s['conversations_handled'] ?></td></tr><?php endforeach; ?></tbody>
  </table></div></div>

<?php elseif ($type === 'system_logs'): ?>
  <div class="ab-table-card"><div class="table-responsive"><table class="table ab-table">
    <thead><tr><th>Category</th><th>Count</th></tr></thead>
    <tbody><?php foreach ($report['by_category'] as $c): ?><tr><td><?= e(ucfirst($c['category'])) ?></td><td><?= (int) $c['count'] ?></td></tr><?php endforeach; ?>
    <?php if (empty($report['by_category'])): ?><tr><td colspan="2" class="text-center p-3 ab-text-muted">No activity in this period.</td></tr><?php endif; ?></tbody>
  </table></div>
  <div class="text-end mt-2"><a href="<?= base_url('activity-logs') ?>" class="small fw-semibold">View full Activity Log <i class="bi bi-arrow-right"></i></a></div></div>
<?php endif; ?>
</div>

<?php if ($type === 'overview'): ?>
<div class="alert alert-light border mt-3 small no-print" style="border-radius:12px;"><i class="bi bi-robot text-primary me-1"></i>This exact data is also available to the AI Assistant via its <code>generate_report</code> tool.</div>
<?php endif; ?>

<script>
document.getElementById('abReportDays').addEventListener('change', function () {
  var p = new URLSearchParams(window.location.search);
  p.set('days', this.value);
  window.location.search = p.toString();
});
</script>
