<?php
$must_login             = true ;
$require_path           = '../../../' ;
$require_sub            = '../../' ;
require( $require_sub.'header.php' ) ;

if ( $boolean_login ){
	$status   		    = '201' ;

    $main_id            = $array['main_id'] ;
    $sub_id             = $array['sub_id'] ;
    $searchyearmonth    = $array['searchyearmonth'] ;
    $searchday          = $array['searchday'] ;
    $searchday          = ( $searchday != '' ? $searchday : date( 'd', time() ) ) ;
    $searchyearmonthday = $searchyearmonth . '-' . $searchday ;

    $list               = [] ;
    $selected           = [] ;
    $marked             = [] ;

    $search_query       = '' ;
    $search_query      .= " AND ( a.date_from LIKE '%".$searchyearmonth."%' OR a.date_to LIKE '%".$searchyearmonth."%' )" ;

    $query              = "SELECT a.request_id, a.staff_id, a.is_main, a.main_id, a.sub_id, a.type, a.title, a.description, a.reason, a.date_from, a.date_to, a.status, a.created_at, b.staff_idno, b.staff_name, b.staff_shortname FROM request a
                           LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
                           WHERE a.deleted_at IS NULL AND a.branch_id = '".$array['branch_id']."' AND a.main_id = '".$main_id."' AND a.sub_id = '".$sub_id."' AND a.type = 'reservation' AND a.status IN ( 'confirmed' ) " . $search_query ;

    $mysqli_query       = $mysqli->query( $query . " ORDER BY a.date_from" ) ;

    if ( $mysqli_query->num_rows > 0 ){

        $status         = '200' ;

        while ( $row    = $mysqli_query->fetch_assoc() ){

            $boolean_loop   = true ;
            $date_from      = date( 'Y-m-d', strtotime( $row['date_from'] ) ) ;
            $date_to        = date( 'Y-m-d', strtotime( $row['date_to'] ) ) ;

            $loop_date_from = $date_from ;
            $loop_date_to   = $date_to ;
            while ( $boolean_loop ){
                if ( $loop_date_from <= $loop_date_to ){
                    $selected[$loop_date_from] = $loop_date_from ;
                }else{
                    $boolean_loop = false ;
                }

                $loop_date_from  = date('Y-m-d', strtotime("+1 day", strtotime($loop_date_from))) ;
            }

            if ( $date_from <= $searchyearmonthday && $date_to >= $searchyearmonthday ){
                $row['title']        = dataFilter( $row['title'] ) ;
                $row['description']  = dataFilter( $row['description'] ) ;
                $row['reason']       = dataFilter( $row['reason'] ) ;
                $row['request_date'] = ( $row['date_from'] != '' ? date( "Y-m-d", strtotime( $row['date_from'] ) ) : '' ) ;
                $row['time_from']    = date( 'h:ia', strtotime( $row['date_from'] ) ) ;
                $row['time_to']      = date( 'h:ia', strtotime( $row['date_to'] ) ) ;
                $list[]              = $row ;
            }
        }
    }


    $first_day_of_month = $searchyearmonth . '-01' ;
    $last_day_of_month  = $searchyearmonth . '-' . date( 't', strtotime( $first_day_of_month ) ) ;
    for ( $a = $first_day_of_month ; $a <= $last_day_of_month ; $a++ ){
        if ( $selected[$a] != null  ){
            $marked[$a] = [ 'marked' => true, 'dotColor' => '#ff9500' ] ;
        }
    }

    $data['yearmonthday'] = $searchyearmonthday ;
    $data['list']         = $list ;
    $data['marked']       = $marked ;

}

require( $require_sub.'footer.php' ) ;
?>