<?php include_once 'fungsi/fungsi.php'; ?>
<?php
date_default_timezone_set("Asia/Jakarta");

if (isset($_POST['hapus']))  { delete_pro_masuk(); }

// Auto-generate invoice number for modal
global $koneksi;
$q_inv = mysqli_query($koneksi, "SELECT MAX(id) as max_id FROM tb_barang_masuk");
$r_inv = mysqli_fetch_array($q_inv);
$next_inv = ($r_inv['max_id'] ? $r_inv['max_id'] : 0) + 1;
$auto_inv = "INV/" . date('Ymd') . "/" . sprintf("%04s", $next_inv);

if (isset($_POST['simpan'])) {
	barang_masuk();
}
?>
<?php include 'comp/header.php'; ?>

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

  <section class="content">
    <div class="container-fluid">
      <div class="table-container">
        <div class="table-toolbar">
          <span class="table-toolbar-title">
            <i class="fas fa-dolly-flatbed" style="margin-right:8px;color:var(--primary);"></i>
            Riwayat Barang Masuk
          </span>
          <form action="" method="GET" style="display:flex;gap:8px;align-items:center;flex-wrap:wrap;">
            <input type="hidden" name="m" value="barangmasuk">
            <input type="hidden" name="s" value="title">
            <div style="display:flex;align-items:center;gap:6px;">
              <input type="date" name="start_date" class="form-control form-control-sm" value="<?= isset($_GET['start_date']) ? $_GET['start_date'] : '' ?>" style="width:150px;border-radius:8px;">
              <span style="color:var(--text-muted);font-weight:600;font-size:0.82rem;">s/d</span>
              <input type="date" name="end_date" class="form-control form-control-sm" value="<?= isset($_GET['end_date']) ? $_GET['end_date'] : '' ?>" style="width:150px;border-radius:8px;">
            </div>
            <div style="display:flex;align-items:center;gap:6px;">
              <input type="text" class="form-control form-control-sm" name="cari" placeholder="Cari barang..." value="<?= isset($_GET['cari']) ? $_GET['cari'] : '' ?>" style="width:180px;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>
            </div>
            <?php if(isset($_GET['start_date']) || isset($_GET['end_date']) || isset($_GET['cari'])): ?>
              <a href="?m=barangmasuk&s=title" class="btn btn-light btn-sm" style="border:1px solid var(--border);border-radius:8px;height:31px;display:inline-flex;align-items:center;">Reset</a>
            <?php endif; ?>
          </form>
        </div>
        <div class="table-responsive">
          <table class="table">
            <thead>
              <tr>
                <th>No</th>
                <th>No. Invoice</th>
                <th>Tanggal</th>
                <th>Jam</th>
                <th>Kode Barang</th>
                <th>Nama Barang</th>
                <th>Kategori</th>

                <th>Stok Lama</th>
                <th>Jml Masuk</th>
                <th>Admin</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=barangmasuk&s=title&halaman=$previous".(isset($_GET['start_date']) ? '&start_date='.$_GET['start_date'] : '').(isset($_GET['end_date']) ? '&end_date='.$_GET['end_date'] : '')."'"; } ?>>
                <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=barangmasuk&s=title&halaman=<?= $x ?><?= isset($_GET['start_date']) ? '&start_date='.$_GET['start_date'] : '' ?><?= isset($_GET['end_date']) ? '&end_date='.$_GET['end_date'] : '' ?>"><?= $x ?></a>
            </li>
            <?php endfor; ?>
            <li class="page-item">
              <a class="page-link" <?php if($halaman < $total_halaman){ echo "href='?m=barangmasuk&s=title&halaman=$next".(isset($_GET['start_date']) ? '&start_date='.$_GET['start_date'] : '').(isset($_GET['end_date']) ? '&end_date='.$_GET['end_date'] : '')."'"; } ?>>
                Next&nbsp;<i class="fas fa-chevron-right" style="font-size:10px;"></i>
              </a>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </section>
</div>



