<?php
use App\Services\CustomFieldService;

$cfFields = $cfFields ?? [];
$cfValues = $cfValues ?? [];
$cfPrefix = $cfPrefix ?? 'cf';
?>

<?php if (!empty($cfFields)): ?>
    <hr class="my-4">
    <h3 class="h5 fw-bold mb-3"><?= e(trans('custom_fields')) ?></h3>

    <div class="row g-3">
        <?php foreach ($cfFields as $field): ?>
            <?php
                $key = (string) $field['field_key'];
                $type = (string) $field['field_type'];
                $label = current_lang() === 'ar' ? (string) $field['label_ar'] : (string) $field['label_en'];
                $placeholder = current_lang() === 'ar' ? (string) ($field['placeholder_ar'] ?? '') : (string) ($field['placeholder_en'] ?? '');
                $help = current_lang() === 'ar' ? (string) ($field['help_text_ar'] ?? '') : (string) ($field['help_text_en'] ?? '');
                $value = $cfValues[$key] ?? '';
                $options = CustomFieldService::optionsArray((string) ($field['field_options'] ?? ''));
                $inputName = $cfPrefix . '[' . $key . ']';
            ?>
            <div class="col-md-12">
                <label class="form-label"><?= e($label) ?><?= !empty($field['is_required']) ? ' *' : '' ?></label>

                <?php if (in_array($type, ['text','email','number','date'], true)): ?>
                    <input
                        type="<?= e($type) ?>"
                        name="<?= e($inputName) ?>"
                        class="form-control rounded-3"
                        value="<?= e((string) $value) ?>"
                        placeholder="<?= e($placeholder) ?>"
                        <?= !empty($field['is_required']) ? 'required' : '' ?>
                    >
                <?php elseif ($type === 'textarea'): ?>
                    <textarea
                        name="<?= e($inputName) ?>"
                        class="form-control rounded-3"
                        rows="4"
                        placeholder="<?= e($placeholder) ?>"
                        <?= !empty($field['is_required']) ? 'required' : '' ?>
                    ><?= e((string) $value) ?></textarea>
                <?php elseif ($type === 'select'): ?>
                    <select
                        name="<?= e($inputName) ?>"
                        class="form-select rounded-3"
                        <?= !empty($field['is_required']) ? 'required' : '' ?>
                    >
                        <option value="">--</option>
                        <?php foreach ($options as $opt): ?>
                            <option value="<?= e($opt) ?>" <?= (string) $value === $opt ? 'selected' : '' ?>><?= e($opt) ?></option>
                        <?php endforeach; ?>
                    </select>
                <?php elseif ($type === 'radio'): ?>
                    <div class="d-flex gap-3 flex-wrap">
                        <?php foreach ($options as $idx => $opt): ?>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="<?= e($inputName) ?>" id="<?= e($key . '_' . $idx) ?>" value="<?= e($opt) ?>" <?= (string) $value === $opt ? 'checked' : '' ?>>
                                <label class="form-check-label" for="<?= e($key . '_' . $idx) ?>"><?= e($opt) ?></label>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php elseif ($type === 'checkbox'): ?>
                    <?php
                        $selectedValues = array_filter(array_map('trim', explode(',', (string) $value)));
                    ?>
                    <div class="d-flex gap-3 flex-wrap">
                        <?php foreach ($options as $idx => $opt): ?>
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox" name="<?= e($inputName) ?>[]" id="<?= e($key . '_' . $idx) ?>" value="<?= e($opt) ?>" <?= in_array($opt, $selectedValues, true) ? 'checked' : '' ?>>
                                <label class="form-check-label" for="<?= e($key . '_' . $idx) ?>"><?= e($opt) ?></label>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>

                <?php if ($help !== ''): ?>
                    <div class="form-text"><?= e($help) ?></div>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>
