<?php
/* Smarty version 4.5.6, created on 2026-01-24 18:34:59
  from '/var/www/html/tivi/panel/templates/settings/definition_form.tpl' */

/* @var Smarty_Internal_Template $_smarty_tpl */
if ($_smarty_tpl->_decodeProperties($_smarty_tpl, array (
  'version' => '4.5.6',
  'unifunc' => 'content_697502c3efaf70_21360160',
  'has_nocache_code' => false,
  'file_dependency' => 
  array (
    '5bd9489f01e2c0ff85ee9277a2efeba2cc8ec880' => 
    array (
      0 => '/var/www/html/tivi/panel/templates/settings/definition_form.tpl',
      1 => 1769276021,
      2 => 'file',
    ),
  ),
  'includes' => 
  array (
  ),
),false)) {
function content_697502c3efaf70_21360160 (Smarty_Internal_Template $_smarty_tpl) {
$_smarty_tpl->_loadInheritance();
$_smarty_tpl->inheritance->init($_smarty_tpl, true);
?>


<?php 
$_smarty_tpl->inheritance->instanceBlock($_smarty_tpl, 'Block_1040573700697502c3eef417_31520986', "content");
?>


<?php 
$_smarty_tpl->inheritance->instanceBlock($_smarty_tpl, 'Block_989315422697502c3ef8766_40085344', "scripts");
?>

<?php $_smarty_tpl->inheritance->endChild($_smarty_tpl, "layout.tpl");
}
/* {block "content"} */
class Block_1040573700697502c3eef417_31520986 extends Smarty_Internal_Block
{
public $subBlocks = array (
  'content' => 
  array (
    0 => 'Block_1040573700697502c3eef417_31520986',
  ),
);
public function callBlock(Smarty_Internal_Template $_smarty_tpl) {
?>

<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <!-- Header -->
            <div class="d-flex align-items-center mb-4">
                <a href="<?php echo $_smarty_tpl->tpl_vars['baseUrl']->value;?>
/settings/definitions" class="btn btn-outline-secondary me-3">
                    <i class="bi bi-arrow-left"></i>
                </a>
                <div>
                    <h4 class="mb-1"><?php echo $_smarty_tpl->tpl_vars['pageTitle']->value;?>
</h4>
                    <p class="text-muted mb-0">
                        <?php if ($_smarty_tpl->tpl_vars['definition']->value) {?>Edytuj parametr: <code><?php echo $_smarty_tpl->tpl_vars['definition']->value['key'];?>
</code><?php } else { ?>Dodaj nowy parametr dla urządzeń<?php }?>
                    </p>
                </div>
            </div>

            <form method="post" id="definitionForm">
                <div class="card">
                    <div class="card-body">
                        <input type="hidden" name="csrf_token" value="<?php echo $_smarty_tpl->tpl_vars['csrfToken']->value;?>
">

                        <div class="row g-3">
                            <!-- Klucz -->
                            <div class="col-md-6">
                                <label class="form-label">Klucz <span class="text-danger">*</span></label>
                                <input type="text" name="key" class="form-control font-monospace" 
                                       value="<?php echo (($tmp = $_smarty_tpl->tpl_vars['definition']->value['key'] ?? null)===null||$tmp==='' ? '' ?? null : $tmp);?>
" required
                                       pattern="[a-zA-Z][a-zA-Z0-9_]*"
                                       placeholder="np. isPlayerClockEnabled">
                                <div class="form-text">Nazwa parametru (camelCase, bez spacji)</div>
                            </div>

                            <!-- Typ -->
                            <div class="col-md-6">
                                <label class="form-label">Typ <span class="text-danger">*</span></label>
                                <select name="type" class="form-select" id="typeSelect" required>
                                    <?php
$_from = $_smarty_tpl->smarty->ext->_foreach->init($_smarty_tpl, $_smarty_tpl->tpl_vars['typeLabels']->value, 'label', false, 'value');
$_smarty_tpl->tpl_vars['label']->do_else = true;
if ($_from !== null) foreach ($_from as $_smarty_tpl->tpl_vars['value']->value => $_smarty_tpl->tpl_vars['label']->value) {
$_smarty_tpl->tpl_vars['label']->do_else = false;
?>
                                    <option value="<?php echo $_smarty_tpl->tpl_vars['value']->value;?>
" <?php if (((($tmp = $_smarty_tpl->tpl_vars['definition']->value['type'] ?? null)===null||$tmp==='' ? 'boolean' ?? null : $tmp)) == $_smarty_tpl->tpl_vars['value']->value) {?>selected<?php }?>>
                                        <?php echo $_smarty_tpl->tpl_vars['label']->value;?>

                                    </option>
                                    <?php
}
$_smarty_tpl->smarty->ext->_foreach->restore($_smarty_tpl, 1);?>
                                </select>
                            </div>

                            <!-- Etykieta -->
                            <div class="col-12">
                                <label class="form-label">Etykieta <span class="text-danger">*</span></label>
                                <input type="text" name="label" class="form-control" 
                                       value="<?php echo (($tmp = $_smarty_tpl->tpl_vars['definition']->value['label'] ?? null)===null||$tmp==='' ? '' ?? null : $tmp);?>
" required
                                       placeholder="np. Włącz zegar w playerze">
                            </div>

                            <!-- Opis -->
                            <div class="col-12">
                                <label class="form-label">Opis</label>
                                <textarea name="description" class="form-control" rows="2"
                                          placeholder="Opcjonalny opis dla administratora"><?php echo (($tmp = $_smarty_tpl->tpl_vars['definition']->value['description'] ?? null)===null||$tmp==='' ? '' ?? null : $tmp);?>
</textarea>
                            </div>

                            <!-- Grupa -->
                            <div class="col-md-6">
                                <label class="form-label">Grupa</label>
                                <select name="group_name" class="form-select">
                                    <?php
$_from = $_smarty_tpl->smarty->ext->_foreach->init($_smarty_tpl, $_smarty_tpl->tpl_vars['groupLabels']->value, 'label', false, 'value');
$_smarty_tpl->tpl_vars['label']->do_else = true;
if ($_from !== null) foreach ($_from as $_smarty_tpl->tpl_vars['value']->value => $_smarty_tpl->tpl_vars['label']->value) {
$_smarty_tpl->tpl_vars['label']->do_else = false;
?>
                                    <option value="<?php echo $_smarty_tpl->tpl_vars['value']->value;?>
" <?php if (((($tmp = $_smarty_tpl->tpl_vars['definition']->value['group_name'] ?? null)===null||$tmp==='' ? 'general' ?? null : $tmp)) == $_smarty_tpl->tpl_vars['value']->value) {?>selected<?php }?>>
                                        <?php echo $_smarty_tpl->tpl_vars['label']->value;?>

                                    </option>
                                    <?php
}
$_smarty_tpl->smarty->ext->_foreach->restore($_smarty_tpl, 1);?>
                                </select>
                            </div>

                            <!-- Min. poziom edycji -->
                            <div class="col-md-6">
                                <label class="form-label">Minimalny poziom edycji</label>
                                <select name="min_scope" class="form-select">
                                    <?php
$_from = $_smarty_tpl->smarty->ext->_foreach->init($_smarty_tpl, $_smarty_tpl->tpl_vars['scopeLabels']->value, 'label', false, 'value');
$_smarty_tpl->tpl_vars['label']->do_else = true;
if ($_from !== null) foreach ($_from as $_smarty_tpl->tpl_vars['value']->value => $_smarty_tpl->tpl_vars['label']->value) {
$_smarty_tpl->tpl_vars['label']->do_else = false;
?>
                                    <option value="<?php echo $_smarty_tpl->tpl_vars['value']->value;?>
" <?php if (((($tmp = $_smarty_tpl->tpl_vars['definition']->value['min_scope'] ?? null)===null||$tmp==='' ? 'device' ?? null : $tmp)) == $_smarty_tpl->tpl_vars['value']->value) {?>selected<?php }?>>
                                        <?php echo $_smarty_tpl->tpl_vars['label']->value;?>

                                    </option>
                                    <?php
}
$_smarty_tpl->smarty->ext->_foreach->restore($_smarty_tpl, 1);?>
                                </select>
                                <div class="form-text">Kto może edytować to ustawienie</div>
                            </div>

                            <!-- Wartość domyślna -->
                            <div class="col-md-6">
                                <label class="form-label">Wartość domyślna</label>
                                <div id="defaultValueContainer">
                                    <!-- Dynamicznie wypełniane przez JS -->
                                </div>
                            </div>

                            <!-- Kolejność sortowania -->
                            <div class="col-md-6">
                                <label class="form-label">Kolejność sortowania</label>
                                <input type="number" name="sort_order" class="form-control" 
                                       value="<?php echo (($tmp = $_smarty_tpl->tpl_vars['definition']->value['sort_order'] ?? null)===null||$tmp==='' ? 0 ?? null : $tmp);?>
" min="0">
                            </div>

                            <!-- Opcje (dla int/long) -->
                            <div class="col-12" id="optionsContainer" style="display:none;">
                                <label class="form-label">
                                    Opcje do wyboru
                                    <small class="text-muted">(tylko dla typów int/long - zostaw puste dla dowolnej wartości liczbowej)</small>
                                </label>
                                
                                <div id="optionsList" class="mb-2">
                                    <!-- Opcje dodawane dynamicznie -->
                                </div>
                                
                                <button type="button" class="btn btn-sm btn-outline-success" id="addOptionBtn">
                                    <i class="bi bi-plus-lg me-1"></i>Dodaj opcję
                                </button>
                                
                                <input type="hidden" name="options" id="optionsJson">
                            </div>

                            <!-- Aktywna -->
                            <div class="col-12">
                                <div class="form-check form-switch">
                                    <input type="checkbox" name="is_active" class="form-check-input" id="isActive" value="1"
                                           <?php if ((($tmp = $_smarty_tpl->tpl_vars['definition']->value['is_active'] ?? null)===null||$tmp==='' ? 1 ?? null : $tmp)) {?>checked<?php }?>>
                                    <label class="form-check-label" for="isActive">Aktywna</label>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="card-footer d-flex justify-content-between">
                        <a href="<?php echo $_smarty_tpl->tpl_vars['baseUrl']->value;?>
/settings/definitions" class="btn btn-outline-secondary">Anuluj</a>
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-lg me-1"></i>
                            <?php if ($_smarty_tpl->tpl_vars['definition']->value) {?>Zapisz zmiany<?php } else { ?>Dodaj definicję<?php }?>
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<?php
}
}
/* {/block "content"} */
/* {block "scripts"} */
class Block_989315422697502c3ef8766_40085344 extends Smarty_Internal_Block
{
public $subBlocks = array (
  'scripts' => 
  array (
    0 => 'Block_989315422697502c3ef8766_40085344',
  ),
);
public function callBlock(Smarty_Internal_Template $_smarty_tpl) {
?>

<?php echo '<script'; ?>
>
var definition = <?php echo (($tmp = json_encode($_smarty_tpl->tpl_vars['definition']->value) ?? null)===null||$tmp==='' ? 'null' ?? null : $tmp);?>
;
var currentOptions = <?php if ($_smarty_tpl->tpl_vars['definition']->value && $_smarty_tpl->tpl_vars['definition']->value['options']) {
echo json_encode($_smarty_tpl->tpl_vars['definition']->value['options']);
} else { ?>[]<?php }?>;

document.addEventListener('DOMContentLoaded', function() {
    var typeSelect = document.getElementById('typeSelect');
    var defaultContainer = document.getElementById('defaultValueContainer');
    var optionsContainer = document.getElementById('optionsContainer');
    var optionsList = document.getElementById('optionsList');
    var addOptionBtn = document.getElementById('addOptionBtn');
    var optionsJson = document.getElementById('optionsJson');
    var form = document.getElementById('definitionForm');
    
    // Template dla opcji
    function createOptionRow(value, label) {
        var row = document.createElement('div');
        row.className = 'input-group mb-2 option-row';
        row.innerHTML = 
            '<span class="input-group-text">Wartość</span>' +
            '<input type="number" class="form-control option-value" value="' + (value || '') + '" placeholder="0" style="max-width:100px">' +
            '<span class="input-group-text">Etykieta</span>' +
            '<input type="text" class="form-control option-label" value="' + (label || '') + '" placeholder="np. Góra-lewo">' +
            '<button type="button" class="btn btn-outline-danger remove-option-btn"><i class="bi bi-trash"></i></button>';
        return row;
    }
    
    // Dodaj opcję
    addOptionBtn.addEventListener('click', function() {
        optionsList.appendChild(createOptionRow('', ''));
    });
    
    // Usuń opcję (delegacja)
    optionsList.addEventListener('click', function(e) {
        if (e.target.closest('.remove-option-btn')) {
            e.target.closest('.option-row').remove();
        }
    });
    
    // Załaduj istniejące opcje
    function loadOptions() {
        optionsList.innerHTML = '';
        if (currentOptions && currentOptions.length > 0) {
            currentOptions.forEach(function(opt) {
                optionsList.appendChild(createOptionRow(opt.value, opt.label));
            });
        }
    }
    
    // Zbierz opcje do JSON przed wysłaniem
    form.addEventListener('submit', function() {
        var options = [];
        document.querySelectorAll('.option-row').forEach(function(row) {
            var value = row.querySelector('.option-value').value;
            var label = row.querySelector('.option-label').value;
            if (value !== '' && label !== '') {
                options.push({
                    value: parseInt(value),
                    label: label
                });
            }
        });
        optionsJson.value = options.length > 0 ? JSON.stringify(options) : '';
    });
    
    function updateDefaultInput() {
        var type = typeSelect.value;
        var currentValue = definition ? definition.default_value : '';
        
        if (type === 'boolean') {
            defaultContainer.innerHTML = '<select name="default_value" class="form-select">' +
                '<option value="true"' + (currentValue === 'true' || currentValue === '1' ? ' selected' : '') + '>true (włączone)</option>' +
                '<option value="false"' + (currentValue === 'false' || currentValue === '0' || !currentValue ? ' selected' : '') + '>false (wyłączone)</option>' +
                '</select>';
            optionsContainer.style.display = 'none';
        } else if (type === 'string') {
            defaultContainer.innerHTML = '<input type="text" name="default_value" class="form-control" value="' + (currentValue || '') + '">';
            optionsContainer.style.display = 'none';
        } else {
            // int lub long
            defaultContainer.innerHTML = '<input type="number" name="default_value" class="form-control" value="' + (currentValue || '0') + '">';
            optionsContainer.style.display = 'block';
        }
    }
    
    typeSelect.addEventListener('change', function() {
        updateDefaultInput();
        // Wyczyść opcje przy zmianie typu
        if (typeSelect.value === 'boolean' || typeSelect.value === 'string') {
            optionsList.innerHTML = '';
        }
    });
    
    updateDefaultInput();
    loadOptions();
});
<?php echo '</script'; ?>
>
<?php
}
}
/* {/block "scripts"} */
}
