<?php

//setlocale(LC_ALL, 'ru_RU');
//date_default_timezone_set('Europe/Moscow');

function calend_block($month, $year, $script=false){

    if (strlen($month) == 1){
        $str_month = "0$month";
    }
    else{
        $str_month = "$month";
    }

    $find_sql = "
    SELECT `post_id`
     FROM `wp_postmeta` 
     WHERE `meta_key` = 'startevent' 
        AND `meta_value` 
            BETWEEN '$year-$str_month-01T00:00:00' AND '$year-$str_month-31T23:59:59'

     ";
    

    $items = db_get($find_sql);

    $ins = str_to_in($items);

    if ($ins !== '') {

    $posts = db_get("
    SELECT ID, post_date, post_title FROM  `wp_posts` WHERE ID IN ($ins) AND `post_status` = 'publish'
    ");
    }

    
    

  /**  $args = array(
        'numberposts' => -1,
        'category'    => 61, // 61 - мероприятия
        'meta_query' => array(
            array(
              'key'     =>  'startevent',
              'value'   => array( "$year-$month-01T00:00:00", "$year-$month-31T23:59:59" ),
              'compare' => 'BETWEEN',
              'type' => 'DATE'
            ),
         ),
        'post_type'   => 'post',
        'suppress_filters' => true, // подавление работы фильтров изменения SQL запроса
    );*/

    $args = array(
        'numberposts' => -1,
        'post__in' => $ins
    );
    
    //$posts = get_posts($args);

    $events = [];

    if (is_array($posts)){

        foreach ($posts as $post){
            $eventdate = get_post_meta($post['ID'], 'startevent', true);
            $key = date('d.n.Y', strtotime($eventdate));
            $events[$key]['text'] = $post['post_title'];
            $events[$key]['link'] = get_permalink($post['ID']);

        }
        
    }?>

    <div id="event-calendar">   
        <?=Calendar::getMonth(date($month), date($year), $events);?>
    </div>

    <?php if ($script){?>
        <script>
    
            var calend_link_base = "<?=get_site_url()?>/wp-content/themes/vij/src/eventcalendar.php";
            var calend_curr_year = "<?=$year?>";
            var calend_curr_month = "<?=$month?>";
            
            
            function calend_prev_month(){
                load_calendar_month(type = 'prev');
            }
            
            function calend_next_month(){
                load_calendar_month(type = 'next');
            }
            
            
            function load_calendar_month(type = 'next'){
    
                if (type == 'next'){
                    
                    if (calend_curr_month == 12 ){
                        calend_curr_month = 1;
                        calend_curr_year = parseInt(calend_curr_year) + 1;
                    }
                    else {
                        calend_curr_month = parseInt(calend_curr_month) + 1;
                    }    
    
                }
                else if (type == 'prev'){

                    if (calend_curr_month == 1 ){
                        calend_curr_month = 12;
                        calend_curr_year = parseInt(calend_curr_year) - 1;
                    }
                    else {
                        calend_curr_month = parseInt(calend_curr_month) - 1;
                    }    
                }

                $currlink = calend_link_base + '?year=' + calend_curr_year + '&month=' + calend_curr_month;

                fetch($currlink).then(function (response) {
                    return response.text();
                }).then(function (html) {
                    document.getElementById('event-calendar').innerHTML = html;
                }).catch(function (err) {
                    console.warn('Something went wrong.', err);
                });
    
            }
    
        </script>
        
        <?}

}