<?php
function buildForm($dataObj) 
{
    $vldFunction = $dataObj['function'];
    $vldTableType = $dataObj['type'];
    $vldFilter = $dataObj['filter'];
    $data = $dataObj['data'];
    $screenLayout = $dataObj['screenlayout'];
    $fieldLayout = $dataObj['fieldlayout'];
    $headerArray = $dataObj['header'];
    $message = $dataObj['message'];
    $numRows = $dataObj['numrows'];

	$html = 
	'<!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">';
    $html .= 
	'<title>Dynamic Form</title>
    <style>
    body { font-family: Arial, sans-serif; }
    #formContainer { margin: 20px; }
    .form-group {
        margin-bottom: 15px;
        display: flex; /* Use flexbox for alignment */
        align-items: center; /* Center items vertically */
    }
    .form-group label {
        flex: 1; /* Allow label to take up available space */
        margin-right: 10px; /* Space between label and input */
    }
    .form-group input {
        flex: 2; /* Allow input to take up more space */
        padding: 8px; /* Add padding for better appearance */
        border: 1px solid #ccc; /* Border styling */
        border-radius: 4px; /* Rounded corners for input */
    }
    .footer
    {
        width: 100%;
        
    }
    label { display: block; margin-bottom: 5px; }
    input { padding: 8px; width: 100%; box-sizing: border-box; }
	.modal {
        display: flex;
        position: fixed;
        z-index: 1;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgba(0, 0, 0, 0.4);
        padding-top: 60px;
    }
    .modal-content {
        background-color: #fefefe;
        margin: 5% auto;
        padding: 20px;
        border: 1px solid #888;
        width: 80%;
        border-radius: 5px;
    }
	.close {
        color: #aaa;
        float: right;
        font-size: 28px;
        font-weight: bold;
    }
    .close:hover,
    .close:focus {
        color: black;
        text-decoration: none;
        cursor: pointer;
    }
    </style>
    </head>';
	
	$html .=
    '<body>
        <div id="editModal" class="modal">
            <div class="modal-content">
                <span class="close" onClick="closeModal()">&times;</span>
                <h2>Modal</h2>';
				
    $html .= '<div id="formContainer">';

    if (count($data) > 0) {
        foreach ($data as $row) {
            foreach ($row as $key => $value) {
                $input = inputFormat($screenLayout[$key], $key, $value);
                $labelStyle = '';

                if ($screenLayout[$key] === "H") {
                    $labelStyle = 'style="display:none;"';
                    $input = 'style="display:none;"'; // Adjust this based on how inputFormat works
                }

                $html .= '<div class="form-group">';
                $html .= '<label for=\"$key\" $labelStyle>' . ucfirst($key) . ':</label>';
                $html .= $input;
                $html .= '</div>';
            }
        }
    } else {
        foreach ($headerArray as $header) {
            $input = inputFormat($screenLayout[$header], $header);
            $labelStyle = '';

            $hiddenFields = ['date_created', 'created_by', 'last_updated', 'last_updated_date', 'last_updated_by'];
            if (in_array($header, $hiddenFields) || $screenLayout[$header] === "H") {
                $labelStyle = 'style="display:none;"';
                $input = 'style="display:none;"';
            }

            $html .= '<div class="form-group">';
            $html .= '<label for=\"$header\" $labelStyle>' . ucfirst($header) . ':</label>';
            $html .= $input;
            $html .= '</div>';
        }
    }

    $html .= '</div>';
	
	$html .= '<div id="editNavPanel" class="editNavPanel"></div>';
    $html .= '</div>';
    $html .= '</div>';
    $html .= '</body>';
    $html .= '</html>';
	echo $html;
}

function inputFormat($layoutType, $key, $value = null)
{
    // This function should return the appropriate input HTML based on the layout type
    switch ($layoutType) 
	{
        case 'text':
            return "<input type=\"text\" id=\"$key\" name=\"$key\" value=\"$value\">";
        case 'email':
            return "<input type=\"email\" id=\"$key\" name=\"$key\" value=\"$value\">";
        case 'number':
            return "<input type=\"number\" id=\"$key\" name=\"$key\" value=\"$value\">";
		case 'date':
            return "<input type=\"date\" id=\"$key\" name=\"$key\" value=\"$value\">";
        case 'checkbox':
            $checked = $value ? 'checked' : '';
            return "<input type=\"checkbox\" id=\"$key\" name=\"$key\" $checked>";
        case 'radio':
            // Assuming value is an array of options
            $options = '';
            foreach ($value as $option) 
			{
                $isChecked = (isset($option['checked']) && $option['checked']) ? 'checked' : '';
                $options .= "<label><input type=\"radio\" id=\"$key\" name=\"$key\" value=\"{$option['value']}\" $isChecked> {$option['label']}</label><br>";
            }
            return $options;
        default:
            return "<input type=\"text\" id=\"$key\" name=\"$key\" value=\"" . htmlspecialchars($value) . "\">";
    }
}

// Example usage
// Example data for testing
$dataObj = [
    'data' => [['name' => 'John', 'age' => 30, 'gender' => ['value' => 'male'], 'DoB' => '', 'smoker' => true]],
    'screenlayout' => [
        'name' => 'text',
        'age' => 'number',
        'gender' => 'radio',
		'DoB' => 'date',
		'smoker' => 'checkbox',
    ],
    'header' => ['name', 'age', 'gender','DoB','smoker'],
];

// Linked options for gender
$genderOptions = [
    ['value' => 'male', 'label' => 'Male', 'checked' => true],
    ['value' => 'female', 'label' => 'Female', 'checked' => false],
];

$yesnoOptions = [
    ['value' => 'yes', 'label' => 'Yes', 'checked' => true],
    ['value' => 'no', 'label' => 'No', 'checked' => false],
];

// Update the data to include gender options
$dataObj['data'][0]['gender'] = $genderOptions;
$dataObj['data'][0]['smoker'] = $yesnoOptions; 

buildForm($dataObj);
?>