<div class="correlation__row">
    <div class="correlation__key">
        {$base_params.description}:
    </div>
    <div style="text-align: left; padding: 3px;" id="formula"></div>
</div>
<script>
    var last_num = -1;
    var cnt_num = 0;
    function addField(num, type, field, value, oper) {
        if (num > last_num) {
            if (num > 0) $('#null_oper').remove();
            var part = '<div id="formula_part' + num + '">' +
                    '<select class="form-control dont_use_chosen" style="width:60px" name="calc_params[{$tpl_num}][{$base_params.name}][' + num + '][type]" id="formula_type' + num + '" onchange="switchType(' + num + ')">' +
                        '<option value="field">{$lang.field}</option>' +
                        '<option value="value">{$lang.value}</option>' +
                    '</select> ' +
                    '<select class="form-control form-control-160 dont_use_chosen" name="calc_params[{$tpl_num}][{$base_params.name}][' + num + '][field]" id="formula_field' + num + '">' +
                        {foreach from=$type_params.fields item=field_info}
                            '<option value="{$field_info.int_name}">{$field_info.name_field}</option>' +
                        {/foreach}
                    '</select> ' +
                    '<input type="text" class="form-control form-control-160" name="calc_params[{$tpl_num}][{$base_params.name}][' + num + '][value]" id="formula_value' + num + '"> ' +
                    '<select class="form-control dont_use_chosen" style="width:35px" name="calc_params[{$tpl_num}][{$base_params.name}][' + num + '][oper]" id="formula_oper' + num + '" onchange="addField(' + (num + 1) + ')">' +
                        '<option value="" id="null_oper"></option>' +
                        '<option value=".">.</option>' +
                        '<option value="+">+</option>' +
                        '<option value="-">-</option>' +
                        '<option value="*">*</option>' +
                        '<option value="/">/</option>' +
                    '</select> &nbsp;' +
                    '<img src="images/b_drop.png" style="cursor:pointer; padding-bottom:3px;" onclick="delField(' + num + ')">' +
                '</div>';
            $('#formula').append(part);
            if (type) $('#formula_type' + num).val(type);
            if (field) $('#formula_field' + num).val(field);
            if (value) $('#formula_value' + num).val(value);
            if (oper)  $('#formula_oper'  + num).val(oper);
            switchType(num);
            last_num = num;
            cnt_num++;
        }
    }
    function delField(num) {
        $('#formula_part' + num).remove();
        cnt_num--;
        if (num == last_num) {
            for (var i = last_num; i > 0; i--) {
                last_num--;
                if ($('#formula_oper' + last_num).val()) {
                    $('#formula_oper' + last_num).prepend('<option id="null_oper"></option>');
                    $('#formula_oper' + last_num).val('');
                    break;
                }
            }
        }
        if (cnt_num == 0) {
            last_num = -1;
            addField(0);
        }
    }
    function switchType(num) {
        if ($('#formula_type' + num).val() == 'field') {
            $('#formula_field' + num).show();
            $('#formula_value' + num).hide();
        } else {
            $('#formula_field' + num).hide();
            $('#formula_value' + num).show();
        }
    }
    {foreach from=$base_params.set_params item=one_part key=num}
        addField({$num}, '{$one_part.type}', '{$one_part.field}', '{$one_part.value}', '{$one_part.oper}');
    {foreachelse}
        addField(0);
    {/foreach}
</script>