<?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 <?php wc_product_class('product-bed-three d-flex align-items-md-center flex-column flex-md-row gap-4 gap-md-0', $product); ?>>
<!-- Start Carousel Images -->
<div class="images flex-fill flex-grow-0 flex-shrink-0 w-100">
    <div class="swiper swiper-images-product-bed-three overflow-hidden position-relative">
        <div class="swiper-wrapper">
            <!-- Featured Image -->
            <div class="swiper-slide">
                <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_html($product_name);
} else {
    if ( wp_is_mobile() ) {
        echo wp_trim_words( $product_name, 5, '...' );
    } else {
        echo wp_trim_words( $product_name, 8, '...' );
    }
}
?>" width="170" height="168"
                    class="d-block w-100">
            </div>
            
            <?php
            // Get product gallery images
            $product = wc_get_product(get_the_ID());
            $gallery_images = $product->get_gallery_image_ids();
            
            // Display up to 2 gallery images
            $count = 0;
            foreach ($gallery_images as $image_id) {
                if ($count >= 2) break;
                $image_url = wp_get_attachment_image_url($image_id, 'full');
                ?>
                <div class="swiper-slide">
                    <img src="<?php echo esc_url($image_url); ?>" alt="<?php
$product_name = $product->get_name();
if ( wp_is_mobile() ) {
    echo wp_trim_words( $product_name, 5, '...' );
} else {
    echo wp_trim_words( $product_name, 8, '...' );
}
?>" width="170" height="168"
                        class="d-block w-100">
                </div>
                <?php
                $count++;
            }
            
            // If there are less than 2 gallery images, fill with placeholder
            while ($count < 2) {
                ?>
                <div class="swiper-slide">
                    <img src="<?php echo wc_placeholder_img_src(); ?>" alt="<?php
$product_name = $product->get_name();
if ( wp_is_mobile() ) {
    echo wp_trim_words( $product_name, 5, '...' );
} else {
    echo wp_trim_words( $product_name, 8, '...' );
}
?>" width="170" height="168"
                        class="d-block w-100">
                </div>
                <?php
                $count++;
            }
            ?>
        </div>
        <div class="swiper-pagination d-flex align-items-center justify-content-center gap-2"></div>
    </div>
</div>
<!-- End Carousel Images -->
    <div class="detail flex-grow-1">
        <div class="title-rate d-flex align-items-center justify-content-between gap-2">
            <h2 class="title-product overflow-hidden mb-0">
                <a href="<?= get_permalink( $product->get_id() ); ?>"> <?php
$product_name = $product->get_name();
if ( wp_is_mobile() ) {
    echo wp_trim_words( $product_name, 5, '...' );
} else {
    echo wp_trim_words( $product_name, 8, '...' );
}
?> </a>
            </h2>

        </div>
        <?php if ($first_category): ?>
        <a href="<?php echo esc_url($first_category['link']); ?>" class="category d-inline-block"> <?php echo esc_html($first_category['title']); ?> </a>
        <?php endif; ?>
        <!-- <ul class="options d-flex align-items-center flex-wrap">
            <li class="d-flex align-items-center">
                <span class="dot"></span>
                <?php esc_html_e('Weight:', 'kavehTheme'); ?>
                <span class="val"> <?php esc_html_e('2 kg', 'kavehTheme'); ?> </span>
            </li>
            <li class="d-flex align-items-center">
                <span class="dot"></span>
                <?php esc_html_e('Weight:', 'kavehTheme'); ?>:
                <span class="val"> <?php esc_html_e('2 kg', 'kavehTheme'); ?> </span>
            </li>
        </ul> -->
        <div class="btns-price d-flex align-items-center justify-content-between flex-wrap">
        <div class="btns d-flex align-items-center flex-grow-1">
    <?php
    $product_id = $product->get_id();
    $product_type = $product->get_type();
    $is_purchasable = $product->is_purchasable() && $product->is_in_stock();
    
    // View Product Button (always shown)
    ?>
    <a href="<?php echo esc_url($product->get_permalink()); ?>"
        class="btn btn-primary-3 btn-view d-flex align-items-center justify-content-between gap-2 flex-grow-1">
        <?php esc_html_e('View', 'kavehTheme'); ?>
        <i class="isax isax-arrow-left4 lh-1 fw-normal"></i>
    </a>

    <?php
    // Add to Cart Button (conditionally shown)
    if ($is_purchasable) {
        $add_to_cart_classes = [
            'btn',
            'btn-orange',
            'btn-add-to-cart',
            'd-flex',
            'align-items-center',
            'justify-content-center',
            'p-0',
            '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($product_type === 'variable' ? $product->get_permalink() : $product->add_to_cart_url()); ?>"
            data-product_id="<?php echo esc_attr($product_id); ?>"
            data-product_sku="<?php echo esc_attr($product->get_sku()); ?>"
            aria-label="<?php echo esc_attr($product->add_to_cart_description()); ?>"
            class="<?php echo esc_attr(implode(' ', $add_to_cart_classes)); ?>"
            <?php echo ($product_type === 'simple') ? 'data-quantity="1"' : ''; ?>>
            <i class="isax isax-bag lh-1"></i>
        </a>
    <?php } ?>
</div>
            <div class="price d-flex flex-column">
            <?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;
        }
        ?>
        <style>
            .product-bed-three .detail .price span {
    font-size: 14px !important;
    line-height: 10px !important;

}
        </style>
            </div>
        </div>
    </div>
</div>