<div id="batchDiscount">
<div class="form-group form-group-flex">
   <div class="col-md-4 control-label">
      <label>Sleva z produktu</label>
   </div>
   <div class="col-md-3">
      <div class="input-group">
         <input type="number" class="form-control input-sm" name="config[discount]" step="any" maxlength="11" min="0" max="100"
                value="0">
         <span class="input-group-addon">%</span>
      </div>
   </div>
</div>

<div class="form-group form-group-flex" >

   <div class="col-md-4 control-label">
      <label>Výsledná cena</label>
   </div>

   <div class="col-md-5" >
      <div class="input-group" id="priceDiscountDropdown">
         <input type="text" class="form-control input-sm" name="config[price]" data-price />
         <input type="hidden" data-price-original>
         <input type="hidden" name="config[with_vat]" data-price-vat value="1">
         <input type="hidden" name="config[vat]" data-vat >
         <span class="input-group-addon">{$dbcfg.currency}</span>
         <div class="input-group-btn" data-dropdown="">
            <a class="btn btn-primary dropdown-toggle btn-sm" data-toggle="dropdown">
               <span data-dropdown-state=""></span>
               <span class="caret"></span>
            </a>
            <ul class="dropdown-menu">
               <li><a data-value="1">{'withTax'|translate:'choice'}</a></li>
               <li><a data-value="0">{'withoutTax'|translate:'choice'}</a></li>
            </ul>
         </div>
      </div>
   </div>
</div>
   <script type="text/javascript">
      var $dropdown = $('#priceDiscountDropdown');
      var $price = $('[name="config[price]"]');
      var $discount = $('[name="config[discount]"]');
      var $vat = $('[name="config[vat]"]');
      var $vatState = $dropdown.find('[data-price-vat]');
      var $originalPrice = $dropdown.find('[data-price-original]');

      function reloadDropdown() {
         $dropdown.find('[data-dropdown]').off('click');
         const state = $vatState.val();
         $vatState.val(0);
         priceVatDropdown($dropdown, state);
      }

      reloadDropdown()

      $discount.on('change', function () {
         $price.val($originalPrice.val() * (100 - $discount.val()) / 100);
         reloadDropdown();
      });

      $price.on('change', function () {
         var price = $price.val();
         if($vatState.val() === '1') {
            price /= (parseInt($vat.val()) + 100 ) / 100
         }
         price =  Math.max(Math.min(price, $originalPrice.val()), 0);
         $price.val(price);
         $discount.val((1 - price / $originalPrice.val()) * 100)

         reloadDropdown();
      });

      $originalPrice.on('change', function () {
         $price.val($(this).val());
         $discount.trigger('change');
      });

   </script>

</div>
