<?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['acceptAssignedToolMachineID']) && 
        isset($_GET['acceptAssignedToolPosition']) && 
        isset($userRow['CanModify']) && $userRow['CanModify']==1){
        // Setup a message for the user to let them know where to get the tool
        $userMessage = '';
                
        // Get the variables to work with
        $acceptAssignedToolMachineID = $_GET['acceptAssignedToolMachineID'];
        $acceptAssignedToolPosition = $_GET['acceptAssignedToolPosition'];
        $acceptAssignedToolMachineName = $_GET['acceptAssignedToolMachineName'];
        $acceptSuggestedChangeNewToolID = $_GET['acceptSuggestedChangeNewToolID'];

        // Determine if the suggested tool is setup
        if ($acceptSuggestedChangeNewToolID != '-1'){
            // Get the values for the new tool we want to change to
            $sql = "SELECT  t.* " .
                   "FROM tools t " .
                   "WHERE t.ToolID=" . $acceptSuggestedChangeNewToolID . ";";

            // Attempt to get the other new tool row
            $toolRow = $db->queryDB($sql, $userRow, "accept_suggestedtool Form occurance 1");

            // Attempt to get the other new tool assoc row
            $toolAssocRow = $toolRow->fetch_assoc();

            // Get the query to update this assigned tool
            $sql="UPDATE assignedtools asst " . 
                 "SET CurrentToolID=" . $acceptSuggestedChangeNewToolID . ", " . 
                     "asst.LoadedOn=NOW(), asst.Notes=asst.SuggestedNote, " . 
                     "asst.ExposedLength=asst.SuggestedExposedLength, " . 
                     "asst.RemoveAfterUse=asst.SuggestedRemoveToolAfterUse, " .
                     "asst.SuggestedChangeNewToolID=-1, asst.SuggestedRemoveToolAfterUse=0, " . 
                     "asst.SuggestedExposedLength=0, asst.SuggestedNote='', " .
                     "asst.SuggestedUseNewTool=0 " .
                 "WHERE asst.MachineID=" . $acceptAssignedToolMachineID . " " .
                 "AND asst.Position=" . $acceptAssignedToolPosition . ";";
            
            // Make the message that the user will see informing them to get the new tool
            $userMessage = "Please ensure you get a new tool that has the folowing characteristics:\n " . 
                           "\nBrand: " . str_replace("\"","",(str_replace("'","",$toolAssocRow["Brand"]==null?'':$toolAssocRow["Brand"]))) . 
                           "\nPart Number: " . str_replace("\"","",(str_replace("'","",$toolAssocRow["PartNumber"]==null?'':$toolAssocRow["PartNumber"]))) . 
                           "\nType: " . str_replace("\"","",(str_replace("'","",$toolAssocRow["Type"]==null?'':$toolAssocRow["Type"]))) . 
                           "\nDiameter: " . $toolAssocRow["Diameter"] . 
                           "\nFlute Number: " . $toolAssocRow["FluteNumber"] . 
                           "\nAngle: " . $toolAssocRow["Angle"] . 
                           "\nRadius: " . $toolAssocRow["Radius"] . 
                           "\nCutting Length: " . $toolAssocRow["CuttingLength"] . 
                           "\nShank Length: " . $toolAssocRow["ShankLength"] . 
                           "\nTotal Length: " . $toolAssocRow["TotalLength"] . 
                           "\nTaper Angle: " . $toolAssocRow["TaperAngle"] . 
                           "\nShank Diameter: " . $toolAssocRow["ShankDiameter"] . 
                           "\nShoulder Length: " . $toolAssocRow["ShoulderLength"] . 
                           "\n\n...gets placed in '" . $acceptAssignedToolMachineName . " at position " . 
                           $acceptAssignedToolPosition . ".";
            
            // Run the update query
            $db->queryDB($sql, $userRow, "accept_suggestedtool Form occurance 2");
        
            // Go to the tools list
            echo ('<script>window.location = "../machineToolList.php?toolsMachineID=' . $acceptAssignedToolMachineID . '";</script>');
        }
    }
    else {
        // redirect to the invalid access page
        echo ('<script>window.location = "../invalid_access.php";</script>');

        exit;
    }
?>