<?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 stocked material ID to delete has been sent via post
    if (isset($_GET['deleteStockedMaterialID']) && $userRow["CanModify"] == 1){
        // Get the stocked material ID to work with
        $stockedMaterialID = $_GET['deleteStockedMaterialID'];
        
        // Create the delete query
        $sql = 'DELETE FROM materialstock WHERE MaterialStockID=' . $stockedMaterialID . ';';

        // Try to delete the stocked material
        $db->queryDB($sql, $userRow, "delete_stocked_material Form occurance 1");

        // Update the blanks using this stock to unknown
        $sql="UPDATE jobblanks SET MaterialStockID=-1 where MaterialStockID=" . $stockedMaterialID . ";";

        // Run the update query for the job blanks table
        $db->queryDB($sql, $userRow, "delete_stocked_material Form occurance 2");
    }

    // Go to the task that is in memory to update
    echo ('<script>window.location = "../stockedMaterials.php";</script>');
?>