//  - 2018
jQuery(function () {

    jQuery("body").on('click', ".go-parasut-product", function (ev) {
        ev.preventDefault();
        var
            button = jQuery(this);

        jQuery.post(ajaxurl, {
            'action': 'get_product_parasut_url',
            'id': button.data('id')
        }, function(response) {
            if(response==='false')
            {
                alert('Ürün bulunamadı.');
            } else {
                window.open(response, '_blank');
            }
        });

    });

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

    jQuery("body").on('click', ".link-parasut-product", function (ev) {
        ev.preventDefault();
        var popupWindow=null;
        var prod_id = jQuery(this).data('id');
        popupWindow = window.open(grilabs_baseurl + '/wc-api/productmatch?for=' + prod_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_product',
            'id': button.data('id')
        };

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

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

        });

    });

    jQuery("body").on('click', ".unlink-parasut-product", 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_product',
            'id': button.data('id')
        };

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

        });


    });


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

    function re_sort_fee_rows() {
        jQuery('#fees-body tr').each(function (value) {
            jQuery(this).find('.num-label').text("#"+value);
            jQuery(this).find('.name-text input').attr("name", "grilabsparasut_defined_fees_array["+ value +"][name]");
            jQuery(this).find('.pid-text input').attr("name", "grilabsparasut_defined_fees_array["+ value +"][pid]");
        })
    }

    function append_new_fee_row() {
        var $cloneRow = jQuery("#original-row");
        var $newRow = $cloneRow.clone();

        var total_rows = jQuery('#fees-body tr').length;
        $newRow[0].innerHTML = $newRow[0].innerHTML.replace('{$num}', total_rows);
        jQuery('#fees-body tr:last:not(\'#last-row\')').after($newRow);
    }

    jQuery('#new-parasut-fee').on('click', function (ev) {
        ev.preventDefault();
        append_new_fee_row();
        re_sort_fee_rows();
    });

    jQuery('body').on('click', '.remove-fee-row', function () {
        if(jQuery('#fees-body tr').length == 1)
        {
            append_new_fee_row();
        }
        jQuery(this).closest('tr').remove();
    });

    jQuery('body').on('click', '.select-fee-product', function (ev) {
        ev.preventDefault();
        var rand = Math.random() + "";
        var rand_id = "fee" + rand.substr(2,7);

        jQuery(this).attr('id', rand_id);
        var popupWindow=null;
        popupWindow = window.open(grilabs_baseurl + '/wc-api/productmatch?type=fee&id='+rand_id,'name','width=700,height=500');
        popupWindow.focus();
    });

})