<?php
// Pastikan session hanya dimulai sekali
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// Ambil ulang user_cabang terbaru dari database jika sudah login
if (isset($_SESSION['user_id'])) {
    $userId = $_SESSION['user_id'];
    include 'aksi/koneksi.php'; // Pastikan file ini berisi $conn

    if ($conn) {
        $stmt = $conn->prepare("SELECT user_cabang FROM user WHERE user_id = ?");
        if ($stmt) {
            $stmt->bind_param("i", $userId);
            $stmt->execute();
            $stmt->bind_result($userCabangTerbaru);
            if ($stmt->fetch()) {
                $_SESSION['user_cabang'] = $userCabangTerbaru;
            }
            $stmt->close();
        } else {
            echo "Query error: " . $conn->error;
        }
    } else {
        echo "Koneksi database gagal.";
    }
}

// Set level login dari session (pastikan ada di session)
$levelLogin = isset($_SESSION['level']) ? $_SESSION['level'] : '';

// Include tampilan
include '_header.php';
include '_nav.php';
include '_sidebar.php'; 

// Arahkan kurir ke halaman berbeda
if ($levelLogin === "kurir") {
    echo "<script>document.location.href = 'bo';</script>";
    exit;
}

// Proses form jika tombol simpan ditekan
if (isset($_POST["submit_stok"])) {
    include 'functions.php'; // Pastikan fungsi tambahStokPending tersedia
    $result = tambahStokPending($_POST);

    if ($result) {
        echo "<script>
                alert('Data stok berhasil diinput, menunggu konfirmasi super admin.');
                document.location.href = 'stok_plus_min';
              </script>";
    } else {
        echo "<script>alert('Data gagal ditambahkan.');</script>";
    }
}
?>

<div class="content-wrapper">
  <section class="content-header">
    <div class="container-fluid">
      <div class="row mb-2">
        <div class="col-sm-6">
          <h1>Input Hasil Stok Opname</h1>
        </div>
        <div class="col-sm-6">
          <ol class="breadcrumb float-sm-right">
            <li class="breadcrumb-item"><a href="beranda">Home</a></li>
            <li class="breadcrumb-item active">SO Min-Plus</li>
          </ol>
        </div>
      </div>
    </div>
  </section>

  <section class="content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-md-6">
          <div class="card card-primary">
            <div class="card-header">
              <h3 class="card-title">Input Data</h3>
            </div>
            <form role="form" action="" method="post">
              <div class="card-body">
                <input type="hidden" name="cabang_id" value="<?= $_SESSION['user_cabang'] ?>">

                <div class="form-group">
                  <label for="barang_kode">Kode/Barcode</label>
                  <input type="text" name="barang_kode" class="form-control" id="barang_kode" placeholder="Input Kode/Barcode" autofocus required>
                </div>

                <div class="form-group">
                  <label for="jenis">Jenis Min Atau Plus</label>
                  <select name="jenis" id="jenis" class="form-control" required>
                    <option value="">-- Pilih Plus atau Min --</option>
                    <option value="plus">PLUS</option>
                    <option value="min">MIN</option>
                  </select>
                </div>

                <div class="form-group">
                  <label for="jumlah">Jumlah</label>
                  <input type="number" name="jumlah" id="jumlah" class="form-control" placeholder="Input Jumlah" required min="1">
                </div>

                <div class="form-group">
                  <label for="keterangan">Keterangan</label>
                  <textarea class="form-control" name="keterangan" id="keterangan" rows="3" placeholder="Contoh : Plus salah transfer atau Min di pakai toko"></textarea>
                </div>
              </div>

              <div class="card-footer text-right">
                <button type="submit" name="submit_stok" class="btn btn-primary">Simpan</button>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </section>
</div>

<?php include '_footer.php'; ?>
