<?php
/**
 * Filename : update.url.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');

include_once ('pages/header.part.php');

$urlid      = intval($_GET['id']);
$url        = $clickVoodooV4Database->getUrlById($urlid);
$options    = get_option($clickVoodooV4Database->tableOptions);
if (!$url) {
    echo '<script>window.location.href="'. admin_url('admin.php?page='. CLICKVOODOOV4_CODE) .'";</script>';
}
?>

    <div class="ui-tabs">
        <ul class="ui-tabs-nav">
            <li><a href="#update">Update <?php echo $url['name']; ?></a></li>
        </ul>

        <div id="update">
            <!-- holder - addnew -->
            <div id="holder-addnew">
                <form name="addNewForm" id="addNewForm" method="POST" action="">

                    <div class="row cell">
                        <h3>Update <?php echo $url['name']; ?></h3> <hr />
                    </div>

                    <div class="row cells2">
                        <div class="cell">
                            <div class="input-control text full-size">
                                <label for="click-name">Name <span class="text-bold fg-red">(NOTE: Reference only)</span></label>
                                <input type="text" name="click-name" id="click-name" placeholder="Enter name (reference only)" value="<?php echo $url['name']; ?>" />
                            </div>
                        </div>
                    </div>

                    <div class="row cells2">
                        <div class="cell">
                            <div class="input-control text full-size">
                                <label for="click-url">URL to Shorten</label>
                                <input type="text" name="click-url" id="click-url" placeholder="Enter URL to shorten" value="<?php echo $url['url']; ?>" />
                            </div>
                        </div>

                        <div class="cell">
                            <div class="row cells12">
                                <div class="cell colspan10">
                                    <div class="input-control text full-size">
                                        <label for="click-url-value">Shortened URL</label>
                                        <input type="text" name="click-url-value" id="click-url-value" value="<?php echo $url['url_value']; ?>" />
                                    </div>
                                </div>
                                <div class="cell colspan2" style="margin-top:9px;">
                                    <button name="cmdGenerateUrlValue" id="cmdGenerateUrlValue" class="button button-primary">
                                        <span class="icon mif-spinner2"></span>
                                    </button>
                                </div>
                            </div>
                        </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>/<span id="preview-click-url-value"></span>/';
                            }
                            else {
                                echo get_bloginfo('home') .'/?<span id="preview-url-parser">'. $options['url_parser'] .'</span>=<span id="preview-click-url-value"></span>';
                            }
                            ?>
                        </div>
                    </div> <br />


                    <div class="row cell">
                        <button name="cmdSubmitNew" id="cmdSubmitNew" class="button button-primary text-bold">
                            <span class="icon mif-pencil"></span> Update <?php echo $url['name']; ?>
                        </button>
                        <a href="<?php echo admin_url('admin.php?page='. CLICKVOODOOV4_CODE); ?>" name="cmdCancelNew" id="cmdCancelNew" class="button button-secondary fg-red text-bold">
                            <span class="icon mif-cross"></span> Cancel
                        </a>

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

    <script>
        (function($) {
            //* preview generated url value
            $('#preview-click-url-value').empty().append($('#click-url-value').val());


            //* generate url value
            $('#cmdGenerateUrlValue').on('click', function() {
                var $this   = $(this),
                    $value  = $('#click-url-value'),

                    $icon   = $this.children('span'),

                    $data   = {
                        action  : '<?php echo CLICKVOODOOV4_CODE; ?>-generate-url-value'
                    };


                $value.val('').prop('disabled', 'disabled').prop('placeholder', 'Generating value... Please wait.');
                $icon.addClass('mif-ani-spin');
                $.post('<?php echo admin_url('admin-ajax.php'); ?>', $data, function(res) {
                    if (res.length <= 0) {
                        alert('There was a problem processing your request. Please try again.');
                        return false;
                    }
                    else {
                        $('#cmdGenerateUrlValue').children('span').removeClass('mif-ani-spin');
                        $('#click-url-value').prop('disabled', false).prop('placeholder', '').val(res);
                        $('#preview-click-url-value').empty().append($('#click-url-value').val());
                    }
                });

                return false;
            });


            //* submit
            $('#addNewForm').on('submit', function(e) {
                e.preventDefault();
            });

            $('#cmdSubmitNew').on('click', function() {
                var $name   = $('#click-name'),
                    $url    = $('#click-url'),
                    $value  = $('#click-url-value'),

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

                    $data   = {
                        action  : '<?php echo CLICKVOODOOV4_CODE .'-update-url'; ?>',
                        id      : '<?php echo $urlid; ?>',
                        name    : $name.val(),
                        url     : $url.val(),
                        value   : $value.val()
                    };

                if ($name.val().length <= 0) {
                    alert('Name is required.');
                    return false;
                }

                if ($url.val().length <= 0) {
                    alert('URL to shorten is required.');
                    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) {
                    if (res == 'error') {
                        $dialog.empty().append('<span class="mif-cross fg-red"></span> There was a problem with your request. Please try again.');
                        return false;
                    }
                    else if (res == 'duplicate') {
                        $dialog.empty().append('<span class="mif-cross fg-red"></span> Duplicate record found. Please try again.');
                        return false;
                    }
                    else {
                        $dialog.empty().append('<span class="mif-checkmark fg-green"></span> Your request has been processed successfully.');

                        setTimeout(function() {
                            window.location.href = '<?php echo admin_url('admin.php?page='. CLICKVOODOOV4_CODE); ?>';
                        }, 2000);
                    }
                });

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


<?php
include_once ('pages/footer.part.php');
