<?php
/**
 * Filename : settings.php
 * Created  : Andy Christian S. Loyola <acl20032003@gmail.com> (http://AndysDesigns.Net)
 * Copyright 2023 - Andy Christian S. Loyola <acl20032003@gmail.com> (http://AndysDesigns.Net)
 *
 * Skype    : andy_christian_loyola_
 * Hangouts : acl20032003@gmail.com
 * Email    : acl20032003@gmail.com
 */
defined('_IN_MAIN_MODULE') or die ('ACCESS DENIED');

global $clickVoodooV4Database;

$options = get_option($clickVoodooV4Database->tableOptions);
?>

<div id="settings">
    <div id="holder-settings">
        <div class="row cell">
            <h3>Settings</h3> <hr />
        </div>
    </div>

    <div class="row cell">
        <form name="settingsForm" id="settingsForm" method="POST" action="">
            <div class="row cell">
                <h3>Update Settings</h3> <hr />
            </div>

            <div class="row cell">
                <div class="input-control text full-size">
                    <label for="url-parser">URL Parser</label>
                    <input type="text" name="url-parser" id="url-parser" placeholder="Enter URL parser" value="<?php echo $options['url_parser']; ?>" />
                </div>
            </div>

            <div class="row cell">
                <div class="bg-gray fg-white padding10">
                    <strong>URL Preview</strong> <br />

                    <?php
                    if (get_option('permalink_structure')) {
                        echo get_bloginfo('home') .'/<span id="preview-url-parser">'. $options['url_parser'] .'</span>/marcus/';
                    }
                    else {
                        echo get_bloginfo('home') .'/?<span id="preview-url-parser">'. $options['url_parser'] .'</span>=marcus';
                    }
                    ?>
                </div>
            </div> <br />

            <div class="row cell">
                <button name="cmdUpdateSettings" id="cmdUpdateSettings" class="button button-primary">
                    <span class="icon mif-pencil"></span> Update Settings
                </button>

                <div id="dialog-settings" style="margin-top:10px;"></div>
            </div>
        </form>
    </div>
</div>


<script>
    (function($) {
        //* preview
        $('#url-parser').on('keyup', function() {
            $('#preview-url-parser').empty().append($(this).val());
        })

        //* disable form submission
        $('#settingsForm').on('submit', function(e) {
            e.preventDefault();
        });


        $('#cmdUpdateSettings').on('click', function() {
            var $url_parser = $('#url-parser'),
                $preview    = $('#preview-url-parser'),
                $icon       = $(this).children('span'),

                $dialog     = $('#dialog-settings'),

                $data       = {
                    action      : '<?php echo CLICKVOODOOV4_CODE; ?>-update-settings',
                    url_parser  : $url_parser.val()
                };


            $icon.removeClass('mif-pencil').addClass('mif-spinner2 mif-ani-spin');
            if ($url_parser.val().length <= 0) {
                alert ('URL parser is required.');

                $icon.removeClass('mif-spinner2 mif-ani-spin').addClass('mif-cross fg-red');
                setTimeout(function() {
                    $icon.removeClass('mif-cross fg-red').addClass('mif-pencil');
                }, 2000);

                return false;
            }

            $dialog.empty().append('<span class="icon mif-spinner2 mif-ani-spin"></span> Processing your request... Please wait.');
            $.post('<?php echo admin_url('admin-ajax.php'); ?>', $data, function(res) {
                alertify.dismissAll();
                if (res != 'done') {
                    $dialog.empty().append('<span class="mif-cross fg-red"></span> There was a problem with your request. Please try again.');

                    $icon.removeClass('mif-spinner2 mif-ani-spin').addClass('mif-cross fg-red');
                    setTimeout(function() {
                        $icon.removeClass('mif-cross fg-red').addClass('icon mif-pencil');
                    }, 2000);


                    return false;
                }
                else {
                    $dialog.empty().append('<span class="mif-checkmark fg-green"></span> Your request has been processed successfully.');

                    setTimeout(function() {
                        var current_index = $('.ui-tabs').tabs('option', 'active');
                        $('.ui-tabs').tabs('load', current_index);
                    }, 1000);
                }
            });

            return false;
        });
    })(jQuery);
</script>