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

<div class="row">
  <div class="col-md-4">
    <div class="card">
      <div class="card-header">Master Bentuk Sediaan</div>
      <div class="card-body">
        <form method="post" action="<?= site_url('farmasi/simpan_bentuk_sediaan'); ?>">
          <div class="form-group">
            <label>Nama Bentuk</label>
            <input type="text" name="nama_bentuk" class="form-control" placeholder="Tablet, Kapsul, Sirup" required>
          </div>
          <div class="form-group">
            <label>Deskripsi</label>
            <textarea name="deskripsi" class="form-control" rows="2" placeholder="Opsional"></textarea>
          </div>
          <button class="btn btn-outline-primary btn-block"><i class="fas fa-plus"></i> Tambah Bentuk</button>
        </form>
        <hr>
        <div style="max-height: 220px; overflow:auto;">
          <table class="table table-sm table-bordered mb-0">
            <thead>
              <tr>
                <th>Bentuk</th>
                <th>Deskripsi</th>
              </tr>
            </thead>
            <tbody>
              <?php if (!empty($bentuk_sediaan)): ?>
                <?php foreach ($bentuk_sediaan as $bentuk): ?>
                  <tr>
                    <td><?= html_escape($bentuk->nama_bentuk); ?></td>
                    <td><?= html_escape($bentuk->deskripsi ?: '-'); ?></td>
                  </tr>
                <?php endforeach; ?>
              <?php else: ?>
                <tr><td colspan="2" class="text-center">Belum ada data.</td></tr>
              <?php endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

  <div class="col-md-4">
    <div class="card">
      <div class="card-header">Tambah Obat</div>
      <div class="card-body">
        <form method="post" action="<?= site_url('farmasi/simpan_obat'); ?>">
          <div class="form-group">
            <label>Kode Obat</label>
            <input type="text" name="kode_obat" class="form-control" required>
          </div>
          <div class="form-group">
            <label>Nama Obat</label>
            <input type="text" name="nama_obat" class="form-control" required>
          </div>
          <div class="form-group">
            <label>Bentuk Sediaan</label>
            <select name="bentuk_sediaan_id" class="form-control">
              <option value="">Pilih bentuk sediaan</option>
              <?php foreach ($bentuk_sediaan as $bentuk): ?>
                <option value="<?= $bentuk->bentuk_sediaan_id; ?>"><?= html_escape($bentuk->nama_bentuk); ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="form-group">
            <label>Satuan</label>
            <input type="text" name="satuan_kecil" class="form-control" required>
          </div>
          <div class="form-group">
            <label>Kategori</label>
            <input type="text" name="kategori" class="form-control" placeholder="Antibiotik / Analgesik">
          </div>
          <div class="form-row">
            <div class="form-group col-md-6">
              <label>Harga Beli</label>
              <input type="number" step="0.01" name="harga_beli" class="form-control" value="0">
            </div>
            <div class="form-group col-md-6">
              <label>Harga Jual</label>
              <input type="number" step="0.01" name="harga_jual" class="form-control" value="0">
            </div>
          </div>
          <div class="form-group">
            <label>Stok Minimum</label>
            <input type="number" step="0.01" name="stok_minimum" class="form-control" value="0">
          </div>
          <button class="btn btn-primary btn-block"><i class="fas fa-save"></i> Simpan</button>
        </form>
      </div>
    </div>
  </div>

  <div class="col-md-4">
    <div class="card">
      <div class="card-header">
        <form method="get" class="form-inline">
          <input type="text" name="keyword" class="form-control mr-2" value="<?= html_escape($keyword); ?>" placeholder="Cari kode atau nama obat">
          <button class="btn btn-outline-primary">Cari</button>
        </form>
      </div>
      <div class="card-body table-responsive">
        <table class="table table-bordered table-striped">
          <thead>
            <tr>
              <th>Kode</th>
              <th>Nama Obat</th>
              <th>Sediaan</th>
              <th>Satuan</th>
              <th>Kategori</th>
              <th>Harga Jual</th>
              <th>Stok Min.</th>
            </tr>
          </thead>
          <tbody>
            <?php if (!empty($obat)): ?>
              <?php foreach ($obat as $row): ?>
                <tr>
                  <td><?= html_escape($row->kode_obat); ?></td>
                  <td><?= html_escape($row->nama_obat); ?></td>
                  <td><?= html_escape($row->bentuk_sediaan ?: '-'); ?></td>
                  <td><?= html_escape($row->satuan_kecil); ?></td>
                  <td><?= html_escape($row->kategori ?: '-'); ?></td>
                  <td><?= number_format((float) $row->harga_jual, 2, ',', '.'); ?></td>
                  <td><?= rtrim(rtrim(number_format((float) $row->stok_minimum, 2, '.', ''), '0'), '.'); ?></td>
                </tr>
              <?php endforeach; ?>
            <?php else: ?>
              <tr><td colspan="7" class="text-center">Belum ada master obat.</td></tr>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
