<?php
/**
 * BSS Commerce Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://bsscommerce.com/Bss-Commerce-License.txt
 *
 * @category   BSS
 * @package    Bss_ImportExportCore
 * @author     Extension Team
 * @copyright  Copyright (c) 2020 BSS Commerce Co. ( http://bsscommerce.com )
 * @license    http://bsscommerce.com/Bss-Commerce-License.txt
 */

//  @codingStandardsIgnoreFile

?>

<?php /** @var  $block \Bss\ImportExportCore\Block\Adminhtml\Import\Before */?>

<script>
    window.BssImportExport = {
        "versions": <?= json_encode($block->getVersionInfo()) ?>,
        "configs": <?= json_encode($block->getConfigs()) ?>
    };
</script>

<script>
    require([
        'jquery',
        'Magento_Ui/js/modal/alert',
        'prototype'
    ], function(jQuery, alert){

//<![CDATA[
        // Temporary Class will be replaced after refactoring of import/export functionality
        varienImport = {
            /**
             * Name and ID for iframe for data POST-ing.
             * @type {string}
             */
            ifrElemName: 'import_post_target_frame',

            /**
             * List of existing behavior sets
             * @type {Array}
             */
            uniqueBehaviors: <?= /* @noEscape */ $block->getUniqueBehaviors() ?>,

            /**
             * Behaviour codes for import entities
             * @type {Array}
             */
            entityBehaviors: <?= /* @noEscape */ $block->getEntityBehaviors() ?>,

            /**
             * Behaviour notes for import entities
             * @type {Array}
             */
            entityBehaviorsNotes: <?= /* @noEscape */ $block->getEntityBehaviorsNotes() ?>,

            /**
             * Base url
             * @type {string}
             */
            sampleFilesBaseUrl: '<?= $block->escapeJs(
                $block->escapeUrl($block->getUrl('*/*/download/', ['filename' => 'entity-name']))
            ) ?>',

            /**
             * Update version number
             * @param {string} entity
             */
            updateModuleVersion: function(entity) {
                if (window.BssImportExport && window.BssImportExport.versions) {
                    if (entity) {
                        if (window.BssImportExport.versions[entity]) {
                            $("bss-version-number").update(window.BssImportExport.versions[entity]);
                            $("bss-version").show();
                            return;
                        }
                    }

                    $("bss-version").hide();
                }
            },

            updateImageSection: function(entity) {
                if (entity) {
                    if (this.ableImportImage(entity)) {
                        jQuery(".field-import_images_file_dir").show();
                    } else {
                        jQuery(".field-import_images_file_dir").hide();
                    }
                }
            },

            /**
             * Check entity able import image
             * @param {string} entity
             * @return {Boolean}
             */
            ableImportImage: function(entity) {
                if (!entity) {
                    return false;
                }
                if (window.BssImportExport && window.BssImportExport.configs) {
                    if (window.BssImportExport.configs[entity]) {
                        var entityConfig = window.BssImportExport.configs[entity];
                        if (!entityConfig.image_import) {
                            return false;
                        }
                    }
                }
                return true;
            },

            /**
             * Get Url to redirect after entity selected
             * @param {string} entity
             * @return {string}|{Boolean}
             */
            getEntityDirectUrl: function(entity) {
                if (window.BssImportExport && window.BssImportExport.configs) {
                    if (entity) {
                        if (window.BssImportExport.configs[entity]) {
                            var entityConfig = window.BssImportExport.configs[entity];
                            if (entityConfig.type == "redirect" && entityConfig.url) {
                                return entityConfig.url;
                            }
                        }
                    }
                }
                return false;
            },

            /**
             * Reset selected index
             * @param {string} elementId
             */
            resetSelectIndex: function(elementId) {
                var element = jQuery('#' + elementId);
                if (element) {
                    element[0].selectedIndex = 0;
                }
            },

            /**
             * Show behavior selector
             * @param {string|boolean} entity If false, then hide all behavior selectors
             */
            showBehavior: function(entity) {
                var requiredBehavior = this.entityBehaviors[entity];
                for (i = 0; i < this.uniqueBehaviors.length; i++) {
                    var currentBehavior = this.uniqueBehaviors[i];
                    this.showBehaviorFieldset(currentBehavior, requiredBehavior == currentBehavior);
                }
            },

            /**
             * Show/hide one behavior fieldset
             * @param {string} behaviorId
             * @param {boolean} isShow
             */
            showBehaviorFieldset: function(behaviorId, isShow) {
                var behaviorFieldset = jQuery('#' + behaviorId + '_fieldset'),
                    behaviorFields = jQuery('.' + behaviorId);
                if (isShow) {
                    behaviorFieldset.show();
                } else {
                    behaviorFieldset.hide();
                    this.resetSelectIndex(behaviorId);
                }
                behaviorFields.each(function(k, behaviorField){
                    behaviorField = jQuery(behaviorField);
                    if (isShow) {
                        behaviorField.prop('disabled', false);
                        behaviorField.addClass('required-entry');
                    } else {
                        behaviorField.prop('disabled', 'disabled');
                        behaviorField.removeClass('required-entry');
                    }
                });
            },

            /**
             * Show upload file field
             * @param {boolean} isShow
             */
            showUploadFile: function(isShow) {
                if (isShow == true) {
                    jQuery('#upload_file_fieldset').show();
                } else {
                    jQuery('#upload_file_fieldset').hide();
                }
            },

            /**
             * Show/hide sample file link
             * @param {string} entityId
             */
            showSampleFile: function(entityId) {
                var sampleFileSpan = jQuery('#sample-file-span');
                if (entityId) {
                    var sampleFileLink = this.sampleFilesBaseUrl.replace('entity-name', entityId);
                    jQuery('#sample-file-link').attr('href', sampleFileLink);
                    if (sampleFileSpan.is(':hidden')) {
                        sampleFileSpan.show();
                    }
                } else {
                    sampleFileSpan.hide();
                }
            },

            /**
             * Handle value change in entity type selector
             */
            handleEntityTypeSelector: function() {
                var entity = jQuery('#entity');

                if (this.getEntityDirectUrl(entity.val())) {
                    window.location.href = this.getEntityDirectUrl(entity.val());
                    return;
                }

                if (entity && entity.val()) {
                    this.showBehavior(entity.val());
                    this.showUploadFile(true);
                    this.showSampleFile(entity.val());
                } else {
                    this.showBehavior(false);
                    this.showUploadFile(false);
                    this.showSampleFile(false);
                }
                this.handleImportBehaviorSelector();
                this.updateModuleVersion(entity.val());
                this.updateImageSection(entity.val());
            },

            /**
             * Handle value change in behavior selector
             */
            handleImportBehaviorSelector: function() {
                var entity = jQuery('#entity');
                if (entity && entity.val()) {
                    var notes = this.entityBehaviorsNotes[entity.val()];
                    var requiredBehavior = this.entityBehaviors[entity.val()];
                    var behaviorInput = jQuery('#' + requiredBehavior);
                    var behavior = behaviorInput && behaviorInput.val();
                    if (behavior && notes[behavior]) {
                        jQuery('#' + requiredBehavior + '-note').html(notes[behavior]);
                    } else {
                        jQuery('#' + requiredBehavior + '-note').html('');
                    }
                }
            },

            /**
             * Post form data to dynamic iframe.
             * @param {string} newActionUrl OPTIONAL Change form action to this if specified
             */
            postToFrame: function(newActionUrl) {
                if (!jQuery('[name="' + this.ifrElemName + '"]').length) {
                    jQuery('body').append('<iframe name="' + this.ifrElemName + '" id="' + this.ifrElemName
                        + '" style="display:none;"/>');
                }
                jQuery('body')
                    .loader({
                        icon: jQuery('#loading_mask_loader img').attr('src')
                    })
                    .loader('show');
                var form = jQuery('#edit_form')
                    .one('invalid-form.validate', function(e){jQuery('body').loader('hide')});

                newActionUrl = (newActionUrl ? newActionUrl : form.attr('action'));
                if (newActionUrl.lastIndexOf('form_key') === -1) {
                    newActionUrl = newActionUrl +
                        (newActionUrl.lastIndexOf('?') !== -1 ? '&' : '?') +
                        'form_key=' + encodeURIComponent(form.find('[name="form_key"]').val());
                }

                form.trigger('save', [{
                    action: newActionUrl,
                    target: this.ifrElemName
                }]);
            },

            /**
             * Actions after data post complete. Hide load mask, remove iframe, run response processing method.
             * @param {Object} response
             */
            postToFrameComplete: function(response) {
                jQuery('body').loader('hide');
                jQuery('#' + this.ifrElemName).remove();
                this.postToFrameProcessResponse(response);
            },

            /**
             * Process response JSON.
             * @param {Object} response
             */
            postToFrameProcessResponse: function(response) {
                if ('object' != typeof(response)) {
                    alert({
                        content: '<?= $block->escapeHtml(__('Invalid response')); ?>'
                    });

                    return false;
                }

                $H(response).each(function(pair) {
                    switch (pair.key) {
                        case 'show':
                        case 'clear':
                        case 'hide':
                            $H(pair.value).each(function(val) {
                                if ($(val.value)) {
                                    $(val.value)[pair.key]();
                                }
                            });
                            break;
                        case 'innerHTML':
                        case 'value':
                            $H(pair.value).each(function(val) {
                                var el = $(val.key);
                                if (el) {
                                    el[pair.key] = val.value;
                                    // if element does not visible
                                    el.offsetWidth || el.up('div.entry-edit').show();
                                }
                            });
                            break;
                        case 'removeClassName':
                        case 'addClassName':
                            $H(pair.value).each(function(val) {
                                if ($(val.key)) $(val.key)[pair.key](val.value);
                            });
                            break;
                        case 'hasError':
                            $H(pair.value).each(function(val) {
                                if (val.value) jQuery('#import_file').val('');
                            });
                            break;
                        default:
                            alert({
                                content: pair.key + ': ' + pair.value
                            });
                            break;
                    }
                });
            },

            /**
             * Send form data to Start action when file is validated.
             * @param {string} newActionUrl
             * @param {string} sourceFileField
             */
            startImport: function(newActionUrl, sourceFileField) {
                jQuery('#' + sourceFileField).removeClass('required-entry');
                this.postToFrame(newActionUrl);
                jQuery('#' + sourceFileField).addClass('required-entry');
            }
        };
//]]>

    });
</script>
