<?php
header('Content-Type: text/html; charset=utf-8');

// Arrays dekodieren 
$errorArray = json_decode($_POST['errorArray'] ?? '[]', true);
$warningArray = json_decode($_POST['warningArray'] ?? '[]', true);

// print_r($errorArray); exit; 

// Arrays zusammenfassen 
$dataArrays = [
	'Nicht gefüllte Pflichtfelder' 	=> $errorArray
	// 'Hinweise'			=> $warningArray
];

// Feldlabel-Funktion
function getFieldLabel($fieldId) {
	
    $labels = [
        'error' => 'Fehler'
// 	'warning' => 'Hinweis'
    ];

   	return $labels[$fieldId] ?? $fieldId;
}

?>

<!-- CSS-Styles einbetten -->
<style>

/* Modal-Body Breite anpassen */
.modal-body {
    width: 100% !important;
    max-width: none !important;
    min-width: 650px !important; 
    padding: 20px !important; 
    overflow-x: visible !important; 
    box-sizing: border-box !important;
}

/* Modal-Dialog entsprechend anpassen */
.modal-dialog {
    max-width: 800px !important;
    width: 90% !important;
    margin: 1.75rem auto !important;
}

/* Modal-Content sicherstellen */
.modal-content {
    width: 100% !important;
    min-width: 650px !important;
    box-sizing: border-box !important;
}

    .field-container {
    	display: grid;
    	grid-template-columns: 150px 550px;
    	gap: 15px;
    	align-items: center;
    	padding: 15px;
    	margin-bottom: 15px;
    	background-color: #f8f9fa;
    	border-radius: 5px;
    	border-left: 4px solid #14416b;
    	width: 100%; /* Volle Breite des Containers nutzen */
	/*max-width: 580px; */ /* Maximale Breite begrenzen */
    	box-sizing: border-box;
    } 

    .field-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;
    }
    
    .field-id {
        font-weight: bold;
        color: #495057;
        font-size: 14px;
    }
    
    .field-value {
	text-align: left; /* linksbündig */
	justify-self: start; /* bei Grid zusätzlich für horizontale Ausrichtung */
        background-color: white;
        padding: 10px;
        border-radius: 4px;
        border: 1px solid #dee2e6;
        font-size: 16px;
        min-height: 20px;
    }
    
    .status-selected {
        background-color: #d4edda;
        color: #155724;
        padding: 2px 8px;
        border-radius: 12px;
        font-size: 12px;
        font-weight: bold;
    }
    
    .status-unselected {
        background-color: #f8d7da;
        color: #721c24;
        padding: 2px 8px;
        border-radius: 12px;
        font-size: 12px;
        font-weight: bold;
    }
    
    .modal-buttons {
        text-align: center;
        margin-top: 30px;
        padding-top: 20px;
        border-top: 1px solid #dee2e6;
    }
    
    .btn-correct {
        background-color: #44910d; 
        color: white;
        padding: 8px 15px;
        border: none;
        border-radius: 3px;
        cursor: pointer;
        margin: 0;
	font-weight: bold;
        font-size: 14px;
    }
    
    .btn-publish {
        background-color: #44910d;
        color: white;
        padding: 8px 15px;
        border: none;
        border-radius: 3px;
        cursor: pointer;
	margin: 0;
	font-weight: bold; 
        font-size: 14px;
    }
.btn-publish,.btn-correct :hover{
    border-color:#014701
}


</style>

<!-- Formatierungslogik in PHP -->
<h4 style="text-align: center;">Fehler und Hinweise</h4>

<?php foreach ($dataArrays as $sectionName => $sectionData): ?>
    <?php if (!empty($sectionData)): ?>
        <h5 style="margin-top: 10px; margin-bottom: 10px; border-bottom: 2px solid #14416b; padding-bottom: 5px;">
            <?= htmlspecialchars($sectionName) ?>
        </h5>
        
        <?php foreach ($sectionData as $field): ?>
	<div class="field-container <?php echo ' ' .$field['errorclass'] ?>" >
                    <div class="field-header">
                    	<div class="field-id"><?= htmlspecialchars(getFieldLabel($field['errorclass'])) ?></div>
<!--
                    	<div class="<?//= $field['isSelected'] ? 'status-selected' : 'status-unselected' ?>">
                        	<?// = $field['isSelected'] ? 'Ausgewählt' : 'Nicht ausgewählt' ?>
                    	</div>
-->
                	</div>
                    <div class="field-value"><?= htmlspecialchars($field['message'] ?: '(Leer)') ?></div>
                </div>
        <?php endforeach; ?>
    <?php endif; ?>
<?php endforeach; ?>


<div class="button-container">
        <button class="btn-publish secondary" id="closeModalButton" onclick="closeModal()">Eingaben korrigieren</button>
	<?php if (count($errorArray) == 0) : ?>
		<button class="btn-publish" id="prepareSummary" onclick="closeModal(); showSummary()">Zusammenfassung anzeigen</button>
	<?php endif; ?>
</div>
