<?php $page_title = 'Kasir Rawat Jalan'; ?>
<?php $this->load->view('kasir/_nav', compact('page_title')); ?>

<div class="row">
  <div class="col-md-3">
    <div class="small-box bg-info">
      <div class="inner">
        <h3><?= (int) $summary['total_tagihan']; ?></h3>
        <p>Total Tagihan</p>
      </div>
      <div class="icon"><i class="fas fa-file-invoice-dollar"></i></div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="small-box bg-warning">
      <div class="inner">
        <h3><?= (int) $summary['menunggu']; ?></h3>
        <p>Belum Lunas</p>
      </div>
      <div class="icon"><i class="fas fa-hourglass-half"></i></div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="small-box bg-success">
      <div class="inner">
        <h3><?= (int) $summary['lunas']; ?></h3>
        <p>Sudah Lunas</p>
      </div>
      <div class="icon"><i class="fas fa-check-circle"></i></div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="small-box bg-primary">
      <div class="inner">
        <h3>Rp <?= number_format((float) $summary['nilai_tagihan'], 0, ',', '.'); ?></h3>
        <p>Nilai Tagihan</p>
      </div>
      <div class="icon"><i class="fas fa-wallet"></i></div>
    </div>
  </div>
</div>

<div class="card">
  <div class="card-header">Filter Tagihan</div>
  <div class="card-body">
    <form method="get">
      <div class="form-row">
        <div class="col-md-3 mb-2">
          <label class="small">Tanggal</label>
          <input type="date" name="tanggal" value="<?= html_escape($filters['tanggal']); ?>" class="form-control">
        </div>
        <div class="col-md-3 mb-2">
          <label class="small">Status</label>
          <select name="status" class="form-control">
            <?php foreach (['all' => 'Semua', 'MENUNGGU' => 'Belum Lunas', 'LUNAS' => 'Lunas'] as $value => $label): ?>
              <option value="<?= $value; ?>" <?= $filters['status'] === $value ? 'selected' : ''; ?>><?= $label; ?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-4 mb-2">
          <label class="small">Cari No Tagihan / RM / Pasien / Poli</label>
          <input type="text" name="keyword" value="<?= html_escape($filters['keyword']); ?>" class="form-control" placeholder="Cari data tagihan">
        </div>
        <div class="col-md-2 mb-2 d-flex align-items-end">
          <button class="btn btn-primary btn-block"><i class="fas fa-filter"></i> Terapkan</button>
        </div>
      </div>
    </form>
  </div>
</div>

<div class="card">
  <div class="card-header">Daftar Tagihan Dummy</div>
  <div class="card-body table-responsive">
    <table class="table table-bordered table-striped">
      <thead>
        <tr>
          <th>No. Tagihan</th>
          <th>No. RM</th>
          <th>Pasien</th>
          <th>Poli</th>
          <th>Pembayaran</th>
          <th>Total</th>
          <th>Status</th>
          <th>Aksi</th>
        </tr>
      </thead>
      <tbody>
        <?php if (!empty($tagihan)): ?>
          <?php foreach ($tagihan as $row): ?>
            <tr>
              <td>
                <strong><?= html_escape($row['no_tagihan']); ?></strong><br>
                <small class="text-muted"><?= html_escape($row['tanggal_tagihan']); ?></small>
              </td>
              <td><?= html_escape($row['rme_no']); ?></td>
              <td><?= html_escape($row['nama_pasien']); ?></td>
              <td><?= html_escape($row['nama_poli']); ?></td>
              <td><?= html_escape($row['tipe_pembayaran']); ?></td>
              <td>Rp <?= number_format((float) $row['grand_total'], 0, ',', '.'); ?></td>
              <td>
                <span class="badge badge-<?= $row['status_tagihan'] === 'LUNAS' ? 'success' : 'warning'; ?>">
                  <?= html_escape($row['status_tagihan']); ?>
                </span>
              </td>
              <td>
                <a href="<?= site_url('kasir/detail/' . $row['tagihan_id']); ?>" class="btn btn-sm btn-primary">
                  <i class="fas fa-cash-register"></i> Buka
                </a>
              </td>
            </tr>
          <?php endforeach; ?>
        <?php else: ?>
          <tr><td colspan="8" class="text-center">Tidak ada tagihan pada filter ini.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
