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

<div class="content-wrapper">
  <div class="content-header">
    <div class="container-fluid">
      <div class="page-header">
        <div>
          <h1>Supplier</h1>
          <ul class="page-breadcrumb">
            <li><a href="index.php">Home</a></li>
            <li><span class="sep">›</span></li>
            <li>Supplier</li>
          </ul>
        </div>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#modal-tambah-supplier">
          <i class="fas fa-plus"></i> Tambah Supplier
        </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-truck" style="margin-right:8px;color:var(--primary);"></i>
              Daftar Supplier
            </span>
            <form action="" method="GET" style="display:flex;gap:8px;align-items:center;">
              <input type="hidden" name="m" value="supplier">
              <input type="hidden" name="s" value="title">
              <input type="text" name="cari" class="form-control form-control-sm" placeholder="Cari supplier..." 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;"><i class="fas fa-search"></i></button>
              <?php if(isset($_GET['go']) && isset($_GET['cari'])): ?>
                <a href="?m=supplier&s=title" class="btn btn-light btn-sm" style="border-radius:8px;border:1px solid var(--border);">Reset</a>
              <?php endif; ?>
            </form>
          </div>
        </div>
        <div class="table-responsive">
          <table class="table">
            <thead>
              <tr>
                <th>No</th>
                <th>Kode</th>
                <th>Nama Supplier</th>
                <th>Alamat</th>
                <th>Kontak</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=supplier&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=supplier&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=supplier&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 Supplier -->
<div class="modal fade" id="modal-tambah-supplier" 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-truck" style="margin-right:8px;color:var(--primary);"></i>Tambah Supplier</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-supplier">
          <?php
            global $koneksi;
            $q_spl = mysqli_query($koneksi, "SELECT MAX(id) as max_id FROM tb_supplier");
            $d_spl = mysqli_fetch_array($q_spl);
            $next_spl = ($d_spl['max_id'] ? $d_spl['max_id'] : 0) + 1;
            $auto_spl = "SPL" . sprintf("%03s", $next_spl);
          ?>
          <div class="form-group">
            <label>Kode Supplier</label>
            <input type="text" class="form-control" name="kdspl" value="<?= $auto_spl ?>" readonly style="background:var(--surface-2);font-weight:700;color:var(--primary);">
          </div>
          <div class="form-group">
            <label>Nama Supplier</label>
            <input type="text" class="form-control" name="namaspl" placeholder="Nama supplier">
          </div>
          <div class="form-group">
            <label>Alamat</label>
            <input type="text" class="form-control" name="alamatspl" placeholder="Alamat supplier">
          </div>
          <div class="form-group">
            <label>Kontak</label>
            <input type="text" class="form-control" name="kontakspl" placeholder="No. telp atau WA">
          </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-supplier" name="simpan" class="btn btn-primary">
          <i class="fas fa-save"></i> Simpan
        </button>
      </div>
    </div>
  </div>
</div>

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