<?php

$error_message  = '' ;
$staff_id       = escapeString($_POST['modal_staff_id']) ;
$check_group    = escapeString($_POST['modal_check_group']) ;

if ( $staff_id != '' && $check_group != '' ){

	$error_message = '<div class="result_error">Out must be greater than in.</div>' ;

    // get previous check group
    $previous_group    = date('Y-m-d', strtotime($check_group.' -1 days')) ;

    // default parameter
    $attendance_id_0     = escapeString($_POST['attendance_id_0']) ;
    $attendance_time_0   = escapeString($_POST['attendance_time_0']) ;
    $attendance_remark_0 = escapeString($_POST['attendance_remark_0']) ;
    $new_in_out_0        = ( $attendance_time_0 != '' ? ( escapeString($_POST['attendance_date_0']) . ' ' . $attendance_time_0 ) : '' ) ;

    $attendance_id_1     = escapeString($_POST['attendance_id_1']) ;
    $attendance_time_1   = escapeString($_POST['attendance_time_1']) ;
    $attendance_remark_1 = escapeString($_POST['attendance_remark_1']) ;
    $new_in_out_1        = ( $attendance_time_1 != '' ? ( escapeString($_POST['attendance_date_1']) . ' ' . $attendance_time_1 ) : '' ) ;

    $attendance_id_2     = escapeString($_POST['attendance_id_2']) ;
    $attendance_time_2   = escapeString($_POST['attendance_time_2']) ;
    $attendance_remark_2 = escapeString($_POST['attendance_remark_2']) ;
    $new_in_out_2        = ( $attendance_time_2 != '' ? ( escapeString($_POST['attendance_date_2']) . ' ' . $attendance_time_2 ) : '' ) ;

    $attendance_id_3     = escapeString($_POST['attendance_id_3']) ;
    $attendance_time_3   = escapeString($_POST['attendance_time_3']) ;
    $attendance_remark_3 = escapeString($_POST['attendance_remark_3']) ;
    $new_in_out_3        = ( $attendance_time_3 != '' ? ( escapeString($_POST['attendance_date_3']) . ' ' . $attendance_time_3 ) : '' ) ;

    $attendance_id_4     = escapeString($_POST['attendance_id_4']) ;
    $attendance_time_4   = escapeString($_POST['attendance_time_4']) ;
    $attendance_remark_4 = escapeString($_POST['attendance_remark_4']) ;
    $new_in_out_4        = ( $attendance_time_4 != '' ? ( escapeString($_POST['attendance_date_4']) . ' ' . $attendance_time_4 ) : '' ) ;

    $attendance_id_5     = escapeString($_POST['attendance_id_5']) ;
    $attendance_time_5   = escapeString($_POST['attendance_time_5']) ;
    $attendance_remark_5 = escapeString($_POST['attendance_remark_5']) ;
    $new_in_out_5        = ( $attendance_time_5 != '' ? ( escapeString($_POST['attendance_date_5']) . ' ' . $attendance_time_5 ) : '' ) ;

    $attendance_id_6     = escapeString($_POST['attendance_id_6']) ;
    $attendance_time_6   = escapeString($_POST['attendance_time_6']) ;
    $attendance_remark_6 = escapeString($_POST['attendance_remark_6']) ;
    $new_in_out_6        = ( $attendance_time_6 != '' ? ( escapeString($_POST['attendance_date_6']) . ' ' . $attendance_time_6 ) : '' ) ;

    $attendance_id_7     = escapeString($_POST['attendance_id_7']) ;
    $attendance_time_7   = escapeString($_POST['attendance_time_7']) ;
    $attendance_remark_7 = escapeString($_POST['attendance_remark_7']) ;
    $new_in_out_7        = ( $attendance_time_7 != '' ? ( escapeString($_POST['attendance_date_7']) . ' ' . $attendance_time_7 ) : '' ) ;

    // check out must be greater than in
    $boolean_check     = false ;
    if ( $attendance_time_0 != '' && $attendance_time_1 != '' ){
         if ( $attendance_time_1 >= $attendance_time_0 ){ }else{ $boolean_check = true ; }
    }
    if ( $attendance_time_1 != '' && $attendance_time_2 != '' ){
         if ( $attendance_time_2 >= $attendance_time_1 ){ }else{ $boolean_check = true ; }
    }
    if ( $attendance_time_2 != '' && $attendance_time_3 != '' ){
         if ( $attendance_time_3 >= $attendance_time_2 ){ }else{ $boolean_check = true ; }
    }
    if ( $attendance_time_3 != '' && $attendance_time_4 != '' ){
         if ( $attendance_time_4 >= $attendance_time_3 ){ }else{ $boolean_check = true ; }
    }
    if ( $attendance_time_4 != '' && $attendance_time_5 != '' ){
         if ( $attendance_time_5 >= $attendance_time_4 ){ }else{ $boolean_check = true ; }
    }
    if ( $attendance_time_5 != '' && $attendance_time_6 != '' ){
         if ( $attendance_time_6 >= $attendance_time_5 ){ }else{ $boolean_check = true ; }
    }
    if ( $attendance_time_6 != '' && $attendance_time_7 != '' ){
         if ( $attendance_time_7 >= $attendance_time_6 ){ }else{ $boolean_check = true ; }
    }

    // if false then allow to access
    if ( !$boolean_check ){

        $error_message = '<div class="result_error">Record failed to updated.</div>' ;

        // start commit
        $error      = 0 ;
        $mysqli->autocommit( false ) ;

        // insert or update attendance
        for ( $a = 0 ; $a < 8 ; $a++ ){

            $attendance_id = '' ;
            $remark        = '' ;
            $new_in_out    = '' ;

            switch ( $a ){
                case '0' : 
                    $attendance_id   = $attendance_id_0 ; 
                    $remark          = $attendance_remark_0 ; 
                    $new_in_out      = $new_in_out_0 ; 
                break ;
                case '1' : 
                    $attendance_id   = $attendance_id_1 ; 
                    $remark          = $attendance_remark_1 ; 
                    $new_in_out      = $new_in_out_1 ; 
                break ;
                case '2' : 
                    $attendance_id   = $attendance_id_2 ; 
                    $remark          = $attendance_remark_2 ; 
                    $new_in_out      = $new_in_out_2 ; 
                break ;
                case '3' : 
                    $attendance_id   = $attendance_id_3 ; 
                    $remark          = $attendance_remark_3 ; 
                    $new_in_out      = $new_in_out_3 ; 
                break ;
                case '4' : 
                    $attendance_id   = $attendance_id_4 ; 
                    $remark          = $attendance_remark_4 ; 
                    $new_in_out      = $new_in_out_4 ; 
                break ;
                case '5' : 
                    $attendance_id   = $attendance_id_5 ; 
                    $remark          = $attendance_remark_5 ; 
                    $new_in_out      = $new_in_out_5 ; 
                break ;
                case '6' : 
                    $attendance_id   = $attendance_id_6 ; 
                    $remark          = $attendance_remark_6 ; 
                    $new_in_out      = $new_in_out_6 ; 
                break ;
                case '7' : 
                    $attendance_id   = $attendance_id_7 ; 
                    $remark          = $attendance_remark_7 ; 
                    $new_in_out      = $new_in_out_7 ; 
                break ;
            }

            if ( $new_in_out != '' ){
                
                if ( $attendance_id == '0' ){
                    // add attendance
                    if ( $mysqli->query("INSERT INTO staff_attendance
                                         (staff_id, check_group, type, code, record_from, mac_address, ip_address, latitude, longitude, check_area, remark, created_at, updated_at) VALUES
                                         ('".$staff_id."', '".$check_group."', '".$attendance_type."', '', 'manual', '', '', '', '', 'in', '".$remark."', '".$new_in_out."', '".TODAYDATE."')") ){ }else{
                        $error++ ;
                    }
                }else{

                    // update attendance
                    $attendance_q = $mysqli->query("SELECT * FROM staff_attendance
                                                    WHERE deleted_at IS NULL AND attendance_id = '".$attendance_id."' LIMIT 1") ;
                    if ( $attendance_q->num_rows > 0 ){
                        $attendance  = $attendance_q->fetch_assoc() ;
                        $query_date  = '' ;
                        if ( $attendance['created_at'] != $new_in_out ){
                            $query_date .= "created_at = '".$new_in_out."'," ; 
                        }
                        // update attendance
                        if ( $mysqli->query("UPDATE staff_attendance SET
                                             ".$query_date."
                                             remark              = '".$remark."',
                                             updated_at          = '".TODAYDATE."'
                                             WHERE attendance_id = '".$attendance_id."'") ){ }else{
                            $error++ ;
                        }
                    }

                }
            }else{

                if ( $attendance_id != '0' ){

                    // update attendance
                    $attendance_q = $mysqli->query("SELECT * FROM staff_attendance
                                                    WHERE deleted_at IS NULL AND attendance_id = '".$attendance_id."' LIMIT 1") ;
                    if ( $attendance_q->num_rows > 0 ){
                        $attendance  = $attendance_q->fetch_assoc() ;

                        // delete attendance list
                        if ( $mysqli->query("UPDATE staff_attendance SET
                                             deleted_at          = '".TODAYDATE."'
                                             WHERE attendance_id = '".$attendance_id."'") ){ }else{
                            $error++ ;
                        }
                    }
                }
            }
        }

        // remove attendance list, direct remove 2 days, yesterday and today
        /*
        $array_group = [ $previous_group, $check_group ] ;
        foreach ( $array_group as $k => $v ){
            $get_list_q = $mysqli->query("SELECT list_id FROM staff_attendance_list
                                          WHERE staff_id = '".$staff_id."' AND list_date = '".$v."' AND deleted_at IS NULL LIMIT 1") ;
            if ( $get_list_q->num_rows > 0 ){
                // delete attendance list
                $get_list = $get_list_q->fetch_assoc() ;
                $list_id  = $get_list['list_id'] ;
                if ( $mysqli->query("UPDATE staff_attendance_list SET
                                     deleted_at    = '".TODAYDATE."'
                                     WHERE list_id = '".$list_id."'") ){

                    // delete the merge record
                    if ( $mysqli->query("UPDATE staff_attendance SET
                                         list_id       = '0'
                                         WHERE list_id = '".$list_id."'") ){ }else{
                        $error++ ; 
                    }

                }else{
                    $error++ ; 
                }
            }
        }
        */

        if( $error == 0 ) {

            // commit query
            $mysqli->commit() ; 
            $error_message = '<div class="result_success">Thank you, your attendance record success to updated.</div>' ;

        }else{
            $mysqli->rollback() ;
        }

    }

    // refresh page
    header("Refresh: 0") ;
    $_SESSION['system_result'] = $error_message ;
    exit ;

}

?>