<?php
require_once '../includes/require_admin.php';

// Only allow POST requests for deletion
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    redirect(SITE_URL . '/admin/products.php');
}

// Get product ID from POST data
$product_id = isset($_POST['product_id']) ? intval($_POST['product_id']) : 0;

if ($product_id > 0) {
    try {
        // Delete the product
        $stmt = $pdo->prepare("DELETE FROM products WHERE id = ?");
        $stmt->execute([$product_id]);
        
        // Set success message
        $_SESSION['success_message'] = "Product deleted successfully.";
    } catch (PDOException $e) {
        // Set error message
        $_SESSION['error_message'] = "Error deleting product: " . $e->getMessage();
    }
} else {
    $_SESSION['error_message'] = "Invalid product ID.";
}

// Redirect back to products page
redirect(SITE_URL . '/admin/products.php');
?>