<td colspan="2" align="center">
    <h2>{$params_tpl['title']}{$params_tpl['help']}</h2>
    <div style="display: flex; flex-direction: column;">{$tpl_types['put_field']}</div>
    <div id="case-type{$tpl_num}" style="display: flex; flex-direction: column;">{$tpl_types['case_type']}</div>
    <div id="add-value{$tpl_num}" style="display: flex; flex-direction: column;">{$tpl_types['add_value']}</div>
</td>
<script>
(function() {
    let putField = $('[name="calc_params[{$tpl_num}][put_field][field]"]');
    let calc_id = {$params_tpl['calc_id']};

    function putFieldChange() {
        let typeField = putField.children('option[value="' + putField.val() + '"]').attr('data-type');
        let multField = putField.children('option[value="' + putField.val() + '"]').attr('data-mult');
        $('#case-type{$tpl_num}').toggle(typeField == '2');
        $('#add-value{$tpl_num}').toggle(multField == '1');

        setTimeout(function() {
            currentUser(calc_id);
        }, 300);
    }

    function currentUser(calcId) {
        let set_value = '';
        if (calcId > 0) {
            let calculate = new XMLHttpRequest();
            calculate.open('GET', 'edit_calc.php?calc-ajax-action=1&action=getCalculate&calcId=' + calcId);
            calculate.send();
            calculate.onload = function () {
                if (calculate.response.toString() !== "[]") {
                    let calc_params = JSON.parse(calculate.response);

                    if (calc_params[{$tpl_num}]) {
                        let construct_params = JSON.parse(calc_params[{$tpl_num}]['construct_params']);
                        const field = construct_params['params'][{$tpl_num}]['put_field'];
                        
                        if (field) {
                            if (construct_params['params'][{$tpl_num}]['put_field']['set_value'] !== "") {
                                set_value = construct_params['params']['0']['put_field']['set_value'];
                            }
                        }

                        if ($('[name="calc_params[{$tpl_num}][put_field][field]"] option:selected').attr('data-type') == 7) {
                            $('[name="calc_params[{$tpl_num}][put_field][set_value]"]').prepend('<option '+ (set_value == '{literal}{текущий}{/literal}' ? 'selected' : '') +'>{literal}{текущий}{/literal}</option>');
                        }
                    } else {
                        if ($('[name="calc_params[{$tpl_num}][put_field][field]"] option:selected').attr('data-type') == 7) {
                            $('[name="calc_params[{$tpl_num}][put_field][set_value]"]').prepend('<option>{literal}{текущий}{/literal}</option>');
                        }
                    }
                }
            };
        } else {
            if ($('[name="calc_params[{$tpl_num}][put_field][field]"] option:selected').attr('data-type') == 7) {
                $('[name="calc_params[{$tpl_num}][put_field][set_value]"]').prepend('<option>{literal}{текущий}{/literal}</option>');
            }
        }
    }

    $(document).ready(putFieldChange);
    putField.change(putFieldChange);
})();
</script>
