<?php
/**
 * Renders a form field — with enhanced styling and Lucide icon support.
 *
 * @var string $type
 * @var string $name
 * @var string $label_ar
 * @var mixed $value
 * @var bool $required
 * @var bool $disabled
 * @var string $error
 * @var array $options (for select/radio)
 * @var string $placeholder
 * @var string $help_text
 * @var string $icon (optional) Lucide icon name
 */
$type = $type ?? 'text';
$name = $name ?? '';
$label_ar = $label_ar ?? '';
$value = $value ?? old($name, $default ?? '');
$required = $required ?? false;
$disabled = $disabled ?? false;
$errors = App::getInstance()->session()->get('_errors', []);
$fieldErrors = $errors[$name] ?? [];
$error = $error ?? (is_array($fieldErrors) ? ($fieldErrors[0] ?? '') : $fieldErrors);
$options = $options ?? [];
$placeholder = $placeholder ?? '';
$help_text = $help_text ?? '';
$id = 'field_' . str_replace(['[', ']', '.'], '_', $name);
?>
<div class="form-group <?= $error ? 'has-error' : '' ?>">
    <?php if ($label_ar && $type !== 'hidden'): ?>
        <label for="<?= e($id) ?>" class="form-label">
            <?= e($label_ar) ?>
            <?php if ($required): ?><span class="required-mark">*</span><?php endif; ?>
        </label>
    <?php endif; ?>

    <?php if ($type === 'text' || $type === 'email' || $type === 'phone' || $type === 'number' || $type === 'password'): ?>
        <input type="<?= $type === 'phone' ? 'tel' : e($type) ?>"
               id="<?= e($id) ?>"
               name="<?= e($name) ?>"
               value="<?= e((string) $value) ?>"
               placeholder="<?= e($placeholder) ?>"
               class="form-input"
               <?= $required ? 'required' : '' ?>
               <?= $disabled ? 'disabled' : '' ?>
        >

    <?php elseif ($type === 'national_id'): ?>
        <input type="text"
               id="<?= e($id) ?>"
               name="<?= e($name) ?>"
               value="<?= e((string) $value) ?>"
               placeholder="أدخل الرقم القومي (14 رقم)"
               class="form-input nid-input"
               maxlength="14"
               pattern="\d{14}"
               data-nid-parser="true"
               <?= $required ? 'required' : '' ?>
               <?= $disabled ? 'disabled' : '' ?>
        >

    <?php elseif ($type === 'date'): ?>
        <input type="date"
               id="<?= e($id) ?>"
               name="<?= e($name) ?>"
               value="<?= e((string) $value) ?>"
               class="form-input"
               <?= $required ? 'required' : '' ?>
               <?= $disabled ? 'disabled' : '' ?>
        >

    <?php elseif ($type === 'select'): ?>
        <select id="<?= e($id) ?>" name="<?= e($name) ?>" class="form-select" <?= $required ? 'required' : '' ?> <?= $disabled ? 'disabled' : '' ?>>
            <option value="">-- اختر --</option>
            <?php foreach ($options as $optVal => $optLabel): ?>
                <option value="<?= e((string) $optVal) ?>" <?= (string) $value === (string) $optVal ? 'selected' : '' ?>><?= e($optLabel) ?></option>
            <?php endforeach; ?>
        </select>

    <?php elseif ($type === 'textarea'): ?>
        <textarea id="<?= e($id) ?>" name="<?= e($name) ?>" class="form-textarea" placeholder="<?= e($placeholder) ?>" <?= $required ? 'required' : '' ?> <?= $disabled ? 'disabled' : '' ?>><?= e((string) $value) ?></textarea>

    <?php elseif ($type === 'file'): ?>
        <input type="file" id="<?= e($id) ?>" name="<?= e($name) ?>" class="form-input" <?= $required ? 'required' : '' ?>>

    <?php elseif ($type === 'checkbox'): ?>
        <label class="checkbox-label">
            <input type="checkbox" name="<?= e($name) ?>" value="1" <?= $value ? 'checked' : '' ?> <?= $disabled ? 'disabled' : '' ?>>
            <span><?= e($label_ar) ?></span>
        </label>

    <?php elseif ($type === 'radio'): ?>
        <div class="radio-group">
            <?php foreach ($options as $optVal => $optLabel): ?>
                <label class="radio-label">
                    <input type="radio" name="<?= e($name) ?>" value="<?= e((string) $optVal) ?>" <?= (string) $value === (string) $optVal ? 'checked' : '' ?> <?= $disabled ? 'disabled' : '' ?>>
                    <span><?= e($optLabel) ?></span>
                </label>
            <?php endforeach; ?>
        </div>

    <?php elseif ($type === 'hidden'): ?>
        <input type="hidden" name="<?= e($name) ?>" value="<?= e((string) $value) ?>">
    <?php endif; ?>

    <?php if ($error): ?>
        <div class="form-error">
            <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>
            <?= e($error) ?>
        </div>
    <?php endif; ?>

    <?php if ($help_text): ?>
        <small class="form-help"><?= e($help_text) ?></small>
    <?php endif; ?>
</div>
