<?php
$batas       = 10;
$halaman     = isset($_GET['halaman']) ? (int)$_GET['halaman'] : 1;
$halaman_awal = ($halaman > 1) ? ($halaman * $batas) - $batas : 0;
$previous    = $halaman - 1;
$next        = $halaman + 1;

$where = '';
if ((isset($_GET['go']) || isset($_POST['go'])) && !empty($_GET['cari'] ?? $_POST['cari'] ?? '')) {
  $cari = mysqli_real_escape_string($koneksi, $_GET['cari'] ?? $_POST['cari']);
  $where = " WHERE namaspl LIKE '%$cari%' OR kdspl LIKE '%$cari%'";
}

$count_query = "SELECT COUNT(*) as total FROM tb_supplier$where";
$count_result = mysqli_query($koneksi, $count_query);
$count_row = mysqli_fetch_assoc($count_result);
$jumlah_data = $count_row['total'];
$total_halaman = ceil($jumlah_data / $batas);

$data_sup = mysqli_query($koneksi, "SELECT * FROM tb_supplier$where ORDER BY id DESC LIMIT $halaman_awal, $batas");

foreach ($data_sup as $pro): ?>
<tr>
  <td><?= $i++; ?></td>
  <td><code style="background:var(--surface-2);padding:3px 8px;border-radius:6px;font-size:0.8rem;color:var(--primary);"><?= htmlspecialchars($pro['kdspl']); ?></code></td>
  <td><strong><?= htmlspecialchars($pro['namaspl']); ?></strong></td>
  <td><?= htmlspecialchars($pro['alamatspl']); ?></td>
  <td><?= htmlspecialchars($pro['kontakspl']); ?></td>
  <td>
    <div class="action-buttons">
      <button type="button" class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#hapus-sup<?= $pro['id'] ?>" title="Hapus">
        <i class="fas fa-trash-alt"></i>
      </button>
      <button type="button" class="btn btn-sm btn-warning" data-bs-toggle="modal" data-bs-target="#edit-sup<?= $pro['id'] ?>" title="Edit">
        <i class="fas fa-edit"></i>
      </button>
    </div>

    <!-- Modal Hapus -->
    <form action="" method="POST">
      <div class="modal fade" id="hapus-sup<?= $pro['id'] ?>" 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-exclamation-triangle" style="color:#DC2626;margin-right:8px;"></i>Hapus Supplier</h5>
              <button type="button" class="close" data-bs-dismiss="modal"><span>&times;</span></button>
            </div>
            <div class="modal-body">
              <p style="color:var(--text-secondary);margin-bottom:12px;">Apakah Anda yakin ingin menghapus supplier ini?</p>
              <div class="modal-detail-row">
                <div class="modal-detail-label">Kode</div>
                <div class="modal-detail-value"><?= htmlspecialchars($pro['kdspl']) ?></div>
              </div>
              <div class="modal-detail-row">
                <div class="modal-detail-label">Nama Supplier</div>
                <div class="modal-detail-value"><?= htmlspecialchars($pro['namaspl']) ?></div>
              </div>
              <input type="hidden" name="id" value="<?= $pro['id'] ?>">
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
              <button type="submit" name="hapus" class="btn btn-danger"><i class="fas fa-trash-alt"></i> Hapus</button>
            </div>
          </div>
        </div>
      </div>
    </form>

    <!-- Modal Edit -->
    <div class="modal fade" id="edit-sup<?= $pro['id'] ?>" 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-edit" style="color:var(--primary);margin-right:8px;"></i>Edit 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-edit-sup<?= $pro['id'] ?>">
              <input type="hidden" name="id" value="<?= $pro['id'] ?>">
              <div class="form-group">
                <label>Kode Supplier</label>
                <input type="text" class="form-control" name="kdspl" value="<?= htmlspecialchars($pro['kdspl']) ?>" readonly>
              </div>
              <div class="form-group">
                <label>Nama Supplier</label>
                <input type="text" class="form-control" name="namaspl" value="<?= htmlspecialchars($pro['namaspl']) ?>">
              </div>
              <div class="form-group">
                <label>Alamat</label>
                <input type="text" class="form-control" name="alamatspl" value="<?= htmlspecialchars($pro['alamatspl']) ?>">
              </div>
              <div class="form-group">
                <label>Kontak</label>
                <input type="text" class="form-control" name="kontakspl" value="<?= htmlspecialchars($pro['kontakspl']) ?>">
              </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-edit-sup<?= $pro['id'] ?>" name="edit" class="btn btn-primary"><i class="fas fa-save"></i> Simpan</button>
          </div>
        </div>
      </div>
    </div>
  </td>
</tr>
<?php endforeach; ?>
