<?php
/**
 * Shared month picker used by PAYE / VAT / Statutory report views.
 * Expects $year, $month, and $form_action on the view data map.
 */
$action = htmlspecialchars($this->data['form_action'] ?? '');
$year   = (int)$this->data['year'];
$month  = (int)$this->data['month'];

$monthNames = [
    1=>'January', 2=>'February', 3=>'March', 4=>'April', 5=>'May', 6=>'June',
    7=>'July', 8=>'August', 9=>'September', 10=>'October', 11=>'November', 12=>'December',
];
?>
<form method="GET" action="<?= $action; ?>" class="card border-0 shadow-sm mb-3">
  <div class="card-body py-2 d-flex flex-wrap gap-2 align-items-end">
    <div>
      <label class="form-label small mb-1 text-uppercase text-muted">Month</label>
      <select name="month" class="form-select form-select-sm">
        <?php foreach ($monthNames as $k => $v): ?>
          <option value="<?= $k; ?>" <?= $k === $month ? 'selected' : ''; ?>><?= $v; ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div>
      <label class="form-label small mb-1 text-uppercase text-muted">Year</label>
      <select name="year" class="form-select form-select-sm">
        <?php $now = (int)date('Y'); for ($y = $now + 1; $y >= $now - 5; $y--): ?>
          <option value="<?= $y; ?>" <?= $y === $year ? 'selected' : ''; ?>><?= $y; ?></option>
        <?php endfor; ?>
      </select>
    </div>
    <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-sync me-1"></i>Refresh</button>

    <div class="ms-auto d-flex gap-1">
      <?php
        $last = strtotime('first day of last month');
        $ly = (int)date('Y', $last); $lm = (int)date('n', $last);
        $tm = (int)date('n');       $ty = (int)date('Y');
        $prev = strtotime(sprintf('%04d-%02d-01', $year, $month) . ' -1 month');
        $py = (int)date('Y', $prev); $pm = (int)date('n', $prev);
        $presets = [
          'Last month' => [$ly, $lm],
          'This month' => [$ty, $tm],
          'Prev'       => [$py, $pm],
        ];
        foreach ($presets as $lab => [$Y, $M]):
      ?>
        <a class="btn btn-sm btn-outline-secondary" href="<?= $action; ?>?year=<?= $Y; ?>&month=<?= $M; ?>"><?= $lab; ?></a>
      <?php endforeach; ?>
    </div>
  </div>
</form>
