$(document).ready(function () {
    $("body").on("click", ".remove", remove);
    $("body").on("blur change keyup focus", '.lcheckin, .lcheckout, .lnopax, .lnonights,.lrate,.roe', calLaund);
    // $("body").on("blur keyup", ".visano, .paxname, .period, .type", newPax);
    $("tbody").on("change blur", ".doi", getDoe);

    $('.visapurch').click(function () {
        $('.pvisa').toggle("slide");
    });
    function daysdifference(firstDate, secondDate) {
        var startDay = new Date(firstDate);
        var endDay = new Date(secondDate);

// Determine the time difference between two dates     
        var millisBetween = startDay.getTime() - endDay.getTime();

// Determine the number of days between two dates  
        var days = millisBetween / (1000 * 3600 * 24);

// Show the final number of days between dates     
        return Math.round(Math.abs(days));
    }
    
    function calLaund() {
        var tr = $(this).closest("tr"),
                lcheckin = tr.find(".lcheckin").val(),
                lcheckout = tr.find(".lcheckout").val();

        if (lcheckin != "0000-00-00" && lcheckout != "0000-00-00") {
            var days = daysdifference(lcheckin, lcheckout);
        }

        tr.find(".lnonights").val(days);
        var nopax = tr.find(".lnopax").val();
        var frate = tr.find(".lrate").val();
        var famount = nopax * frate;
        tr.find(".lamount").val(famount);
        var fpax = 0, fnights = 0, famt = 0;
        $(".lnonights").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                fnights += parseFloat(this.value);
            }
        });
        $(".lnopax").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                fpax += parseFloat(this.value);
            }
        });
        $(".lamount").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                famt += parseFloat(this.value);
            }
        });
        var roe = $(".roe").val();
        var laundinr = Math.round(famt * roe,0);
        $(".laundinramt").val(laundinr);

        $(".laundnights").val(fnights);
        $(".laundpax").val(fpax);
        $(".laundamt").val(famt);

    }

    function getDoe() {
        var tr = $(this).closest("tr");
        var inputDate = new Date(tr.find(".doi").val());
        var resultDate = new Date(inputDate);
        resultDate.setFullYear(inputDate.getFullYear() + 10);
        resultDate.setDate(resultDate.getDate() - 1);
        var resultDateString = resultDate.toISOString().split("T")[0];
        tr.find(".doe").val(resultDateString);
    }

    $("#addnew").click(function () {
        AddRows();
    });

    function AddRows() {
        var ctr = $("#items").val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        var detail = ".fetchpart_" + ctr;

        $.post("laundsl_add.php", {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo(".tbody");
            ctr++;
            $("#items").val(ctr);
            $(detail).select2();
        });
//    newPax();
    }

    function remove() {
        $(this).closest("tr").remove();
        calFood();

    }
});
