<?php
/**
 * Shared date-range picker for TB / P&L / ledger.
 * BS uses a single "as of" date and has its own inline picker.
 *
 * Expects $from and $to in the view data, and a $formAction attribute
 * set on the wrapping div via data-action.
 */
$currentAction = htmlspecialchars($this->data['form_action'] ?? '');
$from = htmlspecialchars($this->data['from'] ?? date('Y-m-01'));
$to   = htmlspecialchars($this->data['to']   ?? date('Y-m-t'));
?>
<form method="GET" action="<?= $currentAction; ?>" 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">From</label>
      <input type="date" name="from" value="<?= $from; ?>" class="form-control form-control-sm">
    </div>
    <div>
      <label class="form-label small mb-1 text-uppercase text-muted">To</label>
      <input type="date" name="to" value="<?= $to; ?>" class="form-control form-control-sm">
    </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
        $today = new DateTimeImmutable('today');
        $presets = [
          'This month' => [$today->format('Y-m-01'), $today->format('Y-m-t')],
          'Last month' => [$today->modify('first day of last month')->format('Y-m-d'), $today->modify('last day of last month')->format('Y-m-d')],
          'This year'  => [$today->format('Y-01-01'), $today->format('Y-12-31')],
          'YTD'        => [$today->format('Y-01-01'), $today->format('Y-m-d')],
        ];
        foreach ($presets as $label => [$f, $t]):
      ?>
        <a class="btn btn-sm btn-outline-secondary"
           href="<?= $currentAction; ?>?from=<?= $f; ?>&to=<?= $t; ?>"><?= $label; ?></a>
      <?php endforeach; ?>
    </div>
  </div>
</form>
