<?php

/**
 * @author Bill Minozzi
 * @copyright 2017 - 2024
 * Search List View
 */
if (!defined('ABSPATH'))
    exit; // Exit if accessed directly


global $wp;
//global $query;
global $wp_query;
global $multidealer_meta_make;

$wp_query->is_404 = false;
//get_header();


$output  =  '<div class="modal-container22"></div>';



$output .= '<div style="margin-top: 20px;">';
$output .= '<div id="multi_dealer_content">';




//
/* 
global $wp_query;
wp_reset_query();
$wp_query = new WP_Query($args);
*/

//$qposts = $wp_query->post_count;
// echo 'q posts: '.$qposts;
$ctd = 0;
$output .= '<div class="multiGallery">';
while ($wp_query->have_posts()):
    $wp_query->the_post();
    $ctd++;
    $price = get_post_meta(get_the_ID(), 'product-price', true);
    if ($price <> '' and $price != '0') {
        $price = number_format_i18n($price, 0);
        $price = multidealer_currency() . $price;
    } else
        $price =  __('Call for Price', 'multidealer');
    $image_id = get_post_thumbnail_id();
    if (empty($image_id)) {
        $image = MULTIDEALERIMAGES . 'image-no-available-800x400_br.jpg';
        $image = str_replace("-", "", $image);
    } else {
        $image_url = wp_get_attachment_image_src($image_id, 'medium', true);
        $image = str_replace("-" . $image_url[1] . "x" . $image_url[2], "", $image_url[0]);
    }
    $thumb = multidealer_theme_thumb($image, 400, 280, 'br'); // Crops from bottom right
    $year = get_post_meta(get_the_ID(), 'product-year', true);
    $output .= '<br /><div class="multidealer_container17">';
    $output .= '<div class="multidealer_gallery_17">';

    //$output .= '<a class="nounderline" href="' . get_permalink() . '">';

    $output .= '<img class="multidealer_caption_img17" src="' . $thumb . '" />';
    //$output .= '</a>';
    $output .= '</div>';
    $output .= '<div class="multiInfoRight17">';
    //
    //
    //
    //
    //
    //

    // $output .= '<a class="nounderline" href="' . get_permalink() . '">';

    $output .= '<div class="multiTitle17">' . get_the_title() . '</div>';
    //$output .= '</a>';
    $output .= '<div class="multiInforightText17">';
    $output .= '<div class="multiInforightbold">';
    $output .= $price  . '  -  ';
    $output .= ($year <> '' ? __('Year', 'multidealer') . ': ' . $year . '     ' :
        '');
    $output .= '</div>';
    $content_post = get_post(get_the_ID());
    $desc = sanitize_textarea_field($content_post->post_content);
    $desc = preg_replace("/\[([^\[\]]++|(?R))*+\]/", "", $desc);
    $output .= '<br>';
    $output .= substr($desc, 0, 200);
    if (substr($desc, 200) <> '')
        $output .= '...';
    $output .= '</div>';

    $output .= '<input type="submit" class="multidealer_btn_view"';

    $output .= ' id="multidealer_btn_view-' . strval(get_the_ID()) . '"';

    //$output .= ' onClick="location.href=\'' . get_permalink() . '\'"';
    $output .= ' value="' . __('View', 'multidealer') . '" />';
    $output .= '</div>';
    $output .= '</a>';
    $output .= '</div>';
endwhile;
$output .= '</div>';
ob_start();




/*
the_posts_pagination(array(
    'mid_size' => 2,
    'prev_text' => __('Back', 'multidealer'),
    'next_text' => __('Onward', 'multidealer'),
));
*/
// Obtém o número total de posts
$total_posts = $qtotalposts;
// echo 'Total de Posts: ' . $total_posts . '<br>'; // Depuração
// Verifique o número de posts por página
$postNumber = (isset($postNumber) && is_numeric($postNumber)) ? $postNumber : 3; // Defina um valor padrão, caso necessário
// echo 'Posts por Página: ' . $postNumber . '<br>'; // Depuração
$total_pages = ceil($total_posts / $postNumber); // Calcula o total de páginas
// echo 'Total de Páginas: ' . $total_pages . '<br>'; // Depuração
// Se houver mais de uma página, exibe a navegação
if ($total_pages > 1) {
    $current_page = max(1, $paged); // Página atual ou 1 se não estiver setada
    // echo 'Página Atual: ' . $current_page . '<br>'; // Depuração
    $range = 3; // Número de páginas antes e depois da página atual
    // echo 'Intervalo de Páginas (range): ' . $range . '<br>'; // Depuração
    $show_prev = ($current_page > 1); // Exibe o botão "Back" se não for a primeira página
    // echo 'Mostrar "Back": ' . ($show_prev ? 'Sim' : 'Não') . '<br>'; // Depuração
    $show_next = ($current_page < $total_pages); // Exibe o botão "Onward" se não for a última página
    // echo 'Mostrar "Onward": ' . ($show_next ? 'Sim' : 'Não') . '<br>'; // Depuração
    $output .= '<div id="multidealer-pagination">';
    // Botão "Back"
    if ($show_prev) {
        $output .= '<a href="?page=' . ($current_page - 1) . '" class="page-link" data-page="' . ($current_page - 1) . '">' . __('Back', 'real-estate-right-now') . '</a>';
    }
    // Exibe os links das páginas de forma limitada
    for ($i = max(1, $current_page - $range); $i <= min($total_pages, $current_page + $range); $i++) {
        // Se for a página atual, não cria o link, apenas exibe o número
        if ($i == $current_page) {
            $output .= '<span class="page-link current">' . $i . '</span>';
        } else {
            // Se não for a página atual, cria o link
            $output .= '<a href="?page=' . $i . '" class="page-link" data-page="' . $i . '">' . $i . '</a>';
        }
    }
    // Botão "Onward"
    if ($show_next) {
        $output .= '<a href="?page=' . ($current_page + 1) . '" class="page-link" data-page="' . ($current_page + 1) . '">' . __('Onward', 'real-estate-right-now') . '</a>';
    }
    $output .= '</div>';
} else {
    // Se houver 1 ou menos posts, não exibe a paginação
    // echo 'Nenhuma página a ser exibida';
}






