<?php
/**
 * SalonPro X - Form Group Component
 * Usage: <?php component('form', ['label' => 'Email', 'name' => 'email', 'type' => 'email', 'value' => old('email')]); ?>
 * @var string $label Field label
 * @var string $name Field name
 * @var string $type Input type (text, email, password, select, textarea, number, date, tel)
 * @var mixed $value Current value
 * @var string $placeholder Placeholder text
 * @var bool $required Whether field is required
 * @var array $options For select type: [value => label]
 * @var string $help Help text below the field
 * @var string $class Additional classes
 * @var string $icon Font Awesome icon prefix
 * @var bool $prepend Show icon/prefix inside input
 * @var mixed $extra Extra attributes string
 */
$label = $label ?? '';
$name = $name ?? '';
$type = $type ?? 'text';
$value = $value ?? old($name, '');
$placeholder = $placeholder ?? '';
$required = $required ?? false;
$options = $options ?? [];
$help = $help ?? '';
$class = $class ?? '';
$icon = $icon ?? '';
$prepend = $prepend ?? false;
$extra = $extra ?? '';
$hasError = function_exists('hasError') ? hasError($name) : false;
$errorMsg = function_exists('error') ? error($name) : '';
?>
<div class="form-group mb-3 <?= e($class) ?>">
    <?php if ($label): ?>
        <label for="<?= e($name) ?>" class="form-label">
            <?= e($label) ?>
            <?php if ($required): ?><span class="text-danger">*</span><?php endif; ?>
        </label>
    <?php endif; ?>

    <?php if ($type === 'select'): ?>
        <select name="<?= e($name) ?>" id="<?= e($name) ?>"
                class="form-select <?= $hasError ? 'is-invalid' : '' ?>"
                <?= $required ? 'required' : '' ?> <?= $extra ?>>
            <?php if ($placeholder): ?>
                <option value=""><?= e($placeholder) ?></option>
            <?php endif; ?>
            <?php foreach ($options as $optVal => $optLabel): ?>
                <option value="<?= e($optVal) ?>" <?= (string)$value === (string)$optVal ? 'selected' : '' ?>>
                    <?= e($optLabel) ?>
                </option>
            <?php endforeach; ?>
        </select>

    <?php elseif ($type === 'textarea'): ?>
        <textarea name="<?= e($name) ?>" id="<?= e($name) ?>"
                  class="form-control <?= $hasError ? 'is-invalid' : '' ?>"
                  placeholder="<?= e($placeholder) ?>"
                  <?= $required ? 'required' : '' ?> <?= $extra ?>
                  rows="3"><?= e($value) ?></textarea>

    <?php else: ?>
        <?php if ($prepend && $icon): ?>
            <div class="input-group">
                <span class="input-group-text"><i class="fas <?= e($icon) ?>"></i></span>
        <?php endif; ?>
        <input type="<?= e($type) ?>" name="<?= e($name) ?>" id="<?= e($name) ?>"
               class="form-control <?= $hasError ? 'is-invalid' : '' ?>"
               value="<?= e($value) ?>"
               placeholder="<?= e($placeholder) ?>"
               <?= $required ? 'required' : '' ?> <?= $extra ?>>
        <?php if ($prepend && $icon): ?>
            </div>
        <?php endif; ?>
    <?php endif; ?>

    <?php if ($hasError && $errorMsg): ?>
        <div class="invalid-feedback"><?= e($errorMsg) ?></div>
    <?php endif; ?>
    <?php if ($help && !$hasError): ?>
        <div class="form-text text-muted"><?= e($help) ?></div>
    <?php endif; ?>
</div>
