<?php
/**
 * Render Book Hero [Author Pro] Block
 */

$block_id = isset($attributes['blockId']) ? $attributes['blockId'] : '';
if (empty($block_id)) {
    $block_id = 'awt-book-hero-' . uniqid();
}

// Data Handling to support Editor vs Frontend
$book_id = get_the_ID();
$is_editor = defined('REST_REQUEST') && REST_REQUEST === true;

// If in editor and not on a book post, try to find the latest book for preview
if ($is_editor && get_post_type($book_id) !== 'book') {
    $min_query = new WP_Query([
        'post_type' => 'book',
        'posts_per_page' => 1,
        'post_status' => 'publish'
    ]);
    if ($min_query->have_posts()) {
        $min_query->the_post();
        $book_id = get_the_ID();
        wp_reset_postdata();
    }
}

// 1. Fetch Data
// Basic
$title = get_the_title($book_id);
$permalink = get_permalink($book_id);
$cover_url = get_the_post_thumbnail_url($book_id, 'full');
$description = get_post_meta($book_id, '_rsbs_short_description', true);

if (empty($description)) {
    // fallback to excerpt or content trimmed
    $post_obj = get_post($book_id);
    $description = $post_obj->post_excerpt ? $post_obj->post_excerpt : wp_trim_words($post_obj->post_content, 30);
}

// Stats
$pages = get_post_meta($book_id, '_rsbs_book_pages', true);
$year = get_post_meta($book_id, '_rsbs_book_publish_year', true);
$language = get_post_meta($book_id, '_rsbs_book_language', true);

// Pricing
$regular_price = get_post_meta($book_id, '_rsbs_book_price', true);
$sale_price = get_post_meta($book_id, '_rsbs_book_sale_price', true);

// Format Prices
$regular_price_clean = floatval(preg_replace('/[^0-9.]/', '', $regular_price));
$sale_price_clean = floatval(preg_replace('/[^0-9.]/', '', $sale_price));
$has_sale = $sale_price_clean > 0 && $sale_price_clean < $regular_price_clean;

$savings_pct = 0;
if ($has_sale && $regular_price_clean > 0) {
    $savings_pct = round((($regular_price_clean - $sale_price_clean) / $regular_price_clean) * 100);
}

// Buttons
$buy_btn_text = get_post_meta($book_id, '_rsbs_buy_btn_text', true);
$buy_btn_link = get_post_meta($book_id, '_rsbs_buy_btn_link', true);
if (empty($buy_btn_text)) $buy_btn_text = 'Buy on Amazon';

// Secondary Buttons/Stores
$stores_list = get_post_meta($book_id, 'rswpbs_also_available_website_list', true);


// Formats
$formats_data = get_post_meta($book_id, 'rswpbs_book_formats', true);
$formats_string = '';
if (!empty($formats_data) && is_array($formats_data)) {
    $format_names = [];
    foreach ($formats_data as $cat) {
        if (!empty($cat['name'])) {
            $format_names[] = $cat['name'];
        }
    }
    if (!empty($format_names)) {
        $formats_string = implode(', ', $format_names);
    }
}


// Reviews Calculation
$review_args = [
    'post_type' => 'book_reviews',
    'posts_per_page' => -1,
    'meta_query' => [
        [
            'key' => '_rswpbs_reviewed_book',
            'value' => $book_id,
            'compare' => '='
        ]
    ]
];
$reviews_query = new WP_Query($review_args);
$total_reviews = $reviews_query->found_posts;
$sum_rating = 0;

if ($reviews_query->have_posts()) {
    while ($reviews_query->have_posts()) {
        $reviews_query->the_post();
        $r_val = get_post_meta(get_the_ID(), '_rswpbs_rating', true);
        $sum_rating += floatval($r_val);
    }
    wp_reset_postdata();
}

$avg_rating = ($total_reviews > 0) ? round($sum_rating / $total_reviews, 1) : 0; 


// 2. Attributes
$show_breadcrumbs = isset($attributes['showBreadcrumbs']) ? $attributes['showBreadcrumbs'] : true;
$show_stats = isset($attributes['showStats']) ? $attributes['showStats'] : true;
$show_badge = isset($attributes['showBadge']) ? $attributes['showBadge'] : true;
$show_review_count = isset($attributes['showReviewCount']) ? $attributes['showReviewCount'] : true;

