<?php
// Template Name: Legislação
get_header();
?>

	<main>

		<div class="intro intro-interna intro-maior-desc">
			<h2 class="titulo-intro"><?php the_field('titulo_introducao'); ?></h2>
			<div class="descricao-secao">
				<?php the_field('descricao_secao'); ?>
			</div>
		</div>

		<div class="container secao-busca">

			<h4 class="subtitulo">Buscar em Legislação</h4>

			<form method="GET" action="">
					<div class="column">

						<label for="ano-legislacao">Ano:</label>
						<input type="number" min="2000" max="2099" id="ano-legislacao" name="fano-legislacao"/>

					</div>

					<div class="column">

						<label for="num-legislacao">Número:</label>
						<input type="number" min="1" max="200" id="num-legislacao" name="fnum-legislacao"/>

					</div>

					<div class="column">

						<label for="desc-legislacao">Descrição:</label>
						<input type="text" maxlength=100 id="desc-legislacao" name="fdesc-legislacao" class="campo-assunto"/>

					</div>

					<div class="column">

						<input type="submit" value="Buscar" class="btn"/>

					</div>

				</form>

		</div>

		<section class="container" data-group="section-info">

			<div class="tab-menu">
				<li><a data-click="resolucoes">Resoluções</a></li>
				<li><a data-click="atas">Atas</a></li>
			</div>
			
			

			<div data-target="resolucoes" class="flex-8">

				<div class="secao-publicacoes">

					<?php
						$categoria = $_GET['fcat-legislacao'];
						$num_legislacao = $_GET['fnum-legislacao'];
						$ano_legislacao = $_GET['fano-legislacao'];
						$desc_legislacao = $_GET['fdesc-legislacao'];
						$pagina = $_GET['pag_resolucoes'];
						if( isset($pagina) ) {				
							$num_pagina = intval($pagina);
						} else {
							$num_pagina = 1;
						}
						if( gettype($num_pagina)=="integer" ){
							$offset = 40*($num_pagina-1);
						}

						$args_resolucoes = array (
							'post_type'		=> 'legislacao',
							'meta_query'	=> array(
								'relation'	=> 'AND',
								array(
									'key'     	=> 'categoria_legislacao',
									'value'   	=> 'Resolução',
									'compare' 	=> 'LIKE',
								),
								array(
									'key'     	=> 'descricao_legislacao',
									'value'   	=> 	$desc_legislacao,
									'compare' 	=> 'LIKE',
								),
								'num_legisl' 	=> array(
									'key'     	=> 'numero_legislacao',
									'value'   	=> 	$num_legislacao,
									'compare' 	=> 'LIKE',
								),
								'ano_legisl' 	=> array(
									'key'     	=> 'ano_legislacao',
									'value'   	=> 	$ano_legislacao,
									'compare' 	=> 'LIKE',
								),
							),
							'orderby'		=> array(
								'ano_legisl'	=> 'DESC',
								'num_legisl'	=> 'DESC'
							),
							'posts_per_page'=> 40,
							'offset'		=> $offset,
						);
						$the_query_resolucoes = new WP_Query ( $args_resolucoes );
						$id_post_resolucoes = 1;
						$tot_posts_resolucoes = $the_query_resolucoes->found_posts;
						$tot_paginas_resolucoes = ceil($tot_posts_resolucoes/40);
					?>		
				
					<?php if ( $the_query_resolucoes->have_posts() ) : while ( $the_query_resolucoes->have_posts() ) : $the_query_resolucoes->the_post(); ?>
					<div id='publicacao<?php echo $id_post_resolucoes; ?>'>

						<div class="conteudo-publicacao pub-diario-legislacao">
							<a href="<?php the_field('arquivo_legislacao') ?>" target="_new">
								<h6 class="titulo-publicacoes">
									<?php the_title(); ?>
									<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none">
										<path d="M9 4H1V19H16V11" stroke-width="2" stroke-linecap="round"/>
										<path d="M9 11L19 1M19 1H11.8571M19 1V8.14286" stroke-width="2" stroke-linecap="round"/>
									</svg>
								</h6>
							</a>
							<p><span>Ano:</span> <?php the_field('ano_legislacao'); ?></p>
							<p><span>Número:</span> <?php the_field('numero_legislacao'); ?></p>
							<p><span>Descrição:</span> <?php the_field('descricao_legislacao'); ?></p>
						</div>

						<svg xmlns="http://www.w3.org/2000/svg" width="24" height="18" viewBox="0 0 24 18" fill="none" class="abrir-secao">
							<path d="M12 16L2 6L6 2L12 8L18 2L22 6L12 16Z" stroke-width="2"/>
						</svg>
					</div>
					<?php $id_post_resolucoes++; ?>
					<?php endwhile; else: endif; ?>
					<ul class="paginacao">
					<?php
						$url_raw = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
						$url_pag = explode("&pag_resolucoes=", $url_raw);
						$url_explode = explode("pag_resolucoes=", $url_pag[0]);
						$intervalo_pag = intval($url_explode[1]);
						if($intervalo_pag == NULL && count($url_pag) > 1) {
							$intervalo_pag = intval($url_pag[1]);
						}
						$url = explode("?", $url_explode[0]);
						if($tot_paginas_resolucoes > 1){
							if($intervalo_pag > 1) {
									$url_primeira = $url[0]."?".$url[1]."&pag_resolucoes=1";
									echo "<li><a class='item-paginacao' href='{$url_final}'>Primeira</a></li>";
							}
							if($intervalo_pag < 4){
								if($tot_paginas_resolucoes > 7){
									for($i = 1; $i <= 7; $i++) {
										if($intervalo_pag == NULL) {
											$intervalo_pag = 1;
										}
										if($i == $intervalo_pag || $intervalo_pag == NULL){
											$url_final = $url[0]."?".$url[1]."&pag_resolucoes={$i}";
											echo "<li><a class='item-paginacao active' href='{$url_final}'>{$i}</a></li>";
										} else {
											$url_final = $url[0]."?".$url[1]."&pag_resolucoes={$i}";
											echo "<li><a class='item-paginacao' href='{$url_final}'>{$i}</a></li>";
										}
									}
								} else {
									for($i = 1; $i <= $tot_paginas_resolucoes; $i++) {
										if($intervalo_pag == NULL) {
											$intervalo_pag = 1;
										}
										if($i == $intervalo_pag || $intervalo_pag == NULL){
											$url_final = $url[0]."?".$url[1]."&pag_resolucoes={$i}";
											echo "<li><a class='item-paginacao active' href='{$url_final}'>{$i}</a></li>";
										} else {
											$url_final = $url[0]."?".$url[1]."&pag_resolucoes={$i}";
											echo "<li><a class='item-paginacao' href='{$url_final}'>{$i}</a></li>";
										}
									}
								}
							} else if($intervalo_pag > $tot_paginas_resolucoes - 4) {
								for($i = $tot_paginas_resolucoes-7; $i <= $tot_paginas_resolucoes; $i++) {
									if($i == $intervalo_pag){
										$url_final = $url[0]."?".$url[1]."&pag_resolucoes={$i}";
										echo "<li><a class='item-paginacao active' href='{$url_final}'>{$i}</a></li>";
									} else {
										$url_final = $url[0]."?".$url[1]."&pag_resolucoes={$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_resolucoes={$i}";
										echo "<li><a class='item-paginacao active' href='{$url_final}'>{$i}</a></li>";
									} else {
										$url_final = $url[0]."?".$url[1]."&pag_resolucoes={$i}";
										echo "<li><a class='item-paginacao' href='{$url_final}'>{$i}</a></li>";
									}
								}
							}
							if($intervalo_pag < $tot_paginas_resolucoes) {
								$url_primeira = $url[0]."?".$url[1]."&pag_resolucoes={$tot_paginas_resolucoes}";
								echo "<li><a class='item-paginacao' href='{$url_final}'>Última</a></li>";
							}
						}
					?>
					</ul>
				<?php wp_reset_query(); wp_reset_postdata(); ?>

				</div>

			</div>



			<div data-target="atas" class="flex-8">

				<div class="secao-publicacoes">

					<?php
						$categoria = $_GET['fcat-legislacao'];
						$num_legislacao = $_GET['fnum-legislacao'];
						$ano_legislacao = $_GET['fano-legislacao'];
						$desc_legislacao = $_GET['fdesc-legislacao'];
						$pagina = $_GET['pag_atas'];
						if( isset($pagina) ) {				
							$num_pagina = intval($pagina);
						} else {
							$num_pagina = 1;
						}
						if( gettype($num_pagina)=="integer" ){
							$offset = 40*($num_pagina-1);
						}

						$args_atas = array (
							'post_type'		=> 'legislacao',
							'meta_query'	=> array(
								'relation'	=> 'AND',
								array(
									'key'     	=> 'categoria_legislacao',
									'value'   	=> 'Ata',
									'compare' 	=> 'LIKE',
								),
								array(
									'key'     	=> 'descricao_legislacao',
									'value'   	=> 	$desc_legislacao,
									'compare' 	=> 'LIKE',
								),
								'num_legisl' 	=> array(
									'key'     	=> 'numero_legislacao',
									'value'   	=> 	$num_legislacao,
									'compare' 	=> 'LIKE',
								),
								'ano_legisl' 	=> array(
									'key'     	=> 'ano_legislacao',
									'value'   	=> 	$ano_legislacao,
									'compare' 	=> 'LIKE',
								),
							),
							'orderby'		=> array(
								'ano_legisl'	=> 'DESC',
								'num_legisl'	=> 'DESC'
							),
							'posts_per_page'=> 40,
							'offset'		=> $offset,
						);
						$the_query_atas = new WP_Query ( $args_atas );
						$id_post_atas = 1;
						$tot_posts_atas = $the_query_atas->found_posts;
						$tot_paginas_atas = ceil($tot_posts_atas/40);
					?>		
				
					<?php if ( $the_query_atas->have_posts() ) : while ( $the_query_atas->have_posts() ) : $the_query_atas->the_post(); ?>
					<div id='publicacao<?php echo $id_post_atas; ?>'>

						<div class="conteudo-publicacao pub-diario-legislacao">
							<a href="<?php the_field('arquivo_legislacao') ?>" target="_new">
								<h6 class="titulo-publicacoes">
									<?php the_title(); ?>
									<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20" fill="none">
										<path d="M9 4H1V19H16V11" stroke-width="2" stroke-linecap="round"/>
										<path d="M9 11L19 1M19 1H11.8571M19 1V8.14286" stroke-width="2" stroke-linecap="round"/>
									</svg>
								</h6>
							</a>
							<p><span>Ano:</span> <?php the_field('ano_legislacao'); ?></p>
							<p><span>Número:</span> <?php the_field('numero_legislacao'); ?></p>
							<p><span>Descrição:</span> <?php the_field('descricao_legislacao'); ?></p>
						</div>

						<svg xmlns="http://www.w3.org/2000/svg" width="24" height="18" viewBox="0 0 24 18" fill="none" class="abrir-secao">
							<path d="M12 16L2 6L6 2L12 8L18 2L22 6L12 16Z" stroke-width="2"/>
						</svg>
					</div>
					<?php $id_post_atas++; ?>
					<?php endwhile; else: endif; ?>
					<ul class="paginacao">
					<?php
						$url_raw = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
						$url_pag = explode("&pag_atas=", $url_raw);
						$url_explode = explode("pag_atas=", $url_pag[0]);
						$intervalo_pag = intval($url_explode[1]);
						if($intervalo_pag == NULL && count($url_pag) > 1) {
							$intervalo_pag = intval($url_pag[1]);
						}
						$url = explode("?", $url_explode[0]);
						if($tot_paginas_atas > 1){
							if($intervalo_pag > 1) {
									$url_primeira = $url[0]."?".$url[1]."&pag_atas=1";
									echo "<li><a class='item-paginacao' href='{$url_final}'>Primeira</a></li>";
							}
							if($intervalo_pag < 4){
								if($tot_paginas_resolucoes > 7){
									for($i = 1; $i <= 7; $i++) {
										if($intervalo_pag == NULL) {
											$intervalo_pag = 1;
										}
										if($i == $intervalo_pag || $intervalo_pag == NULL){
											$url_final = $url[0]."?".$url[1]."&pag_atas={$i}";
											echo "<li><a class='item-paginacao active' href='{$url_final}'>{$i}</a></li>";
										} else {
											$url_final = $url[0]."?".$url[1]."&pag_atas={$i}";
											echo "<li><a class='item-paginacao' href='{$url_final}'>{$i}</a></li>";
										}
									}
								} else {
									for($i = 1; $i <= $tot_paginas_resolucoes; $i++) {
										if($intervalo_pag == NULL) {
											$intervalo_pag = 1;
										}
										if($i == $intervalo_pag || $intervalo_pag == NULL){
											$url_final = $url[0]."?".$url[1]."&pag_atas={$i}";
											echo "<li><a class='item-paginacao active' href='{$url_final}'>{$i}</a></li>";
										} else {
											$url_final = $url[0]."?".$url[1]."&pag_atas={$i}";
											echo "<li><a class='item-paginacao' href='{$url_final}'>{$i}</a></li>";
										}
									}
								}
							} else if($intervalo_pag > $tot_paginas_atas - 4) {
								for($i = $tot_paginas_atas-7; $i <= $tot_paginas_atas; $i++) {
									if($i == $intervalo_pag){
										$url_final = $url[0]."?".$url[1]."&pag_atas={$i}";
										echo "<li><a class='item-paginacao active' href='{$url_final}'>{$i}</a></li>";
									} else {
										$url_final = $url[0]."?".$url[1]."&pag_atas={$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_atas={$i}";
										echo "<li><a class='item-paginacao active' href='{$url_final}'>{$i}</a></li>";
									} else {
										$url_final = $url[0]."?".$url[1]."&pag_atas={$i}";
										echo "<li><a class='item-paginacao' href='{$url_final}'>{$i}</a></li>";
									}
								}
							}
							if($intervalo_pag < $tot_paginas_atas) {
								$url_primeira = $url[0]."?".$url[1]."&pag_atas={$tot_paginas_atas}";
								echo "<li><a class='item-paginacao' href='{$url_final}'>Última</a></li>";
							}
						}
					?>
					</ul>
				<?php wp_reset_query(); wp_reset_postdata(); ?>

				</div>

			</div>

		</section>

	</main>

<?php get_footer(); ?>