<?php
get_header();

    $pagina = $_GET['pag'];
    if( isset($pagina) ) {				
        $num_pagina = intval($pagina);
    } else {
        $num_pagina = 1;
    }
    if( gettype($num_pagina)=="integer" ){
        $offset = 40*($num_pagina-1);
    }

    $search = $_GET['s'];
    $args_search = array (
        's'             =>	$_GET['s'],
        'posts_per_page'=> 40,
        'offset'		=> $offset,
        'orderby'	    => 'date',
        'order'         => 'DESC'
    );
    $the_query_search = new WP_Query ( $args_search );
    $tot_posts = $the_query_search->found_posts;
    $tot_paginas = ceil($tot_posts/40);

?>
<main>
    <section id="busca" class="container">
        <div class="secao-publicacoes">
            <?php if ( $the_query_search->have_posts() ) : while ( $the_query_search->have_posts() ) : $the_query_search->the_post(); ?>
            <div class="secao-busca-geral flex-8">
                <a href="<?php the_permalink(); ?>" class="conteudo-busca">
                    <h3><?php the_title(); ?></h3>
                    <p><?php the_content(); ?></p>
                </a>
                <a href="<?php the_permalink(); ?>"><p class="link-busca"><?php echo the_permalink(); ?></p></a>
            </div>
            <?php endwhile; else: endif; ?>
            <ul class="paginacao">
            <?php
                $url_raw = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                $url_pag = explode("&pag=", $url_raw);
                $url_explode = explode("pag=", $url_pag[0]);
                $intervalo_pag = intval($url_pag[1]);
                $url = explode("?", $url_explode[0]);
                if($intervalo_pag == NULL) {
                    $intervalo_pag = 1;
                }
                if($tot_paginas > 1){
                    if($intervalo_pag > 1) {
                            $url_primeira = $url[0]."?".$url[1]."&pag=1";
                            echo "<li><a class='item-paginacao' href='{$url_primeira}'>Primeira</a></li>";
                    }
                    if($intervalo_pag < 4){
                        if($tot_paginas > 7){
                            for($i = 1; $i <= 7; $i++) {
                                if($i == $intervalo_pag || $intervalo_pag == NULL){
                                    $url_final = $url[0]."?".$url[1]."&pag={$i}";
                                    echo "<li><a class='item-paginacao active' href='{$url_final}'>{$i}</a></li>";
                                } else {
                                    $url_final = $url[0]."?".$url[1]."&pag={$i}";
                                    echo "<li><a class='item-paginacao' href='{$url_final}'>{$i}</a></li>";
                                }
                            }
                        } else {
                            for($i = 1; $i <= $tot_paginas; $i++) {
                                if($i == $intervalo_pag || $intervalo_pag == NULL){
                                    $url_final = $url[0]."?".$url[1]."&pag={$i}";
                                    echo "<li><a class='item-paginacao active' href='{$url_final}'>{$i}</a></li>";
                                } else {
                                    $url_final = $url[0]."?".$url[1]."&pag={$i}";
                                    echo "<li><a class='item-paginacao' href='{$url_final}'>{$i}</a></li>";
                                }
                            }
                        }
                    } else if($intervalo_pag > $tot_paginas - 4) {
                        for($i = $tot_paginas-7; $i <= $tot_paginas; $i++) {
                            if($i == $intervalo_pag){
                                $url_final = $url[0]."?".$url[1]."&pag={$i}";
                                echo "<li><a class='item-paginacao active' href='{$url_final}'>{$i}</a></li>";
                            } else {
                                $url_final = $url[0]."?".$url[1]."&pag={$i}";
                                echo "<li><a class='item-paginacao' href='{$url_final}'>{$i}</a></li>";
                            }
                        }
                    } else {
                        for($i = $intervalo_pag-3; $i <= $intervalo_pag+3; $i++) {
                            if($i == $intervalo_pag){
                                $url_final = $url[0]."?".$url[1]."&pag={$i}";
                                echo "<li><a class='item-paginacao active' href='{$url_final}'>{$i}</a></li>";
                            } else {
                                $url_final = $url[0]."?".$url[1]."&pag={$i}";
                                echo "<li><a class='item-paginacao' href='{$url_final}'>{$i}</a></li>";
                            }
                        }
                    }
                    if($intervalo_pag < $tot_paginas) {
                        $url_ultima = $url[0]."?".$url[1]."&pag={$tot_paginas}";
                        echo "<li><a class='item-paginacao' href='{$url_ultima}'>Última</a></li>";
                    }
                }
            ?>
            </ul>
            <?php wp_reset_query(); wp_reset_postdata(); ?>
        </div>
    </section>
</main>

<?php get_footer(); ?>