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

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

    $task_id            = $array['task_id'] ;
    $incentive2         = $array['incentive2'] ;
    $assigned_to        = $array['assigned_to'] ;
    $joinstaffs         = ( checkExists( $array['joinstaffs'] ) ? $array['joinstaffs'] : [] ) ;

    if ( $task_id != '' && count( $joinstaffs ) > 0 ){
        $status         = '303' ;

        $staff_tier     = [] ;
        $staff_tier     = getRelatedTierID( 'no', $staff_info['staff_tier_level'] ) ;

        $related_staffs = [] ;
        if ( count( $joinstaffs ) > 0 ){
            foreach ( $joinstaffs as $k => $v ){
                $related_staffs[] = $v['id'] ;
            }
        }

        $select_staff   = $mysqli->query( "SELECT a.staff_id, a.staff_idno, a.staff_name, a.staff_shortname, a.staff_tier FROM staff a
                                           WHERE a.deleted_at IS NULL AND ( a.staff_date_resigned >= '".date("Y-m-d",time())."' OR a.staff_date_resigned = '0000-00-00' OR a.staff_date_resigned IS NULL ) AND staff_id IN ( '".implode("', '", $related_staffs)."' ) AND staff_tier IN ( '".implode("', '", $staff_tier)."' )" ) ;

        if ( $select_staff->num_rows == count( $related_staffs ) ){
            $status     = '201' ;

            $select     = $mysqli->query( "SELECT * FROM task a
                                           WHERE a.deleted_at IS NULL AND a.task_id = '".$task_id."' AND ( a.created_by = '".$staff_info['staff_id']."' OR a.assigned_by = '".$staff_info['staff_id']."' OR EXISTS ( SELECT b.staff_id FROM task_joinstaff b WHERE a.task_id = b.task_id AND b.staff_id = '".$staff_info['staff_id']."' LIMIT 1 ) ) AND a.status IN ( 'pending', 'assigned', 'resubmit', 'progress' ) LIMIT 1" ) ;
            if ( $select->num_rows > 0 ){
                $status               = '212' ;

                $row                  = $select->fetch_assoc() ;

                // get all task 
                $update_status        = 'assigned' ;
                $count_todo_done      = 0 ;
                $select_task_todo     = $mysqli->query( "SELECT is_done FROM task_todo
                                                         WHERE deleted_at IS NULL AND task_id = '".$task_id."'" ) ;
                if ( $select_task_todo->num_rows > 0 ){
                    while ( $row_todo = $select_task_todo->fetch_assoc() ){
                        if ( $row_todo['is_done'] == 'yes' ){
                            $count_todo_done++ ;
                        }
                    }
                }

                if ( $count_todo_done > 0 ){
                    $update_status    = 'progress' ;
                }

                if ( $mysqli->query( "UPDATE task SET
                                      incentive2      = '".$incentive2."',
                                      status          = '".$update_status."'
                                      WHERE task_id   = '".$task_id."'" ) ) {
                    $status = '200' ;

                    // deleted all related staff
                    $mysqli->query( "DELETE FROM `task_joinstaff` WHERE task_id = '".$task_id."'" ) ;
                    if ( checkExists($joinstaffs) ){
                        foreach ( $joinstaffs as $k => $v ){
                            $mysqli->query( "INSERT INTO `task_joinstaff` ( `task_id`, `staff_id` ) VALUES ( '".$task_id."', '".$v['id']."' )" ) ;
                        }
                    }

                    // 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 Assign', 'Task ( '.$row['task_so'].' ) has been assigned' ) ;
                    }
                }

            }
        }
    }
}

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