<div class="form-group">
    <div class="col-md-2 control-label"><label>{'pricelistName'|translate}</label></div>
    <div class="col-md-3">
        <input type="text" class="form-control input-sm" name="data[name]" size="30" maxlength="100" value="{$body.data.name}">
    </div>
    <div class="col-md-2 control-label"><label>{'currency'|translate}</label></div>
    <div class="col-md-2">
        <select name="data[id_currency]" id="currencySelecter" class="selecter">
            {foreach $body.currencies as $currency}
                <option value="{$currency->getId()}" {if $body.data.id_currency == $currency->getId()}{assign var="currencyCode" value=$currency->getId()}selected{/if}>{$currency->getId()}</option>
            {/foreach}
        </select>
        </select>
    </div>
</div>

<div class="form-group">
    <div class="col-md-3 control-label"><label>{'onMaxWeight'|translate}</label></div>
    <div class="col-md-3">
        <div class="radio">
            <input type="radio" class="check" name="data[on_max_weight]" value="divide" {if $body.data.on_max_weight == 'divide' or $body.acn == 'add'}checked{/if} id="onMaxW1" />
            <label for="onMaxW1">{'divide'|translate}</label>
            <a class="help-tip" data-toggle="tooltip" title="{'divide_info'|translate}"><i class="bi bi-question-circle"></i></a>
        </div>
    </div>
    <div class="col-md-3">
        <div class="radio">
            <input type="radio" class="check" name="data[on_max_weight]" value="forbid" {if $body.data.on_max_weight == 'forbid'}checked{/if} id="onMaxW2" />
            <label for="onMaxW2">{'forbid'|translate}</label>
        </div>
    </div>
</div>



<div class="row">
    <div class="col-md-12">
        <h1 class="h4 main-panel-title">{'delivery_prices'|translate}</h1>
    </div>
</div>

<div class="row bottom-space">
    <div class="col-md-7">
        Všechny níže uvedené ceny jsou v <span class="currencyCode">{$currencyCode}</span> a <span id="vat">s DPH</span>
    </div>
    <div class="col-md-4 control-label">
        <label>{'showPricesDPH'|translate:'products'}</label>
    </div>
    <div class="col-md-1" style="">
        <div class="input-group">
            {if !$body.data.showPricesWithVat}{$vatValue = 'Y'}{/if}
            {print_toggle name="showPricesWithVat" value=$vatValue}
        </div>
    </div>
</div>

{if $body.regionsCount == 0}
<div>{'regionNeededMessage'|translate}</div>
{else}
<div id="delivery_pricelists_prices">

    <!-- Delivery prices Table -->
    <table class="table table-striped">
        <tr>
            <th>
               {* {'region'|translate} *}
            </th>
            {foreach $body.regions as $region}
                <th style="text-align: center">{$region->getName()}</th>
            {/foreach}
            <th>{'deleteWeightGroup'|translate}</th>
        </tr>
        {foreach $body.weightGroups as $wg}
        <tr>
            <td>
                <div class="input-group">
                    <input class="form-control input-sm" style="min-width:50px" name="data[weight_group][{$wg->getId()}]" value="{$wg->getMaxWeight()}">
                    <span class="input-group-addon">kg</span>
                </div>
            </td>
            {foreach $body.regions as $region}
                <td>
                    <input class="form-control input-sm" data-price="price" style="min-width:75px" name="data[price][{$region->getId()}-{$wg->getId()}]" value="{$body.prices[$wg->getId()][$region->getId()]}" placeholder="{'price'|translate}">
                </td>
            {/foreach}
            <td>
                <button type="submit" class="btn-sm btn btn-danger confirm" name="deleteWg[{$wg->getId()}]" title="Smazat váhovou skupinu">
                    <span class="glyphicon glyphicon-remove"></span>
                </button>
            </td>
        </tr>
        {/foreach}


        <!-- New weight group -->
        <tr data-form-new style="display:none">
            <td>
                <div class="input-group">
                    <input class="form-control input-sm" name="data[new_weight_group][0]" placeholder="{'maxWeight'|translate}">
                    <span class="input-group-addon">kg</span>
                </div>
            </td>
            {foreach $body.regions as $region}
                <td>
                    <div class="input-group">
                        <input class="form-control input-sm" size="30" name="data[price_new][0][{$region->getId()}]" placeholder="{'price'|translate}">
                        <span class="input-group-addon currencyCode">{$currencyCode}</span>
                    </div>
                </td>
            {/foreach}
            <td>
                <a class="btn-sm btn btn-danger" data-form-delete>
                    <span class="glyphicon glyphicon-remove"></span>
                </a>
            </td>
        </tr>

    </table>

    <div class="row bottom-space">
        <div class="col-md-3">
            <a href="#" data-form-add class="btn btn-success btn-block"><span class="glyphicon glyphicon-plus"></span>&nbsp;{'AddWeightGroup'|translate}</a>
        </div>
    </div>
</div>
{/if}



<script>
    function recalcVat(element, type, vat) {
        var priceVat;
        var $price = element,
            $value;

        if ($price.val()) {
            $value = new BigNumber($price.val());

            switch (type) {
                case 1:
                    priceVat = $value.times((100 + vat) / 100).round(4); //$price.val() * (100 + vat) / 100));
                    break;
                case 0:
                    priceVat = $value.times(100).dividedBy(100 + vat).round(4);//$price.val() * 100 / (100 + vat)));
                    break;
            }
            $price.val(priceVat);
        }
    }

    function recalcVats(vat, type) {
        $('#delivery_pricelists_prices').find('[data-price=price]').each(function () {
            recalcVat($(this), type, vat);
        });
    }


    $(document).ready(function () {
       var code = $('#currencySelecter option:selected').text();
       $('.currencyCode').text(code);

        $('#delivery_pricelists_prices').find('[data-price=price]').each(function () {
            $(this).val(formatPrice($(this).val()));
        });
    });

    // vat switch
    $('input[name="data[showPricesWithVat]"]').change(function () {
        {$vat = getAdminVat()}
        if ($(this).is(':checked')) {
            recalcVats({$vat.value}, 1);
            $('#vat').text('s DPH');
        } else {
            recalcVats({$vat.value}, 0);
            $('#vat').text('bez DPH');
        }
    });

    $('#currencySelecter').change(function () {
        var code = $(this).find('option:selected').text();
        $('.currencyCode').html(code);
    });

    initForm({
        selector: '#delivery_pricelists_prices',
    });
</script>

