<?php

/**
 * casedata.php
 */

use phpDocumentor\Reflection\Types\Null_;

$application = new Application;

/**
 * Formats a PHP array string by wrapping the keys in HTML spans.
 *
 * @param string $arrayString The PHP array string to format.
 * @param string $spanClass The CSS class to use for the span. Default is 'fieldname'.
 * @return string The formatted HTML string.
 */
function formatPhpArrayString(string $arrayString, string $spanClass = 'fieldname'): string {
    // Use regular expression to find array keys.
    $pattern = '/\[(\w+)\]/';
    // Replacement function to wrap the key in a span.
    $replacement = "[<span class='" . $spanClass . "'>$1</span>]";

    // Perform the replacement.
    $formattedString = preg_replace($pattern, $replacement, $arrayString);
    return $formattedString;
}

function displayValue($value) {
    if (is_null($value)) {
        return "<div class='null-text'>NULL</div>";
    } elseif ($value === '') {
        return "<div class='empty-string'>empty string</div>";
    } else {
        return htmlspecialchars($value);
    }
}

// Check if the APP_UID is provided in the request
if (isset($_REQUEST['uid'])) {
    $appUid = $_REQUEST['uid'];
    try {
        $app = $application->Load($appUid);
    } catch (\Throwable $th) {
        $app = null;        //throw $th;
    }

    if ($app) {
        // Convert the object to an array for easier handling in the HTML.
        $data = $app;

        $appDelegation = new AppDelegation();
        $appDelegations = $appDelegation->loadDelegationRowsByAppUid($data['APP_UID'], -1);

        $appThread = new AppThread();
        $appThreads = $appThread->loadThreadRowsByAppUid($data['APP_UID'], -1);
        //print_r($appThreads);

        try {
            $app_data = unserialize($data['APP_DATA']);
            if ($data['APP_DATA'] !== serialize(false) && $app_data === false){
                $app_data = "<div class='error-message'>Error: string not serialized</div><br>" . $data['APP_DATA'];
            }

        } catch (\Throwable $th) {
            $app_data = "<div class='error-message'>Error: string not serialized</div><br>" . $data['APP_DATA'];
        }

        // Now, $data contains the application data.
    } else {
        // Handle the case where the application is not found.
        echo "Application not found."; //  Simple error message for demo.  You might redirect or show a nicer error page.
        exit;
    }
} else {
    // Handle the case where APP_UID is not provided.
    echo "APP_UID is missing from the request."; // Simple error for demo.
    exit;
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Application Data</title>
    <style>
        /* Modern and Elegant Form Styles (Div Layout) */
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
            text-align: center;
            font-size: 14px; /* Set font size for the body */
        }

        h2 {
            color: #333;
        }

        .error-message {
            color: #12a;
            word-wrap: break-word;
            background-color: lightcoral;
            padding: 10px 18px;
            border-radius: 5px;
            font-size: 1.1em;
            text-align: left; /* Align value to the left */
            width: 100%;
            max-width: 400px;
            min-height: 26px;
            box-sizing: border-box;
        }

        .data-container {
            width: 100%;
            max-width: 1280px;
            margin: 10px auto;
            background-color: #fff;
            padding: 5px 20px;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            border: 1px solid #eee;
        }

        .data-group {
            margin-bottom: 15px;
            border-bottom: 2px solid #e0e0e0;
            padding-bottom: 10px;
        }

        .data-group h3 {
            color: #3498db;
            margin-bottom: 5px;
            font-size: 1.4em;
            font-weight: 500;
            border-bottom: 1px solid #f0f0f0;
            padding-bottom: 10px;
        }

        .data-row-group {
            margin-left: -10px;
            margin-right: -10px;
            margin-top: -2px;
            padding: 8px 10px 0px 10px;
            border: 2px solid rgba(123, 179, 231, 0.56);
        }

        .data-row {
            display: flex;
            flex-direction: column; /* Default to single column */
            margin-bottom: 10px;
            width: 100%;
            display: flex;
            flex-wrap: wrap;
        }

        .data-item { /* New container for label and value */
            display: flex;
            flex-direction: column;
            width: 100%;
            box-sizing: border-box;
            margin-right: 0;
            margin-bottom: 10px;
        }

        .data-item .label {
            font-weight: 500;
            color: #555;
            margin-bottom: 1px;
            font-size: 0.85em;
            width: 100%;
            display: block;
            text-align: left; /* Align label to the left */
        }

        .data-item .value {
            color: #222;
            word-wrap: break-word;
            background-color: #f0f0f0;
            padding: 5px 8px;
            border-radius: 5px;
            font-size: 0.9em;
            text-align: left; /* Align value to the left */
            width: 100%;
            min-height: 22px;
            box-sizing: border-box;
        }

        .long-text-value {
            background-color: #f5f5f5;
            padding: 15px;
            border-radius: 8px;
            margin-top: 15px;
            white-space: pre-wrap;
            border: 1px solid #e0e0e0;
            width: 100%;
            box-sizing: border-box;
            grid-column: 1 / -1;
        }

        .fieldname {
            font-weight: bold;
            font-size: 1.1em;
            color: #2488cb;
            padding: 0% 3px;
        }

        .two-column {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            width: 100%;
            margin-bottom: 15px;
        }

        .two-column .data-item{
            width: 49%;
            margin-right: 0;
        }

        .four-column {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            margin-bottom: 15px;
            width: 100%;
            flex-wrap: wrap;
        }

        .four-column .data-item {
            width: 24%;
            margin-right: 0;
            margin-bottom: 15px; /* Add margin to create space between items */
        }

        .four-column .span-two{
            width: 49%;
        }

        .six-column {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            margin-bottom: 15px;
            width: 100%;
            flex-wrap: wrap;
        }

        .six-column .data-item {
            width: 15%;
            margin-right: 0;
            margin-bottom: 15px; /* Add margin to create space between items */
        }
        .six-column .span-three{
            width: 49%;
        }
        .six-column .span-two-three{
            width: 21%;
        }
        .empty-string {
            background-color: #f8f8f8;
            color: #686888; /* Light gray color to indicate an empty string */
            padding: 1px 10px; /* Same padding as value for consistency */
            font-size: 0.9em; /* Same font size as value */
            text-align: left; /* Align text to the left */
            min-height: 10px; /* Minimum height for consistency */
        }

        .null-text {
            background-color: #f8f8f8;
            color: #ff8080; /* Red color to indicate a null value */
            padding: 1px 10px; /* Same padding as value for consistency */
            font-size: 0.9em; /* Same font size as value */
            text-align: left; /* Align text to the left */
        }

        .one-column {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            margin-bottom: 15px;
            width: 100%;
            flex-wrap: wrap;
        }

        .one-column .data-item {
            width: 100%;
            margin-right: 0;
        }

        @media (max-width: 768px) {
            .data-row {
                flex-direction: column; /* Stack items vertically on smaller screens */
            }
            .data-item {
                width: 100%; /* Full width for each item */
                margin-bottom: 10px; /* Space between items */
            }
        }
    </style>
