<?php require __DIR__ . '/settings_nav.php'; ?>
<div class="mb-3">
    <h2 class="mb-1"><?= h(t('settings.species_heading')) ?></h2>
    <div class="text-muted"><?= h(t('settings.species_intro')) ?></div>
</div>
<?php if ($saved): ?><div class="alert alert-success">✅ <?= h(t('settings.species_saved')) ?></div><?php endif; ?>
<?php if ($error !== ''): ?><div class="alert alert-danger">⚠️ <?= h($error) ?></div><?php endif; ?>
<div class="card mb-3">
    <div class="card-status-top bg-primary"></div>
    <div class="card-header">
        <h3 class="card-title"><?= h(t('settings.add_species')) ?></h3>
    </div>
    <form method="post" action="/settings/species/save"><input type="hidden" name="csrf" value="<?= h(Auth::csrf()) ?>">
        <div class="card-body row g-2 align-items-end">
            <div class="col-md-3"><label class="form-label"><?= h(t('settings.name')) ?></label><input
                    class="form-control" name="name" required placeholder="<?= h(t('settings.species_example')) ?>">
            </div>
            <div class="col-md-2"><label class="form-label"><?= h(t('settings.code')) ?></label><input
                    class="form-control" name="code" required pattern="[a-z0-9][a-z0-9-]{1,49}"
                    placeholder="tortue-hermann"></div>
            <div class="col-md-2"><label class="form-label"><?= h(t('settings.category')) ?></label><select
                    class="form-select" name="category"><?php foreach ($categories as $key => $label): ?><option
                        value="<?= h($key) ?>"><?= h(SpeciesService::categoryLabel($key)) ?></option>
                    <?php endforeach; ?></select></div>
            <div class="col-md-1"><label class="form-label"><?= h(t('settings.icon')) ?></label><input
                    class="form-control" name="icon" value="🐾" maxlength="12"></div>
            <div class="col-md-1"><label class="form-label"><?= h(t('settings.color')) ?></label><input
                    class="form-control form-control-color w-100" type="color" name="color" value="#6c757d"></div>
            <div class="col-md-1"><label class="form-label"><?= h(t('settings.order')) ?></label><input
                    class="form-control" type="number" name="sort_order" value="100" min="0" max="999"></div>
            <div class="col-md-1"><label class="form-check mb-2"><input class="form-check-input" type="checkbox"
                        name="active" checked><span
                        class="form-check-label"><?= h(t('settings.active_f')) ?></span></label></div>
            <div class="col-md-1"><button class="btn btn-primary w-100"><?= h(t('common.add')) ?></button></div>
        </div>
    </form>
</div>
<div class="vstack gap-2"><?php foreach ($species as $item): ?>
    <form method="post" action="/settings/species/save" class="card card-sm"><input type="hidden" name="csrf"
            value="<?= h(Auth::csrf()) ?>"><input type="hidden" name="id" value="<?= (int)$item['id'] ?>">
        <div class="card-body row g-2 align-items-end">
            <div class="col-md-3"><label class="form-label"><?= h(t('settings.name')) ?></label><input
                    class="form-control" name="name" value="<?= h($item['name']) ?>" required></div>
            <div class="col-md-2"><label class="form-label"><?= h(t('settings.stable_code')) ?></label><input
                    class="form-control" name="code" value="<?= h($item['code']) ?>" required
                    pattern="[a-z0-9][a-z0-9-]{1,49}" <?= !empty($item['is_system']) ? 'readonly' : '' ?>></div>
            <div class="col-md-2"><label class="form-label"><?= h(t('settings.category')) ?></label><select
                    class="form-select" name="category"><?php foreach ($categories as $key => $label): ?><option
                        value="<?= h($key) ?>" <?= $item['category'] === $key ? 'selected' : '' ?>>
                        <?= h(SpeciesService::categoryLabel($key)) ?></option><?php endforeach; ?></select></div>
            <div class="col-md-1"><label class="form-label"><?= h(t('settings.icon')) ?></label><input
                    class="form-control" name="icon" value="<?= h($item['icon']) ?>" maxlength="12"></div>
            <div class="col-md-1"><label class="form-label"><?= h(t('settings.color')) ?></label><input
                    class="form-control form-control-color w-100" type="color" name="color"
                    value="<?= h($item['color']) ?>"></div>
            <div class="col-md-1"><label class="form-label"><?= h(t('settings.order')) ?></label><input
                    class="form-control" type="number" name="sort_order" value="<?= (int)$item['sort_order'] ?>" min="0"
                    max="999"></div>
            <div class="col-md-1"><label class="form-check mb-2"><input class="form-check-input" type="checkbox"
                        name="active" <?= (int)$item['active'] === 1 ? 'checked' : '' ?>><span
                        class="form-check-label"><?= h(t('settings.active_f')) ?></span></label>
                <div class="small text-muted">
                    <?= h(t('settings.animal_count', ['count' => (int)$item['animal_count']])) ?></div>
            </div>
            <div class="col-md-1"><button
                    class="btn btn-outline-primary w-100"><?= h(t('common.save_short')) ?></button></div>
        </div>
    </form><?php endforeach; ?>
</div>
