<?php
foreach ($vehicles as $vehicle) {

    $key_l = array_search('L', array_column($vehicle['images'], 'size'));
    $key_xl = array_search('XL', array_column($vehicle['images'], 'size'));

    $image_l_url = ($key_l !== false) ? $vehicle['images'][$key_l]['url'] :
        (($key_xl !== false) ? $vehicle['images'][$key_xl]['url'] : '');
    $image_xl_url = ($key_xl !== false) ? $vehicle['images'][$key_xl]['url'] : $image_l_url;

?>
    <div class="estocks eurostocks-list-item" data-url="<?php if($diflang && ($default_lang != $current_lang)){ echo "/" . esc_html($current_lang); } ?>/auto/<?php echo esc_html($vehicle['vehicleId']); ?>/">
        <a class="estocks-cover-wrapper es-img-box" data-id="<?php echo esc_html($vehicle['vehicleId']); ?>"
            href="<?php if($diflang && ($default_lang != $current_lang)){ echo "/" . esc_html($current_lang); } ?>/auto/<?php echo esc_html($vehicle['vehicleId']); ?>/">
            <picture>
                <source media="(max-width: 768px)" srcset="<?php echo esc_url($image_xl_url); ?>">
                <img class="es-cover" alt="<?php echo esc_html($vehicle['modelDescription']); ?>"
                    src="<?php echo esc_url($image_l_url); ?>" loading="lazy" />
            </picture>
        </a>
        <div class="es-text">
            <a class="estocks-title-wrapper" data-id="<?php echo esc_html($vehicle['vehicleId']); ?>"
                href="<?php if($diflang && ($default_lang != $current_lang)){ echo "/" . esc_html($current_lang); } ?>/auto/<?php echo esc_html($vehicle['vehicleId']); ?>/">
                <h3 class="eurosearch-car-title">
                    <?php echo esc_html($vehicle['make']) . ' ' . esc_html($vehicle['model']); ?>
                </h3>
            </a>
            <h4 class="eurosearch-car-subtitle">
                <?php echo esc_html($vehicle['modelDescription']); ?>
            </h4>

            <div class="eurosearch-car-details es-grid es-grid-columns-3">
                <div class="es-icon es-registration">
                    <?php echo esc_html($vehicle['firstRegistrationDate']); ?>
                </div>
                <div class="es-icon es-pk">
                    <?php echo esc_html(intval($vehicle['power'] * 1.362)) . " " . esc_html__("HP", 'eurostocks'); ?>
                </div>
                <div class="es-icon es-mileage">
                    <?php echo esc_html(number_format(floatval($vehicle['mileage']), 0, ',', '.')); ?>KM
                </div>
                <div class="es-icon es-fueltype">
                    <?php
                    if ($vehicle['fuelType'] == "Hybride (benzine/elektrisch)") {
                        echo "Hybride benzine";
                    } elseif ($vehicle['fuelType'] == "Hybride (diesel/elektrisch)") {
                        echo "Hybride diesel";
                    } else {
                        echo esc_html($vehicle['fuelType']);
                    }
                    ?>
                </div>
                <div class="es-icon es-gearbox">
                    <?php if ($vehicle['gearBox'] == "Automatische transmissie") {
                        echo "Automaat";
                    } else {
                        echo esc_html($vehicle['gearBox']);
                    } ?>
                </div>
            </div>

            <?php

            if ((isset($vehicle['priceInfo']['PriceMobiledeBruto'])) && (is_numeric($vehicle['priceInfo']['PriceMobiledeBruto'])) && $vehicle['priceInfo']['PriceMobiledeBruto'] != 0) {
                if (isset($vehicle['priceInfo']['TransportCosts']) && $vehicle['priceInfo']['TransportCosts'] != '') {
                    $transportCosts = $vehicle['priceInfo']['TransportCosts'];
                } else {
                    $transportCosts = 0;
                }
                if ($options['country'] != 'nl' && $options['country'] != 'nl21') {
                    if (isset($vehicle['priceInfo']['ImportMargin']) && $vehicle['priceInfo']['ImportMargin'] != '') {
                        $importMargin = $vehicle['priceInfo']['ImportMargin'];
                    } else {
                        $importMargin = 0;
                    }
                    if (isset($vehicle['priceInfo']['ImportCosts']) && $vehicle['priceInfo']['ImportCosts'] != '') {
                        $importCosts = $vehicle['priceInfo']['ImportCosts'];
                    } else {
                        $importCosts = 0;
                    }
                    $price = $vehicle['priceInfo']['PriceMobiledeBruto'] + $importMargin + $importCosts + $transportCosts;
                } elseif ($options['country'] == 'nl21') {
                    if (isset($vehicle['priceInfo']['PriceNlWithVatNl']) && $vehicle['priceInfo']['PriceNlWithVatNl'] != '') {
                        $price = $vehicle['priceInfo']['PriceNlWithVatNl'];
                    } else {
                        if(isset($vehicle['priceInfo']['FallbackPriceNlWithVatNl']) && $vehicle['priceInfo']['FallbackPriceNlWithVatNl'] != '' && isset($options['co2_fallback_display']) && $options['co2_fallback_display'] == 1) {
                            $price = $vehicle['priceInfo']['FallbackPriceNlWithVatNl'];
                        }else{
                            $price = false;
                        }
                    }
                } else {
                    if($options['country'] == 'nl') {
                        if(isset($vehicle['priceInfo']['PriceNlWithVatMobilede']) && $vehicle['priceInfo']['PriceNlWithVatMobilede'] != '') {
                            $price = $vehicle['priceInfo']['PriceNlWithVatMobilede'];
                        }
                        elseif(isset($vehicle['priceInfo']['FallbackPriceNlWithVatMobilede']) && $vehicle['priceInfo']['FallbackPriceNlWithVatMobilede'] != '' && isset($options['co2_fallback_display']) && $options['co2_fallback_display'] == 1) {
                            $price = $vehicle['priceInfo']['FallbackPriceNlWithVatMobilede'];
                        } else {
                            $price = false;
                        }
                    }else {
                        $price = false;
                        
                    }
                }
            }

            ?>



            <div class="es-flex price-n-btn">
                <div class="euro-price-wrapper">
                    <?php if ($eurostocks_only_fl != 1) { ?>
                        <div class="euro-price">
                            <?php
                            if ($price) {
                                echo esc_html("&euro; " . number_format(floatval($price), 0, ',', '.')) . ",-";
                            } else {
                                if($diflang && isset($options[esc_html($current_lang)]) && isset($options[esc_html($current_lang)]['txt_priceonrequest']) && $options[esc_html($current_lang)]['txt_priceonrequest'] != ""  ){ 
                                    echo esc_html($options[esc_html($current_lang)]['txt_priceonrequest']); 
                                  }else{ 
                                     if (isset($options['txt_priceonrequest']) && $options['txt_priceonrequest'] != "") {
                                      echo esc_html($options['txt_priceonrequest']);
                                    } else {
                                      esc_html_e("Price on request", 'eurostocks');
                                    } 
                                  }
                            } ?>
                        </div>

                    <?php } ?>


                    <?php if (($eurostocks_fl == 1 || $eurostocks_only_fl == 1)) {
                        if ($price) { ?>
                            <span class="euro-price-wrapper euro-fl <?php if ($eurostocks_only_fl == 1) {
                                                                        echo 'only-fl';
                                                                    } ?>">
                                <span class="euro-helper-text"><?php esc_html_e('Lease from', 'eurostocks'); ?></span> <span
                                    class="euro-price">€
                                    <?php echo esc_html($this->eurostocks_financial_lease($eurostocks_fl_interest, $eurostocks_fl_duration, $eurostocks_fl_rest, $price)); ?>,-
                                </span>
                            </span>
                        <?php } elseif (!$price && $eurostocks_only_fl == 1) { ?>
                            <span class="euro-price-wrapper euro-fl only-fl">
                                <span class="euro-helper-text"><?php esc_html_e('Lease from', 'eurostocks'); ?></span> <span class="euro-price"><?php esc_html_e('On request', 'eurostocks'); ?></span>
                            </span>
                    <?php
                        }
                    } ?>
                </div>






                <a class="euro-btn euro-btn-primary euro-listing-btn"
                    data-id="<?php echo esc_html($vehicle['vehicleId']); ?>"
                    href="<?php if($diflang && ($default_lang != $current_lang)){ echo "/" . esc_html($current_lang); } ?>/auto/<?php echo esc_html($vehicle['vehicleId']); ?>/">
                    <?php echo esc_html($options['txt_btn']); ?>
                </a>
            </div>
        </div>
    </div>
<?php
}
