<?php $page_title = 'Daftar Resep Farmasi'; ?>
<?php $this->load->view('farmasi/_nav', compact('page_title')); ?>

<div class="card">
  <div class="card-header">Filter Resep</div>
  <div class="card-body">
    <form method="get">
      <div class="form-row">
        <div class="col-md-3 mb-2">
          <label class="small">Tanggal Resep</label>
          <input type="date" name="tanggal" class="form-control" value="<?= html_escape($filters['tanggal']); ?>">
        </div>
        <div class="col-md-3 mb-2">
          <label class="small">Status</label>
          <select name="status" class="form-control">
            <?php foreach (['all' => 'Semua', 'MENUNGGU' => 'Menunggu', 'DIPROSES' => 'Diproses', 'SELESAI' => 'Selesai', 'BATAL' => 'Batal'] 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 Pasien / RM / Dokter</label>
          <input type="text" name="keyword" class="form-control" value="<?= html_escape($filters['keyword']); ?>" placeholder="Nama pasien, RM, atau dokter">
        </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 Resep</div>
  <div class="card-body table-responsive">
    <table class="table table-bordered table-striped">
      <thead>
        <tr>
          <th>Tanggal</th>
          <th>No. RM</th>
          <th>Pasien</th>
          <th>Poli</th>
          <th>Dokter</th>
          <th>Item</th>
          <th>Qty</th>
          <th>Status</th>
          <th>Aksi</th>
        </tr>
      </thead>
      <tbody>
        <?php if (!empty($list)): ?>
          <?php foreach ($list as $row): ?>
            <?php
              $badge = 'secondary';
              if ($row->status_resep === 'MENUNGGU') $badge = 'warning';
              if ($row->status_resep === 'DIPROSES') $badge = 'info';
              if ($row->status_resep === 'SELESAI') $badge = 'success';
              if ($row->status_resep === 'BATAL') $badge = 'danger';
            ?>
            <tr>
              <td><?= date('d/m/Y H:i', strtotime($row->tanggal_resep)); ?></td>
              <td><?= html_escape($row->rme_no); ?></td>
              <td><?= html_escape($row->nama_lengkap); ?></td>
              <td><?= html_escape($row->nama_poli ?: '-'); ?></td>
              <td><?= html_escape($row->nama_dokter ?: '-'); ?></td>
              <td><?= (int) $row->total_item; ?></td>
              <td><?= rtrim(rtrim(number_format((float) $row->total_qty_resep, 2, '.', ''), '0'), '.'); ?></td>
              <td><span class="badge badge-<?= $badge; ?>"><?= $row->status_resep; ?></span></td>
              <td>
                <a href="<?= site_url('farmasi/detail/' . $row->resep_id); ?>" class="btn btn-sm btn-primary">
                  <i class="fas fa-capsules"></i> Proses
                </a>
              </td>
            </tr>
          <?php endforeach; ?>
        <?php else: ?>
          <tr><td colspan="9" class="text-center">Belum ada resep farmasi.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
