<?php
/**
 * Bilah filter standar untuk daftar piutang / invoice / aging.
 * @var array  $filters
 * @var array  $opsi
 * @var string $action
 * @var array  $tampilkan  daftar filter yang ditampilkan
 */
$tampilkan = $tampilkan ?? ['cari', 'area', 'bu', 'sales', 'co', 'jobcat', 'revcat', 'status', 'tanggal', 'jatuh_tempo', 'outstanding'];
$show = fn(string $k) => in_array($k, $tampilkan, true);
$sel = function ($key, $val) use ($filters) { return (string)($filters[$key] ?? '') === (string)$val ? 'selected' : ''; };
?>
<form class="filter-bar" method="get" action="<?= attr($action ?? urlCurrent()) ?>">
  <?php foreach (($hidden ?? []) as $hk => $hv): ?>
    <input type="hidden" name="<?= attr($hk) ?>" value="<?= attr($hv) ?>">
  <?php endforeach; ?>
  <div class="row g-2 align-items-end">

    <?php if ($show('cari')): ?>
      <div class="col-12 col-md-4 col-lg-3">
        <label class="form-label" for="ft_cari">Pencarian</label>
        <input type="search" class="form-control form-control-sm" id="ft_cari" name="cari_teks"
               value="<?= attr($filters['cari_teks'] ?? '') ?>" placeholder="No. invoice, faktur pajak, SO, customer, outlet…">
      </div>
    <?php endif; ?>

    <?php if ($show('area')): ?>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_area">Area</label>
        <select class="form-select form-select-sm" id="ft_area" name="area_id">
          <option value="">Semua Area</option>
          <?php foreach ($opsi['area'] as $o): ?>
            <option value="<?= (int)$o['id'] ?>" <?= $sel('area_id', $o['id']) ?>><?= e($o['name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
    <?php endif; ?>

    <?php if ($show('bu')): ?>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_bu">Unit Bisnis</label>
        <select class="form-select form-select-sm" id="ft_bu" name="business_unit_id">
          <option value="">Semua Unit</option>
          <?php foreach ($opsi['business_unit'] as $o): ?>
            <option value="<?= (int)$o['id'] ?>" <?= $sel('business_unit_id', $o['id']) ?>><?= e($o['name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
    <?php endif; ?>

    <?php if ($show('sales')): ?>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_sales">Sales</label>
        <select class="form-select form-select-sm" id="ft_sales" name="salesperson_id">
          <option value="">Semua Sales</option>
          <?php foreach ($opsi['salesperson'] as $o): ?>
            <option value="<?= (int)$o['id'] ?>" <?= $sel('salesperson_id', $o['id']) ?>><?= e($o['name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
    <?php endif; ?>

    <?php if ($show('co')): ?>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_co">Petugas Penagihan</label>
        <select class="form-select form-select-sm" id="ft_co" name="collection_officer_id">
          <option value="">Semua Petugas</option>
          <?php foreach ($opsi['collection_officer'] as $o): ?>
            <option value="<?= (int)$o['id'] ?>" <?= $sel('collection_officer_id', $o['id']) ?>><?= e($o['name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
    <?php endif; ?>

    <?php if ($show('jobcat')): ?>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_job">Kategori Pekerjaan</label>
        <select class="form-select form-select-sm" id="ft_job" name="job_category_id">
          <option value="">Semua Kategori</option>
          <?php foreach ($opsi['job_category'] as $o): ?>
            <option value="<?= (int)$o['id'] ?>" <?= $sel('job_category_id', $o['id']) ?>><?= e($o['name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
    <?php endif; ?>

    <?php if ($show('revcat')): ?>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_rev">PMSC / By Case</label>
        <select class="form-select form-select-sm" id="ft_rev" name="revenue_category_id">
          <option value="">Semua</option>
          <?php foreach ($opsi['revenue_category'] as $o): ?>
            <option value="<?= (int)$o['id'] ?>" <?= $sel('revenue_category_id', $o['id']) ?>><?= e($o['name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
    <?php endif; ?>

    <?php if ($show('status')): ?>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_status">Status Pembayaran</label>
        <select class="form-select form-select-sm" id="ft_status" name="payment_status_code">
          <option value="">Semua Status</option>
          <?php foreach ($opsi['payment_status'] as $o): ?>
            <option value="<?= attr($o['id']) ?>" <?= $sel('payment_status_code', $o['id']) ?>><?= e($o['name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
    <?php endif; ?>

    <?php if ($show('doc_status')): ?>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_doc">Status Dokumen</label>
        <select class="form-select form-select-sm" id="ft_doc" name="doc_status">
          <option value="">Semua Dokumen</option>
          <?php foreach (['active' => 'Aktif', 'cancelled' => 'Dibatalkan', 'replaced' => 'Diganti',
                          'revised' => 'Direvisi', 'written_off' => 'Dihapuskan'] as $k => $v): ?>
            <option value="<?= attr($k) ?>" <?= $sel('doc_status', $k) ?>><?= e($v) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
    <?php endif; ?>

    <?php if ($show('tanggal')): ?>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_d1">Tgl Invoice Dari</label>
        <input type="date" class="form-control form-control-sm" id="ft_d1" name="invoice_date_from" value="<?= attr($filters['invoice_date_from'] ?? '') ?>">
      </div>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_d2">Tgl Invoice Sampai</label>
        <input type="date" class="form-control form-control-sm" id="ft_d2" name="invoice_date_to" value="<?= attr($filters['invoice_date_to'] ?? '') ?>">
      </div>
    <?php endif; ?>

    <?php if ($show('jatuh_tempo')): ?>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_jt1">Jatuh Tempo Dari</label>
        <input type="date" class="form-control form-control-sm" id="ft_jt1" name="due_date_from" value="<?= attr($filters['due_date_from'] ?? '') ?>">
      </div>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_jt2">Jatuh Tempo Sampai</label>
        <input type="date" class="form-control form-control-sm" id="ft_jt2" name="due_date_to" value="<?= attr($filters['due_date_to'] ?? '') ?>">
      </div>
    <?php endif; ?>

    <?php if ($show('asof')): ?>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_asof">Tanggal Pelaporan</label>
        <input type="date" class="form-control form-control-sm" id="ft_asof" name="tanggal_lapor" value="<?= attr($asOf ?? '') ?>">
      </div>
    <?php endif; ?>

    <?php if ($show('nilai')): ?>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label" for="ft_nmin">Nilai Minimal</label>
        <input type="text" class="form-control form-control-sm" id="ft_nmin" name="nilai_min" data-uang value="<?= attr($filters['nilai_min'] ?? '') ?>">
      </div>
    <?php endif; ?>

    <?php if ($show('outstanding')): ?>
      <div class="col-6 col-md-3 col-lg-2">
        <label class="form-label d-block">Tampilkan</label>
        <div class="form-check form-check-inline">
          <input class="form-check-input" type="checkbox" id="ft_out" name="hanya_outstanding" value="1" <?= !empty($filters['hanya_outstanding']) ? 'checked' : '' ?>>
          <label class="form-check-label small" for="ft_out">Hanya bersaldo</label>
        </div>
      </div>
    <?php endif; ?>

    <div class="col-12 col-lg-auto d-flex gap-2 ms-lg-auto">
      <button class="btn btn-primary btn-sm" type="submit"><i class="bi bi-funnel me-1"></i>Terapkan Filter</button>
      <a class="btn btn-light btn-sm" href="<?= attr($action ?? urlCurrent([], array_keys($filters))) ?>"><i class="bi bi-arrow-counterclockwise me-1"></i>Reset</a>
    </div>
  </div>
</form>
