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

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

    $task_id            = $array['task_id'] ;
    $todo_list          = $array['todo_list'] ;

    if ( $task_id != '' && count( $todo_list ) > 0 ){
        $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', 'completed' ) LIMIT 1" ) ;
        if ( $select->num_rows > 0 ){
            $status          = '202' ;

            $row             = $select->fetch_assoc() ;

            $count_todo_list = 0 ;
            $count_todo_done = 0 ;
            if ( checkExists($array['todo_list']) ){
                foreach ( $array['todo_list'] as $k => $v ){
                    if ( $v['is_delete'] == 'no' ){
                        $count_todo_list++ ;

                        if ( $v['is_done'] == 'yes' ){
                            $count_todo_done++ ;
                        }
                    }
                }
            }

            if ( checkExists($array['todo_list']) ){
                foreach ( $array['todo_list'] as $k => $v ){
                    if ( $v['is_delete'] == 'no' ){
                        if ( $v['id'] > 0 ){
                            $mysqli->query( "UPDATE task_todo SET
                                             title         = '".$v['title']."',
                                             sortable      = '".$v['sortable']."'
                                             WHERE todo_id = '".$v['id']."'" ) ;
                        }else{
                            $mysqli->query( "INSERT INTO task_todo
                                             ( `task_id`, `title`, `sortable` ) VALUES
                                             ( '".$task_id."', '".$v['title']."', '".$v['sortable']."' )" ) ;
                        }
                    }else{
                        if ( $v['id'] > 0 ){
                            $mysqli->query( "UPDATE task_todo SET
                                             deleted_at    = '".TODAYDATE."'
                                             WHERE todo_id = '".$v['id']."'" ) ;
                        }
                    }
                }
            }

            $update_status     = 'pending' ;
            if ( $row['assigned_by'] > 0 ){
                $update_status = 'assigned' ;
            }
            if ( $count_todo_done > 0 ){
                $update_status = 'progress' ;
            }
            if ( $count_todo_list == $count_todo_done ){
                $update_status = 'completed' ;
            }

            if ( $mysqli->query( "UPDATE task SET
                                  status        = '".$update_status."',
                                  todo_list     = '".$count_todo_list."',
                                  todo_done     = '".$count_todo_done."'
                                  WHERE task_id = '".$task_id."'" ) ) {
                $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 Todo', 'Task ( '.$row['task_so'].' ) todo has been updated' ) ;
                }
            }


        }

    }
}

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