<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category    Mageplaza
 * @package     Mageplaza_Blog
 * @copyright   Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license     https://www.mageplaza.com/LICENSE.txt
 */
?>
<script>
    require(['jquery'], function ($) {
        var dataImport = {};
        var defaultTablePrefix = ['wp_', 'm1_', 'm2_'];
        var imagePathHint = [
            '*Please copy the <b>wp_content/uploads</b> folder to <b>pub/media/wysiwyg/</b> folder for post image content <br>' +
            '*Please copy the <b>wp_content/uploads</b> folder to <b>pub/media/mageplaza/blog/post/</b> folder for post image banner <br>',
            '*Please copy the <b>media</b> folder to <b>pub/media/</b> folder',
            '*Please copy the <b>pub/media/magefan_blog</b> folder to <b>pub/media/mageplaza/blog/post/</b> folder for post image banner '
        ];

        mpBlogImport = {
            typeSelector: <?= $block->getTypeSelector();?>,
            initImportFieldsSet: function () {
                for (var i = 0; i < this.typeSelector.length; i++) {
                    if ($('#import_type').attr("value") == this.typeSelector[i]) {
                        this.showFieldSet('#' + this.typeSelector[i] + '_fieldset');
                        dataImport = {
                            type: this.typeSelector[i],
                            importName: '#' + this.typeSelector[i] + '_import_name',
                            dbName: '#' + this.typeSelector[i] + '_db_name',
                            userName: '#' + this.typeSelector[i] + '_user_name',
                            pwd: '#' + this.typeSelector[i] + '_db_password',
                            host: '#' + this.typeSelector[i] + '_db_host',
                            tablePrefix: '#' + this.typeSelector[i] + '_table_prefix',
                            behaviourSelector: '#' + this.typeSelector[i] + '_import_behaviour',
                            expandBehaviourSelector: '#' + this.typeSelector[i] + '_import_behaviour_expand'
                        };

                        $(dataImport.tablePrefix).val(defaultTablePrefix[i]);
                        $("#" + this.typeSelector[i] + "_import_image_path").html(imagePathHint[i]);
                        $("#" + this.typeSelector[i] + "_db_name").addClass('required-entry');
                        $("#" + this.typeSelector[i] + "_user_name").addClass('required-entry');
                        $("#" + this.typeSelector[i] + "_db_host").addClass('required-entry');
                    } else {
                        this.hideFieldSet('#' + this.typeSelector[i] + '_fieldset');
                        $("#" + this.typeSelector[i] + "_db_name").removeClass('required-entry');
                        $("#" + this.typeSelector[i] + "_user_name").removeClass('required-entry');
                        $("#" + this.typeSelector[i] + "_db_host").removeClass('required-entry');
                    }
                }
            },

            initExpandBehaviour: function () {
                if ($(dataImport.behaviourSelector).attr("value") == 'update') {
                    $(dataImport.expandBehaviourSelector).parent().parent().removeClass('hidden');
                } else {
                    $(dataImport.expandBehaviourSelector).parent().parent().addClass('hidden');
                }
            },

            showFieldSet: function (selector) {
                $(selector).show();
            },

            hideFieldSet: function (selector) {
                $(selector).hide();
            },

            initImportCheckConnection: function () {
                if ($('#edit_form').valid()) {
                    $('body').loader('show');
                    $.ajax({
                        url: '<?= $block->getUrl('mageplaza_blog/import/validate', ['form_key' => $block->getFormKey()]);?>',
                        data: {
                            type: dataImport.type,
                            import_name: $(dataImport.importName).val(),
                            database: $(dataImport.dbName).val(),
                            user_name: $(dataImport.userName).val(),
                            password: $(dataImport.pwd).val(),
                            host: $(dataImport.host).val(),
                            table_prefix: $(dataImport.tablePrefix).val(),
                            behaviour: $(dataImport.behaviourSelector).val(),
                            expand_behaviour: $(dataImport.expandBehaviourSelector).val()
                        },
                        cache: false,
                        success: function (result) {
                            var messageHtml;
                            if (result.status == 'false') {
                                messageHtml = '<?= $block->getMessagesHtml('adderror', 'False connection. Please check the credentials and try again!') ?>';
                                $(".message-error").hide();
                                $(messageHtml).appendTo($(".page-columns"));
                                $(".message-success").hide();
                            } else {
                                messageHtml = '<?= $block->getMessagesHtml('addsuccess', 'Connect successfully. To start import process press "Import" button' . $block->getImportButtonHtml()) ?>';
                                $(".message-success").hide();
                                $(messageHtml).appendTo($(".page-columns"));
                                $(".message-error").hide();
                            }
                        },
                        complete: function () {
                            $('body').loader('hide');
                        }
                    });
                }
            },

            importAction: function () {
                $('body').loader('show');
                $.ajax({
                    url: '<?= $block->getUrl('mageplaza_blog/import/import', ['form_key' => $block->getFormKey()]);?>',
                    cache: false,
                    success: function (result) {
                        var statisticMessage = result.statistic;
                        $(".message-success").hide();
                        $(".message-error").hide();
                        $(statisticMessage).appendTo($(".page-columns"));
                    },
                    complete: function () {
                        $('body').loader('hide');
                    }
                });
            }
        };
    });
</script>
