<?php
/** @var \Magetop\Advancepreview\Block\PreviewWishlist $block */
$orderUrl = $block->getUrl('advance_preview/checkout/createOrderFromWishlist');
$offerUrl = $block->getUrl('advance_preview/product/requestOfferFromWishlist');
?>
<input type="hidden" id="txt-preview-order-url" value="<?php echo $orderUrl; ?>">
<input type="hidden" id="txt-preview-offer-url" value="<?php echo $offerUrl; ?>">
<input type="hidden" id="txt-test-text-warning" value="<?php echo __('It is not possible to create order, the offer is no longer up to date'); ?>">
<input type="hidden" id="txt-error" value="<?php echo __('Error'); ?>">
<input type="hidden" id="txt-request-text-warning" value="<?php echo __('It is not possible to request offer again'); ?>">
<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();
                self.offer_url = $('#txt-preview-offer-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') {
                    document.getElementById("btn-custom-add-to-cart-wishlist-"+itemId).click();
                      /*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
                      })
                  }
              })
            },
            requestOffer : function(itemId,productId,infoRequest) {
              var self = this;
              var data = {
                  item_id: itemId,
                  product_id: productId,
                  info_request: infoRequest
              };
              var url = self.offer_url;
              var loading = 'advance-preview-loading';
              var textKA = $('#txt-request-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
                      })
                      window.location.href = response.export_url;
                  }
                  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-preview-request-offer-again').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.requestOffer(itemId,productId, infoRequest);
                })
                $('.btn-custom-add-to-cart').click(function () {
                    var url = $(this).attr('custom-url');
                    window.location.href = url;
                })
            }
        }
        previewWishlist.init();
    })
</script>
