<?php
/** @var \Magetop\Advancepreview\Block\PreviewWishlist $block */
$orderUrl = $block->getUrl('advance_preview/checkout/createOrderFromWishlist');
?>
<input type="hidden" id="txt-preview-order-url" value="<?php echo $orderUrl; ?>">
<script>
    require([
        'jquery',
        'Magento_Ui/js/modal/confirm',
        'Magento_Ui/js/modal/modal',
        'Magento_Ui/js/modal/alert',
        'mage/translate',
        'mgt_advance_preview_helper',
    ],function ($,confirmation,modal,alert,$tr,helper) {
        var previewWishlist = {
            version: '2.0',
            order_url: '',
            intData : function () {
                var self = this;
                self.order_url = $('#txt-preview-order-url').val();
            },
            createOrder : function(itemId,productId,infoRequest) {
              var self = this;
              var data = {
                  item_id: itemId,
                  product_id: productId,
                  info_request: infoRequest
              };
              var url = self.order_url;
              var loading = 'advance-preview-loading';
              helper.doRequest(url,data,loading,'',function (response) {
                    // console.log(response);
                  if(response.status === 'success') {
                      alert({
                          title: $tr('Success'),
                          content: response.message
                      })
                      $('.product-item').each(function () {
                            var tmpItemId = $(this).attr('id');
                            if(tmpItemId === 'item_'+itemId) {
                                $(this).css('display','none');
                                return false;
                            }
                      })
                      if(response.total_wishlist !== undefined && !isNaN(response.total_wishlist)) {
                          var newTotal = response.total_wishlist - 1;
                          newTotal = newTotal + ' ' + $tr('Item')
                          $('.toolbar-number').html(newTotal);
                      }
                  }
                  else {
                      alert({
                          title: $tr('Error'),
                          content: $tr('It is not possible to create order, the offer is no longer up to date')
                      })
                  }
              })
            },
            init: function () {
                var self = this;
                self.intData();
                $('.btn-preview-add-item-to-order').click(function () {
                    var itemId = $(this).attr('item-id');
                    var productId = $(this).attr('product-id');
                    var infoRequest = $('#txt-preview-wishlist-'+itemId).val();
                    infoRequest = JSON.parse(infoRequest) || null;
                    self.createOrder(itemId,productId, infoRequest);
                })
                $('.btn-custom-add-to-cart').click(function () {
                    var url = $(this).attr('custom-url');
                    window.location.href = url;
                })
            }
        }
        previewWishlist.init();
    })
</script>
