<?php 
    include_once dirname(__FILE__) . '/../database.php';
    include_once dirname(__FILE__) . '/../global_functions.php';
    
    // attempt to delete the full route
    function deleteFullStatusRoute($db, $userRow, $idToDelete) {
        try {
            // Determine if we have a route to delete
            if ($idToDelete!=null && $idToDelete!=""){
                // Create a query to delete the selected route fully
                $sql='DELETE FROM taskstatusinfo_initial ' . 
                     'WHERE TaskID = ' . $idToDelete . ';';

                // Run the delete query
                $db->queryDB($sql, $userRow, "delete_status_route Form occurance 1");
            }
        } 
        catch (Exception $exc) {
            // Create the error string to return back to the caller
            return 'ABORT DELETE STATUS ROUTE (delete_status_route)::There was an uncaught error while trying to delete status route.  The exception is: ' . $exc->getMessage() . '. Deletion aborted...';
        }
        
        // 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 DELETE STATUS ROUTE (delete_status_route)::Your session has expired.  ' . 
             'Refresh the page before deleting route.';
    }
    else {
        // Attempt to log in with this user
        $userRow = $currentUser->fetch_assoc();
        
        // Set defaults for the passed in variables
        $updateTaskNumber=-1;
        $idToDelete='';
        // check the passed in ID to delete
        if (isset($_GET['idToDelete']) ){
            // get the passed in ID to delete
            $idToDelete = $_GET['idToDelete'];
        }
        
        // Attempt to delete the status route they requested
        $returnString=deleteFullStatusRoute($db, $userRow, $idToDelete);

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