$badge_text = !empty($attributes['badgeText']) ? $attributes['badgeText'] : '#1 New York Times Bestseller';
$sub_title = !empty($attributes['subTitle']) ? $attributes['subTitle'] : ''; 
if (empty($sub_title)) {
   $sub_title = get_post_meta($book_id, '_rsbs_book_reading_age', true);
   if($sub_title) $sub_title = esc_html($reading_age_label) . ": " . $sub_title;
}

$formats_title = !empty($attributes['formatsTitle']) ? $attributes['formatsTitle'] : 'Available Formats';

$pages_label = !empty($attributes['pagesLabel']) ? $attributes['pagesLabel'] : 'Pages';
$year_label = !empty($attributes['yearLabel']) ? $attributes['yearLabel'] : 'Year';
$language_label = !empty($attributes['languageLabel']) ? $attributes['languageLabel'] : 'Language';
$reading_age_label = !empty($attributes['readingAgeLabel']) ? $attributes['readingAgeLabel'] : 'Reading Age';
$review_link_text = !empty($attributes['reviewLinkText']) ? $attributes['reviewLinkText'] : 'Read %s Reviews';

// Colors
$section_bg = isset($attributes['sectionBg']) ? $attributes['sectionBg'] : '';
$primary_color = isset($attributes['primaryColor']) ? $attributes['primaryColor'] : (isset($attributes['titleColor']) ? $attributes['titleColor'] : '');
$secondary_color = isset($attributes['secondaryColor']) ? $attributes['secondaryColor'] : (isset($attributes['textColor']) ? $attributes['textColor'] : '');
$accent_color = isset($attributes['accentColor']) ? $attributes['accentColor'] : '';

$btn_bg = isset($attributes['btnBg']) ? $attributes['btnBg'] : ($accent_color ? $accent_color : '');
$btn_text = isset($attributes['btnText']) ? $attributes['btnText'] : '#ffffff';
$btn_hover_bg = isset($attributes['btnHoverBg']) ? $attributes['btnHoverBg'] : '';
$btn_hover_text = isset($attributes['btnHoverText']) ? $attributes['btnHoverText'] : '';


// 3. Dynamic CSS
$anchor = isset($attributes['anchor']) ? $attributes['anchor'] : '';
$element_id = !empty($anchor) ? $anchor : $block_id;
$css_id = '#' . $element_id;
$custom_css = "";

if ($section_bg) $custom_css .= "$css_id .awt-section-bg { background-color: $section_bg !important; }";
if ($primary_color) {
    $custom_css .= "$css_id .text-primary { color: $primary_color !important; }";
    $custom_css .= "$css_id .border-primary { border-color: $primary_color !important; }";
    $custom_css .= "$css_id .bg-primary { background-color: $primary_color !important; }";
    $custom_css .= "$css_id .hover\:text-primary:hover { color: $primary_color !important; }";
}
if ($secondary_color) {
    $custom_css .= "$css_id .text-secondary { color: $secondary_color !important; }";
    $custom_css .= "$css_id .prose-slate { color: $secondary_color !important; }";
}
if ($accent_color) {
    $custom_css .= "$css_id .text-accent { color: $accent_color !important; }";
    $custom_css .= "$css_id .bg-accent { background-color: $accent_color !important; }";
    $custom_css .= "$css_id .border-accent { border-color: $accent_color !important; }";
    $custom_css .= "$css_id .hover\:text-accent:hover { color: $accent_color !important; }";
    $custom_css .= "$css_id .hover\:bg-accent:hover { background-color: $accent_color !important; }";
    $custom_css .= "$css_id .hover\:border-accent:hover { border-color: $accent_color !important; }";
    
    // SVG Fills
    $custom_css .= "$css_id .awt-svg-accent { color: $accent_color !important; }";
    $custom_css .= "$css_id .awt-blur-accent { background-color: " . rswpthemes_awt_hex2rgba($accent_color, 0.05) . " !important; }";
}

// Button Overrides
if ($btn_bg) $custom_css .= "$css_id .awt-btn-primary { background-color: $btn_bg !important; }";
if ($btn_text) $custom_css .= "$css_id .awt-btn-primary { color: $btn_text !important; }";
if ($btn_hover_bg) $custom_css .= "$css_id .awt-btn-primary:hover { background-color: $btn_hover_bg !important; }";
if ($btn_hover_text) $custom_css .= "$css_id .awt-btn-primary:hover { color: $btn_hover_text !important; }";


