<?php $title = htmlspecialchars($form->title); ?>
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold"><?= htmlspecialchars($form->title) ?></h1>
</div>
<div class="glass-card">
    <?php if ($form->description): ?>
    <p style="color: rgba(255,255,255,0.7);" class="mb-4"><?= htmlspecialchars($form->description) ?></p>
    <?php endif; ?>
    <form method="POST" action="<?= $baseUrl ?>/forms/<?= $form->id ?>/submit" enctype="multipart/form-data">
        <?php if (!empty($form->fields)): ?>
            <?php foreach ($form->fields as $field): ?>
            <div class="form-group">
                <label style="color: rgba(255,255,255,0.7);"><?= htmlspecialchars($field->label) ?><?= $field->is_required ? ' <span class="text-danger">*</span>' : '' ?></label>
                <?php if ($field->field_type === 'textarea'): ?>
                    <textarea name="<?= htmlspecialchars($field->field_name) ?>" class="form-control glass-input" rows="4" <?= $field->is_required ? 'required' : '' ?> placeholder="<?= htmlspecialchars($field->placeholder ?? '') ?>"></textarea>
                <?php elseif ($field->field_type === 'select' && $field->options): ?>
                    <select name="<?= htmlspecialchars($field->field_name) ?>" class="form-control glass-input" <?= $field->is_required ? 'required' : '' ?>>
                        <option value="">Select...</option>
                        <?php
                        $opts = (new \App\Models\DynamicFormField())->parseOptions($field->options);
                        foreach ($opts as $opt): ?>
                        <option value="<?= htmlspecialchars($opt) ?>"><?= htmlspecialchars($opt) ?></option>
                        <?php endforeach; ?>
                    </select>
                <?php elseif ($field->field_type === 'radio' && $field->options): ?>
                    <div>
                        <?php
                        $opts = (new \App\Models\DynamicFormField())->parseOptions($field->options);
                        foreach ($opts as $opt):
                            $id = 'field_' . $field->id . '_' . preg_replace('/[^a-z0-9]/i', '', $opt);
                        ?>
                        <div class="custom-control custom-radio custom-control-inline mb-2">
                            <input type="radio" class="custom-control-input" name="<?= htmlspecialchars($field->field_name) ?>" id="<?= $id ?>" value="<?= htmlspecialchars($opt) ?>" <?= $field->is_required ? 'required' : '' ?>>
                            <label class="custom-control-label" for="<?= $id ?>" style="color: rgba(255,255,255,0.7);"><?= htmlspecialchars($opt) ?></label>
                        </div>
                        <?php endforeach; ?>
                    </div>
                <?php elseif ($field->field_type === 'checkbox'): ?>
                    <div class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input" id="field_<?= $field->id ?>" name="<?= htmlspecialchars($field->field_name) ?>" value="1">
                        <label class="custom-control-label" for="field_<?= $field->id ?>" style="color: rgba(255,255,255,0.7);"><?= htmlspecialchars($field->label) ?></label>
                    </div>
                <?php elseif ($field->field_type === 'file'): ?>
                    <input type="file" name="<?= htmlspecialchars($field->field_name) ?>" class="form-control glass-input" <?= $field->is_required ? 'required' : '' ?>>
                <?php else: ?>
                    <input type="<?= $field->field_type === 'email' ? 'email' : ($field->field_type === 'number' ? 'number' : ($field->field_type === 'date' ? 'date' : ($field->field_type === 'url' ? 'url' : ($field->field_type === 'phone' ? 'tel' : 'text'))))) ?>" name="<?= htmlspecialchars($field->field_name) ?>" class="form-control glass-input" <?= $field->is_required ? 'required' : '' ?> placeholder="<?= htmlspecialchars($field->placeholder ?? '') ?>">
                <?php endif; ?>
            </div>
            <?php endforeach; ?>
        <?php endif; ?>
        <button type="submit" class="glass-btn glass-btn-primary btn-lg"><i class="fas fa-paper-plane mr-2"></i>Submit</button>
    </form>
</div>
