<?php
include "connect.php"; // Pastikan file connect.php sudah benar dan berisi koneksi database

// Ambil data dari POST request
$id         = isset($_POST['id']) ? intval($_POST['id']) : 0;
$nama_menu  = isset($_POST['nama_menu']) ? htmlentities($_POST['nama_menu']) : "";
$keterangan = isset($_POST['keterangan']) ? htmlentities($_POST['keterangan']) : "";
$harga      = isset($_POST['harga']) ? floatval($_POST['harga']) : 0;
$stok       = isset($_POST['stok']) ? intval($_POST['stok']) : 0;

if (!empty($_POST['input_menu_validate'])) {
    $kode_rand = rand(10000, 99999) . "-"; // Generate random code
    $target_dir = "../assets/img/";
    $image_name = $kode_rand . basename($_FILES['foto']['name']);
    $target_file = $target_dir . $image_name;
    $imageType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
    $statusUpload = 1;
    $message = "";

    // Validasi input harga dan stok
    if (!is_numeric($harga)) {
        $message = "Harga harus berupa angka.";
        $statusUpload = 0;
    }
    if (!filter_var($stok, FILTER_VALIDATE_INT)) {
        $message = "Stok harus berupa bilangan bulat.";
        $statusUpload = 0;
    }

    // Cek apakah file adalah gambar
    $check = getimagesize($_FILES['foto']['tmp_name']);
    if ($check === false) {
        $message = "Ini bukan file gambar.";
        $statusUpload = 0;
    }

    // Cek apakah file sudah ada
    if (file_exists($target_file)) {
        $message = "Maaf, file dengan nama ini sudah ada.";
        $statusUpload = 0;
    }

    // Cek ukuran file
    if ($_FILES['foto']['size'] > 500000) {
        $message = "File terlalu besar (maksimal 500KB).";
        $statusUpload = 0;
    }

    // Izinkan format tertentu
    $allowed_types = ["jpg", "jpeg", "png", "gif"];
    if (!in_array($imageType, $allowed_types)) {
        $message = "Hanya file JPG, JPEG, PNG, dan GIF yang diperbolehkan.";
        $statusUpload = 0;
    }

    if ($statusUpload == 0) {
        echo '<script>alert("' . $message . '"); window.location="../menu";</script>';
    } else {
        // Cek apakah nama menu sudah ada (kecuali untuk ID yang sedang diupdate)
        $check_query = mysqli_prepare($conn, "SELECT id FROM tb_daftar_menu WHERE nama_menu = ? AND id != ?");
        mysqli_stmt_bind_param($check_query, "si", $nama_menu, $id);
        mysqli_stmt_execute($check_query);
        mysqli_stmt_store_result($check_query);

        if (mysqli_stmt_num_rows($check_query) > 0) {
            echo '<script>alert("Nama menu sudah digunakan."); window.location="../menu";</script>';
            mysqli_stmt_close($check_query);
            exit;
        }
        mysqli_stmt_close($check_query);

        // Upload gambar
        if (move_uploaded_file($_FILES['foto']['tmp_name'], $target_file)) {
            // Update data menu
            $stmt = $conn->prepare("UPDATE tb_daftar_menu SET foto = ?, nama_menu = ?, keterangan = ?, harga = ?, stok = ? WHERE id = ?");
            $stmt->bind_param("sssddi", $image_name, $nama_menu, $keterangan, $harga, $stok, $id);

            if ($stmt->execute()) {
                echo '<script>alert("Menu berhasil diperbarui."); window.location="../menu";</script>';
            } else {
                echo '<script>alert("Gagal memperbarui menu: ' . $stmt->error . '"); window.location="../menu";</script>';
            }

            $stmt->close();
        } else {
            echo '<script>alert("Upload gambar gagal."); window.location="../menu";</script>';
        }
    }
} else {
    echo '<script>alert("Akses tidak sah."); window.location="../menu";</script>';
}
?>
