<?php

    use lim\_shared\models\question;

    $fd = $_view["formData"];
    $question = $_view["question"];
    $optional = $question->weight == 0;
    $hasParent = ($question->parentId ?? 0) > 0;
    $value = $fd["question"][$question->id] ?? null;
    $requiredMap = ["int" => "requiredInt", "percentage" => "requiredPercentageNumber"];

?>
<div 
    class="<?= $hasParent ? ' hasParent ' . ($value ? "" : "d-none") : '';?><?= $optional ? " optional" : ""; ?>" 
    <?= $hasParent ? ' data-parentId="' . $question->parentId . '" data-parentValue="' . $question->parentValue . '"' : ''; ?>
    data-step="<?= $question->section; ?>" data-answered="<?= $value ? "true" : "false"; ?>"
>
    <label for="question_<?= $question->id;?>" class="form-label"><?= $question->title;?></label>
    <p class="mb-2"><?= $question->description;?></p>
    <div class="input-group">
        <?php if (in_array($question->type, ["string", "int", "percentage"])) { ?>
            <input type="text" class="form-control required<?= $optional ? " optional" : "";?> requiredIfSet requiredIfInvisible <?=  $requiredMap[$question->type] ?? ""; ?>"
                name="question[<?= $question->id;?>]" id="question_<?= $question->id;?>" 
                    placeholder="<?= $question->placeholder; ?>" value="<?= sanitizeFormInput($value ?? ""); ?>"
                <?= $optional ? " disabled" : "" ?>>
            <?php if ($question->type == "percentage") {  ?>
                <span class="input-group-text">%</span>
            <?php } ?>
        <?php } else { ?>
            <select class="form-select required<?= $optional ? " optional" : "";?> requiredIfSet requiredIfInvisible" 
                name="question[<?= $question->id;?>]" id="question_<?= $question->id;?>"<?= $optional ? " disabled" : "" ?>>
                <?php arrayToOptionsSet($question->options ?? question::BOOL_POOL, $value ?? "", ["_choose" => "- bitte wählen -"], forceKeys: true); ?>
            </select>
        <?php } ?>
    </div>
    <div class="form-text form-error d-none" id="error_<?= $question->id; ?>">
        <?= question::ERROR_MSG_MAP[$question->type]; ?>
    </div>
</div>