<?php
$file = '_get_instance_details.php';
$content = file_get_contents($file);

$start = "<?php foreach ($currentNode['ui_hints']['form_schema'] as $field): ";
$end = "<?php endforeach; ";

$pos_start = strpos($content, $start);
$pos_end = strpos($content, $end, $pos_start);

if ($pos_start !== false && $pos_end !== false) {
    $full_end = $pos_end + strlen($end);
    
    $new_block = '<?php foreach ($currentNode[\'ui_hints\'][\'form_schema\'] as $field): 
                            $fieldName = $field[\'name\'];
                            $currentValue = $instanceData[$fieldName] ?? null;
                        ?>
                            <?php if ($field[\'type\'] === \'checkbox\'): ?>
                                <div class="form-check mb-3">
                                    <input type="hidden" name=\"<?= $fieldName ?>\" value="0">
                                    <input type="checkbox" id=\"<?= $fieldName ?>\" name=\"<?= $fieldName ?>\" class="form-check-input" value="1" <?= (!empty($currentValue) || (!isset($currentValue) && !empty($field[\'default\']))) ? \'checked\' : \'\' ?>/>
                                    <label for=\"<?= $fieldName ?>\" class="form-check-label"><?= $field[\'label\'] ?></label>
                                </div>
                            <?php else: ?>
                                <div class="mb-3">
                                    <label for=\"<?= $fieldName ?>\" class="form-label"><?= $field[\'label\'] ?></label>
                                    <?php if ($field[\'type\'] === \'textarea\'): ?>
                                        <textarea id=\"<?= $fieldName ?>\" name=\"<?= $fieldName ?>\" class="form-control"><?= htmlspecialchars((string)($currentValue ?? \'\')) ?></textarea>
                                    <?php elseif ($field[\'type\'] === \'select\'): ?>
                                        <select id=\"<?= $fieldName ?>\" name=\"<?= $fieldName ?>\" class="form-select">
                                            <?php foreach ($field[\'options\'] as $option): 
                                                $selected = ($currentValue !== null && (string)$currentValue === (string)$option[\'value\']) ? \'selected\' : \'\';
                                            ?>
                                                <option value=\"<?= $option[\'value\'] ?>\" <?= $selected ?>><?= $option[\'label\'] ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    <?php else: ?>
                                        <?php 
                                            $defaultVal = ($field[\'default\'] ?? \'\') === \'now\' ? date(\'Y-m-d\\TH:i\') : ($field[\'default\'] ?? \'\');
                                            $valToUse = $currentValue ?? $defaultVal;
                                        ?>
                                        <input type=\"<?= $field[\'type\'] ?>\" id=\"<?= $fieldName ?>\" name=\"<?= $fieldName ?>\" class="form-control" value=\"<?= htmlspecialchars((string)$valToUse) ?>\">
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>
                        <?php endforeach; ?>';

    $content = substr_replace($content, $new_block, $pos_start, $full_end - $pos_start);
    file_put_contents($file, $content);
    echo "Patched successfully\n";
} else {
    echo "Not found!\n";
}

?>