<?php
/**
 * Shared filter bar + export/print toolbar for every report. Expects:
 *   $show   — which filter fields this report accepts, e.g. ['date'=>true,'customer'=>true]
 *   $filters — dropdown option lists from ReportController::filterOptions()
 *   $query  — current GET params, to keep filters selected after a submit
 * Filters are a GET form so the result, and every export link, share one URL.
 */
$show = $show ?? [];
$currentPath = \App\Core\Request::path();
$qs = $query;
unset($qs['export'], $qs['view']);
$baseQuery = http_build_query($qs);
?>
<form method="get" class="filter-bar toolbar" style="margin-bottom:14px">
  <?php if (!empty($show['date'])) : ?>
    <label class="text-sm text-faint mb-0">From <input type="date" name="from" value="<?= e($query['from'] ?? '') ?>" style="width:auto"></label>
    <label class="text-sm text-faint mb-0">To <input type="date" name="to" value="<?= e($query['to'] ?? '') ?>" style="width:auto"></label>
  <?php endif; ?>
  <?php if (!empty($show['customer'])) : ?>
    <select name="customer_id">
      <option value="">All customers</option>
      <?php foreach ($filters['customers'] as $c) : ?>
        <option value="<?= $c['id'] ?>" <?= (string)($query['customer_id'] ?? '') === (string)$c['id'] ? 'selected' : '' ?>><?= e($c['name']) ?></option>
      <?php endforeach; ?>
    </select>
  <?php endif; ?>
  <?php if (!empty($show['priority'])) : ?>
    <select name="priority_id">
      <option value="">All priorities</option>
      <?php foreach ($filters['priorities'] as $p) : ?>
        <option value="<?= $p['id'] ?>" <?= (string)($query['priority_id'] ?? '') === (string)$p['id'] ? 'selected' : '' ?>><?= e($p['code']) ?> — <?= e($p['name']) ?></option>
      <?php endforeach; ?>
    </select>
  <?php endif; ?>
  <?php if (!empty($show['queue'])) : ?>
    <select name="queue_id">
      <option value="">All queues</option>
      <?php foreach ($filters['queues'] as $q) : ?>
        <option value="<?= $q['id'] ?>" <?= (string)($query['queue_id'] ?? '') === (string)$q['id'] ? 'selected' : '' ?>><?= e($q['name']) ?></option>
      <?php endforeach; ?>
    </select>
  <?php endif; ?>
  <?php if (!empty($show['agent'])) : ?>
    <select name="agent_id">
      <option value="">All agents</option>
      <?php foreach ($filters['agents'] as $a) : ?>
        <option value="<?= $a['id'] ?>" <?= (string)($query['agent_id'] ?? '') === (string)$a['id'] ? 'selected' : '' ?>><?= e($a['name']) ?></option>
      <?php endforeach; ?>
    </select>
  <?php endif; ?>
  <?php if (!empty($show['tier'])) : ?>
    <select name="tier">
      <option value="">All tiers</option>
      <?php foreach (['Standard', 'Gold', 'Platinum'] as $t) : ?>
        <option <?= ($query['tier'] ?? '') === $t ? 'selected' : '' ?>><?= $t ?></option>
      <?php endforeach; ?>
    </select>
  <?php endif; ?>
  <?php if (!empty($show['minRating'])) : ?>
    <select name="min_rating">
      <option value="">Any rating</option>
      <?php for ($r = 5; $r >= 1; $r--) : ?>
        <option value="<?= $r ?>" <?= (string)($query['min_rating'] ?? '') === (string)$r ? 'selected' : '' ?>><?= $r ?>★ and up</option>
      <?php endfor; ?>
    </select>
  <?php endif; ?>
  <button type="submit" class="btn btn-sm">Apply filters</button>
  <?php if ($baseQuery !== '') : ?><a href="<?= url($currentPath) ?>" class="btn btn-sm btn-ghost">Clear</a><?php endif; ?>

  <span style="flex:1"></span>
  <a href="<?= url($currentPath) ?>?<?= $baseQuery ? $baseQuery . '&' : '' ?>view=print" target="_blank" class="btn btn-sm">🖨 Print view</a>
  <a href="<?= url($currentPath) ?>?<?= $baseQuery ? $baseQuery . '&' : '' ?>export=pdf" class="btn btn-sm">⬇ PDF</a>
  <a href="<?= url($currentPath) ?>?<?= $baseQuery ? $baseQuery . '&' : '' ?>export=xlsx" class="btn btn-sm">⬇ Excel</a>
  <a href="<?= url($currentPath) ?>?<?= $baseQuery ? $baseQuery . '&' : '' ?>export=csv" class="btn btn-sm">⬇ CSV</a>
</form>
