<?php include_once 'fungsi/fungsi.php'; ?>
<?php
if (isset($_POST['simpan'])) { insert_barang(); }
if (isset($_POST['hapus']))  { delete_barang(); }
if (isset($_POST['edit']))   { update_barang(); }
?>
<?php include 'comp/header.php'; ?>

<div class="content-wrapper">
  <div class="content-header">
    <div class="container-fluid">
      <div class="page-header">
        <div>
          <h1>Barang</h1>
          <ul class="page-breadcrumb">
            <li><a href="index.php">Home</a></li>
            <li><span class="sep">›</span></li>
            <li>Barang</li>
          </ul>
        </div>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modal-tambah-barang">
          <i class="fas fa-plus"></i> Tambah Barang
        </button>
      </div>
    </div>
  </div>

  <section class="content">
    <div class="container-fluid">

      <!-- Table Container -->
      <div class="table-container">
        <div class="table-toolbar" style="flex-direction:column;align-items:stretch;gap:12px;">
          <div style="display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:12px;width:100%;">
            <span class="table-toolbar-title">
              <i class="fas fa-box-open" style="margin-right:8px;color:var(--primary);"></i>
              Daftar Barang
            </span>
            <form action="" method="GET" style="display:flex;gap:8px;align-items:center;">
              <input type="hidden" name="m" value="barang">
              <input type="hidden" name="s" value="title">
              <input type="text" name="cari" class="form-control form-control-sm" placeholder="Cari nama barang..." value="<?= isset($_GET['cari']) ? $_GET['cari'] : '' ?>" style="width:200px;border-radius:8px;">
              <button type="submit" name="go" class="btn btn-primary btn-sm" style="border-radius:8px;height:31px;display:inline-flex;align-items:center;"><i class="fas fa-search"></i></button>
              <?php if(isset($_GET['go']) && isset($_GET['cari'])): ?>
                <a href="?m=barang&s=title" class="btn btn-light btn-sm" style="border-radius:8px;border:1px solid var(--border);height:31px;display:inline-flex;align-items:center;">Reset</a>
              <?php endif; ?>
            </form>
          </div>
        </div>
        <div class="table-responsive">
          <table class="table">
            <thead>
              <tr>
                <th>No</th>
                <th>Kode</th>
                <th>Nama Barang</th>
                <th>Kategori</th>
                <th>Stok</th>

                <th>Status</th>
                <th>Harga Beli</th>
                <th>Harga Jual</th>
                <th>Satuan</th>
                <th>Aksi</th>
              </tr>
            </thead>
            <tbody>
              <?php $i = 1; include 'paging.php'; ?>
            </tbody>
          </table>
        </div>
        <!-- Pagination -->
        <div class="pagination-wrapper">
          <ul class="pagination">
            <li class="page-item">
              <a class="page-link" <?php if($halaman > 1){ echo "href='?m=barang&s=title&halaman=$previous'"; } ?>>
                <i class="fas fa-chevron-left" style="font-size:10px;"></i>&nbsp;Prev
              </a>
            </li>
            <?php for($x=1; $x<=$total_halaman; $x++): ?>
            <li class="page-item <?= ($x == $halaman) ? 'active' : '' ?>">
              <a class="page-link" href="?m=barang&s=title&halaman=<?= $x ?>"><?= $x ?></a>
            </li>
            <?php endfor; ?>
            <li class="page-item">
              <a class="page-link" <?php if($halaman < $total_halaman){ echo "href='?m=barang&s=title&halaman=$next'"; } ?>>
                Next&nbsp;<i class="fas fa-chevron-right" style="font-size:10px;"></i>
              </a>
            </li>
          </ul>
        </div>
      </div>

    </div>
  </section>
</div><!-- /.content-wrapper -->

<!-- ============================================================
     MODAL: TAMBAH BARANG
============================================================= -->
<div class="modal fade" id="modal-tambah-barang" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title"><i class="fas fa-plus-circle" style="margin-right:8px;color:var(--primary);"></i>Tambah Barang Baru</h5>
        <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close"><span>&times;</span></button>
      </div>
      <div class="modal-body">
        <form action="" method="POST" enctype="multipart/form-data" id="form-tambah-barang">
          <?php
            global $koneksi;
            $query_kode = mysqli_query($koneksi, "SELECT MAX(id) as max_id FROM tb_barang");
            $data_kode  = mysqli_fetch_array($query_kode);
            $max_id     = $data_kode['max_id'];
            $next_id    = $max_id + 1;
            $auto_kode  = "KD" . sprintf("%03s", $next_id);
          ?>
          <div class="form-group">
            <label>Kode Barang</label>
            <input type="text" class="form-control" name="kdbarang" value="<?= $auto_kode ?>" readonly>
          </div>
          <div class="form-group">
            <label>Nama Barang</label>
            <input type="text" class="form-control" name="nm_barang" placeholder="Masukkan nama barang" autofocus>
          </div>
          <div class="form-group">
            <label>Kategori Barang</label>
            <select name="kategori" class="form-control">
              <?php
                global $koneksi;
                $Kategori = mysqli_query($koneksi, "SELECT * FROM tb_kat");
                foreach ($Kategori as $kat):
              ?>
              <option value="<?= $kat['id'] ?>"><?= $kat['kategori'] ?></option>
              <?php endforeach; ?>
            </select>
          </div>
          <div class="form-group">
            <label>Stok</label>
            <input type="text" class="form-control" name="stok" placeholder="Jumlah stok">
          </div>
          <div class="form-group">
          </div>
          <div class="form-group">
          </div>
          <div class="form-group">
            <label>Harga Beli</label>
            <input type="number" class="form-control" name="harga_beli" placeholder="Contoh: 20000">
          </div>
          <div class="form-group">
            <label>Harga Jual</label>
            <input type="number" class="form-control" name="harga_jual" placeholder="Contoh: 25000">
          </div>
          <div class="form-group">
            <label>Satuan</label>
            <input type="text" class="form-control" name="satuan" placeholder="Contoh: pcs, unit, botol">
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
        <button type="submit" form="form-tambah-barang" name="simpan" class="btn btn-primary">
          <i class="fas fa-save"></i> Simpan
        </button>
      </div>
    </div>
  </div>
</div>

<?php include 'comp/footer.php'; ?>

