<?php
defined('ABSPATH') || exit;

global $product;

// Ensure $product is valid
if (!$product || !$product->is_visible()) {
    return;
}

// Get category information
$category_ids = $product->get_category_ids();
$first_category = null;

if (!empty($category_ids)) {
    $first_category_id = $category_ids[0];
    $category = get_term($first_category_id, 'product_cat');

    if (!is_wp_error($category)) {
        $first_category = [
            'title' => $category->name,
            'link' => get_term_link($first_category_id, 'product_cat')
        ];
    }
}
?>
<div class="product-travel-equipment-item d-flex flex-column bg-white h-100 position-relative">
<?php if ($product->is_on_sale()): ?>

    <span
        class="discount position-absolute d-flex align-items-center justify-content-center rounded-pill text-white fw-bold">
                    <?php if (class_exists('kavehWoo')) {
                $offer_percentage = kavehWoo::showofferpercentage($product);
                echo $offer_percentage;
            } ?> </span>
            <?php endif; ?>
    <a href="<?= get_permalink($product->get_id()); ?>" class="image mx-auto">
        <img src="<?php if (get_the_post_thumbnail_url()): ?><?php the_post_thumbnail_url('full'); ?><?php else: ?><?php echo wc_placeholder_img_src(); ?><?php endif; ?>"
            alt="<?php
$product_name = $product->get_name();
$show_full_titles = kaveh_get_option('kaveh_product_title_display', false);
if ($show_full_titles) {
    echo esc_attr($product_name);
} else {
    if ( wp_is_mobile() ) {
        echo esc_attr(wp_trim_words( $product_name, 5, '...' ));
    } else {
        echo esc_attr(wp_trim_words( $product_name, 8, '...' ));
    }
}
?>" width="183" height="183" class="d-block mx-auto w-auto">
    </a>
    <h2 class="title-product fw-bold overflow-hidden">
        <a href="<?= get_permalink($product->get_id()); ?>"> <?php
$product_name = $product->get_name();
$show_full_titles = kaveh_get_option('kaveh_product_title_display', false);
if ($show_full_titles) {
    echo esc_html($product_name);
} else {
    if ( wp_is_mobile() ) {
        echo wp_trim_words( $product_name, 5, '...' );
    } else {
        echo wp_trim_words( $product_name, 8, '...' );
    }
}
?> </a>
    </h2>

    <div class="d-flex align-items-center justify-content-end justify-content-sm-between gap-2">
        <?php
        $product_id = $product->get_id();
        $product_type = $product->get_type();
        $is_purchasable = $product->is_purchasable() && $product->is_in_stock();

        // Base classes (always include these)
        $add_to_cart_classes = [
            'btn-add-to-cart',
            'd-none',
            'd-sm-flex',
            'align-items-center',
            'justify-content-center'
        ];

        // Additional classes for purchasable products
        if ($is_purchasable) {
            $add_to_cart_classes[] = 'product_type_' . $product_type;

            if ($product_type === 'simple' && $product->supports('ajax_add_to_cart')) {
                $add_to_cart_classes[] = 'add_to_cart_button';
                $add_to_cart_classes[] = 'ajax_add_to_cart';
            }
        }
        ?>

        <a href="<?php echo esc_url($is_purchasable ? ($product_type === 'variable' ? $product->get_permalink() : $product->add_to_cart_url()) : $product->get_permalink()); ?>"
            <?php if ($is_purchasable): ?> data-product_id="<?php echo esc_attr($product_id); ?>"
                data-product_sku="<?php echo esc_attr($product->get_sku()); ?>" <?php if ($product_type === 'simple'): ?>data-quantity="1" <?php endif; ?> <?php endif; ?>
            aria-label="<?php echo esc_attr($product->add_to_cart_description()); ?>"
            class="<?php echo esc_attr(implode(' ', $add_to_cart_classes)); ?>">
            <i class="isax isax-bag lh-1"></i>
        </a>
        <div class="price d-flex flex-column justify-content-end fw-bold">
            <?php
            // Use WooCommerce's built-in price HTML output
            $price_html = $product->get_price_html();
            // If we have price HTML (not empty)
            if ($price_html) {
                echo $price_html;
            }
            ?>
        </div>
    </div>
</div>