<?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; ?>">
<input type="hidden" id="txt-test-text-warning" value="<?php echo __('It is not possible to create order, either offer is no longer up to date or fill in the address details to proceed with the purchase'); ?>">
<input type="hidden" id="txt-error" value="<?php echo __('Error'); ?>">
<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';
              var textKA = $('#txt-test-text-warning').val();
              var textError = $('#txt-error').val();
              helper.doRequest(url,data,loading,'',function (response) {
                    // console.log(response);
                  if(response.status === 'success') {
                      alert({
                          title: response.message_title,
                          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: textError,
                          content: textKA
                      })
                  }
              })
            },
            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>
