<?php 
    include_once dirname(__FILE__) . '/../database.php';
    include_once dirname(__FILE__) . '/../global_functions.php';
    
    // Attempt to get all of the nodes as of current for this task
    function getStatusInfoNodesHTML($db, $userRow, $taskID) {
        $result='';
        
        try {
            // Create the query to run
            $sql='SELECT tsi.*, s.Name AS StatusName, s.StatusRowColor ' .
                 'FROM taskstatusinfo_initial tsi ' . 
                 'LEFT JOIN status s ON tsi.StatusID = s.StatusID '  .
                 'WHERE tsi.TaskID = '  . $taskID . ' ' .
                 'ORDER BY tsi.ExecutionOrder;';

            // Query the database for the user 
            $rows = $db->queryDB($sql, $userRow, "get_initial_routing Form occurance 1");

            // If there are rows, then we are good to go.
            if ($rows == null || mysqli_num_rows($rows)<1){
                // Create the error string to return back to the caller
                return 'ABORT GET ROUTING (get_initial_routing)::There was not a template found with that ID.  Try again.';
            }
            else{
                $loopNumber=1;
                // Loop through the status rows
                while ($row=mysqli_fetch_array($rows)){
                    // Add to the result
                    $result=$result . '<li class="Table ViableStatus" style="background-color: #' . $row["StatusRowColor"] . '; cursor: pointer;">
                                        <div style="font-weight:bold; v-alignment:middle;"> ' .
                                          $row["StatusName"]  .
                                          '<a id="expandLI" name="expandLI" style="float: left; cursor: s-resize; display: block;" onclick="toggleProperties(this);" title="Expand the properties of this status routing.">+</a>
                                        </div>
                                        <div id="expandableLI" name="expandableLI" style="background-color: white; display:none;"> 
                                          <input type="hidden" id="taskStatusInfoID" name="taskStatusInfoID" value=' . $row["TaskStatusInfoID"] . '>
                                          <input type="hidden" id="infoStatusID" name="infoStatusID" value=' . $row["StatusID"] . '>
                                          <table>
                                            <tr>
                                                <td colspan=1 align="right">
                                                    <strong><label style="font-size:9pt;" title="The estimated amount of time that it will take to complete this state.">Time (Hrs):</label></strong>
                                                </td>
                                                <td colspan=1 align="left" width="15%">
                                                    <input name="statusInfoEstimatedTime" id="statusInfoEstimatedTime" type="text" maxlength="5" style="width: 100%; height: 15px; font-size:9pt;" value="' . $row["EstimatedTime"] . '" onchange="div_show(\'loadSavedTrunk\');" title="The estimated amount of time that it will take to complete this state.">
                                                </td>
                                                <td colspan=1 align="right">
                                                    <strong><label style="font-size:9pt;" title="The the fudge factor allowed for time completion.  Should be a value over one.">Time Factor:</label></strong>
                                                </td>
                                                <td colspan=1 width="15%">
                                                    <input name="statusInfoCompletionFactor" id="statusInfoCompletionFactor" type="text" maxlength="5" style="width: 100%; height: 15px; font-size:9pt;" value="' . $row["CompletionFactor"] . '" onchange="div_show(\'loadSavedTrunk\');" title="The the fudge factor allowed for time completion.  Should be a value over one."> 
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan=4 align="center">
                                                    <br>
                                                    <strong><label style="font-size:9pt;">Notes On Status:</label></strong>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan=4>
                                                    <textarea name="statusInfoNote" id="statusInfoNote" maxlength="4000" class="intext" style="width: 100%; height: 70px; overflow: vertical; resize:vertical">' . $row["StatusNotes"] . '</textarea>
                                                </td>
                                            </tr>
                                          </table>
                                        </div>
                                      </li>';
                    
                    $loopNumber++;
                }
                
                // Create the head of the table and add the rows
                $result = '    <li class="ui-state-default ui-state-disabled Table" style="clear: both; width: 400px;"></li>
                          ' . $result;
            }
        } 
        catch (Exception $exc) {
            // Create the error string to return back to the caller
            return 'ABORT GET ROUTING (get_initial_routing)::There was an uncaught error while trying to get the routing template.  The exception is: ' . $exc->getMessage() . '. Aborted...';
        }
        
        // If we got here then assume success
        return $result;
    }
    
    $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 GET ROUTING (get_initial_routing)::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
        $taskID=null;
        // check the passed in TID
        if (isset($_GET['taskID']) ){
            // get the passed in TID
            $taskID = $_GET['taskID'];
        }
        
        // Attempt to get the nodes requested
        $returnString=getStatusInfoNodesHTML($db, $userRow, $taskID);
        
        // return the result
        echo $returnString;
    }
    
?>