<?php
/**
 * The main template file.
 *
 */


get_header();?>

<?$s = trim($s);?>


<?//if( !empty( $query ) ){


    $args = array(
        'post_type'   => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 20,
        'paged' => get_query_var('paged') ?: 1
    );

    if ( $_GET['search_type'] == 'date_down'){

        $args['order'] = 'DESC';
        $args['orderby'] = 'date';

    }
    else if($_GET['search_type'] == 'date_up') {

        $args['order'] = 'ASC';
        $args['orderby'] = 'date';

    }   

    $findtag = strpos($s, "#");

    if( $findtag !== false OR $_GET['search_select'] == 'tag') {

        $active_tag = True;
            
        if ($findtag == false) {
            $terms = $s;
        }
        else {
            $terms = substr($s, 1);
        }

        $args['tax_query'] = [
            'relation' => 'OR',
            [
                'taxonomy' => 'post_tag', 
                'field' => 'name',
                'terms' => $terms
            ]
        ];
    } 
    else {
        $active_tag = false;
        $args['s'] = $s;
    }


if($_GET['search_date'] == 'week') {

    $args['date_query'] = [
        [
			'after' => '1 week ago' 
        ]
	];

}
else if ($_GET['search_date'] == 'month'){

    $args['date_query'] = [
		    [
			    'after' => '1 month ago' 
            ]
    ];

}
else if ($_GET['search_date'] == 'year'){

    $args['date_query'] = [
		[
			'after' => '1 year ago'
        ]
    ];

}

else if ($_GET['search_date'] == 'range'){

    if (isset($_GET['search-start-date']) and $_GET['search-start-date'] !== ''){
        $date1 = explode('-', $_GET['search-start-date']);
    }

    if (isset($_GET['search-end-date']) and $_GET['search-end-date'] !== ''){
        $date2 = explode('-', $_GET['search-end-date']);
    }


   $args['date_query'] = [
        [
            array(
                'after'     => array( 
                    'year'  => $date1[0],
                    'month' => $date1[1],
                    'day'   => $date1[13],
                ),
                'before'    => array( // до этой даты
                    'year'  => $date2[0],
                    'month' => $date2[1],
                    'day'   => $date2[2],
                ),    
            )
        ] 
];


}    

$posts = new WP_Query($args);



//}?>



<div id="content">    

<h1 id="mainpage-title">Поиск:</h1>



<div id="search-form">
	<form action="<?php bloginfo( 'url' ); ?>" method="get">
        <input class="search-text" type="text" name="s" placeholder="Найти" value="<?=$s?>"/>
        <input class="search_submit" type="submit" value="Найти"/>
<?if ($s !== ''):?>
        <div class="search-result">
        По запросу «<?php

$key = esc_html($s, 1);
$count = $posts->found_posts; _e('');
_e('<span class="search-terms">');
echo $key; _e('</span>» найдено ');
if ($count == 1):
echo $count . ' '; _e('результат');
else:
 if ($count > 4):
  echo $count . ' '; _e('результатов');
 else:
  echo $count . ' '; _e('результата');
 endif;
endif; 
//wp_reset_query(); ?>

        </div>
<?endif?>        

        <div id="find_panel">
            <select class="search-select search-select-type" id="search_type" name="search_type">
                <option value="date_down">По дате&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="date_arrow">↓</span></option>
                <option value="date_up" <?if($_GET['search_type'] == 'date_up'):?> selected<?endif?>>По дате&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="date_arrow">&#8593;</span></option>
                <option value="revel" <?if($_GET['search_type'] == 'revel'):?> selected<?endif?>>По релевантности</option>
            </select>  
            <select class="search-select search-select-date" id="search_date" name="search_date">
                <option value="all">За все время</option>
                <option value="week" <?if($_GET['search_date'] == 'week'):?> selected<?endif?>>За неделю</option>
                <option value="month" <?if($_GET['search_date'] == 'month'):?> selected<?endif?>>За месяц</option>
                <option value="year" <?if($_GET['search_date'] == 'year'):?> selected<?endif?>>За год</option>
                <option value="range" <?if($_GET['search_date'] == 'range'):?> selected<?endif?>>За период</option>    
            </select>
            <select class="search-select search-select-tags" id="search_select" name="search_select">
                <option value="all">Все</option>
                <option value="tag"<?if ($active_tag):?> selected<?endif?>>Теги</option>
            </select> 
            
            <div id="search-calendar" style="display: <?if($_GET['search_date'] == 'range'):?>block<?else:?>none<?endif?>;">
                <input id="search-start-date" name="search-start-date" type="date" value = "<?if (isset($_GET['search-start-date']) and $_GET['search-start-date'] !== '' ):?><?=$_GET['search-start-date']?><?else:?><?=date('Y-m-d')?><?endif?>">
                -      
                <input id="search-end-date" name="search-end-date" value = "<?if (isset($_GET['search-end-date']) and $_GET['search-end-date'] !== ''):?><?=$_GET['search-end-date']?><?else:?><?=date('Y-m-d')?><?endif?>" type="date">  
            </div>   

        </div> 
            
    </form>
</div>	

<?//print_r($posts)?>


<?php if ($s !== '' and $posts->have_posts()) :?>
<div class="search-card-box">

	<?php while($posts->have_posts()) : $posts->the_post();?>


    
	<div class="search-card">
        
        <div class="search-card-title"><a href="<?the_permalink();?>"><?the_title();?></a></div>
		<?if ( has_post_thumbnail() ) :?>
            <img class="search-card-img" src="<?php echo get_the_post_thumbnail_url(null, 'full'); ?>" width="239" alt="<?the_title();?>">
        <?endif?>
		<div><?the_excerpt();?></div>  
        <div class="mini-card-body">
            <div class="news_date"><?the_time('j F Y, G:i');?></div>
        </div>

    </div>    
 


<?php endwhile;?> 
		</div>



<?
$GLOBALS['wp_query']->max_num_pages = $posts->max_num_pages;
get_template_part( '/blocks/paginator');?>


<?php endif;?>


<?wp_reset_postdata();?>


</div>


<?php get_footer(); ?>

<script>

    var search_date_select = document.getElementById('search_date');
    var data_range_panel  = document.getElementById('search-calendar');

    search_date_select.addEventListener('change', function() {
            
        if (search_date_select.selectedIndex == 4) {
           data_range_panel.style.display = 'block';
        } 
        else {
           data_range_panel.style.display = 'none';
        }       
  
    })
    

</script>    