<?php
// Start the session and include the database connection
if (!isset($_SESSION)) {
    session_start();
    include('includes/dbcon.php');
}

// Check if the 'id' parameter exists in the URL
if (isset($_GET['id'])) {
    // Retrieve the item_id from the URL
    $item_id = $_GET['id'];

    // Sanitize the input to prevent SQL injection or other attacks
    $item_id = htmlspecialchars($item_id);

    // Fetch product details from the database
    $sql = "SELECT item_name, description, sales_price, image_path FROM item_master WHERE item_id = $item_id";
    $result = mysqli_query($con, $sql);

   
    if ($row = mysqli_fetch_assoc($result)) {
        // Product details
        $item_name = $row['item_name'];
        $description = $row['description'];
        $sales_price = $row['sales_price'];
        $image_path = $row['image_path']; // Assuming you have an 'image_path' column in your database
    } else {
        // If no product is found, display an error message
        die("Product not found.");
    }
} else {
    // If no 'id' parameter is provided, display an error message
    die("Invalid request. No product ID provided.");
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($item_name); ?> - Product Details</title>
    <!-- Bootstrap CSS (optional) -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- FontAwesome for icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        .product-details {
            margin-top: 50px;
        }
        .product-image img {
            max-width: 100%;
            height: auto;
            border-radius: 10px;
        }
        .product-info {
            padding: 20px;
        }
        .product-info h1 {
            font-size: 2.5rem;
            margin-bottom: 20px;
        }
        .product-info p {
            font-size: 1.2rem;
            line-height: 1.6;
        }
        .product-info .price {
            font-size: 1.5rem;
            color: #ff5733;
            font-weight: bold;
        }
        .add-to-cart-btn {
            background-color: #ff5733;
            color: #fff;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            font-size: 1.2rem;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .add-to-cart-btn:hover {
            background-color: #e64a2e;
        }
    </style>
</head>
<body>
    <div class="container product-details">
        <div class="row">
            <!-- Product Image -->
            <div class="col-md-6">
                <div class="product-image">
                    <img src="<?php echo htmlspecialchars($image_path); ?>" alt="<?php echo htmlspecialchars($item_name); ?>">
                </div>
            </div>
            <!-- Product Details -->
            <div class="col-md-6">
                <div class="product-info">
                    <h1><?php echo htmlspecialchars($item_name); ?></h1>
                    <p><?php echo htmlspecialchars($description); ?></p>
                    <p class="price">Rs. <?php echo number_format($sales_price, 2); ?></p>
                    <!-- Add to Cart Button -->
                    <form action="add_to_cart.php" method="POST">
                        <input type="hidden" name="item_id" value="<?php echo $item_id; ?>">
                        <input type="hidden" name="item_name" value="<?php echo htmlspecialchars($item_name); ?>">
                        <input type="hidden" name="sales_price" value="<?php echo $sales_price; ?>">
                        <button type="submit" class="add-to-cart-btn">
                            <i class="fas fa-shopping-cart"></i> Add to Cart
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- Bootstrap JS (optional) -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>