//  - 2018

jQuery(function () {


    jQuery(".go-parasut-invoice").on('click', function (ev) {
        ev.preventDefault();

        var button = jQuery(this),
            buttonsave = button.html(),
            loadinghtml = "<img style='margin-top: 3px;' src='"+ loading_img +"' width='20' class='grispin'>"
        ;

        button.html(loadinghtml);
        jQuery.post(ajaxurl, {
            'action': 'get_invoice_parasut_url',
            'id': button.data('id')
        }, function(response) {
            if(response==='false')
            {
                alert('Fatura bulunamadı.');
            } else {
                window.open(response, '_blank');
            }

            button.html(buttonsave);

        });

    });

    jQuery("#link-parasut-invoice").on('click', function (ev) {
        ev.preventDefault();
        var popupWindow=null;
        var order_id = jQuery(this).data('id');
        popupWindow = window.open(grilabs_baseurl + '/wc-api/invoicematch?for=' + order_id,'name','width=700,height=500');
        popupWindow.focus();
    });

    jQuery("#complete-invoice").on('click', function (ev) {
        ev.preventDefault();

        var button = jQuery(this),
            buttonsave = button.html(),
            loadinghtml = "<img style='margin-top: 3px;' src='"+ loading_img +"' width='20' class='grispin'>"
        ;


        var order_id = button.data('id');
        button.html(loadinghtml);

        var data = {
            'action': 'grilabsparasut_create_earchive',
            'id': order_id
        };

        jQuery.post(ajaxurl, data, function(response) {

            response = JSON.parse(response);
            if(typeof response.error !== 'undefined')
            {
                alert(response.error);
                button.html(buttonsave);
            } else {
                window.location.reload();
            }
        });



    });


    jQuery(".link-parasut-invoice").on('click', function (ev) {
        ev.preventDefault();
        var popupWindow=null;
        var order_id = jQuery(this).data('id');
        popupWindow = window.open(grilabs_baseurl + '/wc-api/invoicematch?for=' + order_id + '&reload=1','name','width=700,height=500');
        popupWindow.focus();
    });




    jQuery("#unlink-parasut-product").on('click', function (ev) {
        ev.preventDefault();

        var button = jQuery(this),
            buttonsave = button.html(),
            loadinghtml = "<img style='margin-top: 3px;' src='"+ loading_img +"' width='20' class='grispin'>"
        ;

        jQuery(this).html(loadinghtml);

        var data = {
            'action': 'parasut_unlink_invoice',
            'id': button.data('id')
        };

        jQuery.post(ajaxurl, data, function(response) {
            if(response==='true')
            {
                Array.prototype.forEach.call(document.getElementsByClassName('parasut-invoice-connected'), function(el) {el.style.display="none";});
                Array.prototype.forEach.call(document.getElementsByClassName('parasut-invoice-completed'), function(el) {el.style.display="none";});
                Array.prototype.forEach.call(document.getElementsByClassName('parasut-invoice-notconnected'), function(el) {el.removeAttribute('style');});
            } else {
                alert('Fatura bağlantısı koparılamadı. Yeniden deneyiniz.');
            }

            setTimeout(function () {
                button.html(buttonsave);
            },500);

        });

    });

    jQuery(".unlink-parasut-invoice").on('click', function (ev) {
        ev.preventDefault();

        var button = jQuery(this),
            buttonsave = button.html(),
            loadinghtml = "<img style='margin-top: 3px;' src='"+ loading_img +"' width='20' class='grispin'>"
        ;

        jQuery(this).html(loadinghtml);

        var data = {
            'action': 'parasut_unlink_invoice',
            'id': button.data('id')
        };

        jQuery.post(ajaxurl, data, function(response) {
            if(response==='true')
            {
                window.location.reload();
            } else {
                alert('Fatura bağlantısı koparılamadı. Yeniden deneyiniz.');
                setTimeout(function () {
                    button.html(buttonsave);
                },500);
            }


        });

    });


    jQuery("#send-parasut-invoice").on('click', function (ev) {
        ev.preventDefault();

        var button = jQuery(this),
            buttonsave = button.html(),
            loadinghtml = "<img style='margin-top: 3px;' src='"+ loading_img +"' width='20' class='grispin'>"
        ;

        button.html(loadinghtml);

        var data = {
            'action': 'send_invoice_parasut',
            'id': button.data('id')
        };

        jQuery.post(ajaxurl, data, function (response) {
            response = JSON.parse(response);
            if(typeof response.error !== 'undefined')
            {
                alert(response.error);
            } else {
                Array.prototype.forEach.call(document.getElementsByClassName('parasut-invoice-connected'), function(el) {el.removeAttribute('style');});
                Array.prototype.forEach.call(document.getElementsByClassName('parasut-invoice-notconnected'), function(el) {el.style.display="none";});
                Array.prototype.forEach.call(document.getElementsByClassName('parasut-invoice-completed'), function(el) {el.style.display="none";});
            }

            setTimeout(function () {
                button.html(buttonsave);
            },500);

        });

    });

    jQuery(".send-parasut-invoice").on('click', function (ev) {
        ev.preventDefault();

        var button = jQuery(this),
            buttonsave = button.html(),
            loadinghtml = "<img style='margin-top: 3px;' src='"+ loading_img +"' width='20' class='grispin'>"
        ;

        button.html(loadinghtml);

        var data = {
            'action': 'send_invoice_parasut',
            'id': button.data('id')
        };

        jQuery.post(ajaxurl, data, function (response) {
            response = JSON.parse(response);
            if(typeof response.error !== 'undefined')
            {
                alert(response.error);
                setTimeout(function () {
                    button.html(buttonsave);
                },500);
            } else {
                window.location.reload();
            }

        });

    });

    jQuery(".complete-parasut-invoice").on('click', function (ev) {
        ev.preventDefault();

        var button = jQuery(this),
            buttonsave = button.html(),
            loadinghtml = "<img style='margin-top: 3px;' src='"+ loading_img +"' width='20' class='grispin'>"
        ;

        button.html(loadinghtml);

        var data = {
            'action': 'grilabsparasut_create_earchive',
            'id': button.data('id')
        };

        jQuery.post(ajaxurl, data, function (response) {
            response = JSON.parse(response);
            if(typeof response.error !== 'undefined')
            {
                alert(response.error);
                setTimeout(function () {
                    button.html(buttonsave);
                },500);
            } else {
                window.location.reload();
            }

        });

    });

    jQuery(".go-formal-invoice").on('click', function () {

        var button = jQuery(this),
            buttonsave = button.html(),
            loadinghtml = "<img style='margin-top: 3px;' src='"+ loading_img +"' width='20' class='grispin'>"
        ;

        button.html(loadinghtml);

        var data = {
            'action': 'get_formal_invoice',
            'id': button.data('id')
        };

        jQuery.post(ajaxurl, data, function (response) {
            response = JSON.parse(response);
            if(typeof response.error !== 'undefined')
            {
                alert(response.error);
                setTimeout(function () {
                    button.html(buttonsave);
                },100);
            } else {
                var w = window.open( response.url, '_blank' );
                setTimeout(function () {
                    button.html(buttonsave);
                },100);
                w.focus();
            }
        });
    });

    jQuery(".send-email-invoice").on('click', function () {

        var button = jQuery(this),
            buttonsave = button.html(),
            loadinghtml = "<img style='margin-top: 3px;' src='"+ loading_img +"' width='20' class='grispin'>"
        ;

        button.html(loadinghtml);

        var data = {
            'action': 'send_email_formal',
            'id': button.data('id')
        };

        jQuery.post(ajaxurl, data, function (response) {
            response = JSON.parse(response);
            if(typeof response.error !== 'undefined')
            {
                alert(response.error);
                setTimeout(function () {
                    button.html(buttonsave);
                },100);
            } else {
                alert("Gönderildi!");
                setTimeout(function () {
                    button.html(buttonsave);
                },100);
            }
        });
    });

});