<?php $this->start('body'); ?>
<?php $suppliers = $this->data['suppliers']; $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>Supplier Statements</h5>
</div>

<p class="text-muted small mb-4">
  Pacific's ledger view for one supplier — every bill and payment in the window with a running balance
  and 5-bucket aging. Share this with a supplier when your balance doesn't match theirs so both sides
  can reconcile.
</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">Supplier <span class="text-danger">*</span></label>
        <select id="fSupplier" class="form-select" required>
          <option value="">— pick —</option>
          <?php foreach ($suppliers as $v): ?>
            <option value="<?= (int)$v['c0d']; ?>"><?= htmlspecialchars($v['vendor_name']); ?> <?php if ($v['vendor_no']): ?>· <?= htmlspecialchars($v['vendor_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 vid  = document.getElementById('fSupplier').value;
    const from = document.getElementById('fFrom').value;
    const to   = document.getElementById('fTo').value;
    if (!vid)  { alert('Pick a supplier.'); return null; }
    if (!from || !to) { alert('Pick a date range.'); return null; }
    return '<?= PROOT; ?>SupplierStatements/pdf/' + vid + '?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(); ?>
