<?php
session_start();
error_reporting(0);
include('includes/config.php');
include_once('includes/settings.php');
if (isset($_GET['action']) && $_GET['action'] == "add") {
    $id = intval($_GET['id']);
    if (isset($_SESSION['cart'][$id])) {
        $_SESSION['cart'][$id]['quantity']++;
    } else {
        $sql_p = "SELECT * FROM products WHERE id={$id} AND approval_status='approved' AND productAvailability='In Stock' AND stock_quantity > 0";
        $query_p = mysqli_query($con, $sql_p);
        if (mysqli_num_rows($query_p) != 0) {
            $row_p = mysqli_fetch_array($query_p);
            $_SESSION['cart'][$row_p['id']] = array("quantity" => 1, "price" => $row_p['productPrice']);
            echo "<script>alert('Product has been added to the cart')</script>";
            echo "<script type='text/javascript'> document.location ='my-cart.php'; </script>";
        } else {
            $message = "Product ID is invalid";
        }
    }
}
$pid = intval($_GET['pid']);
if (isset($_GET['pid']) && isset($_GET['action']) && $_GET['action'] == "wishlist") {
    if (strlen($_SESSION['login']) == 0) {
        header('location:login.php');
    } else {
        mysqli_query($con, "insert into wishlist(userId,productId) values('" . $_SESSION['id'] . "','$pid')");
        echo "<script>alert('Product added to wishlist');</script>";
        header('location:my-wishlist.php');

    }
}
if (isset($_POST['submit'])) {
    $qty = $_POST['quality'];
    $price = $_POST['price'];
    $value = $_POST['value'];
    $name = $_POST['name'];
    $summary = $_POST['summary'];
    $review = $_POST['review'];
    mysqli_query($con, "insert into productreviews(productId,quality,price,value,name,summary,review) values('$pid','$qty','$price','$value','$name','$summary','$review')");
}


?>
<!DOCTYPE html>
<html lang="en">
<?php include('includes/head.php'); ?>
<link rel="stylesheet" href="assets/css/delivery-sidebar.css">