</head>
<body>
    <div class="data-container">
        <div class="data-group">
            <h3>Application Details</h3>
            <div class="data-row four-column">
                <div class="data-item">
                    <div class="label">APP_UID</div>
                    <div class="value"><?php echo displayValue($data['APP_UID']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">APP_NUMBER</div>
                    <div class="value"><?php echo displayValue($data['APP_NUMBER']); ?></div>
                </div>
                <div class="data-item span-two">
                    <div class="label">APP_TITLE</div>
                    <div class="value"><?php echo displayValue($data['APP_TITLE']); ?></div>
                </div>
            </div>

            <div class="data-row four-column">
                <div class="data-item">
                    <div class="label">APP_INIT_USER</div>
                    <div class="value"><?php echo displayValue($data['APP_INIT_USER']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">APP_CUR_USER</div>
                    <div class="value"><?php echo displayValue($data['APP_CUR_USER']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">PRO_UID</div>
                    <div class="value"><?php echo displayValue($data['PRO_UID']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">APP_PIN</div>
                    <div class="value"><?php echo displayValue($data['APP_PIN']); ?></div>
                </div>
            </div>

            <div class="data-row six-column">
                <div class="data-item">
                    <div class="label">APP_STATUS</div>
                    <div class="value"><?php echo displayValue($data['APP_STATUS']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">APP_STATUS_ID</div>
                    <div class="value"><?php echo displayValue($data['APP_STATUS_ID']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">APP_PROC_STATUS</div>
                    <div class="value"><?php echo displayValue($data['APP_PROC_STATUS']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">APP_PROC_CODE</div>
                    <div class="value"><?php echo displayValue($data['APP_PROC_CODE']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">APP_INIT_USER_ID</div>
                    <div class="value"><?php echo displayValue($data['APP_INIT_USER_ID']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">PRO_ID</div>
                    <div class="value"><?php echo displayValue($data['PRO_ID']); ?></div>
                </div>
            </div>

            <div class="data-row six-column">
                <div class="data-item">
                     <div class="label">APP_DURATION</div>
                    <div class="value"><?php echo displayValue($data['APP_DURATION']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">APP_DELAY_DURATION</div>
                    <div class="value"><?php echo displayValue($data['APP_DELAY_DURATION']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">APP_DRIVE_FOLDER_UID</div>
                    <div class="value"><?php echo displayValue($data['APP_DRIVE_FOLDER_UID']); ?></div>
                </div>
                <div class="data-item span-three">
                    <div class="label">APP_DESCRIPTION</div>
                    <div class="value"><?php echo displayValue($data['APP_DESCRIPTION']); ?></div>
                </div>
            </div>

            <div class="data-row six-column">
                <div class="data-item">
                    <div class="label">APP_CREATE_DATE</div>
                    <div class="value"><?php echo displayValue($data['APP_CREATE_DATE']); ?></div>
                </div>
                <div class="data-item">
                     <div class="label">APP_INIT_DATE</div>
                    <div class="value"><?php echo displayValue($data['APP_INIT_DATE']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">APP_FINISH_DATE</div>
                    <div class="value"><?php echo displayValue($data['APP_FINISH_DATE']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">APP_UPDATE_DATE</div>
                    <div class="value"><?php echo displayValue($data['APP_UPDATE_DATE']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">APP_PARENT</div>
                    <div class="value"><?php echo displayValue($data['APP_PARENT']); ?></div>
                </div>
                <div class="data-item">
                    <div class="label">APP_ROUTING_DATA</div>
                    <div class="value"><?php echo displayValue($data['APP_ROUTING_DATA']); ?></div>
                </div>
            </div>

        </div>

        <div class="data-group">
            <h3>App Delegations</h3>
            <?php foreach ($appDelegations as $delegation) {?>
            <div class="data-row-group">
                <div class="data-row six-column">
                    <div class="data-item">
                        <div class="label">DEL_INDEX</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_INDEX']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DELEGATION_ID</div>
                        <div class="value"><?php echo displayValue($delegation['DELEGATION_ID']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">APP_NUMBER</div>
                        <div class="value"><?php echo displayValue($delegation['APP_NUMBER']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_PREVIOUS</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_PREVIOUS']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_LAST_INDEX</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_LAST_INDEX']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_TYPE</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_TYPE']); ?></div>
                    </div>
                </div>

                <div class="data-row six-column">
                    <div class="data-item">
                        <div class="label">DEL_THREAD</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_THREAD']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_THREAD_STATUS</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_THREAD_STATUS']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_PRIORITY</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_PRIORITY']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_DELEGATE_DATE</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_DELEGATE_DATE']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_INIT_DATE</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_INIT_DATE']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_FINISH_DATE</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_FINISH_DATE']); ?></div>
                    </div>
                </div>
                <div class="data-row six-column">
                    <div class="data-item">
                        <div class="label">DEL_TASK_DUE_DATE</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_TASK_DUE_DATE']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_RISK_DATE</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_RISK_DATE']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_DURATION</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_DURATION']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_QUEUE_DURATION</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_QUEUE_DURATION']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_DELAY_DURATION</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_DELAY_DURATION']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_STARTED</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_STARTED']); ?></div>
                    </div>
                </div>
                <div class="data-row six-column">
                    <div class="data-item">
                        <div class="label">DEL_FINISHED</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_FINISHED']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_DELAYED</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_DELAYED']); ?></div>
                    </div>
                    <div class="data-item span-two-three">
                        <div class="label">PRO_UID</div>
                        <div class="value"><?php echo displayValue($delegation['PRO_UID']); ?></div>
                    </div>
                    <div class="data-item span-two-three">
                        <div class="label">TAS_UID</div>
                        <div class="value"><?php echo displayValue($delegation['TAS_UID']); ?></div>
                    </div>
                    <div class="data-item span-two-three">
                        <div class="label">USR_UID</div>
                        <div class="value"><?php echo displayValue($delegation['USR_UID']); ?></div>
                    </div>
                </div>
                <div class="data-row six-column">
                    <div class="data-item">
                        <div class="label">APP_OVERDUE_PERCENTAGE</div>
                        <div class="value"><?php echo displayValue($delegation['APP_OVERDUE_PERCENTAGE']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">USR_ID</div>
                        <div class="value"><?php echo displayValue($delegation['USR_ID']); ?></div>
                    </div>
                    <div class="data-item ">
                        <div class="label">PRO_ID</div>
                        <div class="value"><?php echo displayValue($delegation['PR_UID']); ?></div>
                    </div>
                    <div class="data-item ">
                        <div class="label">TAS_ID</div>
                        <div class="value"><?php echo displayValue($delegation['TAS_ID']); ?></div>
                    </div>
                    <div class="data-item span">
                        <div class="label">DEL_TITLE</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_TITLE']); ?></div>
                    </div>
                    <div class="data-item span">
                        <div class="label">DEL_THREAD_STATUS_ID</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_THREAD_STATUS_ID']); ?></div>
                    </div>
                </div>
                <div class="data-row one-column">
                    <div class="data-item">
                        <div class="label">DEL_DATA</div>
                        <div class="value"><?php echo displayValue($delegation['DEL_DATA']); ?></div>
                    </div>
                </div>

            </div>
            <?php } ?>
        </div>

        <div class="data-group">
            <h3>App Threads</h3>
            <?php foreach ($appThreads as $thread) {?>
            <div class="data-row-group">
                <div class="data-row six-column">
                    <div class="data-item">
                        <div class="label">APP_THREAD_INDEX</div>
                        <div class="value"><?php echo displayValue($thread['APP_THREAD_INDEX']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">APP_THREAD_PARENT</div>
                        <div class="value"><?php echo displayValue($thread['APP_THREAD_PARENT']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">APP_THREAD_STATUS</div>
                        <div class="value"><?php echo displayValue($thread['APP_THREAD_STATUS']); ?></div>
                    </div>
                    <div class="data-item">
                        <div class="label">DEL_INDEX</div>
                        <div class="value"><?php echo displayValue($thread['DEL_INDEX']); ?></div>
                    </div>
                    <div class="data-item">
                    </div>
                    <div class="data-item">
                    </div>
                </div>
            </div>
            <?php } ?>
        </div>

        <div class="data-group">
            <h3>Application Data</h3>
            <div><?php
                print strlen($data['APP_DATA']);
            ?> bytes</div>
            <div class="data-row">
                <div class="data-item">
                    <div class="value long-text-value"><?php
                        $msg = print_r( $app_data,1);
                        $msg = htmlspecialchars($msg);
                        $formatted = formatPhpArrayString($msg);
                        print $formatted;
                    ?></div>
                </div>
            </div>
            <div class="data-row">
                <div class="data-item">
                    <div class="value long-text-value"><?php
                    print htmlspecialchars($data['APP_DATA']);
                    ?></div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>