<?php $page_title = 'Stok 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">Stok Masuk</div>
      <div class="card-body">
        <form method="post" action="<?= site_url('farmasi/tambah_stok'); ?>">
          <div class="form-group">
            <label>Obat</label>
            <select name="obat_id" class="form-control" required>
              <option value="">Pilih Obat</option>
              <?php foreach ($obat_options as $obat): ?>
                <option value="<?= $obat->obat_id; ?>"><?= $obat->kode_obat; ?> - <?= $obat->nama_obat; ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="form-group">
            <label>Lokasi</label>
            <select name="lokasi_id" class="form-control" required>
              <?php foreach ($lokasi as $item): ?>
                <option value="<?= $item->lokasi_id; ?>"><?= $item->nama_lokasi; ?> (<?= $item->jenis_lokasi; ?>)</option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="form-group">
            <label>Qty Masuk</label>
            <input type="number" step="0.01" min="0.01" name="qty" class="form-control" required>
          </div>
          <div class="form-group">
            <label>Keterangan</label>
            <textarea name="keterangan" class="form-control" rows="2" placeholder="Pembelian / Hibah / Retur"></textarea>
          </div>
          <button class="btn btn-primary btn-block">Simpan Stok Masuk</button>
        </form>
      </div>
    </div>

    <div class="card">
      <div class="card-header">Penyesuaian Stok</div>
      <div class="card-body">
        <form method="post" action="<?= site_url('farmasi/sesuaikan_stok'); ?>">
          <div class="form-group">
            <label>Obat</label>
            <select name="obat_id" class="form-control" required>
              <option value="">Pilih Obat</option>
              <?php foreach ($obat_options as $obat): ?>
                <option value="<?= $obat->obat_id; ?>"><?= $obat->kode_obat; ?> - <?= $obat->nama_obat; ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="form-group">
            <label>Lokasi</label>
            <select name="lokasi_id" class="form-control" required>
              <?php foreach ($lokasi as $item): ?>
                <option value="<?= $item->lokasi_id; ?>"><?= $item->nama_lokasi; ?> (<?= $item->jenis_lokasi; ?>)</option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="form-group">
            <label>Qty Aktual</label>
            <input type="number" step="0.01" min="0" name="qty_baru" class="form-control" required>
          </div>
          <div class="form-group">
            <label>Keterangan</label>
            <textarea name="keterangan" class="form-control" rows="2" placeholder="Stock opname / koreksi"></textarea>
          </div>
          <button class="btn btn-warning btn-block">Simpan Penyesuaian</button>
        </form>
      </div>
    </div>
  </div>

  <div class="col-md-8">
    <div class="card">
      <div class="card-header">Ringkasan Stok</div>
      <div class="card-body table-responsive">
        <table class="table table-bordered table-striped">
          <thead>
            <tr>
              <th>Lokasi</th>
              <th>Kode</th>
              <th>Obat</th>
              <th>Qty</th>
              <th>Satuan</th>
              <th>Stok Min.</th>
              <th>Update</th>
            </tr>
          </thead>
          <tbody>
            <?php if (!empty($stok)): ?>
              <?php foreach ($stok as $row): ?>
                <tr class="<?= (float) $row->qty <= (float) $row->stok_minimum ? 'table-warning' : ''; ?>">
                  <td><?= html_escape($row->nama_lokasi); ?></td>
                  <td><?= html_escape($row->kode_obat); ?></td>
                  <td><?= html_escape($row->nama_obat); ?></td>
                  <td><?= rtrim(rtrim(number_format((float) $row->qty, 2, '.', ''), '0'), '.'); ?></td>
                  <td><?= html_escape($row->satuan_kecil); ?></td>
                  <td><?= rtrim(rtrim(number_format((float) $row->stok_minimum, 2, '.', ''), '0'), '.'); ?></td>
                  <td><?= date('d/m/Y H:i', strtotime($row->updated_at)); ?></td>
                </tr>
              <?php endforeach; ?>
            <?php else: ?>
              <tr><td colspan="7" class="text-center">Belum ada data stok.</td></tr>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
