<!--
/**
 * @category   Webkul
 * @package    Webkul_GoogleShoppingFeed
 * @author     Webkul Software Private Limited
 * @copyright  Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license    https://store.webkul.com/license.html
 */  -->
<?php
    $productList = $block->getImportedProduct();
    $productCount = count($productList);
?>
<fieldset class="fieldset">
    <div class="wk-mu-note wk-mu-box">
        <?= /* @noEscape */ __('Starting Execution'); ?>...
    </div>
    <div class="wk-mu-notice wk-mu-box">
        <?= /* @noEscape */ __("Please don't close or refresh the window while exporting product(s)."); ?>
    </div>
    <div class="wk-mu-success wk-mu-box">
        <?= /* @noEscape */ __('Total %1 product(s) to export.', $productCount); ?>
    </div>
    <?php if ($productCount > 0): ?>
        <div class="wk-mu-info-bar">
            <?php __('Exporting '); ?>
            <span class="wk-current">1</span><?= /* @noEscape */ __(' of %1', $productCount);?>
            <?php $loaderImg = $block->getViewFileUrl('Webkul_GoogleShoppingFeed::images/loader.gif') ?>
            <img id="loader-image" src="<?= /* @noEscape */ $loaderImg ?>" />
        </div>
        <div class="wk-mu-progress-bar">
            <div class="wk-mu-progress-bar-current"></div>
        </div>
    <?php else: ?>
        <div class="wk-mu-note wk-mu-box">
            <?= /* @noEscape */ __('No product to export.'); ?>
        </div>
        <div class="wk-mu-note wk-mu-box">
            <?= /* @noEscape */ __('Finsihed Execution.'); ?>
        </div>
    <?php endif; ?>
    <div class="product_create_error_msg"></div>
</fieldset>
<script >
    require(['jquery'], function ($) {
        $(document).ready(function() {
            var productList = JSON.parse('<?= /* @noEscape */ json_encode($productList) ?>');
            var skipCount = 0;
            var exportUrl = "<?= /* @noEscape */ $block->getUrl('googleshoppingfeed/products/exportindividual'); ?>";
            var total = <?= /* @noEscape */ $productCount; ?>;
            var count = 0;
            if(total > 0) {
                exportProduct(0, 0);
            }
            function exportProduct(count, skipCount)
            {
                count = count;
                $.ajax({
                    type: 'post',
                    url:exportUrl,
                    async: true,
                    dataType: 'json',
                    data : {
                        'count':count,
                        'skip':skipCount,
                        'product':productList[count],
                        "form_key" : window.FORM_KEY
                    },
                    success:function(data) {
                        if(data['error'] == 1) {
                            $(".product_create_error_msg").prepend($('<div>')
                                                    .addClass('message message-error error')
                                                    .text(data['message'])
                                                );
                            skipCount++;
                        }
                        count = count+1;
                        width = (100/total)*count;
                        $(".wk-mu-progress-bar-current").animate({width: width+"%"},'slow', function() {
                            if(count == total) {
                                $(".wk-mu-info-bar").text("<?= /* @noEscape */ __('Completed') ?>");
                            } else {
                                $(".wk-current").text(count);
                                exportProduct(count, skipCount);
                            }
                        });
                    }
                });
            }
        });
    });
</script>
