<?php 
    include_once dirname(__FILE__) . '/../database.php';
    include_once dirname(__FILE__) . '/../global_functions.php';
    
    // attempt to copy all tasks inside of or this particular task to the task requested
    function modifyStatusInfoNode($db, $userRow, $taskID,
                                                 $taskStatusInfoID,
                                                 $taskStatusInfoMainBranchID,
                                                 $taskStatusInfoMergedToBranchID,
                                                 $statusID,
                                                 $statusInfoEstimatedTime,
                                                 $statusInfoActualTime,
                                                 $statusInfoCompletionFactor,
                                                 $statusInfoNote,
                                                 $executionOrder,
                                                 $addCurrentRouteNode, 
                                                 $feedbackNote,
                                                 $autoAppendedNotes) {
        try {
            // Determine if we have an update task number to work on
            if ($taskID==null || !is_numeric($taskID) || $taskID<1){
                // Return an error string to be reported to the user
                return 'ABORT UPDATE STATUS INFO NODE (update_current_route_node)::The task number (' . $taskID . ') to add current status info nodes is not valid.  Try a different TID. Aborting...';
            }

            // Determine if we have a status ID to work on
            if ($statusID==null || !is_numeric($statusID) || $statusID<1){
                // Return an error string to be reported to the user
                return 'ABORT UPDATE STATUS INFO NODE (update_current_route_node)::The status ID (' . $statusID . ') to add status info nodes is not valid.  Try a different Status ID. Aborting...';
            }

            // Determine if we have a valid status info ID to work on
            if ($taskStatusInfoID==null || !is_numeric($taskStatusInfoID) || $taskStatusInfoID<1){
                // This should mean that we are adding a status route node.  Now check for elements that need to be there to add
                if ($executionOrder==null || !is_numeric($executionOrder) || $executionOrder<1){
                    // Return an error string to be reported to the user
                    return 'ABORT UPDATE STATUS INFO NODE (update_current_route_node)::There must be an execution order to add a node. Try again. Aborting...';
                }
                
                // Let's create the insert query off of what we have
                $sql='INSERT INTO taskstatusinfo_current (';
                $sql = $sql . 'TaskID, StatusID, ExecutionOrder, UseForInitialRouting';
                if ($statusInfoCompletionFactor!=null && $statusInfoCompletionFactor>0) {$sql = $sql . ', CompletionFactor';}
                if ($statusInfoEstimatedTime!=null && $statusInfoEstimatedTime>0) {$sql = $sql . ', EstimatedTime';}
                if ($statusInfoActualTime!=null && $statusInfoActualTime>0) {$sql = $sql . ', ActualTime';}
                if ($taskStatusInfoMainBranchID!=null && $taskStatusInfoMainBranchID>0) {$sql = $sql . ', MainBranchID';}
                if ($taskStatusInfoMergedToBranchID!=null && $taskStatusInfoMergedToBranchID>0) {$sql = $sql . ', MergedToBranchID';}
                if ($feedbackNote!=null && $feedbackNote!="") {$sql = $sql . ', CompletionFeedbackNotes';}
                if ($autoAppendedNotes!=null && $autoAppendedNotes!="") {$sql = $sql . ', AutoAppendedNotes';}
                $sql = $sql . ', StatusNotes';
                $sql = $sql . ') ';
                $sql = $sql . 'VALUES (' . $taskID . ', ' . $statusID . ', ' . $executionOrder . ', ' . $addCurrentRouteNode;
                if ($statusInfoCompletionFactor!=null && $statusInfoCompletionFactor>0) {$sql = $sql . ', ' . $statusInfoCompletionFactor;}
                if ($statusInfoEstimatedTime!=null && $statusInfoEstimatedTime>0) {$sql = $sql . ', ' . $statusInfoEstimatedTime;}
                if ($statusInfoActualTime!=null && $statusInfoActualTime>0) {$sql = $sql . ', ' . $statusInfoActualTime;}
                if ($taskStatusInfoMainBranchID!=null && $taskStatusInfoMainBranchID>0) {$sql = $sql . ', ' . $taskStatusInfoMainBranchID;}
                if ($taskStatusInfoMergedToBranchID!=null && $taskStatusInfoMergedToBranchID>0) {$sql = $sql . ', ' . $taskStatusInfoMergedToBranchID;}
                if ($feedbackNote!=null && $feedbackNote!="") {$sql = $sql . ', "' . $feedbackNote . '"';}
                if ($autoAppendedNotes!=null && $autoAppendedNotes!="") {$sql = $sql . ', "' . $currentDateString . ': ' . $autoAppendedNotes . '" ';}
                $sql = $sql . ', "' . $statusInfoNote . '" ';
                $sql = $sql . ');';

                // Run the insert query
                $db->queryDB($sql, $userRow, "updateTask Form occurance 2");
            }
            else {
                // Get the current date string
                $currentDate = new DateTime();
                $currentDate->modify('-1 hour');
                $currentDateString = $currentDate->format('Y-m-d H:i:s');
                
                // Make an update to change modified characteristics
                $sql='UPDATE taskstatusinfo_current SET ';
                $sql = $sql . 'TaskID=' . $taskID;
                $sql = $sql . ', StatusID=' . $statusID;
                if ($executionOrder!=null && $executionOrder>0) {$sql = $sql . ', ExecutionOrder=' . $executionOrder;}
                if ($addCurrentRouteNode!=null && $addCurrentRouteNode>0) {$sql = $sql . ', UseForInitialRouting=' . $addCurrentRouteNode;}
                if ($statusInfoCompletionFactor!=null && $statusInfoCompletionFactor>0) {$sql = $sql . ', CompletionFactor=' . $statusInfoCompletionFactor;}
                if ($statusInfoEstimatedTime!=null && $statusInfoEstimatedTime>0) {$sql = $sql . ', EstimatedTime=' . $statusInfoEstimatedTime;}
                if ($statusInfoActualTime!=null && $statusInfoActualTime>0) {$sql = $sql . ', ActualTime=' . $statusInfoActualTime;}
                if ($taskStatusInfoMainBranchID!=null && $taskStatusInfoMainBranchID>0) {$sql = $sql . ', MainBranchID=' . $taskStatusInfoMainBranchID;}
                if ($taskStatusInfoMergedToBranchID!=null && $taskStatusInfoMergedToBranchID>0) {$sql = $sql . ', MergedToBranchID=' . $taskStatusInfoMergedToBranchID;}
                $sql = $sql . ', StatusNotes="' . trim($statusInfoNote) . '" ';
                if ($feedbackNote!=null && $feedbackNote!="") {$sql = $sql . ', CompletionFeedbackNotes="' . $feedbackNote . '"';}
                if ($autoAppendedNotes!=null && $autoAppendedNotes!="") {$sql = $sql . ', AutoAppendedNotes=CONCAT(AutoAppendedNotes,"\n\n' . $currentDateString . ': ' . $autoAppendedNotes . '")';}

                // Finish up with the where clause
                $sql=$sql .' WHERE TaskStatusInfoID=' . $taskStatusInfoID . ';';

                // Run the update query
                $db->queryDB($sql, $userRow, "updateTask Form occurance 3");
            }
        } 
        catch (Exception $exc) {
            // Create the error string to return back to the caller
            return 'ABORT UPDATE STATUS INFO NODE (update_current_route_node)::There was an uncaught error while adding the rout node.  The exception is: ' . $exc->getMessage() . '. Aborting...';
        }
        
        // If we got here then assume success
        return "";
    }
    
    $db = new Database();
    $db->setupTmsDB();
    
    // Set defaults for the passed in variables
    $sessionID=-1;

    // check the passed in session ID
    if (isset($_GET['sessionID']) ){
        // get the passed in session ID
        $sessionID = $_GET['sessionID'];
    }

    // Attempt to log in with this user
    $currentUser = $db->getLoggedInSessionUser($sessionID);
    if (mysqli_num_rows($currentUser)!=1){
        echo 'ABORT UPDATE STATUS INFO NODE (update_current_route_node)::Your session has expired.  ' . 
             'Refresh the page before cleaning this task.';
    }
    else {
        // Attempt to log in with this user
        $userRow = $currentUser->fetch_assoc();
        
        // Set defaults for the passed in variables
        $updateTaskNumber=-1;
        $taskStatusInfoID=-1;
        $taskStatusInfoMainBranchID=0;
        $taskStatusInfoMergedToBranchID=-1;
        $infoStatusID=null;
        $statusInfoEstimatedTime=0;
        $statusInfoActualTime=0;
        $statusInfoCompletionFactor=1;
        $statusInfoNote='';
        $executionOrder=-1;
        $addCurrentRouteNode=0;
        $feedbackNote='';        
        $autoAppendedNotes='';        
        
        // check the passed in update task ID
        if (isset($_GET['updateTaskNumber']) ){
            // get the passed in update task ID
            $updateTaskNumber = $_GET['updateTaskNumber'];
        }

        // check the passed in Status Info ID
        if (isset($_GET['taskStatusInfoID']) ){
            // get the passed in Status Info ID
            $taskStatusInfoID = $_GET['taskStatusInfoID'];
        }

        // check the passed in Main Branch ID
        if (isset($_GET['taskStatusInfoMainBranchID']) ){
            // get the passed in Main Branch ID
            $taskStatusInfoMainBranchID = $_GET['taskStatusInfoMainBranchID'];
        }

        // check the passed in Merged To Branch ID
        if (isset($_GET['taskStatusInfoMergedToBranchID']) ){
            // get the passed in Merged To Branch ID
            $taskStatusInfoMergedToBranchID = $_GET['taskStatusInfoMergedToBranchID'];
        }

        // check the passed in Info Status ID
        if (isset($_GET['infoStatusID']) ){
            // get the passed in Info Status ID
            $infoStatusID = $_GET['infoStatusID'];
        }

        // check the passed in Estimated Time
        if (isset($_GET['statusInfoEstimatedTime']) ){
            // get the passed in Estimated Time
            $statusInfoEstimatedTime = $_GET['statusInfoEstimatedTime'];
        }

        // check the passed in Actual Time
        if (isset($_GET['statusInfoActualTime']) ){
            // get the passed in Actual Time
            $statusInfoActualTime = $_GET['statusInfoActualTime'];
        }

        // check the passed in Completion Factor
        if (isset($_GET['statusInfoCompletionFactor'])){
            // get the passed in Completion Factor
            $statusInfoCompletionFactor = $_GET['statusInfoCompletionFactor'];
        }

        // check the passed in Status Info Note
        if (isset($_GET['statusInfoNote']) ){
            // get the passed in Status Info Note
            $statusInfoNote = $_GET['statusInfoNote'];
        }

        // check the passed in Execution Order
        if (isset($_GET['executionOrder']) ){
            // get the passed in Execution Order
            $executionOrder = $_GET['executionOrder'];
        }

        // check the passed in info as to if to use this to populate the initial
        if (isset($_GET['addCurrentRouteNode']) ){
            // get the passed in Use As Current Route Node
            $addCurrentRouteNode = $_GET['addCurrentRouteNode'];
        }

        // check the passed in feedback notes
        if (isset($_GET['feedbackNote']) ){
            // get the passed in feedback note
            $feedbackNote = $_GET['feedbackNote'];
        }

        // check the passed in Auto Appended Notes
        if (isset($_GET['autoAppendedNotes']) ){
            // get the passed in Auto Appended Notes
            $autoAppendedNotes = $_GET['autoAppendedNotes'];
        }

        // Attempt to clean the task of the task passed in.
        $returnString=modifyStatusInfoNode($db, $userRow,$updateTaskNumber,
                                                         $taskStatusInfoID,
                                                         $taskStatusInfoMainBranchID,
                                                         $taskStatusInfoMergedToBranchID,
                                                         $infoStatusID,
                                                         $statusInfoEstimatedTime,
                                                         $statusInfoActualTime,
                                                         $statusInfoCompletionFactor,
                                                         $statusInfoNote,
                                                         $executionOrder, 
                                                         $addCurrentRouteNode, 
                                                         $feedbackNote,
                                                         $autoAppendedNotes);

        // return the result
        echo $returnString;
    }
    
?>