<?php
session_start();
include 'config.php'; // Menghubungkan ke database

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Ambil data dari form
    $productName = $_POST['productName'];
    $description = $_POST['description'];
    $price = $_POST['price'];

    // Validasi data - pastikan semua field yang diperlukan diisi
    if (empty($productName) || empty($price)) {
        echo "<script>
                Swal.fire({
                    icon: 'error',
                    title: 'Oops...',
                    text: 'Product name and price are required!',
                });
              </script>";
        exit;
    }

    // Insert ke tabel products
    $query = "INSERT INTO products (product_name, description, price) VALUES (:product_name, :description, :price)";
    $stmt = $db->prepare($query);
    $stmt->bindParam(':product_name', $productName);
    $stmt->bindParam(':description', $description);
    $stmt->bindParam(':price', $price);

    // Eksekusi query dan cek hasil
    if ($stmt->execute()) {
        echo "<script>
                Swal.fire({
                    icon: 'success',
                    title: 'Success!',
                    text: 'Product added successfully!',
                }).then((result) => {
                    if (result.isConfirmed) {
                        window.location.reload(); // Refresh page setelah klik OK
                    }
                });
              </script>";
    } else {
        echo "<script>
                Swal.fire({
                    icon: 'error',
                    title: 'Failed',
                    text: 'Failed to add product. Please try again.',
                });
              </script>";
    }
}
?>