<!-- MODAL TAMBAH STOK -->
<div class="modal fade" id="modal-tambah-stok" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog modal-lg 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 Stok Masuk</h5>
        <button type="button" class="close" data-bs-dismiss="modal"><span>&times;</span></button>
      </div>
      <div class="modal-body">
        <div id="panel-pilih-barang" style="display:none;margin-bottom:12px;border:1px solid var(--border);border-radius:var(--r-md);overflow:hidden;background:var(--surface);">
          <div style="padding:8px 12px;background:var(--surface-2);border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;">
            <strong style="font-size:0.85rem;">Pilih Barang</strong>
            <div style="display:flex; align-items:center; gap:8px;">
                <input type="text" id="cariBarangInput" onkeyup="cariBarangList()" class="form-control form-control-sm" placeholder="Cari kode/nama..." style="width:180px; border-radius:6px; font-size:0.75rem; height:28px;">
                <button type="button" class="btn btn-sm btn-light" onclick="tutupPanelBarang()" style="border-radius:6px;border:1px solid var(--border);padding:2px 10px;font-size:0.75rem;height:28px;display:flex;align-items:center;gap:4px;"><i class="fas fa-times"></i> Tutup</button>
            </div>
          </div>
          <div style="max-height:250px;overflow-y:auto;padding:4px;">
            <table width="100%" class="table table-sm" id="tabel-pilih-barang">
              <thead>
                <tr>
                  <th style="font-size:0.65rem;padding:4px 8px;">Kode</th>
                  <th style="font-size:0.65rem;padding:4px 8px;">Nama Barang</th>
                  <th style="font-size:0.65rem;padding:4px 8px;">Stok</th>
                  <th style="font-size:0.65rem;padding:4px 8px;">Aksi</th>
                </tr>
              </thead>
              <tbody>
                <?php
                  $data_p = mysqli_query($koneksi, "SELECT p.*, k.kategori as nama_kategori FROM tb_barang p LEFT JOIN tb_kat k ON p.kategori = k.id ORDER BY p.id ASC");
                  foreach ($data_p as $sa_p):
                ?>
                <tr style="cursor:pointer;" onclick="pilihBarang('<?= htmlspecialchars($sa_p['kdbarang'], ENT_QUOTES) ?>','<?= htmlspecialchars($sa_p['nm_barang'], ENT_QUOTES) ?>','<?= htmlspecialchars($sa_p['nama_kategori'], ENT_QUOTES) ?>','<?= $sa_p['stok'] ?>')">
                  <td style="padding:4px 8px;font-size:0.75rem;"><code><?= $sa_p['kdbarang'] ?></code></td>
                  <td style="padding:4px 8px;font-size:0.8rem;font-weight:600;"><?= $sa_p['nm_barang'] ?></td>
                  <td style="padding:4px 8px;font-size:0.75rem;"><?= $sa_p['stok'] ?></td>
                  <td style="padding:4px 8px;"><button type="button" class="btn btn-sm btn-primary" style="padding:1px 8px;font-size:0.65rem;border-radius:4px;">Pilih</button></td>
                </tr>
                <?php endforeach; ?>
              </tbody>
            </table>
          </div>
        </div>
        <form action="" method="POST" enctype="multipart/form-data">
          <div class="row">
            <div class="col-md-6 form-group">
              <label>No. Invoice</label>
              <input type="text" class="form-control" name="noinv" value="<?= $auto_inv; ?>" readonly style="background:var(--surface-2);font-weight:700;color:var(--primary);">
            </div>
            <div class="col-md-3 form-group">
              <label>Tanggal</label>
              <input type="date" class="form-control" name="tanggal" value="<?= date('Y-m-d'); ?>">
            </div>
            <div class="col-md-3 form-group">
              <label>Jam</label>
              <input type="time" class="form-control" name="jam" value="<?= date('H:i'); ?>">
            </div>
          </div>

          <div class="form-group mt-3">
            <label>Pilih Barang</label>
            <div class="input-group">
              <input type="text" class="form-control" id="kdbarang" name="kdbarang" placeholder="Kode Barang" readonly required style="background:var(--surface-2);">
              <div class="input-group-append">
                <button type="button" class="btn btn-primary" onclick="bukaPanelBarang()" style="border-radius:0 12px 12px 0;">
                  <i class="fas fa-search"></i> Cari
                </button>
              </div>
            </div>
          </div>

          <div class="row">
            <div class="col-md-6 form-group">
              <label>Nama Barang</label>
              <input type="text" class="form-control" id="nm_barang" name="nm_barang" readonly style="background:var(--surface-2);">
            </div>
            <div class="col-md-6 form-group">
              <label>Kategori</label>
              <input type="text" class="form-control" id="kategori" name="kategori" readonly style="background:var(--surface-2);">
            </div>
          </div>

          <div class="row">
            <div class="col-md-12 form-group">
              <label>Stok Saat Ini</label>
              <input type="text" class="form-control" id="stok" name="stok" readonly style="background:var(--surface-2);color:var(--primary);font-weight:700;">
            </div>
          </div>

          <div class="row mt-3">
            <div class="col-md-6 form-group">
              <label>Admin Pencatat</label>
              <input type="text" class="form-control" name="admin" value="<?= $key['nama'];?>" readonly style="background:var(--surface-2);">
            </div>
            <div class="col-md-6 form-group">
              <label style="color:var(--primary);">Jumlah Barang Masuk <span class="text-danger">*</span></label>
              <input type="number" class="form-control" name="jml_masuk" placeholder="Contoh: 50" required min="1" style="border:2px solid var(--primary-muted);font-weight:700;font-size:1.1rem;height:48px;">
            </div>
          </div>

          <div class="form-group mt-4" style="display:flex;justify-content:flex-end;gap:12px;">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
            <button type="submit" name="simpan" class="btn btn-primary">
              <i class="fas fa-save"></i> Simpan Stok
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

<script>
function bukaPanelBarang() {
    document.getElementById('panel-pilih-barang').style.display = 'block';
}
function tutupPanelBarang() {
    document.getElementById('panel-pilih-barang').style.display = 'none';
}
function pilihBarang(kd, nm, kat, stok) {
    document.getElementById('kdbarang').value = kd;
    document.getElementById('nm_barang').value = nm;
    document.getElementById('kategori').value = kat;
    document.getElementById('stok').value = stok;
    tutupPanelBarang();
}
function cariBarangList() {
    var input, filter, table, tr, td1, td2, i, txtValue1, txtValue2;
    input = document.getElementById("cariBarangInput");
    filter = input.value.toUpperCase();
    table = document.getElementById("tabel-pilih-barang");
    tr = table.getElementsByTagName("tr");
    for (i = 1; i < tr.length; i++) {
        td1 = tr[i].getElementsByTagName("td")[0];
        td2 = tr[i].getElementsByTagName("td")[1];
        if (td1 || td2) {
            txtValue1 = td1.textContent || td1.innerText;
            txtValue2 = td2.textContent || td2.innerText;
            if (txtValue1.toUpperCase().indexOf(filter) > -1 || txtValue2.toUpperCase().indexOf(filter) > -1) {
                tr[i].style.display = "";
            } else {
                tr[i].style.display = "none";
            }
        }
    }
}
</script>


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