<?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 = [];

// Search
if ((isset($_GET['go']) || isset($_POST['go'])) && !empty($_GET['cari'] ?? $_POST['cari'] ?? '')) {
  $cari = mysqli_real_escape_string($koneksi, $_GET['cari'] ?? $_POST['cari']);
  $where[] = "(nm_barang LIKE '%$cari%' OR kdbarang LIKE '%$cari%' OR noinv LIKE '%$cari%')";
}

// Date range
if (isset($_GET['start_date']) && $_GET['start_date'] != '') {
  $start = mysqli_real_escape_string($koneksi, $_GET['start_date']);
  $where[] = "tanggal >= '$start'";
}
if (isset($_GET['end_date']) && $_GET['end_date'] != '') {
  $end = mysqli_real_escape_string($koneksi, $_GET['end_date']);
  $where[] = "tanggal <= '$end'";
}

$where_clause = count($where) > 0 ? ' WHERE ' . implode(' AND ', $where) : '';

$count_query = "SELECT COUNT(*) as total FROM tb_barang_masuk$where_clause";
$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);

$q = mysqli_query($koneksi, "SELECT * FROM tb_barang_masuk$where_clause ORDER BY id DESC LIMIT $halaman_awal, $batas");

// Buffer rows first
$rows = [];
while ($r = mysqli_fetch_assoc($q)) { $rows[] = $r; }

foreach ($rows 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(--text-secondary);"><?= htmlspecialchars($pro['noinv']); ?></code></td>
  <td><span style="font-size:0.8rem;color:var(--text-secondary);"><?= htmlspecialchars($pro['tanggal']); ?></span></td>
  <td><span style="font-size:0.8rem;color:var(--text-secondary);"><?= htmlspecialchars($pro['jam']); ?></span></td>
  <td><code style="background:var(--surface-2);padding:3px 8px;border-radius:6px;font-size:0.8rem;color:var(--primary);"><?= htmlspecialchars($pro['kdbarang']); ?></code></td>
  <td><strong><?= htmlspecialchars($pro['nm_barang']); ?></strong></td>
  <td><?= htmlspecialchars($pro['kategori']); ?></td>
  <td><?= $pro['stok']; ?></td>
  <td><span class="badge badge-ada" style="font-size:0.8rem;">+ <?= $pro['jml_masuk']; ?></span></td>
  <td><?= htmlspecialchars($pro['admin']); ?></td>
  <td>
    <div class="action-buttons">
      <button type="button" class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#hapus-barang<?= $pro['id'] ?>" title="Hapus Riwayat">
        <i class="fas fa-trash-alt"></i>
      </button>
    </div>
  </td>
</tr>
<?php endforeach; ?>

<?php
// Render all modals OUTSIDE the table
foreach ($rows as $pro): ?>

<!-- Modal Hapus Riwayat Barang -->
<form action="" method="POST" id="form-hapus-prodmasuk<?= $pro['id'] ?>">
  <input type="hidden" name="id" value="<?= $pro['id'] ?>">
  <input type="hidden" name="hapus" value="1">
  <div class="modal fade" id="hapus-barang<?= $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 Riwayat Barang</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 riwayat stok masuk ini?</p>
          <div class="modal-detail-row">
            <div class="modal-detail-label">No Invoice</div>
            <div class="modal-detail-value"><?= htmlspecialchars($pro['noinv']) ?></div>
          </div>
          <div class="modal-detail-row">
            <div class="modal-detail-label">Barang</div>
            <div class="modal-detail-value"><?= htmlspecialchars($pro['nm_barang']) ?> (+<?= $pro['jml_masuk'] ?>)</div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
          <button type="submit" class="btn btn-danger" form="form-hapus-prodmasuk<?= $pro['id'] ?>"><i class="fas fa-trash-alt"></i> Hapus</button>
        </div>
      </div>
    </div>
  </div>
</form>

<?php endforeach; ?>

