{if $result.positions}
   <div class="form-group form-group-flex">
      <div class="col-md-4 control-label">
         <label>Na pozici</label>
      </div>
      <div class="col-md-6">
         <select class="selecter" name="config[selectedPosition]" id="positionSelect">
             <option value="-1" selected>
                    Všechny pozice
                </option>
             {foreach $result.positions as $id => $position}
                <option value="{$id}">
                    {$position.code}
                </option>
             {/foreach}
         </select>
      </div>
   </div>

   <div class="form-group form-group-flex">
      <div class="col-md-4 control-label">
         <label>Vytvořit z varianty</label>
      </div>
      <div class="col-md-6">
         <select class="selecter" name="config[selectedBatch]" id="batchSelect">
             {foreach $batches as $id => $batch}
                <option value="{$id}">
                    {$batch.title}: {$batch.code} ({$batch.date_expiry|date_format:"d.m.Y"})
                </option>
             {/foreach}
         </select>
      </div>
   </div>
   <div class="form-group form-group-flex">
      <div class="col-md-4 control-label">
         <label>Bude přesunuto kusů: </label>
      </div>
      <div class="col-md-6" id="piecesToMove">
         -
      </div>
   </div>
   {if !$productExists}
   <div class="form-group form-group-flex">
      <div class="col-md-4 control-label">
         <label>Přidat text před název:</label>
      </div>
      <div class="col-md-6">
         <input type="text" class="form-control input-sm" name="config[prependText]" value="{t}Zachraňte! {/t}">
      </div>
   </div>
      {/if}
    {include 'actions/createExpiringProduct_discount.tpl'}

   <div class="form-group form-group-flex" style="display: none;" id="batchExists">
      <div class="alert alert-success col-md-10 col-md-offset-1"><span class="glyphicon glyphicon-exclamation-sign"></span>
         Expirující produkt/varianta s tímto DMT již existuje. Šarže bude přiřazena k již existujícímu produktu/variantě se stejným DMT.
      </div>
   </div>

   <hr>
   <script>
      var result = JSON.parse('{$result|json_encode nofilter}');

      $positionSelect = $('#positionSelect');
      $batchSelect = $('#batchSelect');

      $positionSelect.on("change", function () {
         const selectedId = parseInt($(this).val());
         $batchSelect.empty();
         const variations = (result.positions[selectedId]) ? result.positions[selectedId].variations : result.allVariations;

         Object.values(variations).forEach((val) => {
            $batchSelect.append($("<option></option>").attr("value", (val.id) ? val.id : -1).text(val.title))
         });
         $batchSelect.trigger('change');

      });

      $batchSelect.on("change", function () {
         let selectedId = $(this).val();
         selectedId = selectedId > 0 ? parseInt(selectedId): '';
         const selectedPosition = parseInt($('#positionSelect').val());
         try {
            var variation = (selectedPosition > 0) ? result.positions[selectedPosition].variations[selectedId ?? ''] : result.allVariations[selectedId ?? ''];
         } catch (e) {

         }

         $('#piecesToMove').html(variation.availablePieces ?? '-');

         $('#priceDiscountDropdown [data-vat]').val(variation.vat ?? 0);
         $('#priceDiscountDropdown [data-price-original]').val(variation.price).trigger('change');

         if (variation.alreadyExists ?? false) {
            $('#batchExists').show();
            $('#batchDiscount').hide();

         } else {
            $('#batchExists').hide();
            $('#batchDiscount').show();
         }
      });
      $positionSelect.trigger('change');
      $batchSelect.trigger('change');

   </script>
{else}
   <input required hidden>
   <p class="text-center m-b-3"><strong>Nelze provést, produkt nemá žádné kusy k rozdělení.</strong></p>
{/if}
