jQuery(document).ready(function ($) {

    $('.fleximp-notice-coupon-btn').on('click', function(){

        var $btn = $(this);
        var coupon = $btn.data('coupon');

        navigator.clipboard.writeText(coupon).then(function() {

            var originalText = $btn.html();

            $btn.html('Copied!');

            setTimeout(function(){
                $btn.html(originalText);
            }, 2000);

        });

    });

    jQuery('form#fleximp-license-key-form').on('submit', function (e) {
        e.preventDefault();
        var license_key = jQuery('form#fleximp-license-key-form').serializeArray()[0].value;

        if (license_key == "") {
            alert('Please Enter the license key first!');
            return;
        } else {
            jQuery.post(
                localize_script_arr.ajaxurl, {
                action: 'license_activate_fleximp',
                wpnonce: localize_script_arr.wpnonce,
                fleximp_is_premium: license_key,
                text_domain: 'flex-import'
            },
                function (data, status) {
                    if (status == 'success') {
                        if (data.status) {
                            // location.reload(true);
                            window.location.href = localize_script_arr.redirect_url;
                            jQuery.notify(data.msg, {
                                position: "right bottom",
                                className: "success"
                            });
                        } else {
                            jQuery.notify(data.msg, {
                                position: "right bottom"
                            });
                        }
                    }
                }
            );
        }
    });

    jQuery('form#fleximp-license-key-form .next-step-activation').on('click', function (e) {
        e.preventDefault();
        jQuery('.demo-import-wrap').show();
        jQuery('.license-key-wrap').hide();
    });

    jQuery('form#fleximp-license-key-form .deactivate-domain').on('click', function (e) {

        var license_key = jQuery('form#fleximp-license-key-form').serializeArray()[0].value;

        if (confirm('Are you sure you want to deactivate the key!')) {
            jQuery.post(
                localize_script_arr.ajaxurl, {
                action: 'license_deactivate_fleximp',
                wpnonce: localize_script_arr.wpnonce,
                fleximp_license_key: license_key
            },
                function (data) {

                    if (data.status == true) {
                        location.reload(true);

                        jQuery.notify(data.msg, {
                            position: "right bottom",
                            className: "success"
                        });
                    } else {
                        jQuery.notify(data.msg, {
                            position: "right bottom"
                        });
                    }
                }
            );
        }
    });
});