$(document).ready(function () {

    $("body").on("click", '.remove', remove);
    $(".tbody").on("change", '.city', getHotel);
    $(".tbody").on("change blur", '.checkin, .checkout', chkPrevDate);
    // $("body").on("change", '.hotelnm', getHotelCatg);
    $("body").on("keyup", '.rate', getcalc);
    $("body").on("change blur", '.adultchg, .adultpax, .childchg, .childpax, .chwobed, .chwobedpax, .infantpax, .infantchg', calTran);
    $("body").on("blur change", '.checkin, .checkout', calHotel);

    function calTran() {
        var adultchg = $(".adultchg").val() || 0,
            adultpax = $(".adultpax").val() || 0,
            childchg = $(".childchg").val() || 0,
            childpax = $(".childpax").val() || 0,
            chwobed = $(".chwobed").val() || 0,
            chwobedpax = $(".chwobedpax").val() || 0,
            infantchg = $(".infantchg").val() || 0,
            infantpax = $(".infantpax").val() || 0,
            
            totadult = parseFloat(adultchg) * parseFloat(adultpax); 
            totchild = parseFloat(childchg) * parseFloat(childpax); 
            totchildwb = parseFloat(chwobed) * parseFloat(chwobedpax); 
            totinfant = parseFloat(infantchg) * parseFloat(infantpax); 

            $('.totadult').val(totadult);
            $('.totchildbed').val(totchild);
            $('.totchildwobed').val(totchildwb);
            $('.totinfant').val(totinfant);

            var netamt = parseFloat(totadult) + parseFloat(totchild) + parseFloat(totchildwb) + parseFloat(totinfant);
            $('.netamt').val(netamt);
        }

    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 chkPrevDate() {
    var tr = $(this).closest("tr");
    var checkin = tr.find('.checkin').val();
    var checkout = tr.find('.checkout').val();

    if (checkin && checkout) {
        // Convert strings to Date objects for proper comparison
        var checkinDate = new Date(checkin);
        var checkoutDate = new Date(checkout);

        if (checkoutDate < checkinDate) {
            alert('Invalid Checkout Date — must be after Check-in');
            tr.find('.checkout').val('');
            tr.find('.checkout').focus();
        }
    }
}


    function calHotel() {
        var tr = $(this).closest("tr"),
            fcheckin = tr.find(".checkin").val(),
            fcheckout = tr.find(".checkout").val();
        if (fcheckin != "0000-00-00" && fcheckout != "0000-00-00") {
            var days = daysdifference(fcheckin, fcheckout);
//        alert(days);
        }
        tr.find(".nonights").val(days);
    }
    
    
    function getcalc() {
        var tr = $(this).closest("tr"),
        
        days = tr.find(".nonights").val();
        qty = tr.find(".qty").val(),
        rate = tr.find(".rate").val();

        amount = (days * qty * rate).toFixed(2);

        tr.find(".amount").val(amount);

    }


    function getHotel() {
        var tr = $(this).closest("tr");
        var city = tr.find(".city").val();
        $.post('getHotel_code.php', {city: city}, function (data) {
            tr.find(".hotelnm").html(data);
        });
    }


    // function getHotelCatg() {
    //     var hotelnm = $(".hotelnm").val();
    //     $.post('getHotelCatg.php', {hotelnm: hotelnm}, function (data) {
    //         $(".hotelcatg").html(data);
    //     });
    // }


    $('#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('quotepax_add.php', {ctr: ctr, hide: hide_check}, function (data) {
            $(data).appendTo('.tbody');
            ctr++;
            $('#items').val(ctr);
            $(detail).select2();
        });
    }

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


});