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

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

    $today              = TODAYDAY ;
    $today_month        = date( 'Y-m', strtotime( TODAYDATE ) ) ;
    $last_day           = date( 'j', strtotime( TODAYDATE ) ) ;

    $attendances_q      = $mysqli->query("SELECT * FROM staff_attendance
                                          WHERE deleted_at IS NULL AND staff_id = '".$staff_info['staff_id']."' AND created_at LIKE '".$today_month."%' ORDER BY created_at ASC") ;
    $list               = [] ;
    if ( $attendances_q->num_rows > 0 ){
        while ( $attendance = $attendances_q->fetch_assoc() ){
            $created_at     = $attendance['created_at'] ;
            $created_date   = date('Y-m-d', strtotime($created_at)) ;
            $list[$created_date][] = date('H:i', strtotime($attendance['created_at'])) ;
        }
    }
        

    $all                = [] ;
    for ( $a            = $last_day ; $a >= 1 ; $a-- ){

        $lead           = $today_month.'-'.str_pad( $a, '2', '0', STR_PAD_LEFT ) ;
        $temp           = [] ;
        for ( $b        = 1 ; $b <= 6 ; $b++ ){
            $temp[]     = '' ;
        }

        if ( !empty($list[$lead]) ){
            $count      = 0 ;
            foreach ( $list[$lead] as $k => $v ){
                if ( $count < 6 ){
                    $temp[$count] = $v ;
                }
                $count++ ;
            }
        }

        // get attendance list
        $attendance_list_q = $mysqli->query("SELECT list_work_day, list_type_remark FROM staff_attendance_list
                                             WHERE deleted_at IS NULL AND staff_id = '".$staff_info['staff_id']."' AND list_date = '".$lead."' LIMIT 1") ;
        $work_day       = '0' ;
        $work_type      = '' ;
        if ( $attendance_list_q->num_rows > 0 ){
            $attendance_list = $attendance_list_q->fetch_assoc() ;
            $work_day   = $attendance_list['list_work_day'] ;
            $work_type  = $attendance_list['list_type_remark'] ;
        }    
        
        $all[]          = [
            'date'      => $lead,
            'date_name' => date('d M Y', strtotime($lead)),
            'work_day'  => $work_day,
            'type'      => $work_type,
            'time'      => $temp
        ] ;
    }

    $data = [
        'attendance' => $all
    ] ;
}

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