// Wrapper Attributes
$wrapper_attributes = get_block_wrapper_attributes([
    'class' => 'awt-book-hero',
    'id'    => $element_id,
]);

// Helper
if (!function_exists('rswpthemes_awt_hex2rgba')) {
    function rswpthemes_awt_hex2rgba($color, $opacity = false) {
        $default = 'rgb(0,0,0)';
        if(empty($color)) return $default; 
        if ($color[0] == '#' ) {
            $color = substr( $color, 1 );
        }
        if (strlen($color) == 6) {
                $hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
        } elseif ( strlen( $color ) == 3 ) {
                $hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
        } else {
                return $default;
        }
        $rgb =  array_map('hexdec', $hex);
        if($opacity){
            if(abs($opacity) > 1) $opacity = 1.0;
            $output = 'rgba('.implode(",",$rgb).','.$opacity.')';
        } else {
            $output = 'rgb('.implode(",",$rgb).')';
        }
        return $output;
    }
}
?>

<?php if (!empty($custom_css)) : ?>
    <style><?php echo $custom_css; ?></style>
<?php endif; ?>

<div <?php echo $wrapper_attributes; ?>>
    <?php if ($show_breadcrumbs) : ?>
    <div class="pt-4 pb-4 bg-paper border-b border-gray-100 awt-breadcrumb-section">
        <div class="awt-container">
            <nav class="flex text-xs font-medium text-gray-400" aria-label="Breadcrumb">
                <ol class="flex list-none items-center space-x-2">
                    <li><a href="<?php echo home_url(); ?>" class="hover:text-primary text-gray-400 transition">Home</a></li>
                    <li>
                         <!-- Chevron Right -->
                         <svg class="w-2.5 h-2.5" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M471.1 297.4C483.6 309.9 483.6 330.2 471.1 342.7L279.1 534.7C266.6 547.2 246.3 547.2 233.8 534.7C221.3 522.2 221.3 501.9 233.8 489.4L403.2 320L233.9 150.6C221.4 138.1 221.4 117.8 233.9 105.3C246.4 92.8 266.7 92.8 279.2 105.3L471.2 297.3z"/></svg>
                    </li>
                    <li><a href="<?php echo get_post_type_archive_link('book'); ?>" class="hover:text-primary text-gray-400 transition">Books</a></li>
                    <li>
                        <!-- Chevron Right -->
                        <svg class="w-2.5 h-2.5" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M471.1 297.4C483.6 309.9 483.6 330.2 471.1 342.7L279.1 534.7C266.6 547.2 246.3 547.2 233.8 534.7C221.3 522.2 221.3 501.9 233.8 489.4L403.2 320L233.9 150.6C221.4 138.1 221.4 117.8 233.9 105.3C246.4 92.8 266.7 92.8 279.2 105.3L471.2 297.3z"/></svg>
                    </li>
                    <li class="text-accent font-bold"><?php echo esc_html($title); ?></li>
                </ol>
            </nav>
        </div>
    </div>
    <?php endif; ?>

    <section class="py-12 lg:py-20 bg-white awt-section-bg">
        <div class="awt-container">
            <div class="grid grid-cols-1 lg:grid-cols-12 gap-12 lg:gap-20">

                <div class="lg:col-span-5 flex flex-col items-center">
                    <div class="relative w-full max-w-md mx-auto group">
                        <div
                            class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-[120%] h-[120%] bg-accent/5 rounded-full blur-3xl -z-10 awt-blur-accent"
                            <?php if ($cover_url) echo 'style="background-image: url(' . esc_url($cover_url) . '); background-size: cover; opacity: 0.15;"'; ?>
                        >
                        </div>

                        <div
                            class="relative rounded-lg shadow-2xl overflow-hidden transform transition duration-500 hover:scale-[1.02]">
                            <?php if ($cover_url) : ?>
                                <img src="<?php echo esc_url($cover_url); ?>"
                                    alt="<?php echo esc_attr($title); ?>" class="w-full h-auto object-cover">
                            <?php else : ?>
                                <div class="w-full h-96 bg-gray-200 flex items-center justify-center text-gray-400">No Image</div>
                            <?php endif; ?>

                            <a href="#"
                                class="absolute bottom-6 right-6 w-12 h-12 bg-white rounded-full shadow-lg flex items-center justify-center text-accent hover:bg-accent hover:text-white transition group-hover:scale-110 awt-svg-accent">
                                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" viewBox="0 0 16 16"> <path d="M16 8s-3-5.5-8-5.5S0 8 0 8s3 5.5 8 5.5S16 8 16 8zM1.173 8a13.133 13.133 0 0 1 1.66-2.043C4.12 4.668 5.88 3.5 8 3.5c2.12 0 3.879 1.168 5.168 2.457A13.133 13.133 0 0 1 14.828 8c-.058.087-.122.183-.195.288-.335.48-.83 1.12-1.465 1.755C11.879 11.332 10.119 12.5 8 12.5c-2.12 0-3.879-1.168-5.168-2.457A13.134 13.134 0 0 1 1.172 8z"/> <path d="M8 5.5a2.5 2.5 0 1 0 0 5 2.5 2.5 0 0 0 0-5z"/> </svg>
                            </a>
                        </div>
                    </div>

                    <?php if ($show_stats) : ?>
                    <div class="grid grid-cols-3 gap-4 w-full max-w-md mt-8 border-t border-b border-gray-100 py-6">
                        <div class="text-center border-r border-gray-100">
                            <span class="block text-xl font-bold text-primary font-serif"><?php echo esc_html($pages); ?></span>
                            <span class="text-xs text-gray-400 uppercase tracking-wider"><?php echo esc_html($pages_label); ?></span>
                        </div>
                        <div class="text-center border-r border-gray-100">
                            <span class="block text-xl font-bold text-primary font-serif"><?php echo esc_html($year); ?></span>
                            <span class="text-xs text-gray-400 uppercase tracking-wider"><?php echo esc_html($year_label); ?></span>
                        </div>
                        <div class="text-center">
                            <span class="block text-xl font-bold text-primary font-serif"><?php echo esc_html($language); ?></span>
                            <span class="text-xs text-gray-400 uppercase tracking-wider"><?php echo esc_html($language_label); ?></span>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>

                <div class="lg:col-span-7 flex flex-col justify-center">
                    <div class="mb-6">
                        <?php if ($show_badge && !empty($badge_text)) : ?>
                        <div
                            class="inline-flex items-center gap-2 px-3 py-1 bg-yellow-50 text-yellow-700 rounded-full text-xs font-bold uppercase tracking-wider mb-4 awt-badge-container">
                             <!-- Trophy Icon -->
                            <svg class="w-3 h-3 mr-1" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M208.3 64L432.3 64C458.8 64 480.4 85.8 479.4 112.2C479.2 117.5 479 122.8 478.7 128L528.3 128C554.4 128 577.4 149.6 575.4 177.8C567.9 281.5 514.9 338.5 457.4 368.3C441.6 376.5 425.5 382.6 410.2 387.1C390 415.7 369 430.8 352.3 438.9L352.3 512L416.3 512C434 512 448.3 526.3 448.3 544C448.3 561.7 434 576 416.3 576L224.3 576C206.6 576 192.3 561.7 192.3 544C192.3 526.3 206.6 512 224.3 512L288.3 512L288.3 438.9C272.3 431.2 252.4 416.9 233 390.6C214.6 385.8 194.6 378.5 175.1 367.5C121 337.2 72.2 280.1 65.2 177.6C63.3 149.5 86.2 127.9 112.3 127.9L161.9 127.9C161.6 122.7 161.4 117.5 161.2 112.1C160.2 85.6 181.8 63.9 208.3 63.9zM165.5 176L113.1 176C119.3 260.7 158.2 303.1 198.3 325.6C183.9 288.3 172 239.6 165.5 176zM444 320.8C484.5 297 521.1 254.7 527.3 176L475 176C468.8 236.9 457.6 284.2 444 320.8z"/></svg>
                            <?php echo esc_html($badge_text); ?>
                        </div>
                        <?php endif; ?>
                        <h1 class="text-4xl lg:text-5xl font-serif font-bold text-primary mb-2"><?php echo esc_html($title); ?></h1>
                        <?php if ($sub_title) : ?>
                        <p class="text-lg text-secondary font-medium"><?php echo esc_html($sub_title); ?></p>
                        <?php endif; ?>
                    </div>

                    <?php if ($show_review_count) : ?>
                    <div class="flex items-center gap-4 mb-8">
                        <div class="flex text-yellow-500 text-sm stars awt-svg-accent">
                             <?php 
                                $stars = ($avg_rating > 0) ? round($avg_rating) : 0;
                                for($i=0; $i<5; $i++) {
                                    if ($i < $stars) {
                                      echo '<svg class="w-3 h-3" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M341.5 45.1C337.4 37.1 329.1 32 320.1 32C311.1 32 302.8 37.1 298.7 45.1L225.1 189.3L65.2 214.7C56.3 216.1 48.9 222.4 46.1 231C43.3 239.6 45.6 249 51.9 255.4L166.3 369.9L141.1 529.8C139.7 538.7 143.4 547.7 150.7 553C158 558.3 167.6 559.1 175.7 555L320.1 481.6L464.4 555C472.4 559.1 482.1 558.3 489.4 553C496.7 547.7 500.4 538.8 499 529.8L473.7 369.9L588.1 255.4C594.5 249 596.7 239.6 593.9 231C591.1 222.4 583.8 216.1 574.8 214.7L415 189.3L341.5 45.1z"/></svg>';
                                    } else {
                                       echo '<svg class="w-3 h-3 text-gray-300" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M341.5 45.1C337.4 37.1 329.1 32 320.1 32C311.1 32 302.8 37.1 298.7 45.1L225.1 189.3L65.2 214.7C56.3 216.1 48.9 222.4 46.1 231C43.3 239.6 45.6 249 51.9 255.4L166.3 369.9L141.1 529.8C139.7 538.7 143.4 547.7 150.7 553C158 558.3 167.6 559.1 175.7 555L320.1 481.6L464.4 555C472.4 559.1 482.1 558.3 489.4 553C496.7 547.7 500.4 538.8 499 529.8L473.7 369.9L588.1 255.4C594.5 249 596.7 239.6 593.9 231C591.1 222.4 583.8 216.1 574.8 214.7L415 189.3L341.5 45.1z"/></svg>';
                                    }
                                }
                             ?>
                        </div>
                        <a href="#reviews"
                            class="text-sm text-gray-500 font-medium hover:text-accent cursor-pointer underline decoration-dotted transition">
                            <?php printf(esc_html($review_link_text), number_format($total_reviews)); ?>    
                        </a>
                    </div>
                    <?php endif; ?>

                    <div class="prose prose-slate text-secondary mb-8 leading-relaxed">
                        <?php echo wp_kses_post($description); ?>
                    </div>

                    <div class="bg-paper p-6 rounded-xl border border-gray-100 mb-8 awt-pricing-box">
                        <?php 
                        
                        $regular_price_clean = '';
                        $sale_price_clean = '';
                        $has_sale = false;
                        $savings_pct = 0;

                        
                        if ( function_exists('rswpbs_get_linked_product') ) {
                            $linked_product = rswpbs_get_linked_product();
                            
                            if ( $linked_product ) {
                                $regular_price = $linked_product->get_regular_price();
                                $sale_price = $linked_product->get_sale_price();

                                
                                if ( '' !== $regular_price && null !== $regular_price ) {
                                    $has_sale = $sale_price && $sale_price < $regular_price;
                                    
                                    
                                    $regular_price_clean = number_format( (float) $regular_price, 2 );
                                    $sale_price_clean = $sale_price ? number_format( (float) $sale_price, 2 ) : '';
                                    
                                    if ( $has_sale ) {
                                        $savings_pct = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
                                    }
                                }
                            }
                        }

      
                        if ( ! empty( $regular_price_clean ) ) : ?>
                            <div class="flex flex-wrap items-end gap-3 mb-6">
                                <?php if ( $has_sale ) : ?>
                                    <span class="text-3xl font-serif font-bold text-accent"><?php echo esc_html( $sale_price_clean ); ?></span>
                                    <span class="text-lg text-gray-400 line-through decoration-red-500 decoration-2 mb-1"><?php echo esc_html( $regular_price_clean ); ?></span>
                                    <span class="text-xs font-bold text-green-600 bg-green-100 px-2 py-1 rounded ml-2 mb-1">Save <?php echo $savings_pct; ?>%</span>
                                <?php else: ?>
                                    <span class="text-3xl font-serif font-bold text-accent"><?php echo esc_html( $regular_price_clean ); ?></span>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>

                        <div class="flex flex-wrap flex-col sm:flex-row gap-4 mb-4">

                            <?php 
                            echo shortcode_exists('rswpbs_book_cart') ? do_shortcode( "[rswpbs_book_cart/]" ) : '' ;
                        
                            if ($buy_btn_link) : ?>
                            <a target="_blank" href="<?php echo esc_url($buy_btn_link); ?>"
                                class="w-full sm:w-auto self-start bg-accent awt-btn-primary text-white font-bold py-3.5 px-6 rounded text-center hover:bg-orange-700 transition shadow-lg shadow-orange-100 flex items-center justify-center gap-2">
                                <?php echo esc_html($buy_btn_text); ?>
                            </a>
                            <?php endif; ?>
                            
                            <?php 
                            if (!empty($stores_list) && is_array($stores_list)) {
                                foreach ($stores_list as $store) {
                                    $store_name = isset($store['website_name']) ? $store['website_name'] : 'Store';
                                    $store_url = isset($store['book_url']) ? $store['book_url'] : '#';
                                    
                                    echo '<a target="_blank" href="'.esc_url($store_url).'" class="w-full sm:w-auto self-start bg-white border border-gray-300 text-primary font-bold py-3.5 px-6 rounded text-center hover:border-accent hover:text-accent transition flex items-center justify-center gap-2">';
                                    echo esc_html($store_name);
                                    echo '</a>';
                                }
                            }
                            ?>
                        </div>

                        <?php if ($formats_string) : ?>
                        <p class="text-xs text-center text-gray-400 flex items-center justify-center">
                            <svg class="w-3 h-3 mr-2 text-green-500" fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 640"><path d="M530.8 134.1C545.1 144.5 548.3 164.5 537.9 178.8L281.9 530.8C276.4 538.4 267.9 543.1 258.5 543.9C249.1 544.7 240 541.2 233.4 534.6L105.4 406.6C92.9 394.1 92.9 373.8 105.4 361.3C117.9 348.8 138.2 348.8 150.7 361.3L252.2 462.8L486.2 141.1C496.6 126.8 516.6 123.6 530.9 134z"/></svg> 
                            Available in <?php echo esc_html($formats_string); ?>
                        </p>
                        <?php endif; ?>
                    </div>

                    <div>
                        <h3 class="text-sm font-bold text-primary uppercase tracking-widest mb-4"><?php echo esc_html($formats_title); ?></h3>
                        
                        <?php if (!empty($formats_data) && is_array($formats_data)) : ?>
                        <ul class="border border-gray-100 rounded-xl list-none pl-0 bg-paper overflow-hidden">
                            <?php foreach ($formats_data as $format) : 
                                $f_name = !empty($format['name']) ? $format['name'] : '';
                                $f_price = !empty($format['price']) ? $format['price'] : '';
                                $f_link = !empty($format['link']) ? $format['link'] : '';
                            ?>
                            <li class="group border-b border-gray-100 last:border-0 transition hover:bg-white">
                                <a target="_blank" href="<?php echo esc_url($f_link ? $f_link : '#'); ?>" class="flex items-center justify-between p-4 no-underline <?php echo empty($f_link) ? 'pointer-events-none' : ''; ?>">
                                    <div class="flex items-center gap-3">
                                        <svg class="w-4 h-4 awt-svg-accent text-accent" fill="currentColor" viewBox="0 0 512 512">
                                        <path d="M256 512A256 256 0 1 0 256 0a256 256 0 1 0 0 512zM369 209L241 337c-9.4 9.4-24.6 9.4-33.9 0l-64-64c-9.4-9.4-9.4-24.6 0-33.9s24.6-9.4 33.9 0l47 47L335 175c9.4-9.4 24.6-9.4 33.9 0s9.4 24.6 0 33.9z"/>
                                        </svg>
                                        <span class="text-sm font-bold text-primary"><?php echo esc_html($f_name); ?></span>
                                    </div>
                                    <div class="flex items-center gap-2">
                                        <span class="text-sm font-bold text-accent"><?php echo esc_html($f_price); ?></span>
                                        <?php if ($f_link) : ?>
                                        <svg class="w-3 h-3 text-gray-400 group-hover:text-accent transition" fill="currentColor" viewBox="0 0 320 512">
                                            <path d="M310.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-192 192c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L242.7 256 73.4 86.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l192 192z"/>
                                        </svg>
                                        <?php endif; ?>
                                    </div>
                                </a>
                            </li>
                            <?php endforeach; ?>
                        </ul>
                        <?php endif; ?>
                    </div>

                </div>
            </div>
        </div>
    </section>
</div>
