<?php
include "koneksi.php";

$id = intval($_GET['id'] ?? 0);

// Ambil data lama
$q = $conn->query("SELECT * FROM tabel_produk WHERE id=$id");
$data = $q->fetch_assoc();

$pesan = "";
$redirect = false; // flag redirect

// ==== PROSES UPDATE ====
if (isset($_POST['update'])) {  // <- disamakan dengan tombol "Update"
    $kode_barang  = trim($_POST['kode_barang']);
    $nama_produk  = trim($_POST['nama_produk']);
    $stok         = (int)$_POST['stok'];
    $harga_beli   = (float)$_POST['harga_beli'];
    $harga_eceran = (float)$_POST['harga_eceran'];
    $kategori     = trim($_POST['kategori']);

    // cek duplikat kode_barang selain id yg sedang diedit
    $cek = $conn->prepare("SELECT id FROM tabel_produk WHERE kode_barang=? AND id<>?");
    $cek->bind_param("si", $kode_barang, $id);
    $cek->execute();
    $hasil = $cek->get_result();

    if ($hasil->num_rows > 0) {
        $pesan = "Gagal! Kode Barang sudah digunakan, silakan pakai kode lain.";
    } else {
        $stmt = $conn->prepare("UPDATE tabel_produk 
                                SET kode_barang=?, nama_produk=?, stok=?, harga_beli=?, harga_eceran=?, kategori=? 
                                WHERE id=?");
        $stmt->bind_param("ssiddsi", $kode_barang, $nama_produk, $stok, $harga_beli, $harga_eceran, $kategori, $id);

        if ($stmt->execute()) {
            $pesan = "Produk berhasil diperbarui!";
            $redirect = true; // set redirect
        } else {
            $pesan = "Terjadi kesalahan saat update produk!";
        }
    }
}
?>

<?php if ($pesan): ?>
<script>
    alert("<?= $pesan ?>");
    <?php if ($redirect): ?>
        // redirect otomatis setelah sukses update
        window.location = "index.php?page=produk";
    <?php endif; ?>
</script>
<?php endif; ?>

<div class="container mt-4">
    <h3>Edit Produk</h3>
    <form method="post">
        <div class="mb-3">
            <label>Kode Barang</label>
            <input type="text" name="kode_barang" class="form-control" 
                   value="<?= htmlspecialchars($data['kode_barang']) ?>" required>
        </div>
        <div class="mb-3">
            <label>Nama Produk</label>
            <input type="text" name="nama_produk" class="form-control" 
                   value="<?= htmlspecialchars($data['nama_produk']) ?>" required>
        </div>
        <div class="mb-3">
            <label>Stok</label>
            <input type="number" name="stok" class="form-control" value="<?= $data['stok'] ?>" required>
        </div>
        <div class="mb-3">
            <label>Harga Beli</label>
            <input type="number" name="harga_beli" class="form-control" value="<?= $data['harga_beli'] ?>" required>
        </div>
        <div class="mb-3">
            <label>Harga Eceran</label>
            <input type="number" name="harga_eceran" class="form-control" value="<?= $data['harga_eceran'] ?>" required>
        </div>
        <div class="mb-3">
            <label>Kategori</label>
            <input type="text" name="kategori" class="form-control" 
                   value="<?= htmlspecialchars($data['kategori']) ?>">
        </div>
        <button type="submit" name="update" class="btn btn-primary">Update</button>
        <a href="index.php?page=produk" class="btn btn-secondary">Kembali</a>
    </form>
</div>