<body class="cnt-home">

    <header class="header-style-1">

        <!-- ============================================== TOP MENU ============================================== -->
        <?php include('includes/top-header.php'); ?>
        <!-- ============================================== TOP MENU : END ============================================== -->
        <?php include('includes/main-header.php'); ?>
        <!-- ============================================== NAVBAR ============================================== -->
        <?php include('includes/menu-bar.php'); ?>
        <!-- ============================================== NAVBAR : END ============================================== -->

    </header>

    <!-- ============================================== HEADER : END ============================================== -->
    <div class="breadcrumb mt-2 mb-0 p-0">
        <div class="container mb-0 pb-0">
            <div class="breadcrumb-inner mt-0 mb-0 p-0">
                <?php
                $ret = mysqli_query($con, "SELECT category.categoryName AS catname, subcategory.subcategory AS subcatname, products.productName AS pname 
                                       FROM products 
                                       JOIN category ON category.id = products.category 
                                       JOIN subcategory ON subcategory.id = products.subcategory 
                                       WHERE products.id = '$pid' AND products.approval_status='approved'");
                while ($rw = mysqli_fetch_array($ret)) {
                    ?>
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="index.php">Home</a></li>
                            <li class="breadcrumb-item"><?php echo htmlentities($rw['catname']); ?></li>
                            <li class="breadcrumb-item"><?php echo htmlentities($rw['subcatname']); ?></li>
                            <li class="breadcrumb-item active" aria-current="page"><?php echo html_entity_decode($rw['pname'], ENT_QUOTES); ?>
                            </li>
                        </ol>
                    </nav>
                <?php } ?>
            </div><!-- /.breadcrumb-inner -->
        </div><!-- /.container -->
    </div><!-- /.breadcrumb -->


    <div class="body-content outer-top-xs mt-0">
        <div class="container mt-0">
            <div class="row single-product outer-bottom-sm">
                <?php
                $ret = mysqli_query($con, "select * from products where id='$pid' and approval_status='approved'");
                while ($row = mysqli_fetch_array($ret)) {
                    $discount = 0;
                    if ($row['productPriceBeforeDiscount'] > $row['productPrice']) {
                        $discount = round((($row['productPriceBeforeDiscount'] - $row['productPrice']) / $row['productPriceBeforeDiscount']) * 100);
                    }
                    ?>

                    <div class="col-md-12">
                        <div class="single-product-container">
                            <div class="row">
                                <!-- Product Gallery -->
                                <div class="col-md-5">
                                    <div class="product-gallery-holder single-product-gallery">
                                        <div id="owl-single-product" class="owl-carousel owl-theme">
                                            <div class="single-product-gallery-item" id="slide1">
                                                <div class="ribbon-new"><span>New!</span></div>
                                                <a data-lightbox="image-1"
                                                    data-title="<?php echo htmlspecialchars(html_entity_decode($row['productName'], ENT_QUOTES)); ?>"
                                                    href="__admin__/productimages/<?php echo htmlentities($row['id']); ?>/<?php echo htmlentities($row['productImage1']); ?>">
                                                    <img class="img-fluid w-100" alt=""
                                                        src="__admin__/productimages/<?php echo htmlentities($row['id']); ?>/<?php echo htmlentities($row['productImage1']); ?>"
                                                        onerror="this.src='assets/images/blank.gif'" />
                                                </a>
                                            </div>
                                            <?php if ($row['productImage2'] != "") { ?>
                                                <div class="single-product-gallery-item" id="slide2">
                                                    <a data-lightbox="image-1" data-title="Gallery"
                                                        href="__admin__/productimages/<?php echo htmlentities($row['id']); ?>/<?php echo htmlentities($row['productImage2']); ?>">
                                                        <img class="img-fluid w-100" alt=""
                                                            src="__admin__/productimages/<?php echo htmlentities($row['id']); ?>/<?php echo htmlentities($row['productImage2']); ?>"
                                                            onerror="this.src='assets/images/blank.gif'" />
                                                    </a>
                                                </div>
                                            <?php } ?>
                                            <?php if ($row['productImage3'] != "") { ?>
                                                <div class="single-product-gallery-item" id="slide3">
                                                    <a data-lightbox="image-1" data-title="Gallery"
                                                        href="__admin__/productimages/<?php echo htmlentities($row['id']); ?>/<?php echo htmlentities($row['productImage3']); ?>">
                                                        <img class="img-fluid w-100" alt=""
                                                            src="__admin__/productimages/<?php echo htmlentities($row['id']); ?>/<?php echo htmlentities($row['productImage3']); ?>"
                                                            onerror="this.src='assets/images/blank.gif'" />
                                                    </a>
                                                </div>
                                            <?php } ?>
                                        </div><!-- /.owl-carousel -->

                                        <!-- Thumbnails -->
                                        <div class="gallery-thumbs mt-3">
                                            <div id="owl-single-product-thumbnails" class="owl-carousel owl-theme">
                                                <div class="item">
                                                    <a class="horizontal-thumb active" data-target="#owl-single-product"
                                                        data-slide="1" href="#slide1">
                                                        <img class="img-fluid" alt=""
                                                            src="__admin__/productimages/<?php echo htmlentities($row['id']); ?>/<?php echo htmlentities($row['productImage1']); ?>"
                                                            onerror="this.src='assets/images/blank.gif'" />
                                                    </a>
                                                </div>
                                                <?php if ($row['productImage2'] != "") { ?>
                                                    <div class="item">
                                                        <a class="horizontal-thumb" data-target="#owl-single-product"
                                                            data-slide="2" href="#slide2">
                                                            <img class="img-fluid" alt=""
                                                                src="__admin__/productimages/<?php echo htmlentities($row['id']); ?>/<?php echo htmlentities($row['productImage2']); ?>"
                                                                onerror="this.src='assets/images/blank.gif'" />
                                                        </a>
                                                    </div>
                                                <?php } ?>
                                                <?php if ($row['productImage3'] != "") { ?>
                                                    <div class="item">
                                                        <a class="horizontal-thumb" data-target="#owl-single-product"
                                                            data-slide="3" href="#slide3">
                                                            <img class="img-fluid" alt=""
                                                                src="__admin__/productimages/<?php echo htmlentities($row['id']); ?>/<?php echo htmlentities($row['productImage3']); ?>"
                                                                onerror="this.src='assets/images/blank.gif'" />
                                                        </a>
                                                    </div>
                                                <?php } ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <!-- Product Info Column -->
                                <div class="col-md-4 product-info-column">
                                    <h1 class="p-name-main"><?php echo html_entity_decode($row['productName'], ENT_QUOTES); ?></h1>

                                    <?php
                                    $rt = mysqli_query($con, "SELECT * FROM productreviews WHERE productId='$pid'");
                                    $num_reviews = mysqli_num_rows($rt);
                                    ?>
                                    <div class="p-meta-row">
                                        <div class="p-rating">
                                            <i class="fa fa-star"></i>
                                            <i class="fa fa-star"></i>
                                            <i class="fa fa-star"></i>
                                            <i class="fa fa-star"></i>
                                            <i class="fa fa-star-o"></i>
                                            <span class="text-muted small ml-1">(<?php echo $num_reviews; ?> reviews)</span>
                                        </div>
                                        <div
                                            class="p-availability <?php echo ($row['productAvailability'] == 'In Stock') ? 'in-stock' : 'out-of-stock'; ?>">
                                            <?php echo htmlentities($row['productAvailability']); ?>
                                        </div>
                                    </div>
                                    <?php if (!empty($row['product_location'])) { ?>
                                        <div class="product-location mb-2">
                                            <i class="fa fa-map-marker text-danger me-1"></i>
                                            <span class="text-muted small">Location:
                                                <strong><?php echo htmlentities($row['product_location']); ?></strong></span>
                                        </div>
                                    <?php } ?>

                                    <div class="p-price-row">
                                        <span class="p-current-price">Ghs.
                                            <?php echo htmlentities($row['productPrice']); ?></span>
                                        <?php if ($discount > 0) { ?>
                                            <span class="p-old-price-main">Ghs.
                                                <?php echo htmlentities($row['productPriceBeforeDiscount']); ?></span>
                                            <span class="p-discount-pill">-<?php echo $discount; ?>%</span>
                                        <?php } ?>
                                    </div>

                                    <div class="buy-box-actions mb-5">
                                        <input type="hidden" id="mainQty" value="1">

                                        <div class="d-flex align-items-center gap-3" style="gap: 15px;">
                                            <?php if ($row['productAvailability'] == 'In Stock') { ?>
                                                <button type="button" class="btn btn-primary flex-grow-1 py-3 shadow-sm"
                                                    id="addToCartBtn" data-id="<?php echo $row['id']; ?>"
                                                    style="border-radius: 12px; font-weight: 700; font-size: 1.1rem; background-color: var(--am-primary); border: none; height: 54px;">
                                                    <i class="fa fa-shopping-cart me-2"></i> Add to Cart
                                                </button>
                                            <?php } else { ?>
                                                <button class="btn btn-secondary flex-grow-1 py-3" disabled
                                                    style="border-radius: 12px; height: 54px; font-weight: 700;">
                                                    OUT OF STOCK
                                                </button>
                                            <?php } ?>

                                            <a href="product-details.php?pid=<?php echo htmlentities($row['id']); ?>&action=wishlist"
                                                class="btn-wishlist-app d-flex align-items-center justify-content-center shadow-sm"
                                                data-id="<?php echo htmlentities($row['id']); ?>" title="Add to Wishlist"
                                                style="width: 54px; height: 54px; border-radius: 12px; background: white; border: 1px solid #f0f0f0; color: #ff5e57; position: relative; top: 0; right: auto; bottom: auto; left: auto; margin: 0;">
                                                <i class="fa fa-heart" style="font-size: 1.4rem;"></i>
                                            </a>
                                        </div>
                                    </div>

                                    <!-- Call to Order (Moved here) -->
                                    <div class="call-to-order mt-4">
                                        <h6>Call to order now:</h6>
                                        <div class="call-numbers">
                                            <?php
                                            $phone1 = get_setting('support_phone', '+233 123 456 789');
                                            $phone2 = get_setting('support_phone_2', '');
                                            echo htmlentities($phone1 . ($phone2 ? ' , ' . $phone2 : ''));
                                            ?>
                                        </div>
                                    </div>
                                </div> <!-- End col-md-4 -->

                                <!-- Delivery & Returns Sidebar -->
                                <div class="col-md-3 mt-3 mt-md-0">
                                    <div class="delivery-sidebar p-3 border rounded bg-white">
                                        <?php
                                        // Resolution Logic for Fees & Days
                                        $pickup_fee = ($row['shipping_pickup_fee'] !== null) ? $row['shipping_pickup_fee'] : get_setting('pickup_fee', '11.00');
                                        $door_fee = ($row['shipping_door_fee'] !== null) ? $row['shipping_door_fee'] : get_setting('door_delivery_fee', '25.00');

                                        $min_days = ($row['shipping_days_min'] !== null) ? $row['shipping_days_min'] : 1;
                                        $max_days = ($row['shipping_days_max'] !== null) ? $row['shipping_days_max'] : 2;

                                        // For Pickup date range (assuming +min days to +max days)
                                        $pickup_date_start = date('d F', strtotime("+$min_days days"));
                                        $pickup_date_end = date('d F', strtotime("+$max_days days"));

                                        // For Door Delivery date range (assuming +min+1 to +max+1 days so it's slightly longer)
                                        $door_min = $min_days + 1;
                                        $door_max = $max_days + 1;
                                        $door_date_start = date('d F', strtotime("+$door_min days"));
                                        $door_date_end = date('d F', strtotime("+$door_max days"));

                                        $free_threshold = get_setting('free_shipping_threshold', '150.00');
                                        ?>
                                        <!-- Pickup Station -->
                                        <div class="d-flex mb-3 pb-3 border-bottom wrapper-delivery">
                                            <div class="icon me-3 pr-2"><i class="fa fa-cubes fa-2x text-warning"></i></div>
                                            <div class="content w-100">
                                                <div class="d-flex justify-content-between align-items-center">
                                                    <h6 class="fw-bold mb-1" style="font-size: 14px;">Pickup Station</h6>
                                                    <a href="#" class="text-primary" style="font-size: 12px;">Details</a>
                                                </div>
                                                <p class="mb-1 text-dark" style="font-size: 13px;">Delivery GH₵
                                                    <?php echo htmlentities($pickup_fee); ?>
                                                    <span class="text-muted">(free delivery if order above GH₵
                                                        <?php echo htmlentities($free_threshold); ?>)</span>
                                                </p>
                                                <p class="mb-0 text-muted" style="font-size: 12px;">Ready for pickup between
                                                    <strong><?php echo $pickup_date_start; ?></strong> and
                                                    <strong><?php echo $pickup_date_end; ?></strong>
                                                </p>
                                            </div>
                                        </div>

                                        <!-- Door Delivery -->
                                        <div class="d-flex mb-3 pb-3 border-bottom wrapper-delivery">
                                            <div class="icon me-3 pr-2"><i class="fa fa-truck fa-2x text-warning"></i></div>
                                            <div class="content w-100">
                                                <div class="d-flex justify-content-between align-items-center">
                                                    <h6 class="fw-bold mb-1" style="font-size: 14px;">Door Delivery</h6>
                                                    <a href="#" class="text-primary" style="font-size: 12px;">Details</a>
                                                </div>
                                                <p class="mb-1 text-dark" style="font-size: 13px;">Delivery Fees GH₵
                                                    <?php echo htmlentities($door_fee); ?>
                                                </p>
                                                <p class="mb-0 text-muted" style="font-size: 12px;">Ready for delivery
                                                    between <strong><?php echo $door_date_start; ?></strong> and
                                                    <strong><?php echo $door_date_end; ?></strong>
                                                </p>
                                            </div>
                                        </div>

                                        <!-- Return Policy -->
                                        <div class="d-flex wrapper-delivery">
                                            <div class="icon me-3 pr-2"><i class="fa fa-undo fa-2x text-warning"></i></div>
                                            <div class="content w-100">
                                                <div class="d-flex justify-content-between align-items-center">
                                                    <h6 class="fw-bold mb-1" style="font-size: 14px;">Return Policy</h6>
                                                    <a href="#" class="text-primary" style="font-size: 12px;">Details</a>
                                                </div>
                                                <p class="mb-0 text-muted" style="font-size: 13px;">Free return within
                                                    <?php echo htmlentities(get_setting('return_policy_days', '15')); ?>
                                                    days for all eligible items.
                                                </p>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <!-- Details Accordion -->
                                <div class="am-accordion-details mt-4" id="productDetailsAccordion">

                                    <!-- Description -->
                                    <div class="accordion-item card mb-3 border-0">
                                        <h2 class="accordion-header card-header bg-white border-bottom-0 py-0"
                                            id="headingDesc">
                                            <button class="accordion-button bg-white text-dark fw-bold shadow-none py-3"
                                                type="button" data-bs-toggle="collapse" data-bs-target="#collapseDesc"
                                                aria-expanded="true">
                                                <i class="fa fa-file-text-o me-2 text-primary"></i> Product Description
                                            </button>
                                        </h2>
                                        <div id="collapseDesc" class="accordion-collapse collapse show"
                                            data-bs-parent="#productDetailsAccordion">
                                            <div class="accordion-body card-body pt-0 text-muted"
                                                style="font-size: 14px; line-height: 1.8;">
                                                <?php echo $row['productDescription']; ?>
                                            </div>
                                        </div>
                                    </div>



                                    <!-- Reviews -->
                                    <div class="accordion-item card mb-3 border-0">
                                        <h2 class="accordion-header card-header bg-white border-bottom-0 py-0"
                                            id="headingReview">
                                            <button
                                                class="accordion-button collapsed bg-white text-dark fw-bold shadow-none py-3"
                                                type="button" data-bs-toggle="collapse" data-bs-target="#collapseReview">
                                                <i class="fa fa-star-o me-2 text-primary"></i> Customer Reviews
                                                (<?php echo $num_reviews; ?>)
                                            </button>
                                        </h2>
                                        <div id="collapseReview" class="accordion-collapse collapse"
                                            data-bs-parent="#productDetailsAccordion">
                                            <div class="accordion-body card-body pt-0">
                                                <div class="reviews-list mb-4">
                                                    <?php
                                                    $qry_rev = mysqli_query($con, "SELECT * FROM productreviews WHERE productId='$pid' ORDER BY reviewDate DESC");
                                                    if (mysqli_num_rows($qry_rev) > 0) {
                                                        while ($rvw = mysqli_fetch_array($qry_rev)) {
                                                            ?>
                                                            <div class="review-card p-3 mb-3 border-light rounded">
                                                                <div class="d-flex align-items-center mb-2">
                                                                    <div class="avatar-circle me-3">
                                                                        <?php echo strtoupper(substr($rvw['name'], 0, 1)); ?>
                                                                    </div>
                                                                    <div>
                                                                        <h6 class="mb-0 fw-bold">
                                                                            <?php echo htmlentities($rvw['name']); ?>
                                                                        </h6>
                                                                        <small
                                                                            class="text-muted"><?php echo date('M d, Y', strtotime($rvw['reviewDate'])); ?></small>
                                                                    </div>
                                                                    <div class="ms-auto text-warning small">
                                                                        <?php
                                                                        $avg_rating = round(($rvw['quality'] + $rvw['price'] + $rvw['value']) / 3);
                                                                        for ($i = 1; $i <= 5; $i++) {
                                                                            echo ($i <= $avg_rating) ? '<i class="fa fa-star"></i>' : '<i class="fa fa-star-o"></i>';
                                                                        }
                                                                        ?>
                                                                    </div>
                                                                </div>
                                                                <p class="mb-1 fw-bold text-dark" style="font-size: 14px;">
                                                                    <?php echo htmlentities($rvw['summary']); ?>
                                                                </p>
                                                                <p class="mb-0 text-muted" style="font-size: 13px;">
                                                                    "<?php echo htmlentities($rvw['review']); ?>"</p>
                                                            </div>
                                                        <?php }
                                                    } else {
                                                        echo '<div class="text-center py-4 bg-light rounded"><i class="fa fa-comments-o fa-3x text-muted mb-2"></i><p class="text-muted mb-0">No reviews yet. Be the first to share your thoughts!</p></div>';
                                                    } ?>
                                                </div>

                                                <!-- Write Review Section -->
                                                <div class="write-review-container border-top pt-4">
                                                    <h5 class="fw-bold mb-4"><i class="fa fa-pencil me-2"></i> Share Your
                                                        Experience</h5>
                                                    <form action="" method="post" class="row g-4">
                                                        <div class="col-md-6">
                                                            <label
                                                                class="form-label small fw-bold text-uppercase tracking-wider text-muted">Your
                                                                Name</label>
                                                            <input type="text" name="name"
                                                                class="form-control border-light bg-light py-2"
                                                                placeholder="e.g. John Doe" required>
                                                        </div>
                                                        <div class="col-md-6">
                                                            <label
                                                                class="form-label small fw-bold text-uppercase tracking-wider text-muted">Review
                                                                Summary</label>
                                                            <input type="text" name="summary"
                                                                class="form-control border-light bg-light py-2"
                                                                placeholder="e.g. Amazing Quality!" required>
                                                        </div>

                                                        <div class="col-12">
                                                            <div class="row">
                                                                <div class="col-md-4 mb-3 mb-md-0">
                                                                    <label
                                                                        class="small fw-bold text-uppercase tracking-wider text-muted d-block mb-2">Product
                                                                        Quality</label>
                                                                    <div class="star-rating-input">
                                                                        <input type="radio" name="quality" value="5" id="q5"
                                                                            required><label for="q5"><i
                                                                                class="fa fa-star"></i></label>
                                                                        <input type="radio" name="quality" value="4"
                                                                            id="q4"><label for="q4"><i
                                                                                class="fa fa-star"></i></label>
                                                                        <input type="radio" name="quality" value="3"
                                                                            id="q3"><label for="q3"><i
                                                                                class="fa fa-star"></i></label>
                                                                        <input type="radio" name="quality" value="2"
                                                                            id="q2"><label for="q2"><i
                                                                                class="fa fa-star"></i></label>
                                                                        <input type="radio" name="quality" value="1"
                                                                            id="q1"><label for="q1"><i
                                                                                class="fa fa-star"></i></label>
                                                                    </div>
                                                                </div>
                                                                <div class="col-md-4 mb-3 mb-md-0">
                                                                    <label
                                                                        class="small fw-bold text-uppercase tracking-wider text-muted d-block mb-2">Fair
                                                                        Price</label>
                                                                    <div class="star-rating-input">
                                                                        <input type="radio" name="price" value="5" id="p5"
                                                                            required><label for="p5"><i
                                                                                class="fa fa-star"></i></label>
                                                                        <input type="radio" name="price" value="4"
                                                                            id="p4"><label for="p4"><i
                                                                                class="fa fa-star"></i></label>
                                                                        <input type="radio" name="price" value="3"
                                                                            id="p3"><label for="p3"><i
                                                                                class="fa fa-star"></i></label>
                                                                        <input type="radio" name="price" value="2"
                                                                            id="p2"><label for="p2"><i
                                                                                class="fa fa-star"></i></label>
                                                                        <input type="radio" name="price" value="1"
                                                                            id="p1"><label for="p1"><i
                                                                                class="fa fa-star"></i></label>
                                                                    </div>
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <label
                                                                        class="small fw-bold text-uppercase tracking-wider text-muted d-block mb-2">Overall
                                                                        Value</label>
                                                                    <div class="star-rating-input">
                                                                        <input type="radio" name="value" value="5" id="v5"
                                                                            required><label for="v5"><i
                                                                                class="fa fa-star"></i></label>
                                                                        <input type="radio" name="value" value="4"
                                                                            id="v4"><label for="v4"><i
                                                                                class="fa fa-star"></i></label>
                                                                        <input type="radio" name="value" value="3"
                                                                            id="v3"><label for="v3"><i
                                                                                class="fa fa-star"></i></label>
                                                                        <input type="radio" name="value" value="2"
                                                                            id="v2"><label for="v2"><i
                                                                                class="fa fa-star"></i></label>
                                                                        <input type="radio" name="value" value="1"
                                                                            id="v1"><label for="v1"><i
                                                                                class="fa fa-star"></i></label>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                        </div>

                                                        <div class="col-12">
                                                            <label
                                                                class="form-label small fw-bold text-uppercase tracking-wider text-muted">Detailed
                                                                Review</label>
                                                            <textarea name="review"
                                                                class="form-control border-light bg-light" rows="4"
                                                                placeholder="How did you find the product?"
                                                                required></textarea>
                                                        </div>
                                                        <div class="col-12 mt-4 text-end">
                                                            <button type="submit" name="submit"
                                                                class="btn btn-primary px-5 py-2 fw-bold shadow-sm">
                                                                Submit Review <i class="fa fa-paper-plane ms-2"></i>
                                                            </button>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- Alternative Products -->
                            <?php
                            $cid = $row['category'];
                            $subcid = $row['subCategory'];
                            $qry_alt = mysqli_query($con, "SELECT * FROM products WHERE (subCategory='$subcid' OR category='$cid') AND id != '$pid' AND approval_status='approved' LIMIT 4");
                            if (mysqli_num_rows($qry_alt) > 0) {
                                ?>
                                <div class="alternative-header">
                                    <h3>Alternative Products</h3>
                                </div>
                                <div class="row">
                                    <?php while ($rw = mysqli_fetch_array($qry_alt)) {
                                        $r_disc = 0;
                                        if ($rw['productPriceBeforeDiscount'] > $rw['productPrice']) {
                                            $r_disc = round((($rw['productPriceBeforeDiscount'] - $rw['productPrice']) / $rw['productPriceBeforeDiscount']) * 100);
                                        }
                                        ?>
                                        <div class="col-6 col-md-3 mb-4">
                                            <div class="am-card">
                                                <div class="img-wrap">
                                                    <a href="product-details.php?pid=<?php echo htmlentities($rw['id']); ?>">
                                                        <img src="__admin__/productimages/<?php echo htmlentities($rw['id']); ?>/<?php echo htmlentities($rw['productImage1']); ?>"
                                                            alt="<?php echo htmlspecialchars(html_entity_decode($rw['productName'], ENT_QUOTES)); ?>"
                                                            onerror="this.src='assets/images/blank.gif'">
                                                    </a>
                                                    <?php if ($r_disc > 0) { ?>
                                                        <div class="discount-tag">-<?php echo $r_disc; ?>%</div>
                                                    <?php } ?>
                                                </div>
                                                <h3 class="p-name"><a
                                                        href="product-details.php?pid=<?php echo htmlentities($rw['id']); ?>"><?php echo html_entity_decode($rw['productName'], ENT_QUOTES); ?></a>
                                                </h3>
                                                <div class="price-wrap">
                                                    <span
                                                        class="p-price">Ghs.<?php echo htmlentities($rw['productPrice']); ?></span>
                                                </div>
                                            </div>
                                        </div>
                                    <?php } ?>
                                </div>
                            <?php } ?>
                        </div>

                    <?php } ?>

                </div><!-- /.col -->
            </div><!-- /.row -->
        </div><!-- /.container -->
    </div><!-- /.body-content -->

    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const qtyContainer = document.getElementById('qtySelector');
            if (qtyContainer) {
                const input = document.getElementById('mainQty');
                qtyContainer.addEventListener('click', function (e) {
                    if (e.target.dataset.action === 'plus') {
                        input.value = parseInt(input.value) + 1;
                    } else if (e.target.dataset.action === 'minus') {
                        if (parseInt(input.value) > 1) {
                            input.value = parseInt(input.value) - 1;
                        }
                    }
                });
            }
        });
    </script>
    <?php include('includes/footer.php'); ?>

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

</html>