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

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

    $list              = [] ;
	$search_query      = '' ;

	if ( $array['search'] != '' ){
        $search_query .= " AND leave_reason LIKE '%".$array['search']."%'" ;
    }

    $query             = "SELECT leave_id, leave_type, leave_from, leave_to, leave_day, leave_reason, leave_file, leave_status, created_at, updated_at FROM staff_leave
                          WHERE deleted_at IS NULL AND staff_id = '".$staff_info['staff_id']."' " . $search_query ;
    $mysqli_query      = $mysqli->query( $query . " ORDER BY created_at DESC LIMIT " . getLimit( $current ) ) ;

    if ( $mysqli_query->num_rows > 0 ){

        $status        = '200' ;

        while ( $row   = $mysqli_query->fetch_assoc() ){
            $row['leave_reason'] = dataFilter( $row['leave_reason'] ) ;
            $list[]    = $row ;
        }

    }

    $data['list']         = $list ;

    

    // get left leave
    $annual_leave         = 0 ;
    $sick_leave           = 0 ;

    $select_leave_year    = $mysqli->query( "SELECT leave_type, leave_days FROM staff_leave_year
                                             WHERE deleted_at IS NULL AND staff_id = '".$staff_info['staff_id']."' AND leave_year = '".date('Y', time())."'" ) ;
    if ( $select_leave_year->num_rows > 0 ){
        while ( $row_leave_year = $select_leave_year->fetch_assoc() ){
            if ( $row_leave_year['leave_type'] == 'annual' ){
                $annual_leave   = $row_leave_year['leave_days'] ;
            }
            if ( $row_leave_year['leave_type'] == 'sick' ){
                $sick_leave     = $row_leave_year['leave_days'] ;
            }
        }
    }



    $data['annual_leave'] = $annual_leave ;
    $data['sick_leave']   = $sick_leave ;
}

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