$(document).ready(function () {
    $("body").on("click", ".remove", remove);
    $("body").on("blur change keyup focus", '.fcheckin, .fcheckout, .fnopax, .fnonights,.frate,.roe', calFood);
    // $("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 calFood() {
        var tr = $(this).closest("tr"),
                fcheckin = tr.find(".fcheckin").val(),
                fcheckout = tr.find(".fcheckout").val();
        if (fcheckin != "0000-00-00" && fcheckout != "0000-00-00") {
            var days = daysdifference(fcheckin, fcheckout);
//        alert(days);
        }
        tr.find(".fnonights").val(days);
         var nopax = tr.find(".fnopax").val();
        var frate = tr.find(".frate").val();
        var famount = days * frate * nopax;
        tr.find(".famount").val(famount);
        var fpax = 0, fnights = 0, famt = 0;
        $(".fnonights").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                fnights += parseFloat(this.value);
            }
        });
        $(".fnopax").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                fpax += parseFloat(this.value);
            }
        });
        $(".famount").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                famt += parseFloat(this.value);
            }
        });
        var roe = $(".roe").val();
        var foodinr = Math.round(famt * roe,0);
        $(".foodinramt").val(foodinr);
        $(".foodnights").val(fnights);
        $(".foodpax").val(fpax);
        $(".foodamt").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("foodsl_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();

    }
});
