<!--
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_MpAdvertisementManager
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
-->
<?php
    $pingUrl = $block->getAjaxPingUrl();
    $pingServices = $block->getPingServices();
    $pingClearUrl = $block->getAjaxClearPingUrl();
?>

<script id="wk_ping_service_template" type="text/x-magento-template">
    <fieldset class="fieldset admin__fieldset fieldset-wide">
        <form id="ping_service_form" >
            <fieldset class="fieldset admin__fieldset fieldset-wide">
                <label class="legend label"><h2><?= /*@noEscape*/ __("Ping Service"); ?></h2></label>
                <div class="admin__field field  required _required">
                    <label class="label admin__field-label"><?= /*@noEscape*/ __("Ping Service"); ?></label>
                    <div class="admin__field-control control">
                        <select name="ping[service]" id="ping-services" 
                        class="select admin__control-select required-entry" data-validate="{required:true}">
                            <option value=""><?= /*@noEscape*/ __("Select Ping Service") ?></option>
                            <?php if (count($pingServices) > 0) {
                                foreach ($pingServices as $service) { ?>
                                    <option value="<?= $block->escapeHtml($service['value']); ?>"><?=
                                     $block->escapeHtml($service['label']); ?></option>
                        <?php   }
                            } ?>
                        </select>
                    
                        <comment>
                            <?= /*@noEscape*/ __("Select Ping Service"); ?>
                        </comment>
                    </div>
                </div>
                <div class="admin__field field required _required custom_url_show" style="display:none">
                    <label class="label admin__field-label"><?= /*@noEscape*/ __("Custom Service Url"); ?></label>
                    <div class="admin__field-control control">
                        <input class="admin__control-text input-text custom_url" type="text" name="ping[custom_url]"/>
                    <comment>
                        <?= /*@noEscape*/ __("Custom Ping Service Url"); ?>
                    </comment>
                    </div>
                </div>
                <div class="admin__field field required _required">
                    <label class="label admin__field-label"><?= /*@noEscape*/ __("Ping Title"); ?></label>
                    <div class="admin__field-control control">
                        <input class="admin__control-text input-text" type="text" name="ping[title]" 
                        data-validate="{required:true}"/>
                    <comment>
                        <?= /*@noEscape*/ __("Title of the Post"); ?>
                    </comment>
                    </div>
                </div>
                <div class="admin__field field required _required">
                    <label class="label admin__field-label"><?= /*@noEscape*/ __("Ping Url"); ?></label>
                    <div class="admin__field-control control">
                        <input class="admin__control-text input-text validate-url" type="text" 
                        name="ping[url]" data-validate="{required:true}" />
                    <comment>
                        <?= /*@noEscape*/ __("Post Url"); ?>
                    </comment>
                    </div>
                </div>
                <div class="admin__field field">
                    <label class="label admin__field-label"><?= /*@noEscape*/ __("Ping RSS Url"); ?></label>
                    <div class="admin__field-control control">
                        <input class="admin__control-text input-text validate-url" type="text" 
                        name="ping[rss_url]" />
                    <comment>
                        <?= /*@noEscape*/ __("Post RSS Feed Url"); ?>
                    </comment>
                    </div>
                </div>
            </fieldset>
        </form>
    </fieldset>
</script>

<?= /*@noEscape*/ $block->getButtonHtml() ?>

<?php if (strlen($block->getPingLog()) > 0) { ?>
    <button id="clear_ping_button" title="<?=
         /*@noEscape*/ __("Clear Ping Log"); ?>" type="button" 
         class="action-default scalable" data-ui-id="widget-button-0">
        <span><?= /*@noEscape*/ __("Clear Ping Log"); ?></span>
    </button>
    <div class="ping-log-container">
        <pre class="ping-log"><?= $block->escapeHtml($block->getPingLog()); ?></pre>
    </div>
<?php } ?>
<script>
/**
 * script for sending ping to search engines
 * @param  jquery $
 * @param  mage/translate $t      for translation
 * @param  Magento_Ui/js/modal/alert alert
 * @param  Magento_Ui/js/modal/confirm confirm
 * @param  Magento_Ui/js/modal/modal modal for ajax popup model
 */
require(
    [
      'jquery',
      'mage/translate',
      'Magento_Ui/js/modal/alert',
      'Magento_Ui/js/modal/confirm',
      'Magento_Ui/js/modal/modal',
      'mage/template',
      'mage/mage',
      'mage/validation',
      'jquery/ui'

    ], function($,$t,alert,confirm,modal,template) {
        
        var self = this;
        var htmlForm = template("#wk_ping_service_template");

        var options = {
            type: "popup",
            autoOpen:false,
            innerScroll:true,
            buttons: [
                {
                    text: $t('Ping'),
                    class: 'save-ping-serivce primary action-default',
                    click: function() {
                    /**
                    * validate validate form
                    * @type boolean
                    */
                       validate =  $("#ping_service_form").validation();
                       if (validate && $("#ping_service_form").validation('isValid')) {
                            /**
                             * validation passed save tax
                             */
                            res = pingService($("#ping_service_form").serialize());
                            //console.log(res);
                            this.closeModal();

                        }

                    }
                },
                {
                    text: $t('Close'),
                    class: 'close-ping-form primary action-default',
                    click: function() {
                        this.closeModal();
                    }
                }
            ],
            closed: function() {
                /**
                 * remove tax slide model created after closing
                 */
                $('body').find('.modal-slide').remove();
            },
            responsive: true
        };

        $("#ping_service_button").on("click", function() {
            
            var popup = modal(options, htmlForm({}));
            popup.openModal();

        });

        $("body").on("change", "#ping-services", function() {
            if ($(this).val() == 'custom') {
                $(".custom_url").addClass("required-entry");
                $(".custom_url_show").show();
            } else {
                $(".custom_url").removeClass("required-entry");
                $(".custom_url_show").hide();
            }
        });

        $("#clear_ping_button").on("click", function() {
            confirm({
                title: $t("Clear Log"),
                content: $t("Are you sure you want to clear all the log information"),
                actions: {
                    confirm: function(){
                        clearPingService();
                    }
                }
            });
            
        });

        /**
         * pingService
         * @param  string $formData seriaized form
         * @return array
         */
        function pingService($formData) {
            res = null;
            $.ajax({
                url: '<?= $block->escapeUrl($pingUrl); ?>',
                method: 'POST',
                async: true,
                data:$formData,
                dataType:'json',
                showLoader: true,
                success: function(result) {
                    if(result.success) {
                        res = result;
                    alert({content: $t("successfully ping")});
                    } else {
                        res = result;
                        alert({content: $t(result.error_message)});
                    }
                    location.reload();
                },
                error: function() {
                    alert({content: $t("There is some error in processing")});
                }
            });
            return res;
        }
    
        /**
         * clearPingService
         * @return void
         */
        function clearPingService() {
            res = null;
            $.ajax({
                url: '<?= $block->escapeUrl($pingClearUrl); ?>',
                method: 'POST',
                async: true,
                data: {
                    form_key: window.FORM_KEY
                },
                dataType:'json',
                showLoader: true,
                success: function(result) {
                    if(result.success) {
                        res = result;
                    alert({content: $t("log successfully cleared")});
                    window.location.reload();
                    } else {
                        res = result;
                        alert({content: $t(result.error_message)});
                    }
                    
                },
                error: function() {
                    alert({content: $t("There is some error in processing")});
                }
            });
        }
    });
</script>