<?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 secondary status ID to delete has been sent via post
    if (isset($_GET['deleteSecondaryStatusID']) && $userRow["CanModify"] == 1){
        // Get the secondary status ID to work with
        $secondaryStatusedID = $_GET['deleteSecondaryStatusID'];
        
        // Create a query to delete the secondary status
        $sql = 'DELETE FROM secondarystatuses WHERE SecondaryStatusID=' . $secondaryStatusedID . ';';

        // Delete the secondary status
        $db->queryDB($sql, $userRow, "delete_secondary_status Form occurance 1");
        
        // Create a query to update the task table to set all occurances of this secondary status ID to -1
        $sql = 'UPDATE tasks SET SecondaryStatusID=-1 WHERE SecondaryStatusID = ' . $secondaryStatusedID . ';';
        
        // Update the secondary status ID
        $db->queryDB($sql, $userRow, "delete_secondary_status Form occurance 2");
    }

    // Go to the secondary status list
    echo ('<script>window.location = "../secondaryStatuses.php";</script>');
?>