<?php 
    // continue the session
    session_start();
    
    include '../php_only_scripts/database.php';
    
    // Init 
    $userRow=null;

    // Create and Setup the database
    $db = new Database();
    $db->setupTmsDB();

    // Attempt to log in with this user
    $currentUser = $db->getLoggedInSessionUser(session_id());

    // If there is only one result for a username/password combo, then they are good to go.
    if ($currentUser == null){
        exit;
    }
    elseif (mysqli_num_rows($currentUser)==1){
        // Attempt to get the user row
        $userRow = $currentUser->fetch_assoc();
    }

    // Determine if the appropriate values have been sent via post
    if (isset($_GET['clearAssignedToolMachineID']) && 
        isset($_GET['clearAssignedToolPosition']) && 
        isset($userRow['CanModify']) && $userRow['CanModify']==1){
        // Get the tool ID to work with
        $clearAssignedToolMachineID = $_GET['clearAssignedToolMachineID'];
        $clearAssignedToolPosition = $_GET['clearAssignedToolPosition'];

        // Get the query to update this assigned tool
        $sql="UPDATE assignedtools " . 
             "SET ExposedLength=0, CurrentToolID=-1, LoadedOn=null," . 
                 "Notes='', CommonTool=0, RemoveAfterUse=0, " . 
                 "SuggestedChangeNewToolID=-1, SuggestedExposedLength=0, SuggestedRemoveToolAfterUse=0, " . 
                 "SuggestedUseNewTool=0, SuggestedNote='' " . 
             "WHERE MachineID=" . $clearAssignedToolMachineID . " " .
             "AND Position=" . $clearAssignedToolPosition . ";";
             
        // Run the update query
        $db->queryDB($sql, $userRow, "clear_assignedtool Form occurance 1");
        
        // Go to the tools list
        echo ('<script>window.location = "../machineToolList.php?toolsMachineID=' . $clearAssignedToolMachineID . '";</script>');
    }
    else {
        // redirect to the invalid access page
        echo ('<script>window.location = "../invalid_access.php";</script>');

        exit;
    }
?>