<?php 
    include_once dirname(__FILE__) . '/../database.php';
    include_once dirname(__FILE__) . '/../global_functions.php';
    
    
    function getRoutingTemplatesToCopyAsSelectOptions($db, $userRow, $style='', $onchange=''){
        // Initialize the return
        $result = '';
        try {
            // Determine if this is an SU user
            if ($userRow["CompanyID"]==1){
                // Create the query to run
                $sql='SELECT DISTINCT TaskID, RoutingTitle 
                      FROM taskstatusinfo_initial
                      WHERE IsRoutingTemplate=1
                      ORDER BY RoutingTitle;';
                
                // Set the control name
                $controlName='routingTemplateToCopy';

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

                // If there are rows, then they are good to go.
                if ($rows == FALSE){
                    return $result;
                }
                else{
                    // Loop through the status rows
                    while ($row=mysqli_fetch_array($rows)){
                        // Add this option to the select
                        $result=$result . '<option value="' . $row["TaskID"] . '">' . $row["RoutingTitle"] . ' </option>';
                    }
                    
                    // Determine if there are any options
                    if ($result!=''){
                        // Start the return with the beginning of the select
                        $result='<select title="These are the routing templates available to use." name="' . $controlName . '" id="' . $controlName . '" style="' . $style . '" onchange="' . $onchange . '">' 
                                    . $result . 
                                '</select>
                                ';
                    }
                    else {
                        // Create a text box that tells them there is no templates yet.
                        $result = '<input type="text" name="' . $controlName . '" id="' . $controlName . '" style="' . $style . '" disabled value="NONE"\>';
                    }
                }
            }
        } catch (Exception $ex) {
            // Assign the output string as to what happened
            return $result;
        }

        // Return whatever we got
        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){
        // Attempt to log in with this user
        $userRow = $currentUser->fetch_assoc();
        
        // Tack on the template info
        $returnString = getRoutingTemplatesToCopyAsSelectOptions($db, $userRow,'float:left; display:none; width:30%','');

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