{extends "../list.tpl"}

{block 'table-class' append} stockInMissingTable {/block}

<script type="text/javascript">
   {block onready append}
   let constData = {
      {if $smarty.get.sellInterval}sellInterval: {$smarty.get.sellInterval},{/if}
      {if $smarty.get.interval}interval: {$smarty.get.interval},{/if}
   };
   function initStockInMissingTableAjax(name, url, selector) {
      var $table = $(selector ? selector : '.stockInMissingTable');
      $table.on("click", '[data-acn="OrdersOfSupplierAdd"]', function (e) {
         e.preventDefault();
         var $this = $(this);
         var addData = $this.data('adddata');
         $pieces = $this.parent().prev().find('[name="pieces"]');
         addData.pieces = $pieces.val() || $pieces.attr('placeholder');
         addData.pieces = /^\d+$/.test(addData.pieces) ? parseInt(addData.pieces) : 0;

         if(addData.pieces <= 0) {
            return;
         }

         var $tr = $this.closest('tr').parent().closest('tr');
         $.ajax({
            url: 'launch.php?s=list.php&type=stockInMissing',
            type: "post",
            data: {
               ...constData,
               data: addData
            },
            success: function (response) {
               $tr.replaceWith($(response).find('.stockInMissingTable > tbody > tr').first());
            },
            error: function (xhr) {
               showInfoMessage('Chyba při objednání produktů', 'danger');
            }
         });
      });
      return this;
   }
   initStockInMissingTableAjax("stockInMissing", "{$ctrl.currUrl.Rel nofilter}");

   $('.stockInMissingTable').on("click", '[data-acn="OrdersAdd"]', function (e) {
      e.preventDefault();
      const $this = $(this);

      $this.parents('tr.no-row-click').siblings().removeClass('highRow');
      $this.parents('tr.no-row-click').addClass('highRow');

      const addData = $this.data('adddata');

      let order_url = addData.order_url.replaceAll('{ldelim}CODE{rdelim}', addData.code);
      const target = 'supplier' + addData.id_supplier.toString();

      const win = window.top.open(order_url, target, '');
      win.focus();
   });

   {/block}
</script>
