<?php
$page_title = 'Proses Resep';
$header = $resep['header'];
$detail = $resep['detail'];
?>
<?php $this->load->view('farmasi/_nav', compact('page_title')); ?>

<div class="card">
  <div class="card-header">Informasi Resep</div>
  <div class="card-body">
    <div class="row">
      <div class="col-md-4">
        <p><strong>No. RM:</strong> <?= html_escape($header->rme_no); ?></p>
        <p><strong>Pasien:</strong> <?= html_escape($header->nama_lengkap); ?></p>
        <p><strong>Poli:</strong> <?= html_escape($header->nama_poli ?: '-'); ?></p>
      </div>
      <div class="col-md-4">
        <p><strong>Dokter:</strong> <?= html_escape($header->nama_dokter ?: '-'); ?></p>
        <p><strong>Tgl Resep:</strong> <?= date('d/m/Y H:i', strtotime($header->tanggal_resep)); ?></p>
        <p><strong>Status:</strong> <span class="badge badge-info"><?= $header->status_resep; ?></span></p>
      </div>
      <div class="col-md-4">
        <p><strong>Petugas Farmasi:</strong> <?= html_escape($header->nama_petugas_farmasi ?: '-'); ?></p>
        <p><strong>Diproses Pada:</strong> <?= $header->processed_at ? date('d/m/Y H:i', strtotime($header->processed_at)) : '-'; ?></p>
      </div>
    </div>
  </div>
</div>

<form method="post" action="<?= site_url('farmasi/serahkan/' . $header->resep_id); ?>">
  <div class="card">
    <div class="card-header">Item Resep</div>
    <div class="card-body table-responsive">
      <table class="table table-bordered">
        <thead>
          <tr>
            <th>Obat</th>
            <th>Dosis</th>
            <th>Frekuensi</th>
            <th>Rute</th>
            <th>Durasi</th>
            <th>Qty Resep</th>
            <th>Stok Farmasi</th>
            <th>Qty Serah</th>
            <th>Status</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($detail as $row): ?>
            <tr>
              <td>
                <strong><?= html_escape($row->nama_obat_snapshot); ?></strong><br>
                <small class="text-muted"><?= html_escape($row->kode_obat ?: 'Non master'); ?></small>
                <input type="hidden" name="resep_detail_id[]" value="<?= $row->resep_detail_id; ?>">
              </td>
              <td><?= html_escape($row->dosis ?: '-'); ?></td>
              <td><?= html_escape($row->frekuensi ?: '-'); ?></td>
              <td><?= html_escape($row->rute ?: '-'); ?></td>
              <td><?= html_escape($row->durasi ?: '-'); ?></td>
              <td><?= rtrim(rtrim(number_format((float) $row->jumlah_resep, 2, '.', ''), '0'), '.'); ?> <?= html_escape($row->satuan_snapshot ?: $row->satuan_kecil); ?></td>
              <td><?= rtrim(rtrim(number_format((float) $row->stok_farmasi, 2, '.', ''), '0'), '.'); ?> <?= html_escape($row->satuan_kecil ?: $row->satuan_snapshot); ?></td>
              <td>
                <input
                  type="number"
                  step="0.01"
                  min="0"
                  max="<?= (float) $row->jumlah_resep; ?>"
                  name="jumlah_serah[]"
                  value="<?= (float) $row->jumlah_serah; ?>"
                  class="form-control"
                  <?= !$row->obat_id ? 'readonly' : ''; ?>
                >
                <?php if (!$row->obat_id): ?>
                  <small class="text-danger">Item ini belum terhubung ke master obat.</small>
                <?php endif; ?>
              </td>
              <td><span class="badge badge-secondary"><?= html_escape($row->status_detail); ?></span></td>
            </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
    <div class="card-footer text-right">
      <a href="<?= site_url('farmasi/resep'); ?>" class="btn btn-outline-secondary">Kembali</a>
      <button type="submit" class="btn btn-success"><i class="fas fa-hand-holding-medical"></i> Simpan Penyerahan</button>
    </div>
  </div>
</form>
