
 	<?php
    include('includes/header.php');



        

?>
  <!--=========================
        PAGE BANNER START
    ==========================-->
    <section class="page_banner" style="background: url(assets/images/page_banner_bg.jpg);">
        <div class="page_banner_overlay">
            <div class="container">
                <div class="row">
                    <div class="col-12">
                        <div class="page_banner_text wow fadeInUp">
                            <h1><?=$result_pro['pro_name']?></h1>
                            <ul>
                                <li><a href="#"><i class="fal fa-home-lg"></i> Home</a></li>
                                <li><a href="#"><?=$result_cats['cate_name']?> </a></li>
                                <li><a href="#"><?=$result_pro['pro_name']?></a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
<section class="cart_page mt_100 mb_100">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 wow fadeInUp" style="visibility: visible; animation-name: fadeInUp;">
                    <div class="cart_table_area">
                      <?php
session_start();
include 'connection.php';

// Handle add to cart form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $productId = isset($_POST['id']) ? intval($_POST['id']) : 0;
    $quantity = isset($_POST['qty']) ? max(1, intval($_POST['qty'])) : 1;

    if ($productId > 0) {
        $stmt = $link->prepare("SELECT pro_id, pro_name, pro_type, pro_image FROM tbl_product WHERE pro_id = ?");
        $stmt->bind_param("i", $productId);
        $stmt->execute();
        $result = $stmt->get_result();
        $product = $result->fetch_assoc();

        if ($product) {
            if (!isset($_SESSION['cart'])) {
                $_SESSION['cart'] = [];
            }

            if (isset($_SESSION['cart'][$productId])) {
                $_SESSION['cart'][$productId]['qty'] += $quantity;
            } else {
                $_SESSION['cart'][$productId] = [
                    'id' => $product['pro_id'],
                    'name' => $product['pro_name'],
                    'price' => $product['pro_type'],  // Assuming this is price
                    'image' => $product['pro_image'],
                    'qty' => $quantity
                ];
            }
        }
    }
}

// Now display the cart contents below

?>

<div class="table-responsive">
    <table class="table">
        <thead>
            <tr>
                <th class="cart_page_checkbox">
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" id="flexCheckDefault">
                    </div>
                </th>
                <th class="cart_page_img">Product image </th>
                <th class="cart_page_details">Product Details</th>
                <th class="cart_page_price">Unit Price</th>
                <th class="cart_page_quantity">Quantity</th>
                <th class="cart_page_total">Subtotal</th>
                <th class="cart_page_action">Action</th>
            </tr>
        </thead>
        <tbody>
            <?php
            if (!empty($_SESSION['cart'])):
                $total = 0;
                foreach ($_SESSION['cart'] as $item):
                    $subtotal = $item['price'] * $item['qty'];
                    $total += $subtotal;
            ?>
            <tr>
                <td>
                    <input type="checkbox" class="form-check-input" name="cart_checkbox[]" value="<?=htmlspecialchars($item['id'])?>">
                </td>
                <td>
                    <img src="product-image/<?=htmlspecialchars($item['image'])?>" alt="<?=htmlspecialchars($item['name'])?>" width="80">
                </td>
                <td><?=htmlspecialchars($item['name'])?></td>
                <td>Rs <?=number_format($item['price'], 2)?></td>
                <td><?=intval($item['qty'])?></td>
                <td>Rs <?=number_format($subtotal, 2)?></td>
                <td>
                    <!-- For simplicity, add remove button -->
                    <form method="post" action="remove-from-cart.php" style="display:inline-block;">
                        <input type="hidden" name="id" value="<?=htmlspecialchars($item['id'])?>">
                        <button type="submit" class="btn btn-danger btn-sm">Remove</button>
                    </form>
                </td>
            </tr>
            <?php
                endforeach;
            ?>
            <tr>
                <td colspan="5" style="text-align: right;"><strong>Total:</strong></td>
                <td colspan="2"><strong>Rs <?=number_format($total, 2)?></strong></td>
            </tr>
            <?php else: ?>
            <tr><td colspan="7" style="text-align:center;">Your cart is empty.</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>

                       
                       

                       
                    </div>
                </div>
                <?php
session_start();

// Calculate subtotal from session cart
$subtotal = 0;
if (!empty($_SESSION['cart'])) {
    foreach ($_SESSION['cart'] as $item) {
        $subtotal += $item['price'] * $item['qty'];
    }
}
$total = $subtotal;  // No tax, no discount
?>

<div class="col-lg-4 col-md-9 wow fadeInRight" style="position: relative;">
    <div id="sticky_sidebar" class="">
        <div class="cart_page_summary">
            <h3>Billing summary</h3>

            <?php if (!empty($_SESSION['cart'])): ?>
                <ul>
                <?php foreach ($_SESSION['cart'] as $item): ?>
                    <li>
                        <a class="img" href="#">
                            <img src="product-image/<?=htmlspecialchars($item['image'])?>" alt="<?=htmlspecialchars($item['name'])?>" class="img-fluid w-100" style="max-width: 80px;">
                        </a>
                        <div class="text">
                            <a class="title" href="shop_details.php?id=<?=htmlspecialchars($item['id'])?>">
                                <?=htmlspecialchars($item['name'])?>
                            </a>
                            <p>Rs <?=number_format($item['price'], 2)?> × <?=intval($item['qty'])?></p>
                        </div>
                    </li>
                <?php endforeach; ?>
                </ul>
            <?php else: ?>
                <p>Your cart is empty.</p>
            <?php endif; ?>

            <h6>Subtotal <span>Rs <?=number_format($subtotal, 2)?></span></h6>
            <h4>Total <span>Rs <?=number_format($total, 2)?></span></h4>

            <form action="#">
                <input type="text" placeholder="Coupon code" name="coupon_code" disabled>
                <button type="submit" class="common_btn" disabled>Apply</button>
                <p>Coupon code currently not available.</p>
            </form>
        </div>
        <div class="cart_summary_btn">
            <a class="common_btn" href="checkout.php">Checkout <i class="fas fa-long-arrow-right"></i></a>
        </div>
    </div>
</div>

            </div>
        </div>
    </section>

<?php include('includes/footer.php'); ?>