$output .= ob_get_contents();
ob_end_clean();
$output .= '</div>';
$output .= '</div>';
wp_reset_postdata();
wp_reset_query();

/*
if ($qposts < 1) {
    $output .= '<h4>' . __('Not Found !', 'multidealer') . '</h4>';
}
    */

$allowed_atts = array(
    'align'      => array(),
    'class'      => array(),
    'type'       => array(),
    'id'         => array(),
    'dir'        => array(),
    'lang'       => array(),
    'style'      => array(),
    'xml:lang'   => array(),
    'src'        => array(),
    'alt'        => array(),
    'href'       => array(),
    'rel'        => array(),
    'rev'        => array(),
    'target'     => array(),
    'novalidate' => array(),
    'type'       => array(),
    'value'      => array(),
    'name'       => array(),
    'tabindex'   => array(),
    'action'     => array(),
    'method'     => array(),
    'for'        => array(),
    'width'      => array(),
    'height'     => array(),
    'data'       => array(),
    'title'      => array(),

    'checked' => array(),
    'selected' => array(),
    "onclick" => array(),


);




$my_allowed['form'] = $allowed_atts;
$my_allowed['select'] = $allowed_atts;
// select options
$my_allowed['option'] = $allowed_atts;
$my_allowed['style'] = $allowed_atts;
$my_allowed['label'] = $allowed_atts;
$my_allowed['input'] = $allowed_atts;
$my_allowed['textarea'] = $allowed_atts;

//more...future...
$my_allowed['form']     = $allowed_atts;
$my_allowed['label']    = $allowed_atts;
$my_allowed['input']    = $allowed_atts;
$my_allowed['textarea'] = $allowed_atts;
$my_allowed['iframe']   = $allowed_atts;
$my_allowed['script']   = $allowed_atts;
$my_allowed['style']    = $allowed_atts;
$my_allowed['strong']   = $allowed_atts;
$my_allowed['small']    = $allowed_atts;
$my_allowed['table']    = $allowed_atts;
$my_allowed['span']     = $allowed_atts;
$my_allowed['abbr']     = $allowed_atts;
$my_allowed['code']     = $allowed_atts;
$my_allowed['pre']      = $allowed_atts;
$my_allowed['div']      = $allowed_atts;
$my_allowed['img']      = $allowed_atts;
$my_allowed['h1']       = $allowed_atts;
$my_allowed['h2']       = $allowed_atts;
$my_allowed['h3']       = $allowed_atts;
$my_allowed['h4']       = $allowed_atts;
$my_allowed['h5']       = $allowed_atts;
$my_allowed['h6']       = $allowed_atts;
$my_allowed['ol']       = $allowed_atts;
$my_allowed['ul']       = $allowed_atts;
$my_allowed['li']       = $allowed_atts;
$my_allowed['em']       = $allowed_atts;
$my_allowed['hr']       = $allowed_atts;
$my_allowed['br']       = $allowed_atts;
$my_allowed['tr']       = $allowed_atts;
$my_allowed['td']       = $allowed_atts;
$my_allowed['p']        = $allowed_atts;
$my_allowed['a']        = $allowed_atts;
$my_allowed['b']        = $allowed_atts;
$my_allowed['i']        = $allowed_atts;


// echo wp_kses($output, $my_allowed);


// echo $output;


$registered_sidebars = wp_get_sidebars_widgets();
if (get_option('sidebar_search_page_result', 'no') == 'yes') {
    foreach ($registered_sidebars as $sidebar_name => $sidebar_widgets) {
        //  unregister_sidebar($sidebar_name);
    }
}
//get_footer(); 
