{extends file="layout.tpl"}

{block name="content"}
<div class="container-fluid">
    <!-- Header -->
    <div class="d-flex align-items-center mb-4">
        <a href="{$baseUrl}/settings/presets" class="btn btn-outline-secondary me-3">
            <i class="bi bi-arrow-left"></i>
        </a>
        <div>
            <h4 class="mb-1">{$pageTitle}</h4>
            <p class="text-muted mb-0">
                {if $preset}Edytuj zestaw ustawień{else}Utwórz nowy zestaw ustawień{/if}
            </p>
        </div>
    </div>

    <form method="post">
        <input type="hidden" name="csrf_token" value="{$csrfToken}">

        <div class="row">
            <!-- Podstawowe info -->
            <div class="col-lg-4">
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="mb-0">Informacje o presecie</h5>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <label class="form-label">Nazwa <span class="text-danger">*</span></label>
                            <input type="text" name="name" class="form-control" 
                                   value="{$preset.name|default:''}" required
                                   placeholder="np. Włącz zegar">
                        </div>

                        <div class="mb-3">
                            <label class="form-label">Opis</label>
                            <textarea name="description" class="form-control" rows="3"
                                      placeholder="Opcjonalny opis...">{$preset.description|default:''}</textarea>
                        </div>

                        <div class="form-check form-switch">
                            <input type="checkbox" name="is_active" class="form-check-input" id="isActive" value="1"
                                   {if $preset.is_active|default:1}checked{/if}>
                            <label class="form-check-label" for="isActive">Aktywny</label>
                        </div>
                    </div>
                </div>

                <div class="d-grid gap-2">
                    <button type="submit" class="btn btn-primary btn-lg">
                        <i class="bi bi-check-lg me-1"></i>
                        {if $preset}Zapisz zmiany{else}Utwórz preset{/if}
                    </button>
                    <a href="{$baseUrl}/settings/presets" class="btn btn-outline-secondary">Anuluj</a>
                </div>
            </div>

            <!-- Ustawienia do presetu -->
            <div class="col-lg-8">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">Wybierz ustawienia dla presetu</h5>
                    </div>
                    <div class="card-body">
                        {foreach $grouped as $groupName => $groupDefs}
                        <div class="mb-4">
                            <h6 class="text-muted border-bottom pb-2 mb-3">
                                <i class="bi bi-folder me-1"></i>
                                {$groupLabels[$groupName]|default:$groupName}
                            </h6>
                            
                            {foreach $groupDefs as $def}
                            <div class="row mb-3 align-items-center">
                                <div class="col-md-5">
                                    <div class="form-check">
                                        <input type="checkbox" class="form-check-input preset-toggle" 
                                               id="toggle_{$def.id}" data-def-id="{$def.id}"
                                               {if isset($presetValues[$def.id])}checked{/if}>
                                        <label class="form-check-label" for="toggle_{$def.id}">
                                            {$def.label}
                                        </label>
                                    </div>
                                    <small class="text-muted ms-4"><code>{$def.key}</code></small>
                                </div>
                                <div class="col-md-7">
                                    <div class="preset-value-container" id="container_{$def.id}" 
                                         style="{if !isset($presetValues[$def.id])}display:none{/if}">
                                        {if $def.type == 'boolean'}
                                        <select name="items[{$def.id}]" class="form-select form-select-sm">
                                            <option value="true" {if ($presetValues[$def.id]|default:$def.default_value) == 'true'}selected{/if}>true</option>
                                            <option value="false" {if ($presetValues[$def.id]|default:$def.default_value) == 'false'}selected{/if}>false</option>
                                        </select>
                                        {elseif $def.options}
                                        <select name="items[{$def.id}]" class="form-select form-select-sm">
                                            {foreach $def.options as $opt}
                                            <option value="{$opt.value}" {if ($presetValues[$def.id]|default:$def.default_value) == $opt.value}selected{/if}>
                                                {$opt.label}
                                            </option>
                                            {/foreach}
                                        </select>
                                        {elseif $def.type == 'string'}
                                        <input type="text" name="items[{$def.id}]" class="form-control form-control-sm"
                                               value="{$presetValues[$def.id]|default:$def.default_value}">
                                        {else}
                                        <input type="number" name="items[{$def.id}]" class="form-control form-control-sm"
                                               value="{$presetValues[$def.id]|default:$def.default_value}">
                                        {/if}
                                    </div>
                                </div>
                            </div>
                            {/foreach}
                        </div>
                        {/foreach}
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
{/block}

{block name="scripts"}
{literal}
<script>
document.querySelectorAll('.preset-toggle').forEach(function(checkbox) {
    checkbox.addEventListener('change', function() {
        var defId = this.dataset.defId;
        var container = document.getElementById('container_' + defId);
        var input = container.querySelector('input, select');
        
        if (this.checked) {
            container.style.display = 'block';
            if (input) input.disabled = false;
        } else {
            container.style.display = 'none';
            if (input) {
                input.disabled = true;
                // Wyczyść wartość żeby nie była wysyłana
                if (input.tagName === 'INPUT') {
                    input.value = '';
                }
            }
        }
    });
    
    // Inicjalizacja - ustaw disabled dla ukrytych
    var defId = checkbox.dataset.defId;
    var container = document.getElementById('container_' + defId);
    var input = container.querySelector('input, select');
    if (!checkbox.checked && input) {
        input.disabled = true;
    }
});
</script>
{/literal}
{/block}
