<h2 class="p5 gradient border-bottom">[{isys type="lang" ident="LC__UNIVERSAL__CUSTOM_COUNTER"}]</h2>

<div class="p10" style = "margin-bottom:24px">
    <button type="button" class="btn fl" id="add_multiple_counters_button">
        <img class="mr5" src="[{$dir_images}]icons/plus-green.gif" />
        <span>[{isys type="lang" ident="LC__UNIVERSAL__ADD_COUNTER"}]</span>
    </button>

    [{isys type="f_count" name="count_of_custom_counters"}]
</div>
<div class="bg-white">

    <div>

        <table id="countersTable" class="listing p0" style="border:0; max-width: 600px;">
            <colgroup>
                <col style="width:65%;" />
                <col style="width:10%;" />
                <col style="width:25%;" />
                <col style="width:12%;" />
            </colgroup>
            <thead>
            <tr>
                <th>[{isys type="lang" ident="LC__MODULE__QCW__TITLE"}]</th>
                <th>[{isys type="lang" ident="LC__REGEDIT__VALUE"}]</th>
                <th>
                    <img title="[{isys type="lang" ident="LC__CUSTOM_COUNTER__RESET_INFO"}]" src="[{$dir_images}]icons/silk/information.png" class="vam">
                    <span class="vam">[{isys type="lang" ident="LC__CUSTOM_COUNTER__RESET"}]</span></th>
                <th>[{isys type="lang" ident="LC__MODULE__QCW__DELETE"}]</th>
            </tr>
            </thead>
            <tbody>
            [{foreach from=$data item="value" key="key"}]
                <tr>
                    <td>
                        <input type="hidden" name="counter[[{$key}]][key]" value="[{$key}]" />
                        <input type="hidden" class="deleter" name="counter[[{$key}]][is_deleted]" value="0" />
                        %[{$key}]%
                    </td>
                    <td class="counter_column">
                        <input type="hidden" class="counter" name="counter[[{$key}]][value]" value="[{$value}]" />
                        <span>[{$value}]</span>
                    </td>
                    <td>
                        <button class="btn element-reset fr"><img src="[{$dir_images}]icons/silk/arrow_rotate_anticlockwise-half.png" class="mr5"> [{isys type="lang" ident="LC__CUSTOM_COUNTER__RESET"}]</button>
                    </td>
                    <td>
                        <button class="btn element-delete"><img src="[{$dir_images}]icons/silk/cross.png" class="mr5">[{isys type="lang" ident="LC__MODULE__QCW__DELETE"}]</button>
                    </td>
                </tr>

                [{/foreach}]
            [{foreach from=$new_counters item="value"}]
                <tr class="new-counter">
                    <td>
                        <div class="input-group">
                            <div class="input-group-addon input-group-addon-unstyled">%COUNTER_</div>
                            <input class="input input-block" type="text" name="new_counters[key][]" value="[{$value}]">
                            <div class="input-group-addon input-group-addon-unstyled">%</div>
                        </div>
                    </td>
                    <td><input class="input input-block" type="hidden" name="new_counters[value][]" value="1">1</td>
                    <td></td>
                    <td><a class="btn element-delete"><img src="[{$dir_images}]icons/silk/cross.png" class="mr5">[{isys type="lang" ident="LC__MODULE__QCW__DELETE"}]</a></td>
                </tr>

                [{/foreach}]
            </tbody>
        </table>
    </div>
</div>
<script>
    const $table = $('countersTable');

    $('add_multiple_counters_button').on('click', function () {
        var countOfFields = $F('count_of_custom_counters');
        do {
            $row = new Element('tr',{className:'new-counter'});
            $row.insert(new Element('td').update(new Element('div', {className: 'input-group'})
                    .insert(new Element('div', {className: 'input-group-addon input-group-addon-unstyled'}).update('%COUNTER_'))
                    .insert(new Element('input',{className: 'input input-block', type: 'text', name: 'new_counters[key][]', value: ''}))
                    .insert(new Element('div', {className: 'input-group-addon input-group-addon-unstyled'}).update('%'))
                    .insert(new Element('input',{className: 'deleter', type:'hidden'}))))
                .insert(new Element('td')
                    .insert(new Element('input',{className: 'input input-block', type: 'hidden', name: 'new_counters[value][]', value: 1}))
                    .insert('1'))
                .insert(new Element('td'))
                .insert(new Element('td')
                    .insert(new Element('a',{className: "btn element-delete"})
                        .insert(new Element('img',{src: '[{$dir_images}]icons/silk/cross.png', className: 'mr5'}))
                        .insert('[{isys type="lang" ident="LC__MODULE__QCW__DELETE"}]')));
            $table.down('tbody').insert($row);
            countOfFields--;
        } while (countOfFields>0)
        // Find out how many fields already exist:

    })
    $table.on('click', '.element-delete', function(ev, el) {
        if(confirm('[{isys type="lang" ident="LC__CUSTOM_COUNTER__DELETE_ALERT" p_bHtmlEncode=0}]')){
            $row = el.up().up();
            if($row.hasClassName('new-counter')) {
                $row.remove();
            } else {
                $row.down('.deleter').value = 1;
                $row.hide();
            }
        }
    })
    $table.on('click', '.element-reset', function(ev, el) {
        if(confirm('[{isys type="lang" ident="LC__CUSTOM_COUNTER__RESET_INFO" p_bHtmlEncode=0}]')){
            el.up().up().down('.counter').value = 1;
            el.up().up().down('.counter_column').down('span').innerText = '1';
        }
    })
</script>