<?php
session_start();
if (!isset($_SESSION['warehouse'])) {
  header("Location: login_warehouse.php");
  exit;
}

<?php
$host = "localhost";
$user = "odmr9632_userdb";
$pass = "@Sukabumi2018";
$db   = "odmr9632_order_db";

$conn = new mysqli($host, $user, $pass, $db);
if ($conn->connect_error) {
  die("Koneksi gagal: " . $conn->connect_error);
}
?>
if ($conn->connect_error) {
  die("Koneksi gagal: " . $conn->connect_error);
}

// Update stok jika form disubmit
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  foreach ($_POST['stok'] as $id => $stok) {
    $nama = $conn->real_escape_string($_POST['nama'][$id]);
    $stok = (int)$stok;
    $id = (int)$id;
    $conn->query("UPDATE produk_fisik SET nama_produk='$nama', stok=$stok WHERE id=$id");
  }
  $msg = "✅ Stok berhasil diperbarui!";
}

// Ambil data produk
$produk = $conn->query("SELECT * FROM produk_fisik ORDER BY nama_produk ASC");
?>

<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <title>Edit Stok Produk</title>
  <style>
    body { font-family: Arial; background: #f4f4f4; padding: 30px; }
    .container { max-width: 900px; margin: auto; background: white; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px #ccc; }
    h2 { text-align: center; color: #f44336; }
    table { width: 100%; border-collapse: collapse; margin-top: 20px; }
    th, td { border: 1px solid #ddd; padding: 10px; text-align: center; }
    th { background: #f44336; color: white; }
    input[type="text"], input[type="number"] {
      width: 100%; padding: 6px; box-sizing: border-box;
    }
    .btn-simpan {
      display: block;
      width: 100%;
      padding: 12px;
      background-color: #4CAF50;
      color: white;
      font-size: 16px;
      font-weight: bold;
      border: none;
      border-radius: 6px;
      margin-top: 30px;
      cursor: pointer;
    }
    .btn-simpan:hover {
      background-color: #45a049;
    }
    .back-link {
      display: inline-block;
      margin-top: 30px;
      text-align: center;
      padding: 10px 20px;
      background: gray;
      color: white;
      text-decoration: none;
      border-radius: 6px;
    }
    .msg { text-align: center; color: green; font-weight: bold; margin-top: 15px; }
  </style>
</head>
<body>
<div class="container">
  <h2>🗘️ Edit Stok Produk</h2>
  <?php if (!empty($msg)) echo "<p class='msg'>$msg</p>"; ?>
  <form method="POST">
    <table>
      <tr>
        <th>Nama Produk</th>
        <th>Stok</th>
      </tr>
      <?php while ($row = $produk->fetch_assoc()): ?>
        <tr>
          <td>
            <input type="text" name="nama[<?= $row['id'] ?>]" value="<?= htmlspecialchars($row['nama_produk']) ?>">
          </td>
          <td>
            <input type="number" name="stok[<?= $row['id'] ?>]" value="<?= (int)$row['stok'] ?>">
          </td>
        </tr>
      <?php endwhile; ?>
    </table>
    <button type="submit" class="btn-simpan">📅 Simpan Perubahan</button>
  </form>

  <div style="text-align:center;">
    <a href="dashboard_warehouse.php" class="back-link">⬅️ Kembali ke Dashboard</a>
  </div>
</div>
</body>
</html>