<?php
session_start();
error_reporting(0);
include('includes/config.php');
include_once('includes/settings.php');
if (strlen($_SESSION['login']) == 0) {
    header('location:login.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<?php include('includes/head.php'); ?>

<body class="cnt-home">
    <header class="header-style-1">
        <?php include('includes/top-header.php'); ?>
        <?php include('includes/main-header.php'); ?>
        <?php include('includes/menu-bar.php'); ?>
    </header>

    <div class="breadcrumb-container bg-light py-3 border-bottom mb-4">
        <div class="container">
            <h3>Wishlist Delete Test Page</h3>
        </div>
    </div>

    <div class="body-content pb-5">
        <div class="container">
            <div class="row g-3">
                <?php
                $ret = mysqli_query($con, "SELECT products.productName as pname, products.id as proid, products.productImage1 as pimage, products.productPrice as pprice, products.productPriceBeforeDiscount as pbefore, products.productAvailability as pavail, wishlist.productId as pid, wishlist.id as wid FROM wishlist JOIN products ON products.id=wishlist.productId WHERE wishlist.userId='" . $_SESSION['id'] . "' AND products.approval_status='approved'");
                $num = mysqli_num_rows($ret);
                if ($num > 0) {
                    while ($row = mysqli_fetch_array($ret)) {
                        $disc = 0;
                        if ($row['pbefore'] > 0 && $row['pbefore'] > $row['pprice']) {
                            $disc = round((($row['pbefore'] - $row['pprice']) / $row['pbefore']) * 100);
                        }
                        ?>
                        <div class="col-6 col-md-4 col-xl-3">
                            <div class="am-card" style="position: relative;">
                                <!-- TEST Delete Button -->
                                <button type="button" data-del-id="<?php echo htmlentities($row['wid']); ?>"
                                    class="btn-wishlist-delete"
                                    style="position: absolute; top: 8px; right: 8px; z-index: 10; color: #ff4757; background: white; border: 1px solid #eee; width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; box-shadow: 0 2px 4px rgba(0,0,0,0.1);"
                                    title="Remove from wishlist">
                                    <i class="fa fa-trash-o"></i>
                                </button>

                                <div class="img-wrap">
                                    <img src="__admin__/productimages/<?php echo htmlentities($row['pid']); ?>/<?php echo htmlentities($row['pimage']); ?>"
                                        alt="<?php echo htmlentities($row['pname']); ?>"
                                        onerror="this.src='assets/images/blank.gif'">
                                </div>

                                <div class="p-name">
                                    <?php echo htmlentities($row['pname']); ?>
                                </div>

                                <div class="price-wrap">
                                    <span class="p-price">
                                        <?php echo htmlentities($settings['currency_symbol'] ?? 'GH₵'); ?>
                                        <?php echo number_format($row['pprice'], 2); ?>
                                    </span>
                                </div>
                            </div>
                        </div>
                    <?php }
                } else { ?>
                    <div class="col-12 text-center py-5">
                        <h4>Your wishlist is empty</h4>
                    </div>
                <?php } ?>
            </div>

            <div class="alert alert-info mt-4">
                <h5>Debug Info:</h5>
                <p>Scripts.js timestamp:
                    <?php echo time(); ?>
                </p>
                <p>Check console (F12) when you click delete button</p>
            </div>
        </div>
    </div>

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

    <!-- Load jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <!-- Load scripts.js with UNIQUE timestamp to bypass cache -->
    <script src="assets/js/scripts.js?v=<?php echo microtime(true); ?>"></script>

    <!-- Inline test script -->
    <script>
        console.log('=================================');
        console.log('TEST PAGE LOADED');
        console.log('Current timestamp:', <?php echo time(); ?>);
        console.log('Scripts.js loaded with microtime:', <?php echo microtime(true); ?>);
        console.log('=================================');
 // Test if our handler is attached
        setTimeout(function () {
             console.log('Delete buttons found:', $('.btn-wishlist-delete').length);
        }, 1000);
    </script>
</body>

</html>