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

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

    $task_id            = $array['task_id'] ;
    $extra              = $array['extra'] ;
    $rating             = $array['rating'] ;

    if ( $task_id      != '' ){
        $status         = '278' ;

        if ( $rating   != '' ){
            $status     = '201' ;

            $select     = $mysqli->query( "SELECT task_so, task_type, difficulty, date_end, assigned_by, incentive, incentive2, confirmed_at, updated_at FROM task
                                           WHERE deleted_at IS NULL AND task_id = '".$task_id."' AND ( created_by = '".$staff_info['staff_id']."' ) AND status = 'confirmed' LIMIT 1" ) ;
            if ( $select->num_rows > 0 ){
                $status              = '212' ;

                $row                 = $select->fetch_assoc() ;
                $is_late             = 'yes' ;
                if ( $row['date_end'] >= $row['confirmed_at'] ){
                    $is_late         = 'no' ;
                }

                if ( $mysqli->query( "UPDATE task SET
                                      extra           = '".$extra."',
                                      rating          = '".$rating."',
                                      is_late         = '".$is_late."',
                                      status          = 'approved'
                                      WHERE task_id   = '".$task_id."'" ) ) {


                    // $earned_assigned = ( $extra ) ;
                    // $earned_executed = ( $extra ) ;
                    // if ( $row['date_end'] >= $row['updated_at'] ){
                        $earned_assigned   = ( $row['incentive']  + $extra ) ;
                        $earned_executed   = ( $row['incentive2'] + $extra ) ;
                    // }

                    $remark1         = 'Earn point from task ' . $row['task_so'] ;
                    $remark2         = 'Earn incentive from task ' . $row['task_so'] ;

                    if ( $row['assigned_by'] > 0 ){
                        pointMovement( 'task', $task_id, $rating, 'normal', $row['assigned_by'], '0', $remark1 ) ;
                        pointMovement( 'task', $task_id, $row['task_type'], $row['difficulty'], $row['assigned_by'], $earned_assigned, $remark2 ) ;
                    }

                    // joinstaff earn point
                    $select_joinstaff          = $mysqli->query( "SELECT staff_id FROM task_joinstaff
                                                                  WHERE task_id = '".$task_id."'" ) ;
                    if ( $select_joinstaff->num_rows > 0 ){
                        while ( $row_joinstaff = $select_joinstaff->fetch_assoc() ){
                            pointMovement( 'task', $task_id, $rating, 'normal', $row_joinstaff['staff_id'], '0', $remark1 ) ;
                            pointMovement( 'task', $task_id, $row['task_type'], $row['difficulty'], $row_joinstaff['staff_id'], $earned_executed, $remark2 ) ;
                        }
                    }

                    $status = '200' ;

                    // push to notification
                    $related_staffid = getTaskRelatedStaff( $task_id, $row['created_by'], $row['assigned_by'] ) ;
                    foreach ( $related_staffid as $k => $v ){
                        pushToUserCron( 'task', $task_id, $v, 'Task Approved', 'Task ( '.$row['task_so'].' ) has been approved' ) ;
                    }
                }

            }
        }
    }
}

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