import $ from 'jquery';
import '../../scss/deactivate.scss';
$(document).ready(function($) {
    const $link = $('#deactivate-devellux-sales-countdown-timer-builder');

    $link.on('click', function(e) {
        e.preventDefault();
        const popup = $(`
        <div id="develux-popup" class="develux-popup">
            <div id="develux-popup-dialog" class="develux-popup-dialog" >
                <p><strong>Do you want to delete all plugin data on deactivation?</strong></p>
                <button id="develux-popup-yes" class="develux-red-bg">Yes, clear all data created by the plugin</button>
                <button id="develux-popup-no" class="develux-orange-bg">No, just deactivate the plugin</button>
                <button id="develux-popup-cancel" class="develux-green-bg">Cancel</button>
            </div>
        </div>
    `);
        $('body').append(popup);

        let deactivateHref = $(this).attr('href');
        $('#develux-popup-cancel').on('click', function() {
            deactivateHref = '';
            $('#develux-popup').remove();
        });

        const spinner = ' <div class="develux-spinner"></div>';

        $('#develux-popup-yes').on('click', function() {
            $('#develux-popup-dialog').html(spinner);
            $.ajax({
                url: ajaxurl,
                method: 'POST',
                data: {
                    action: develuxsct_develuxTimerDeactivateConfirm.action,
                    nonce: develuxsct_develuxTimerDeactivateConfirm.nonce,
                    delete_data: 'yes'
                },
                success: function(response) {
                    console.log(response);
                    window.location.href = deactivateHref;
                }
            });
        });

        $('#develux-popup-no').on('click', function() {
            $('#develux-popup-dialog').html(spinner);
            window.location.href = deactivateHref;
        });
    });
});
