<?php include 'comp/header.php'; ?>
<?php 
  date_default_timezone_set("Asia/Jakarta");
  $tanggalSekarang = date("Y-m-d");
  $jamSekarang = date("h:i a");

  // Auto-generate invoice number
  $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();
  }
?>

<div class="content-wrapper">
  <div class="content-header">
    <div class="container-fluid">
      <div class="page-header">
        <div>
          <h1>Tambah Stok Masuk</h1>
          <ul class="page-breadcrumb">
            <li><a href="index.php">Home</a></li>
            <li><span class="sep">&rsaquo;</span></li>
            <li><a href="index.php?m=barangmasuk&s=title">Barang Masuk</a></li>
            <li><span class="sep">&rsaquo;</span></li>
            <li>Tambah</li>
          </ul>
        </div>
      </div>
    </div>
  </div>

  <section class="content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-lg-8 mx-auto">
          
          <div class="pm-card">
            <div class="pm-card-header">
              <span class="pm-card-title">
                <i class="fas fa-plus-circle" style="margin-right:8px;color:var(--primary);"></i>
                Form Barang Masuk
              </span>
            </div>
            
            <div class="pm-card-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>
                  <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;"><i class="fas fa-times"></i> Tutup</button>
                </div>
                <div style="max-height:250px;overflow-y:auto;padding:4px;">
                  <table width="100%" class="table table-sm">
                    <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="<?= $tanggalSekarang; ?>">
                  </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" onclick="window.history.back()">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> 
    </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();
}
</script>
  </section>
</div>

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

