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

<div class="card">
  <div class="card-header">Filter Mutasi</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">Lokasi</label>
          <select name="lokasi_id" class="form-control">
            <option value="all">Semua Lokasi</option>
            <?php foreach ($lokasi as $item): ?>
              <option value="<?= $item->lokasi_id; ?>" <?= $filters['lokasi_id'] === $item->lokasi_id ? 'selected' : ''; ?>><?= $item->nama_lokasi; ?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-3 mb-2">
          <label class="small">Jenis Mutasi</label>
          <select name="jenis_mutasi" class="form-control">
            <?php foreach (['all' => 'Semua', 'MASUK' => 'Masuk', 'KELUAR' => 'Keluar', 'ADJUSTMENT' => 'Adjustment'] as $value => $label): ?>
              <option value="<?= $value; ?>" <?= $filters['jenis_mutasi'] === $value ? 'selected' : ''; ?>><?= $label; ?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-3 mb-2 d-flex align-items-end">
          <button class="btn btn-primary btn-block">Terapkan</button>
        </div>
      </div>
    </form>
  </div>
</div>

<div class="card">
  <div class="card-header">Riwayat Mutasi</div>
  <div class="card-body table-responsive">
    <table class="table table-bordered table-striped">
      <thead>
        <tr>
          <th>Waktu</th>
          <th>Lokasi</th>
          <th>Obat</th>
          <th>Jenis</th>
          <th>Qty</th>
          <th>Saldo Sebelum</th>
          <th>Saldo Sesudah</th>
          <th>Referensi</th>
          <th>Petugas</th>
        </tr>
      </thead>
      <tbody>
        <?php if (!empty($list)): ?>
          <?php foreach ($list as $row): ?>
            <tr>
              <td><?= date('d/m/Y H:i', strtotime($row->created_at)); ?></td>
              <td><?= html_escape($row->nama_lokasi); ?></td>
              <td><?= html_escape($row->kode_obat . ' - ' . $row->nama_obat); ?></td>
              <td><span class="badge badge-secondary"><?= html_escape($row->jenis_mutasi); ?></span></td>
              <td><?= rtrim(rtrim(number_format((float) $row->qty, 2, '.', ''), '0'), '.'); ?> <?= html_escape($row->satuan_kecil); ?></td>
              <td><?= rtrim(rtrim(number_format((float) $row->saldo_sebelum, 2, '.', ''), '0'), '.'); ?></td>
              <td><?= rtrim(rtrim(number_format((float) $row->saldo_sesudah, 2, '.', ''), '0'), '.'); ?></td>
              <td><?= html_escape($row->referensi_tipe); ?></td>
              <td><?= html_escape($row->nama_petugas ?: '-'); ?></td>
            </tr>
          <?php endforeach; ?>
        <?php else: ?>
          <tr><td colspan="9" class="text-center">Belum ada mutasi stok.</td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
