<?php
/**
 * Custom Product Template
 *
 * This file serves as a custom template for product pages within the Boxyfy plugin.
 * It integrates various functionalities, including:
 *
 * - Displaying product images and implementing an interactive image slider.
 * - Fetching and displaying product rankings within their respective categories.
 * - Providing a detailed price comparison section, including the Amazon price and additional merchant prices.
 * - Calculating and displaying price savings and discount percentages.
 * - Showcasing Amazon Prime availability for products.
 * - Including dynamically generated labels for product rankings such as "Best Price", "Test Winner", and "Comparison Winner".
 * - Displaying customer reviews and star ratings with customizable styles.
 * - Featuring an author information section at the bottom, displaying the author's name, expertise, and biography.
 * - Enabling users to set up a price alert (if the option is enabled in the settings).
 * - Offering advantages and disadvantages lists to highlight key product aspects.
 * - Providing extensive customization through options such as font sizes, colors, and background settings.
 *
 * The template supports responsive design, ensuring optimal display across different screen sizes.
 * It utilizes WordPress functions, custom metadata, and stored plugin settings to dynamically populate product details.
 *
 * @package Boxyfy
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

get_header(); // WordPress Header einfügen

if ( have_posts() ) :
    while ( have_posts() ) :
        the_post();

        // Benutzerdefinierte Einstellungen abrufen.
        $title_font_size                              = get_option( 'boxyfy_title_font_size', 24 );
        $title_font_size_mobile                       = $title_font_size - 4;
        $tsproduct_main_imageslider_dot_color         = get_option( 'boxyfy_main_imageslider_dot_color', '#c1e0ff' );
        $tsproduct_main_imageslider_dot_active_color  = get_option( 'boxyfy_main_imageslider_dot_active_color', '#0173ab' );
        $content_font_size                            = get_option( 'boxyfy_content_font_size', 15 );
        $content_font_size_list                       = $content_font_size + 3;
        $product_content_background_color             = get_option( 'boxyfy_content_background_color' );
        $product_info_top_font_size                   = get_option( 'boxyfy_produktinfos_oben_font_size', 16 );
        $product_info_top_maincolor                   = get_option( 'boxyfy_produktinfos_oben_font_maincolor', '#000' );
        $product_fontsize_other_rankings              = $product_info_top_font_size - 4; // zieht 4 ab.
        $product_fontsize_other_rankings_front        = $product_info_top_font_size - 2; // zieht 2 ab.
        $product_infolabels_top_fontweight            = get_option( 'boxyfy_produktinfos_head_fontweight', 600 );
        $product_amazon_preis_zeile_background_color  = get_option( 'boxyfy_amazon_preis_zeile_background_color', '#ffffff' );
        $product_weitere_preise_zeilen_background_color = get_option( 'boxyfy_weitere_preise_zeilen_background_color', '#ffffff' );
        $product_all_buttons_icon_and_fontcolor       = get_option( 'boxyfy_all_buttons_icon_and_fontcolor', '#ffffff' );
        $product_box_lowest_price_backgroundcolor     = get_option( 'boxyfy_box_lowest_price_backgroundcolor' , '#ededed' );
        $product_box_lowest_price_font_and_border     = get_option( 'boxyfy_box_lowest_price_border_and_font' );
        $tsproduct_autorbox_expertise_tag             = get_option( 'boxyfy_autorbox_expertise_tag', '#0173ab' );

        // Einstellung bzw. Daten für Produktbewertung.
        $bewertung               = get_post_meta( get_the_ID(), '_tsprodukt_bewertung', true );
        $green_min               = get_option( 'boxyfy_green_rating_min', 80 );
        $green_max               = get_option( 'boxyfy_green_rating_max', 100 );
        $yellow_min              = get_option( 'boxyfy_yellow_rating_min', 55 );
        $yellow_max              = get_option( 'boxyfy_yellow_rating_max', 79 );
        $red_min                 = get_option( 'boxyfy_red_rating_min', 0 );
        $red_max                 = get_option( 'boxyfy_red_rating_max', 54 );

        // Hintergrundfarbe und Gradient für Shop-Buttons abrufen.
        $button_color            = get_option( 'boxyfy_preise_buttons_background_color', '#016698' );
        $button_gradient         = get_option( 'boxyfy_preise_buttons_gradient' );
        $button_style = $button_gradient
            ? 'background: ' . esc_attr( $button_gradient ) . ';'
            : 'background-color: ' . esc_attr( $button_color ) . ';';

        // Amazon-Preis abrufen.
        $price = get_post_meta( get_the_ID(), 'tsprodukt_price1', true );

		// UVP-Amazon-Preis abrufen und umwandeln.
		$preis_uvp_produkteseite = boxyfy_convert_price_to_float( get_post_meta( get_the_ID(), 'tsprodukt_saving_basis', true ) );

		if ( ! is_numeric( $preis_uvp_produkteseite ) ) {
    		$preis_uvp_produkteseite = 0;
		}

        // Prime-Status abrufen.
        $prime_status = get_post_meta( get_the_ID(), 'tsprodukt_prime_status', true );
		// Sales-Rank abrufen.
		$sales_rank = get_post_meta( get_the_ID(), 'tsprodukt_sales_rank', true );
        // Berechnung Prozent-Ersparnis gegenüber UVP-Preis.
        $preis_ersparnis_prozent = ( $price > 0 && $preis_uvp_produkteseite > 0 )
            ? floor( 100 - ( ( $price / $preis_uvp_produkteseite ) * 100 ) )
            : 0;
        // ASIN und Partnertag abrufen, URL generieren.
        $asin    = get_post_meta( get_the_ID(), 'tsprodukt_asin_price', true );
        $options = get_option(
            'boxyfy_amazon_settings',
            array(
                'access_key'  => '',
                'secret_key'  => '',
                'partner_tag' => '',
                'host'        => 'webservices.amazon.de',
                'region'      => 'eu-west-1',
            )
        );
        $partner_tag = $options['partner_tag'];
        $purchase_url_produktseite = 'https://www.amazon.de/dp/' . esc_attr( $asin ) . '?tag=' . esc_attr( $partner_tag ) . '&linkCode=ogi&th=1';
        // Hole Amazon-Bilder.
        $amazon_image_ids = get_post_meta( get_the_ID(), 'tsprodukt_amazon_images', true );
        ?>

        <div class="ts-container">
            <div class="ts-main">

                <?php
                // Ausgabe der Labels.
                $ts1_productsite_taglabels              = get_post_meta( get_the_ID(), '_boxyfy_produktlabels', true );
                $ts1custom_label                        = get_post_meta( get_the_ID(), '_boxyfy_custom_label', true );
                $ts1product_toplabel_fontsize           = $product_info_top_font_size - 3;
                $boxyfy_content_label_top_background_color = get_option( 'boxyfy_content_labeltop_background_color', '#0173ab' );
				$boxyfy_checkmark_svg = '<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> 
<path d="M10.5 15.25C10.307 15.2353 10.1276 15.1455 9.99998 15L6.99998 12C6.93314 11.8601 6.91133 11.7029 6.93756 11.55C6.96379 11.3971 7.03676 11.2562 7.14643 11.1465C7.2561 11.0368 7.39707 10.9638 7.54993 10.9376C7.70279 10.9114 7.86003 10.9332 7.99998 11L10.47 13.47L19 5.00004C19.1399 4.9332 19.2972 4.91139 19.45 4.93762C19.6029 4.96385 19.7439 5.03682 19.8535 5.14649C19.9632 5.25616 20.0362 5.39713 20.0624 5.54999C20.0886 5.70286 20.0668 5.86009 20 6.00004L11 15C10.8724 15.1455 10.6929 15.2353 10.5 15.25Z" fill="currentColor"/>
<path d="M12 21C10.3915 20.9974 8.813 20.5638 7.42891 19.7443C6.04481 18.9247 4.90566 17.7492 4.12999 16.34C3.54037 15.29 3.17596 14.1287 3.05999 12.93C2.87697 11.1721 3.2156 9.39921 4.03363 7.83249C4.85167 6.26578 6.1129 4.9746 7.65999 4.12003C8.71001 3.53041 9.87134 3.166 11.07 3.05003C12.2641 2.92157 13.4719 3.03725 14.62 3.39003C14.7224 3.4105 14.8195 3.45215 14.9049 3.51232C14.9903 3.57248 15.0622 3.64983 15.116 3.73941C15.1698 3.82898 15.2043 3.92881 15.2173 4.03249C15.2302 4.13616 15.2214 4.2414 15.1913 4.34146C15.1612 4.44152 15.1105 4.53419 15.0425 4.61352C14.9745 4.69286 14.8907 4.75712 14.7965 4.80217C14.7022 4.84723 14.5995 4.87209 14.4951 4.87516C14.3907 4.87824 14.2867 4.85946 14.19 4.82003C13.2186 4.52795 12.1987 4.43275 11.19 4.54003C10.193 4.64212 9.22694 4.94485 8.34999 5.43003C7.50512 5.89613 6.75813 6.52088 6.14999 7.27003C5.52385 8.03319 5.05628 8.91361 4.77467 9.85974C4.49307 10.8059 4.40308 11.7987 4.50999 12.78C4.61208 13.777 4.91482 14.7431 5.39999 15.62C5.86609 16.4649 6.49084 17.2119 7.23999 17.82C8.00315 18.4462 8.88357 18.9137 9.8297 19.1953C10.7758 19.4769 11.7686 19.5669 12.75 19.46C13.747 19.3579 14.713 19.0552 15.59 18.57C16.4349 18.1039 17.1818 17.4792 17.79 16.73C18.4161 15.9669 18.8837 15.0864 19.1653 14.1403C19.4469 13.1942 19.5369 12.2014 19.43 11.22C19.4201 11.1169 19.4307 11.0129 19.461 10.9139C19.4914 10.8149 19.5409 10.7228 19.6069 10.643C19.6728 10.5631 19.7538 10.497 19.8453 10.4485C19.9368 10.3999 20.0369 10.3699 20.14 10.36C20.2431 10.3502 20.3471 10.3607 20.4461 10.3911C20.5451 10.4214 20.6372 10.471 20.717 10.5369C20.7969 10.6028 20.863 10.6839 20.9115 10.7753C20.9601 10.8668 20.9901 10.9669 21 11.07C21.1821 12.829 20.842 14.6026 20.0221 16.1695C19.2022 17.7363 17.9389 19.0269 16.39 19.88C15.3288 20.4938 14.1495 20.8755 12.93 21C12.62 21 12.3 21 12 21Z" fill="currentColor"/>
				</svg>';

               if ( ! empty( $ts1_productsite_taglabels ) && is_array( $ts1_productsite_taglabels ) ) {
                    foreach ( $ts1_productsite_taglabels as $tslabel_key ) {
                        switch ( $tslabel_key ) {
    						case 'preissieger':
        						echo '<div class="tsprodukt-label">' . $boxyfy_checkmark_svg . ' Preissieger</div>';
        						break;
    						case 'vergleichssieger':
        						echo '<div class="tsprodukt-label">' . $boxyfy_checkmark_svg . ' Vergleichssieger</div>';
        						break;
    						case 'highlight':
        						echo '<div class="tsprodukt-label">' . $boxyfy_checkmark_svg . ' Highlight</div>';
        						break;
    						case 'testsieger':
        						echo '<div class="tsprodukt-label">' . $boxyfy_checkmark_svg . ' Testsieger</div>';
        						break;
						}
                    }
                }
                if ( ! empty( $ts1custom_label ) ) {
    				echo '<div class="tsprodukt-label">' . $boxyfy_checkmark_svg . ' ' . esc_html( $ts1custom_label ) . '</div>';
                }
                ?>

                <!-- Die zuvor vorhandene Inline-CSS-Block wurde entfernt -->

                <h1 class="tsprodukt-content-h1"><?php the_title(); ?></h1>

                <div class="tsprodukt-container">
                    <div class="tsprodukt-image">
                        <div class="tsprodukt-slider-container">
                            <?php
                            if ( has_post_thumbnail() ) {
                                $thumbnail_url = get_the_post_thumbnail_url( get_the_ID(), 'full' );
                                echo '<div class="tsprodukt_image">';
                                echo '<img src="' . esc_url( $thumbnail_url ) . '"/>';
                                echo '</div>';
                            }
                            $image_ids = get_post_meta( get_the_ID(), '_tsprodukt_image_ids', true );
                            if ( is_array( $image_ids ) && ! empty( $image_ids ) ) {
                                foreach ( $image_ids as $index => $image_id ) {
                                    $image_url = wp_get_attachment_url( $image_id );
                                    echo '<div class="tsprodukt_image">';
                                    echo '<img src="' . esc_url( $image_url ) . '"/>';
                                    echo '</div>';
                                }
                            }
                            if ( ! empty( $amazon_image_ids ) && is_array( $amazon_image_ids ) ) {
                                foreach ( $amazon_image_ids as $index => $image_id ) {
                                    $image_url = wp_get_attachment_url( $image_id );
                                    if ( $image_url ) {
                                        echo '<div class="tsprodukt_image">';
                                        echo '<img src="' . esc_url( $image_url ) . '"/>';
                                        echo '</div>';
                                    }
                                }
                            }
                            ?>
                            <button class="tsprodukt-slider-button tsprodukt-prev">❮</button>
                            <button class="tsprodukt-slider-button tsprodukt-next">❯</button>
                            <div class="tsprodukt-pagination"></div>
                        </div>
                    </div>

                    <div class="tsprodukt-content">
                        <div class="produktseite-ranking-ausgabe-container">
                            <?php
                            $ranking_value = get_post_meta( get_the_ID(), 'tsprodukt_ranking', true );
                            $terms = get_the_terms( get_the_ID(), 'boxyfy_tsprodukt_kategorie' );
                            if ( $terms && ! is_wp_error( $terms ) ) {
                                $kategorie      = $terms[0]->name;
                                $kategorie_slug = $terms[0]->slug;
                                $args = array(
                                    'post_type'      => 'boxyfy_tsprodukte',
                                    'posts_per_page' => -1,
                                    'tax_query'      => array(
                                        array(
                                            'taxonomy' => 'boxyfy_tsprodukt_kategorie',
                                            'field'    => 'slug',
                                            'terms'    => sanitize_title( $kategorie_slug ),
                                        ),
                                    ),
                                    'meta_key' => 'tsprodukt_ranking',
                                    'orderby'  => 'meta_value_num',
                                    'order'    => 'ASC',
                                );
                                $query = new WP_Query( $args );
                                if ( $query->have_posts() ) {
                                    $products_array = array();
                                    while ( $query->have_posts() ) {
                                        $query->the_post();
                                        $products_array[] = array(
                                            'id'    => get_the_ID(),
                                            'title' => get_the_title(),
                                        );
                                    }
                                    $gesamt_anzahl = $query->found_posts;
                                    wp_reset_postdata();
                                    $current_id    = get_queried_object_id();
                                    $current_index = null;
                                    foreach ( $products_array as $index => $prod ) {
                                        if ( $prod['id'] === $current_id ) {
                                            $current_index = $index;
                                            break;
                                        }
                                    }
                                    if ( null !== $current_index ) {
                                        $current_position = $current_index + 1;
                                        echo '<div class="tsplatz-noten-container">';
                                        printf(
											'<span>' . esc_html__( 'Platz %1$s von %2$s in %3$s', 'boxyfy' ) . '</span>',
											esc_html( $current_position ),
											esc_html( $gesamt_anzahl ),
											esc_html( $kategorie )
										);

                                        if ( ! empty( $bewertung ) ) {
                                            if ( $bewertung >= $green_min && $bewertung <= $green_max ) {
        										$note_class = 'note-green';
                                            } elseif ( $bewertung >= $yellow_min && $bewertung <= $yellow_max ) {
        										$note_class = 'note-yellow';
    										} else {
        										$note_class = 'note-red';
    										
											}
                                           
                                        }
                                        echo '</div>';
                                        $hide_rating = get_post_meta( get_the_ID(), 'tsprodukte_hide_rating', true );
                                        $show_rating = ( empty( $hide_rating ) || '1' !== $hide_rating ) ? '1' : '0';
                                        $tsproduct_top_starrating   = get_post_meta( get_the_ID(), 'tsprodukte_star_rating', true );
                                        $tsproduct_star_rating_size = get_post_meta( get_the_ID(), 'tsprodukte_star_rating_size', true ) ?: '20';
                                        $rating_label_raw = get_post_meta( get_the_ID(), 'tsprodukte_rating_label', true );
										$tsproduct_rating_label = ! empty( $rating_label_raw ) ? $rating_label_raw : esc_html__( 'Kundenbewertung', 'boxyfy' );
                                        $default_star_color = get_option( 'boxyfy_default_star_color', '#FFD700' );
                                        if ( ! empty( $tsproduct_top_starrating ) && '1' === $show_rating ) {
                                        ?>
							
										<div class="boxyfy-rating-true-grid">
    										<!-- Erste Zeile -->
   				 							<div class="boxyfy-rating-cell">
        										<div class="tsnotenausgabe <?php echo esc_attr( $note_class ); ?>">
            										<?php echo esc_html( $bewertung ); ?>
        										</div>
   											</div>

    										<div class="boxyfy-rating-cell">
        										<div class="boxyfy-rating-stars-wrapper">
            										<div class="boxyfy-rating-stars">
                										<?php
                										$full_stars = floor( $tsproduct_top_starrating );
                										$half_star  = ( $tsproduct_top_starrating - $full_stars ) >= 0.5 ? 1 : 0;
               				 							$empty_stars = 5 - $full_stars - $half_star;
                										for ( $i = 0; $i < $full_stars; $i++ ) {
                    										echo '<i class="fas fa-star"></i>';
                										}
                										if ( $half_star ) {
                    										echo '<i class="fas fa-star-half-alt"></i>';
                										}
                										for ( $i = 0; $i < $empty_stars; $i++ ) {
                    										echo '<i class="far fa-star"></i>';
                										}
                										?>
            										</div>
            										<div class="boxyfy-rating-average">
                										<?php echo esc_html( number_format( $tsproduct_top_starrating, 1, ',', '' ) ); ?>
            										</div>
        										</div>
    										</div>

    										<div class="boxyfy-rating-cell">
        										<?php if ( ! empty( $sales_rank ) ) : ?>
            										<div class="tsverkaufsrang-wert">
														<?php echo esc_html__( 'Platz', 'boxyfy' ) . ' ' . esc_html( $sales_rank ); ?>
													</div>
        										<?php endif; ?>
    										</div>

    										<!-- Zweite Zeile -->
   	 										<div class="boxyfy-rating-cell">
        										<div class="tsrating"><?php esc_html_e( 'Note', 'boxyfy' ); ?></div>
    										</div>

    										<div class="boxyfy-rating-cell">
        										<div class="boxyfy-rating-label"><?php echo esc_html( $tsproduct_rating_label ); ?></div>
    										</div>

    										<div class="boxyfy-rating-cell">
         										<div class="tsverkaufsrang-label boxyfy-salesrank-tooltip"
     												data-tooltip="<?php esc_attr_e( 'Der Verkaufsrang auf Amazon zeigt die Beliebtheit eines Produkts innerhalb einer Kategorie an. Je niedriger der Rang, desto häufiger wird das Produkt verkauft.', 'boxyfy' ); ?>">
    												<?php esc_html_e( 'Verkaufsrang?', 'boxyfy' ); ?>
												</div>
    										</div>
										</div>

                                            <?php
                                        }
                                        function boxyfy_shorten_title( $title, $length = 60 ) {
                                            return mb_strimwidth( $title, 0, $length, '...' );
                                        }
                                        echo '<div class="better-worse-tscontainer">';
                                        if ( $current_index > 0 ) {
                                            $better_product = $products_array[ $current_index - 1 ];
                                            $better_position = $current_position - 1;
                                            $better_product_title = boxyfy_shorten_title( $better_product['title'], 100 );
                                            $better_bewertung = get_post_meta( $better_product['id'], '_tsprodukt_bewertung', true );
                                            $better_thumbnail_url = get_the_post_thumbnail_url( $better_product['id'], 'medium' );
                                            echo '<a href="' . esc_url( get_permalink( $better_product['id'] ) ) . '" class="tsproduct-tsbox-rec">';
                                            echo '<div>' . sprintf( esc_html__( 'Platz %d', 'boxyfy' ), $better_position ) . '</div>';
                                            echo '<span class="tsrating-better-product">' . esc_html( $better_bewertung ) . '</span>';
                                            echo '<img src="' . esc_url( $better_thumbnail_url ) . '" alt="' . esc_attr( $better_product_title ) . '">';
                                            echo '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="#11a62f"><title>arrow top right</title><polygon points="7 7 15.586 7 5.293 17.293 6.707 18.707 17 8.414 17 17 19 17 19 5 7 5 7 7"/></svg>';
                                            echo '<p>' . esc_html( $better_product_title ) . '</p>';
                                            echo '</a>';
                                        }
                                        if ( $current_index < count( $products_array ) - 1 ) {
                                            $worse_product = $products_array[ $current_index + 1 ];
                                            $worse_position = $current_position + 1;
                                            $worse_product_title = boxyfy_shorten_title( $worse_product['title'], 100 );
                                            $worse_bewertung = get_post_meta( $worse_product['id'], '_tsprodukt_bewertung', true );
                                            $worse_thumbnail_url = get_the_post_thumbnail_url( $worse_product['id'], 'medium' );
                                            echo '<a href="' . esc_url( get_permalink( $worse_product['id'] ) ) . '" class="tsproduct-tsbox-rec">';
                                            echo '<div>' . sprintf( esc_html__( 'Platz %d', 'boxyfy' ), $worse_position ) . '</div>';
                                            echo '<span class="tsrating-worse-product">' . esc_html( $worse_bewertung ) . '</span>';
                                            echo '<img src="' . esc_url( $worse_thumbnail_url ) . '" alt="' . esc_attr( $worse_product_title ) . '">';
                                            echo '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="#f93333"><title>Arrow bottom right</title><polygon points="17 15.586 6.707 5.293 5.293 6.707 15.586 17 7 17 7 19 19 19 19 7 17 7 17 15.586"/></svg>';
                                            echo '<p>' . esc_html( $worse_product_title ) . '</p>';
                                            echo '</a>';
                                        }
                                        echo '</div>';
                                    } else {
                                        echo 'Produkt bitte einer Kategorie zuordnen und/oder ein Ranking vergeben.<br>';
                                    }
                                } else {
                                    echo 'Produkt bitte einer Kategorie zuordnen und/oder ein Ranking vergeben.<br>';
                                }
                            } else {
                                echo 'Produkt bitte einer Kategorie zuordnen und/oder ein Ranking vergeben.<br>';
                            }
                            ?>
                        </div>

                        <?php
                        $produkt_info = get_post_meta( get_the_ID(), 'tsprodukt_info', true );
                        if ( ! empty( $produkt_info ) ) {
                            echo '<div class="tsprodukt-produktinfo-list">';
                            $max_visible = 6;
                            $total_items = count( $produkt_info );
                            foreach ( $produkt_info as $index => $info ) {
                                $hidden_class = ( $index >= $max_visible ) ? ' tsprodukt-hidden' : '';
                                echo '<div class="tsprodukt-produktinfo-item' . esc_attr( $hidden_class ) . '">';
                                echo '<span class="tsprodukt-info-label">' . esc_html( $info['name'] ) . '</span>';
                                echo '<span class="tsprodukt-info-value">' . esc_html( $info['value'] ) . ' ' . esc_html( $info['unit'] ) . '</span>';
                                echo '</div>';
                            }
                            echo '</div>';
                            if ( $total_items > $max_visible ) {
                                echo '<button class="tsprodukt-show-more">' . esc_html__( 'Mehr Infos anzeigen', 'boxyfy' ) . '</button>';
								echo '<button class="tsprodukt-show-less">' . esc_html__( 'Weniger Infos anzeigen', 'boxyfy' ) . '</button>';
                            }
                        }
                        ?>
						
                        <?php
                        if ( ! function_exists( 'boxyfy_convert_price_to_float' ) ) {
                            function boxyfy_convert_price_to_float( $price_string ) {
                                $price_string = str_replace( '.', '', $price_string );
                                $price_string = str_replace( ',', '.', $price_string );
                                return (float) $price_string;
                            }
                        }
                        $amazon_price = ! empty( $price ) ? boxyfy_convert_price_to_float( $price ) : PHP_INT_MAX;
                        $weitere_preise = get_post_meta( get_the_ID(), 'boxyfy_weitere_preise', true );
                        $preise_array   = array();
                        if ( $amazon_price < PHP_INT_MAX ) {
                            $preise_array['amazon'] = $amazon_price;
                        }
                        if ( ! empty( $weitere_preise ) ) {
                            foreach ( $weitere_preise as $key => $preis_feld ) {
                                if ( ! empty( $preis_feld['preis'] ) ) {
                                    $preise_array[ "weitere_$key" ] = boxyfy_convert_price_to_float( $preis_feld['preis'] );
                                }
                            }
                        }
                        if ( empty( $preise_array ) ) {
                            echo '<p>Bitte einen Amazon-Preis oder einen weiteren Preis im Backend anlegen.</p>';
                        } else {
                            $guenstigster_preis = min( $preise_array );
                            $guenstigster_anbieter_key = $guenstigster_preis !== PHP_INT_MAX ? array_search( $guenstigster_preis, $preise_array, true ) : null;
                        }
                        ?>

                        <div class="tsprodukt-preisvergleich-container">
                            <?php if ( ! empty( $price ) ) : ?>
                                <p>
                                    <span>
                                        <?php echo esc_html( $price ); ?>
                                        <?php if ( $preis_ersparnis_prozent > 0 ) : ?>
                                            <span class="preis-ersparnis-produktseite">-<?php echo esc_html( round( $preis_ersparnis_prozent, 0 ) ); ?>%</span>
                                            <span class="uvp-preis-ausgabe">
												<?php esc_html_e( 'UVP:', 'boxyfy' ); ?>
												<?php echo esc_html( $preis_uvp_produkteseite ); ?>
											</span>
                                        <?php endif; ?>
                                    </span>
                                    <?php
									$prime_status = get_post_meta( get_the_ID(), 'tsprodukt_prime_status', true );
									if ( 'yes' === $prime_status ) {
   	 									echo '<span class="prime-status-produktseite">⛟ ' . esc_html__( 'Kostenloser Versand mit Prime', 'boxyfy' ) . '</span>';
									} elseif ( 'no' === $prime_status ) {
    									echo '<span class="prime-status-produktseite">⛟ ' . esc_html__( 'Kein kostenloser Versand mit Prime', 'boxyfy' ) . '</span>';
									} else {
    									echo '<span class="prime-status-produktseite">⛟ ' . esc_html__( 'Prime-Status nicht verfügbar', 'boxyfy' ) . '</span>';
									}
									?>
                                    <?php if ( 'amazon' === $guenstigster_anbieter_key ) : ?>
                                        <span class="guenstigster-gesamtpreis">
   				 							<span><?php echo esc_html__( 'Günstigster Preis ✓', 'boxyfy' ); ?></span>
    										<span>
        										<?php echo esc_html__( 'Kaufen für nur', 'boxyfy' ); ?>
        										<?php
        										$price_cleaned = str_replace( '.', '', $price );
        										$price_cleaned = str_replace( ',', '.', $price_cleaned );
        										echo esc_html( number_format( (float) $price_cleaned, 2, ',', '.' ) );
        										?>
         										€
    										</span>
										</span>
									
                                    <?php endif; ?>
                                    <img class="tsprodukt-shop-logo" src="<?php echo esc_url( plugin_dir_url( __DIR__ ) . 'assets/images/amazon_logo.webp' ); ?>" alt="Amazon-Logo">
                                    <a href="<?php echo esc_url( $purchase_url_produktseite ); ?>" class="amazon-button-produktseite" target="_blank" rel="sponsored">
                                        <svg
                                            fill="currentColor"
                                            version="1.1"
                                            id="Capa_1"
                                            xmlns="http://www.w3.org/2000/svg"
                                            xmlns:xlink="http://www.w3.org/1999/xlink"
                                            width="30px"
                                            height="30px"
                                            viewBox="0 0 902.86 902.86"
                                            xml:space="preserve"
                                        >
                                            <g>
                                                <g>
                                                    <path d="M671.504,577.829l110.485-432.609H902.86v-68H729.174L703.128,179.2L0,178.697l74.753,399.129h596.751V577.829z
                                                        M685.766,247.188l-67.077,262.64H131.199L81.928,246.756L685.766,247.188z"/>
                                                    <path d="M578.418,825.641c59.961,0,108.743-48.783,108.743-108.744s-48.782-108.742-108.743-108.742H168.717
                                                        c-59.961,0-108.744,48.781-108.744,108.742s48.782,108.744,108.744,108.744c59.962,0,108.743-48.783,108.743-108.744
                                                        c0-14.4-2.821-28.152-7.927-40.742h208.069c-5.107,12.59-7.928,26.342-7.928,40.742
                                                        C469.675,776.858,518.457,825.641,578.418,825.641z M209.46,716.897c0,22.467-18.277,40.744-40.743,40.744
                                                        c-22.466,0-40.744-18.277-40.744-40.744c0-22.465,18.277-40.742,40.744-40.742C191.183,676.155,209.46,694.432,209.46,716.897z
                                                        M619.162,716.897c0,22.467-18.277,40.744-40.743,40.744s-40.743-18.277-40.743-40.744c0-22.465,18.277-40.742,40.743-40.742
                                                        S619.162,694.432,619.162,716.897z"/>
                                                </g>
                                            </g>
                                        </svg>
                                        zum Shop
                                    </a>
                                </p>
                            <?php endif; ?>

                            <?php
							$preise = get_post_meta( get_the_ID(), 'boxyfy_weitere_preise', true );
							if ( ! empty( $preise ) ) {
    							echo '<div class="produkt-button-weitere-preise-wrapper">';
    							foreach ( $preise as $key => $preis_feld ) {
        							if ( ! empty( $preis_feld['preis'] ) && ! empty( $preis_feld['shop'] ) ) {
            							$current_price = floatval( str_replace( ',', '.', $preis_feld['preis'] ) );
            							$inkl_versand   = isset( $preis_feld['inkl_versandkosten'] ) ? $preis_feld['inkl_versandkosten'] : '';
            							$zzgl_versand   = isset( $preis_feld['zzgl_versandkosten'] ) ? $preis_feld['zzgl_versandkosten'] : '';
            							$kostenfrei     = isset( $preis_feld['versandkosten_kostenfrei'] ) && '1' === $preis_feld['versandkosten_kostenfrei'];
            							echo '<div class="produkt-button-weitere-preise">';
            							echo '<span class="produkt-preis-weitere">' . esc_html( number_format( $current_price, 2, ',', '.' ) ) . ' ' . esc_html__( '€', 'boxyfy' ) . '</span>';
            							if ( $kostenfrei ) {
                							echo '<span class="versand-status-produktseite">⛟ ' . esc_html__( 'Kostenfreie Lieferung', 'boxyfy' ) . '</span>';
            							} elseif ( ! empty( $inkl_versand ) ) {
                							echo '<span class="versand-status-produktseite">⛟ ' . esc_html__( 'Versandkosten sind inkl.', 'boxyfy' ) . ' (' . esc_html( $inkl_versand ) . ' ' . esc_html__( '€', 'boxyfy' ) . ')</span>';
            							} elseif ( ! empty( $zzgl_versand ) ) {
                							echo '<span class="versand-status-produktseite">⛟ ' . esc_html__( 'zzgl. Versandkosten:', 'boxyfy' ) . ' ' . esc_html( $zzgl_versand ) . ' ' . esc_html__( '€', 'boxyfy' ) . '</span>';
            							}
           					 			if ( isset( $guenstigster_anbieter_key ) && "weitere_$key" === $guenstigster_anbieter_key ) {
                							echo '<div class="guenstigster-gesamtpreis">';
                							echo '<span>' . esc_html__( 'Günstigster Preis ✓', 'boxyfy' ) . '</span>';
                							echo '<span>' . esc_html__( 'Kaufen für nur', 'boxyfy' ) . ' ' . esc_html( number_format( $current_price, 2, ',', '.' ) ) . ' ' . esc_html__( '€', 'boxyfy' ) . '</span>';
                							echo '</div>';
            							}
            							if ( ! empty( $preis_feld['logo'] ) ) {
                							$logo_url = esc_url( $preis_feld['logo'] );
                							echo '<img class="tsprodukt-shopx-logo" src="' . esc_url( $logo_url ) . '" alt="' . esc_attr( $preis_feld['shop'] ) . '" />';
            							}
            								echo '<a href="' . esc_url( $preis_feld['link'] ) . '" target="_blank" class="produkt-weitere-buttons-shops">';
            							?>

                                        <svg
                                            fill="currentColor"
                                            version="1.1"
                                            id="Capa_1"
                                            xmlns="http://www.w3.org/2000/svg"
                                            xmlns:xlink="http://www.w3.org/1999/xlink"
                                            width="30px"
                                            height="30px"
                                            viewBox="0 0 902.86 902.86"
                                            xml:space="preserve"
                                        >
                                            <g>
                                                <g>
                                                    <path d="M671.504,577.829l110.485-432.609H902.86v-68H729.174L703.128,179.2L0,178.697l74.753,399.129h596.751V577.829z
                                                        M685.766,247.188l-67.077,262.64H131.199L81.928,246.756L685.766,247.188z"/>
                                                    <path d="M578.418,825.641c59.961,0,108.743-48.783,108.743-108.744s-48.782-108.742-108.743-108.742H168.717
                                                        c-59.961,0-108.744,48.781-108.744,108.742s48.782,108.744,108.744,108.744c59.962,0,108.743-48.783,108.743-108.744
                                                        c0-14.4-2.821-28.152-7.927-40.742h208.069c-5.107,12.59-7.928,26.342-7.928,40.742
                                                        C469.675,776.858,518.457,825.641,578.418,825.641z M209.46,716.897c0,22.467-18.277,40.744-40.743,40.744
                                                        c-22.466,0-40.744-18.277-40.744-40.744c0-22.465,18.277-40.742,40.744-40.742C191.183,676.155,209.46,694.432,209.46,716.897z
                                                        M619.162,716.897c0,22.467-18.277,40.744-40.743,40.744s-40.743-18.277-40.743-40.744c0-22.465,18.277-40.742,40.743-40.742
                                                        S619.162,694.432,619.162,716.897z"/>
                                                </g>
                                            </g>
                                        </svg>
                                        <?php
                                        echo 'zum Shop';
                                        echo '</a>';
                                        echo '</div>';
                                    }
                                }
                                echo '</div>';
                            }
                            ?>
                        </div>
						
						<!------- Affiliate-Disclaimer-Textbereich ------->
						
						<?php
						// Prüfe, ob der Affiliate Disclaimer aktiviert ist
							if ( 'yes' === get_option('boxyfy_show_affiliate_disclaimer', 'no') ) {
    							$disclaimer_text = get_option('boxyfy_affiliate_disclaimer_text', '');
    								if ( ! empty( $disclaimer_text ) ) {
        								echo '<div class="affiliate-disclaimer">' . wp_kses_post( $disclaimer_text ) . '</div>';
    								}
							}
						?>
                        
                        <div class="preiswecker-container">
                            <?php
                            if ( 'yes' === get_option( 'boxyfy_show_preiswecker', 'no' ) ) {
                                echo '<div class="preiswecker-container">';
                                echo do_shortcode( '[boxyfy_preiswecker id="' . get_the_ID() . '"]' );
                                echo '</div>';
                            }
                            ?>
                        </div>
                    </div>
                </div>

                <div class="tsprodukt-post-content">
                    <?php
                    $vorteile   = get_post_meta( get_the_ID(), 'boxyfy_produkt_vorteile', true );
                    $nachteile  = get_post_meta( get_the_ID(), 'boxyfy_produkt_nachteile', true );
                    if ( ! empty( $vorteile ) || ! empty( $nachteile ) ) :
                        ?>
                        <div class="boxyfy_vorteile_nachteile_container">
                            <?php if ( ! empty( $vorteile ) ) : ?>
                                <div class="boxyfy_vorteile">
                                    <h2>
    									<img src="<?php echo esc_url( plugins_url( 'assets/images/check-icon-white.svg', __DIR__ ) ); ?>" alt="<?php esc_attr_e( 'Symbol für Vorteil', 'boxyfy' ); ?>" class="ts-header-icon">
    									<?php esc_html_e( 'Vorteile', 'boxyfy' ); ?>
									</h2>
                                    <ul>
                                        <?php
                                        foreach ( $vorteile as $vorteil ) :
                                            ?>
                                            <li>
                                                <img src="<?php echo esc_url( plugins_url( 'assets/images/check-icon-green.png', __DIR__ ) ); ?>" alt="Vorteil Icon" class="ts-list-icon">
                                                <?php echo esc_html( $vorteil ); ?>
                                            </li>
                                            <?php
                                        endforeach;
                                        ?>
                                    </ul>
                                </div>
                            <?php endif; ?>

                            <?php if ( ! empty( $nachteile ) ) : ?>
                                <div class="boxyfy_nachteile">
                                    <h2>
   	 									<img src="<?php echo esc_url( plugins_url( 'assets/images/cross-icon-white.svg', __DIR__ ) ); ?>" alt="<?php esc_attr_e( 'Symbol für Nachteil', 'boxyfy' ); ?>" class="ts-header-icon">
    									<?php esc_html_e( 'Nachteile', 'boxyfy' ); ?>
									</h2>
                                    <ul>
                                        <?php
                                        foreach ( $nachteile as $nachteil ) :
                                            ?>
                                            <li>
                                                <img src="<?php echo esc_url( plugins_url( 'assets/images/cross-icon-red.png', __DIR__ ) ); ?>" alt="Nachteil Icon" class="ts-list-icon">
                                                <?php echo esc_html( $nachteil ); ?>
                                            </li>
                                            <?php
                                        endforeach;
                                        ?>
                                    </ul>
                                </div>
                            <?php endif; ?>
                        </div>
                        <?php
                    endif;
                    the_content();
                    ?>
                    <?php
                    // Autorenboxen
                    $selected_authors = get_post_meta( get_the_ID(), '_boxyfy_selected_authors', true );
                    $autoren = get_option( 'boxyfy_autoren_profiles', array() );
                    $standard_autoren = array();
                    foreach ( $autoren as $index => $autor ) {
                        if ( ! empty( $autor['standard'] ) ) {
                            $standard_autoren[ $index ] = $autor;
                        }
                    }
                    if ( ! is_array( $selected_authors ) ) {
                        $selected_authors = array();
                    }
                    if ( empty( $selected_authors ) ) {
                        foreach ( $standard_autoren as $index => $autor ) {
                            $selected_authors[] = $index;
                        }
                    }
                    if ( ! empty( $selected_authors ) && is_array( $selected_authors ) ) {
                        $selected_autoren = array();
                        foreach ( $selected_authors as $index ) {
                            if ( isset( $autoren[ $index ] ) ) {
                                $selected_autoren[] = $autoren[ $index ];
                            }
                        }
           				if ( ! empty( $selected_autoren ) ) {
    						echo '<div class="tsprodukt-autoren-container">';
    						echo '<h2>' . esc_html__( 'Wer schreibt hier?', 'boxyfy' ) . '</h2>';
    						foreach ( $selected_autoren as $autor ) {
        						echo '<div class="tsprodukt-autoren-box">';
        
        						// Bildausgabe
        						if ( ! empty( $autor['image'] ) ) {
            						echo '<div class="tsprodukt-autoren-image">';
            						echo '<img src="' . esc_url( $autor['image'] ) . '" alt="' . esc_attr( $autor['name'] ) . '">';
            						echo '</div>';
        						}

        						// Infobereich Autor
        						echo '<div class="tsprodukt-autoren-info">';
        						echo '<h3>' . esc_html( $autor['name'] ) . '</h3>';
        
        						// Expertise-Ausgabe auf zwei Absätze getrennt
        						if ( ! empty( $autor['expertise'] ) ) {
            						echo '<p class="expertise-ausgabe">';
            						echo '<strong><svg class="autor-right-angle-arrow" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 380.904 380.904" xml:space="preserve"><polygon style="fill:currentColor;" points="278.957,212.62 250.671,240.904 286.776,277.008 57.806,277.008 57.806,0 17.806,0 17.806,317.008 286.283,317.008 250.671,352.62 278.957,380.904 363.098,296.762 "/></svg> ' . esc_html__( 'Meine Expertise:', 'boxyfy' ) . '</strong>';
            						echo '</p>';
            						echo '<p class="expertise-ausgabe-text">' . esc_html( $autor['expertise'] ) . '</p>';
            						echo '<hr />';
        						}
        
        						// Beschreibung
        						if ( ! empty( $autor['desc'] ) ) {
            						echo '<p>' . esc_html( $autor['desc'] ) . '</p>';
        						}
        
        						echo '</div>'; // .tsprodukt-autoren-info
        						echo '</div>'; // .tsprodukt-autoren-box
    						}
    						echo '</div>'; // .tsprodukt-autoren-container
						}
                    }
                    ?>
                </div>
            </div>
        </div>

        <?php
    endwhile;
else :
    echo '<p>' . esc_html__( 'Keine Produkte gefunden', 'boxyfy' ) . '</p>';
endif;

get_footer(); // WordPress Footer einfügen.