<?php $this->start('body'); ?>
<?php $customers = $this->data['customers']; $from = $this->data['from']; $to = $this->data['to']; ?>

<div class="d-flex align-items-center justify-content-between mb-3">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-file-invoice me-2 text-muted"></i>Customer Statements</h5>
</div>

<p class="text-muted small mb-4">
  Generate a formal, brand-consistent statement of account for any customer,
  showing every invoice and receipt in the selected window with a running
  balance plus a 5-bucket aging strip.
</p>

<form method="GET" action="" onsubmit="return openStatement(event)" class="card border-0 shadow-sm">
  <div class="card-body">
    <div class="row g-3">
      <div class="col-md-6">
        <label class="form-label">Customer <span class="text-danger">*</span></label>
        <select id="fCustomer" class="form-select" required>
          <option value="">— pick —</option>
          <?php foreach ($customers as $c): ?>
            <option value="<?= (int)$c['c0d']; ?>"><?= htmlspecialchars($c['customer_name']); ?> <?php if ($c['customer_no']): ?>· <?= htmlspecialchars($c['customer_no']); ?><?php endif; ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-3">
        <label class="form-label">From</label>
        <input type="date" id="fFrom" class="form-control" value="<?= htmlspecialchars($from); ?>">
      </div>
      <div class="col-md-3">
        <label class="form-label">To</label>
        <input type="date" id="fTo" class="form-control" value="<?= htmlspecialchars($to); ?>">
      </div>
    </div>

    <div class="d-flex flex-wrap gap-2 mt-4">
      <span class="small text-muted me-2 align-self-center">Preset:</span>
      <?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')],
          'YTD'          => [$today->format('Y-01-01'),                      $today->format('Y-m-d')],
          'Last 90 days' => [$today->modify('-90 days')->format('Y-m-d'),    $today->format('Y-m-d')],
          'This year'    => [$today->format('Y-01-01'),                      $today->format('Y-12-31')],
        ];
        foreach ($presets as $lab => [$f, $t]):
      ?>
        <button type="button" class="btn btn-sm btn-outline-secondary" onclick="setRange('<?= $f; ?>', '<?= $t; ?>')"><?= $lab; ?></button>
      <?php endforeach; ?>
    </div>
  </div>
  <div class="card-footer bg-white d-flex justify-content-end gap-2 py-3">
    <button type="button" class="btn btn-outline-primary" id="btnPreview"><i class="fas fa-eye me-1"></i>Preview PDF</button>
    <button type="button" class="btn btn-primary" id="btnDownload"><i class="fas fa-download me-1"></i>Download PDF</button>
  </div>
</form>

<?php $this->end(); ?>
<?php $this->start('section_js'); ?>
<script>
function setRange(f, t) {
    document.getElementById('fFrom').value = f;
    document.getElementById('fTo').value   = t;
}

function build(dl) {
    const cid  = document.getElementById('fCustomer').value;
    const from = document.getElementById('fFrom').value;
    const to   = document.getElementById('fTo').value;
    if (!cid)  { alert('Pick a customer.'); return null; }
    if (!from || !to) { alert('Pick a date range.'); return null; }
    return '<?= PROOT; ?>CustomerStatements/pdf/' + cid + '?from=' + from + '&to=' + to + (dl ? '&dl=1' : '');
}

document.getElementById('btnPreview').addEventListener('click', () => {
    const u = build(false); if (u) window.open(u, '_blank');
});
document.getElementById('btnDownload').addEventListener('click', () => {
    const u = build(true);  if (u) window.location.href = u;
});

function openStatement(e) { e.preventDefault(); const u = build(false); if (u) window.open(u, '_blank'); return false; }
</script>
<?php $this->end(); ?>
