<?php if ( have_posts() ) :
    $prices = get_option('io_prices');
    $price_overrides = array_filter($prices, function($v){
        if(is_string($v) and $v !== "isset"){
            $split = explode('_', $v);
            if(count($split) > 4){
                return $v;
            }
        }
    });
    $hide_prices = get_option('io_hide_prices');?>
    <ul class="searchio-list">
    <?php while ( have_posts() ) : the_post();?>
        <?php $post_type = get_post_type_object( get_post_type() );
        if($post_type !== null){
            $content = $post->post_content;
            $price = "";
            if(!empty($price_overrides) && strpos($content, "io_category") === false){
                $rental_id_loc = strpos($content, "rental_id");
                $rental_id_start = strpos($content, '"', $rental_id_loc)+1;
                $rental_id_end = strpos($content, '"', $rental_id_start);
                $rental_id = substr($content, $rental_id_start, $rental_id_end-$rental_id_start);
                foreach($price_overrides as $pro){
                    $split = explode('_', $pro);
                    if($rental_id === $split[4]){
                        $hour = $split[3];
                        $price_loc = strpos($content, "price_".$hour);
                        $price_start = strpos($content, '"', $price_loc)+1;
                        $price_end = strpos($content, '"', $price_start);
                        $price = substr($content, $price_start, $price_end-$price_start);
                        break;
                    }
                }
            }
            if(!empty($prices) && strpos($content, "io_category") === false && empty($price)){
                foreach($prices as $pr){
                    if(is_array($pr) || $pr === "isset"){
                        continue;
                    }
                    $split = explode('_', $pr);
                    $hour = $split[3];
                    $price_loc = strpos($content, "price_".$hour);
                    $price_start = strpos($content, '"', $price_loc)+1;
                    $price_end = strpos($content, '"', $price_start);
                    $price = substr($content, $price_start, $price_end-$price_start);
                    break;
                }
            }else if(empty($price)){
                $one_hour_price_loc = strpos($content, "price_1h");
                if($one_hour_price_loc !== false && strpos($content, "io_category") === false){
                    $price_start = strpos($content, '"', $one_hour_price_loc)+1;
                    $price_end = strpos($content, '"', $price_start);
                    $price = substr($content, $price_start, $price_end-$price_start);
                }
            }

            ?>
            <li>
                <a href="<?php echo esc_url( get_permalink() ); ?> " class="searchio-link">
                    <div class="searchio-live-search-result seachio-container elementor-widget-heading" role="option" <?php if($hide_prices == 1) { ?> style="max-width: 100%" <?php } ?> aria-selected="false">
                        <?php
                        $imgLink = get_the_post_thumbnail($id, 'thumbnail', [ 'class'=>'io_searchres_img' ]);
                        if ($imgLink == "") {
                            echo "<img class='io_searchres_img wp-post-image' src='https://rental.software/images/io_noimage.png' alt='placeholder image'>";
                        } else {
                            echo $imgLink;
                        }
                        ?>
                        <p class="elementor-heading-title searchio-text">
                            <?php
                            $resTitle = get_the_title();
                            $parent_title = "";
                            if(strpos($content, "io_rental") !== false){
                                $parent = get_post($post->post_parent);
                                $parent_title = $parent->post_title;
                            }
                            else if (strpos($content, "io_category") !== false){
                                $parent_title = "View all items in this category";
                            }
                            echo $resTitle;
                            if(!empty($parent_title)) { ?>
                                <br> <span class="searchio-subtext"> <?php echo "<i>".$parent_title.'</i>'; ?> </span> <?php
                            }
                            ?>
                        </p>
                    </div>
                    <?php if(!empty($price) && $hide_prices == 0) { ?>
                        <p class="searchio-price">
                            <?php echo $price; ?>
                        </p>
                    <?php } ?>

                </a>
            </li>
        <?php } endwhile; ?>
    </ul>
<?php else : ?>
    <p class="searchio-live-search-no-results" role="option">
        <em><?php esc_html_e( 'No results found.', 'swplas' ); ?></em>
    </p>
<?php endif; ?>