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

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

    $task_id            = $array['task_id'] ;
    $todo_id            = $array['todo_id'] ;
    $photos             = $array['photos'] ;
    $attachments        = $array['attachments'] ;

    if ( $task_id != '' && $todo_id != '' && ( count( $photos ) > 0 || count( $attachments ) > 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', 'confirmed' ) LIMIT 1" ) ;
        if ( $select->num_rows > 0 ){
            $status     = '201' ;

            $row        = $select->fetch_assoc() ;

            $select     = $mysqli->query( "SELECT * FROM task_todo
                                           WHERE deleted_at IS NULL AND task_id = '".$task_id."' AND todo_id = '".$todo_id."' LIMIT 1" ) ;
            if ( $select->num_rows > 0 ){
                $status     = '205' ;

                $row_todo   = $select->fetch_assoc() ;

                if ( checkExists($photos) || checkExists($attachments) ){

                    foreach ( [ $photos, $attachments ] as $upload_key => $upload_value ){
                        foreach ( $upload_value as $k => $v ){
                            if ( $v['type'] == 'local' ){
                                $file_upload = ( $v['file'] ) ;
                                $upload      = uploadImage( 'Task', $task_id.'-'.$todo_id, $file_upload ) ;
                                if ( $upload['status'] != '200' ){
                                    $count_upload++ ;
                                }else{
                                    $mysqli->query( "INSERT INTO task_media
                                                     ( task_id, todo_id, file, filetype ) VALUES
                                                     ( '".$task_id."', '".$todo_id."', '".$upload['data']['file_name']."', '".$upload['data']['file_type']."' )" ) ;
                                    $status = '200' ;
                                }
                            }
                        }
                    }

                    if ( $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 Uploaded', 'Task ( '.$row['task_so'].' ) file uploaded' ) ;
                        }
                    }

                }

            }
        }

    }
}

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