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

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

    $searchyearmonth    = $array['searchyearmonth'] ;
    $searchday          = $array['searchday'] ;
    $searchyearmonthday = '' ;

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


    $services           = $mysqli->query( "SELECT a.module FROM app_service a
                                           WHERE a.deleted_at IS NULL AND a.type = 'service' AND a.module IN ( 'HrBranch' ) AND on_off = 'on' 
                                           LIMIT 1" ) ;
    $is_filterbranch    = 'no' ;
    if ( $services->num_rows > 0 ){
        $is_filterbranch = 'yes' ;
    }

	$search_query       = ( $is_filterbranch == 'yes' ? " AND branch_id = '".$array['branch_id']."'" : '' ) ;
    $search_query2      = '' ;

    $search_query      .= " AND ( a.leave_from LIKE '%".$searchyearmonth."%' OR a.leave_to LIKE '%".$searchyearmonth."%' )" ;
    $search_query2     .= " AND holiday_date LIKE '%".$searchyearmonth."%'" ;

    if ( $searchday    != '' ){
        $searchyearmonthday = $searchyearmonth . '-' . $searchday ;

        $search_query  .= " AND a.leave_from <= '".$searchyearmonthday."' AND a.leave_to >= '".$searchyearmonthday."'" ;
        $search_query2 .= " AND holiday_date = '".$searchyearmonthday."'" ;
    }

    $query              = "SELECT a.leave_type, a.leave_from, a.leave_to, a.leave_day, a.leave_reason, a.leave_status, b.staff_idno, b.staff_name, b.staff_shortname FROM staff_leave a
                           LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
                           WHERE b.deleted_at IS NULL AND ( b.staff_date_resigned >= '".date("Y-m-d",time())."' OR b.staff_date_resigned = '0000-00-00' OR b.staff_date_resigned IS NULL ) AND a.deleted_at IS NULL AND a.leave_status IN ( 'pending', 'confirmed' ) " . $search_query ;

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


    if ( $mysqli_query->num_rows > 0 ){

        $status         = '200' ;

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

            $boolean_loop = true ;
            $leave_from   = $row['leave_from'] ;
            $leave_to     = $row['leave_to'] ;
            $row['leave_reason'] = dataFilter( $row['leave_reason'] ) ;
            while ( $boolean_loop ){
                if ( $leave_from <= $leave_to ){
                    $selected[$leave_from] = $leave_from ;
                }else{
                    $boolean_loop = false ;
                }

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

            $row['type'] = 'leave' ;
            $list[]      = $row ;
        }
    }



    $query_holiday      = "SELECT * FROM setting_holiday
                           WHERE deleted_at IS NULL " . $search_query2 ;
    $mysqli_holiday     = $mysqli->query( $query_holiday . " ORDER BY holiday_date ASC" ) ;
    if ( $mysqli_holiday->num_rows > 0 ){
        while ( $row_holiday = $mysqli_holiday->fetch_assoc() ){
            $row_holiday['type'] = 'holiday' ;
            $list[]     = $row_holiday ;
        }
    }



    $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' ] ;
        }
    }


    // get holiday list
    $select_holiday     = $mysqli->query( "SELECT holiday_date FROM setting_holiday WHERE deleted_at IS NULL AND holiday_date LIKE '%".$searchyearmonth."%'" ) ;
    if ( $select_holiday->num_rows > 0 ){
        while ( $row_holiday = $select_holiday->fetch_assoc() ){
            $marked[$row_holiday['holiday_date']] = [ 'marked' => true, 'dotColor' => '#5188ff' ] ;
        }
    }

    $data['list']       = $list ;
    $data['marked']     = $marked ;
}

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