<?php
session_start();
include 'php/db_connection.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

$userName = $_SESSION['user_name'];
$role = $_SESSION['role'];
$isAdmin = ($role == 'admin');

if (!isset($_GET['id']) || empty($_GET['id'])) {
    echo "<script>alert('No product ID provided!'); window.location.href='add_product_name.php';</script>";
    exit();
}

$Product_id = intval($_GET['id']);

// Fetch product details — id ফিল্ড দিয়ে সার্চ
$stmt = $con->prepare("SELECT * FROM product WHERE id = ?");
$stmt->bind_param("i", $Product_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows == 0) {
    echo "<script>alert('Product not found!'); window.location.href='add_product_name.php';</script>";
    exit();
}

$product = $result->fetch_assoc();
$category_id  = $product['Product_id'];  // FK — product_cat এর Product_id
$product_name = $product['name'];
$product_Quantity = $product['Quantity'] ?? $product['quantity'] ?? 0;
$product_price = $product['price'];
$stmt->close();

// Handle confirmation
if (isset($_POST['confirm_delete'])) {
    // sales_items এ product_id কলাম চেক করো (category Product_id দিয়ে)
    $check_sales = $con->prepare("SELECT COUNT(*) as cnt FROM sales_items WHERE product_id = ?");
    $check_sales->bind_param("i", $category_id);
    $check_sales->execute();
    $sales_data = $check_sales->get_result()->fetch_assoc();
    $check_sales->close();

    if ($sales_data['cnt'] > 0) {
        echo "<script>
            alert('Cannot delete! This product is used in " . $sales_data['cnt'] . " sale(s). Delete or return those sales first.');
            window.location.href='add_product.php?category={$category_id}';
        </script>";
        exit();
    }

    $delete_stmt = $con->prepare("DELETE FROM product WHERE id = ?");
    $delete_stmt->bind_param("i", $Product_id);

    if ($delete_stmt->execute()) {
        echo "<script>
            alert('Product deleted successfully!');
            window.location.href='add_product.php?category={$category_id}';
        </script>";
        exit();
    } else {
        echo "<script>
            alert('Failed to delete product: " . addslashes($con->error) . "');
            window.location.href='add_product.php?category={$category_id}';
        </script>";
        exit();
    }
    $delete_stmt->close();
}

// Handle cancel
if (isset($_POST['cancel_delete'])) {
    header("Location: add_product.php?category={$category_id}");
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Delete Product - Confirmation</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        .delete-container {
            background: white;
            padding: 50px;
            border-radius: 16px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            max-width: 600px;
            width: 100%;
            text-align: center;
            margin: 60px auto;
        }
        .warning-icon { font-size: 80px; margin-bottom: 20px; animation: pulse 2s infinite; }
        @keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.15); } }
        h1 { color: #dc3545; margin-bottom: 15px; font-size: 32px; font-weight: bold; }
        .product-details { background: #f8f9fa; padding: 25px; border-radius: 12px; margin: 25px 0; text-align: left; border: 2px solid #dee2e6; }
        .detail-row { display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid #dee2e6; }
        .detail-row:last-child { border-bottom: none; }
        .detail-label { font-weight: bold; color: #495057; }
        .danger-alert { background-color: #f8d7da; border: 2px solid #f5c6cb; color: #721c24; padding: 15px; border-radius: 8px; margin: 20px 0; font-weight: bold; font-size: 16px; }
        .button-group { display: flex; gap: 15px; margin-top: 30px; }
        .btn-delete { flex: 1; padding: 16px; border: none; border-radius: 8px; cursor: pointer; font-size: 18px; font-weight: bold; background: #dc3545; color: white; }
        .btn-cancel { flex: 1; padding: 16px; border: none; border-radius: 8px; cursor: pointer; font-size: 18px; font-weight: bold; background: #6c757d; color: white; }
    </style>
</head>
<body>
    <?php include 'sidebar.php'; ?>
    <div class="delete-container">
        <div class="warning-icon">⚠️</div>
        <h1>Delete Product?</h1>
        <p style="font-size:20px;color:#333;font-weight:500;">Are you sure you want to permanently delete this product?</p>
        <div class="product-details">
            <h3>📦 Product Information</h3>
            <div class="detail-row">
                <span class="detail-label">Product Name:</span>
                <span class="detail-value"><?php echo htmlspecialchars($product_name); ?></span>
            </div>
            <div class="detail-row">
                <span class="detail-label">Product ID:</span>
                <span class="detail-value"><?php echo htmlspecialchars($category_id); ?></span>
            </div>
            <div class="detail-row">
                <span class="detail-label">Quantity:</span>
                <span class="detail-value"><?php echo htmlspecialchars($product_Quantity); ?></span>
            </div>
            <div class="detail-row">
                <span class="detail-label">Price:</span>
                <span class="detail-value">৳<?php echo number_format($product_price, 2); ?></span>
            </div>
        </div>
        <div class="danger-alert">🚨 This action cannot be undone!</div>
        <form method="POST">
            <div class="button-group">
                <button type="submit" name="confirm_delete" class="btn-delete">🗑️ Yes, Delete Product</button>
                <button type="submit" name="cancel_delete" class="btn-cancel">← No, Keep Product</button>
            </div>
        </form>
    </div>
</body>
</html>
<?php $con->close(); ?>
