<?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();
    }
    
    // Check for user template information
    $userTemplateOnly=0;
    if (isset($_GET['uto'])){
        // Get the user template information
        $userTemplateOnly = $_GET['uto'];
    }
    
    // Determine if the user ID to update has been sent via post
    if (isset($_GET['deleteUserID']) && $userRow["CanModify"] == 1){
        // Get the task ID to work with
        $userID = $_GET['deleteUserID'];
        
        // Get the query to delete this user
        $sql="DELETE FROM users where UserID=" . $userID . ";";

        // Run the delete query for the tasks table
        $db->queryDB($sql, $userRow, "delete_user Form occurance 1");
        
        // Delete from the addresses table all that are associated with this user
        $sql="DELETE FROM addresses where AddressID IN " .
                "(" .
                    "SELECT AddressID FROM addresslinker WHERE UserID=" . $userID . 
                ");";

        // Run the delete query for the tasks table
        $db->queryDB($sql, $userRow, "delete_user Form occurance 2");
            
        // Get the query to delete this user from the linker table
        $sql="DELETE FROM addresslinker where UserID=" . $userID . ";";

        // Run the delete query
        $db->queryDB($sql, $userRow, "delete_user Form occurance 3");
            
        // Get the query to delete this user from the linker table
        $sql="DELETE FROM usertostatusviewinglinker where UserID=" . $userID . ";";

        // Run the delete query
        $db->queryDB($sql, $userRow, "delete_user Form occurance 4");
            
        // Get the query to delete this user from the linker table
        $sql="DELETE FROM usertomachineviewinglinker where UserID=" . $userID . ";";

        // Run the delete query
        $db->queryDB($sql, $userRow, "delete_user Form occurance 5");
            
        // Get the query to delete this user from the linker table
        $sql="DELETE FROM usertostatuslinker where UserID=" . $userID . ";";

        // Run the delete query
        $db->queryDB($sql, $userRow, "delete_user Form occurance 6");
            
        // Get the query to delete this user from the labor clock
        $sql="DELETE FROM laborclock where UserID=" . $userID . ";";

        // Run the delete query
        $db->queryDB($sql, $userRow, "delete_user Form occurance 7");

        // Get the query to delete this user from the benefits table
        $sql="DELETE FROM employeebenefits where UserID=" . $userID . ";";

        // Run the delete query
        $db->queryDB($sql, $userRow, "delete_user Form occurance 8");
    }

    // Determine where to go back to
    if ($userTemplateOnly==1){
        // Go to the task that is in memory to update
        echo ('<script>window.location = "../userTemplates.php";</script>');
    }
    else {
        // Go to the task that is in memory to update
        echo ('<script>window.location = "../users.php";</script>');
    }
?>