// sellbazar/assets/js/custom.js

// Header Sticky
$(window).scroll(function() {
    var scroll = $(window).scrollTop();
    if (scroll > 100) {
        $('.sticky_header').addClass('fixed');
    } else {
        $('.sticky_header').removeClass('fixed');
    }
});

// Mobile Search Toggle
function search() {
    $('#search_id').toggleClass('search_toggle');
}

// Quantity increment/decrement
function Increment() {
    var qty = parseInt($('.product_qty').val()) || 1;
    $('.product_qty').val(qty + 1);
}

function decrement() {
    var qty = parseInt($('.product_qty').val()) || 1;
    if (qty > 1) {
        $('.product_qty').val(qty - 1);
    }
}

// Add to wishlist
function addToWishList(productId) {
    $.ajax({
        url: 'wishlist-ajax.php',
        type: 'POST',
        data: { product_id: productId },
        dataType: 'json',
        success: function(res) {
            if (res.success) {
                toastr.success('Added to wishlist!');
            } else {
                toastr.info(res.message || 'Please login first');
            }
        }
    });
}

// Product search
function searchProduct(value) {
    if (value.length > 2) {
        $.ajax({
            url: 'search-ajax.php',
            type: 'POST',
            data: { keyword: value },
            dataType: 'json',
            success: function(res) {
                if (res.html) {
                    $('#search_product_show').html(res.html);
                    $('#product_search').show();
                } else {
                    $('#product_search').hide();
                }
            }
        });
    } else {
        $('#product_search').hide();
    }
}

// Toastr fallback if not loaded
if (typeof toastr === 'undefined') {
    var toastr = {
        success: function(msg) { alert(msg); },
        error: function(msg) { alert('Error: ' + msg); },
        info: function(msg) { alert(msg); },
        warning: function(msg) { alert('Warning: ' + msg); }
    };
}