/**
 * RCU Admin Global Scripts
 *
 * @category   Scripts
 * @version    1.0.1
 * @since      1.0.0
 */

jQuery(document).ready(function ($) {
    /**
     * increase jQuery Ajax Timeout
     */
    $.ajaxSetup({
        timeout: 120000
    });

    /**
     * Handle Notification Dismiss Ajax
     */
    $(document).on('click', '.RCU-Notification.is-dismissible .notice-dismiss', function () {
        // noinspection JSUnresolvedVariable
        $.ajax({
            method: "POST",
            url   : RCU.URL,
            data  : {
                'action': 'rcuAjaxDismissNotification',
                'nonce' : RCU.Nonce,
                'id'    : $(this).parent().attr('data-id')
            }
        });
    });

    /**
     * Handle Plugin Deactivation Button Click
     */
    $(document).on('click', 'tr[data-plugin="RTL-CareUnit/RTL-CareUnit.php"] .deactivate a', function (e) {
        // noinspection JSUnresolvedVariable
        if (!RCU.hasOwnProperty('canDeactive') || !RCU.canDeactive) {
            return;
        }

        e.preventDefault();

        let html =
                '<div class="RCUG-PDA-Wrapper">' +
                '<div class="RCUG-PDA-Close" id="RCUG-PDA-Close">' +
                '<span class="dashicons dashicons-no-alt"></span>' +
                '</div>' +
                '<div class="RCUG-PDA-Icon">' +
                '<img src="data:image/png;base64,[encoded data, 29068 characters]" alt="هشدار">' +
                '<span class="RCUG-PDA-Icon-Text">' + RCU.Translate.warning + '</span>' +
                '</div>' +
                '<div class="RCUG-PDA-Description">' + RCU.Translate.disablePlugin +
                '<br>' +
                '<span class="RCUG-PDA-Settings-Text">' +
                RCU.Translate.hideToNotShow +
                '</span>' +
                '</div>' +
                '<div class="RCUG-PDA-BTN-Wrapper">' +
                '<div class="RCUG-PDA-Actions">' +
                '<button class="RCUG-BTN RCUG-BTN-Red" id="RCUG-PDA-Deactive">' + RCU.Translate.deactivate + '</button>' +
                '<button class="RCUG-BTN" id="RCUG-PDA-Dismiss">' + RCU.Translate.dismiss + '</button>' +
                '</div>' +
                '<a class="button-link" target="_blank" id="RCUG-PDA-Settings">' + RCU.Translate.setting + '</a>' +
                '</div>' +
                '</div>';

        rcugDimmerShow(html);

        $.RCUG_PD_URL = $(this).attr('href');
    });

    /**
     * Handle PDA Deactive Button
     */
    $(document).on('click', '#RCUG-PDA-Deactive', function () {
        $('.RCUG-PDA-Wrapper').css('pointer-events', 'none').css('opacity', '.7');

        window.location.href = $.RCUG_PD_URL;
    });

    /**
     * Handle PDA Dismiss Buttons
     */
    $(document).on('click', '#RCUG-PDA-Close, #RCUG-PDA-Dismiss', function () {
        rcugDimmerRemove();
    });

    /**
     * Handle PDA Setting Button
     */
    $(document).on('click', '#RCUG-PDA-Settings, .RCUG-PDA-Settings-Link', function () {
        // noinspection JSUnresolvedVariable
        $(this).attr('href', RCU.Page + '&tab=Settings');
    });

    /**
     * Show RCU Dimmer
     */
    function rcugDimmerShow(html = '') {
        $('body').append('<div class="RCUG-Dimmer" style="display: none"><div class="RCUG-Dimmer-Content"></div></div>');

        $('.RCUG-Dimmer').fadeIn(100);

        if (html !== '') {
            $('.RCUG-Dimmer .RCUG-Dimmer-Content').html(html);
        }
    }

    /**
     * Hide RCU Dimmer
     */
    function rcugDimmerRemove() {
        $('body .RCUG-Dimmer').fadeOut(100, function () {
            $(this).remove();
        });
    }
});