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

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

  <section class="content">
    <div class="container-fluid">
      <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-tags" style="margin-right:8px;color:var(--primary);"></i>
              Daftar Kategori
            </span>
            <form action="" method="GET" style="display:flex;gap:8px;align-items:center;">
              <input type="hidden" name="m" value="kategori">
              <input type="hidden" name="s" value="title">
              <input type="text" name="cari" class="form-control form-control-sm" placeholder="Cari kategori..." 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=kategori&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>Nama Kategori</th>
                <th>Aksi</th>
              </tr>
            </thead>
            <tbody>
              <?php $i = 1; include 'paging.php'; ?>
            </tbody>
          </table>
        </div>
        <div class="pagination-wrapper">
          <ul class="pagination">
            <li class="page-item">
              <a class="page-link" <?php if($halaman > 1){ echo "href='?m=kategori&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=kategori&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=kategori&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>

<!-- MODAL: Tambah Kategori -->
<div class="modal fade" id="modal-tambah-kategori" 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-tag" style="margin-right:8px;color:var(--primary);"></i>Tambah Kategori Baru</h5>
        <button type="button" class="close" data-bs-dismiss="modal"><span>&times;</span></button>
      </div>
      <div class="modal-body">
        <form action="" method="POST" id="form-tambah-kategori">
          <div class="form-group">
            <label>Nama Kategori</label>
            <input type="text" class="form-control" name="kategori" placeholder="Contoh: Makanan, Minuman..." autofocus>
          </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-kategori" name="simpan" class="btn btn-primary">
          <i class="fas fa-save"></i> Simpan
        </button>
      </div>
    </div>
  </div>
</div>

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