<?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();

    try {
        // 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 labor category ID to delete has been sent via post
        if ($userRow["CanModify"] == 1 && isset($_GET['deleteLaborCategoryID'])){
            // Get the labor category ID to work with
            $laborCategoryID = $_GET['deleteLaborCategoryID'];

            // Get the labor category we will update
            $sql = 'SELECT * FROM laborcategories WHERE LaborCategoryID=' . $laborCategoryID . ';';

            // Get the labor category row that is being deleted
            $laborCategoryRow = $db->queryDB($sql, $userRow, "delete_labor_category Form occurance 1");

            // Check to ensure there is a row to delete
            if (mysqli_num_rows($laborCategoryRow)==1){
                // Get the Row returned
                $laborCategoryAssocRow = $laborCategoryRow->fetch_assoc();

                // determine if this is a clocked in or out status
                if ($laborCategoryAssocRow["IsOnTheClock"]==1){
                    // Update the users table to change anyone with this labor category ID as standard to be general labor (1)
                    $sql="UPDATE users SET StandardLaborCategoryID=1 where StandardLaborCategoryID=" . $laborCategoryID . " AND UserTemplateOnly=0;";

                    // Run the update query for the users table
                    $db->queryDB($sql, $userRow, "delete_labor_category Form occurance 2");

                    // Update the labor clock table to change anyone with this labor category ID as current to be general labor (1) 
                    $sql="UPDATE laborclock " . 
                         "SET CurrentLaborCategoryID=1, " . 
                         "    CurrentLaborCategoryStartTime=CURRENT_TIMESTAMP " . 
                         "WHERE CurrentLaborCategoryID=" . $laborCategoryID . ";";


                    // Run the update query for the labor clock table
                    $db->queryDB($sql, $userRow, "delete_labor_category Form occurance 3");
                }
                else {
                    // Update the users table to change anyone with this labor category ID as standard to be Clocked Out (2)
                    $sql="UPDATE users SET StandardLaborCategoryID=2 where StandardLaborCategoryID=" . $laborCategoryID . " AND UserTemplateOnly=0;";

                    // Run the update query for the users table
                    $db->queryDB($sql, $userRow, "delete_labor_category Form occurance 4");

                    // Update the labor clock table to change anyone with this labor category ID as current to be clocked out (2) 
                    $sql="UPDATE laborclock " . 
                         "SET CurrentLaborCategoryID=2, " . 
                         "    CurrentLaborCategoryStartTime=CURRENT_TIMESTAMP " . 
                         "WHERE CurrentLaborCategoryID=" . $laborCategoryID . ";";

                    // Run the update query for the labor clock table
                    $db->queryDB($sql, $userRow, "delete_labor_category Form occurance 5");
                }

                // Get the query to delete this labor category
                $sql="DELETE FROM laborcategories where LaborCategoryID=" . $laborCategoryID . ";";

                // Run the delete query
                $db->queryDB($sql, $userRow, "delete_labor_category Form occurance 6");
            }
        }
    } catch (Exception $exc) {
        // Let the know there was an error
        echo '<script>alert("There was an error in deleting the labor category.\n\nPlease inform database admin.");</script>';
    }

    // Go to the labor categories list
   echo ('<script>window.location = "../laborCategories.php";</script>');
?>