$(document).ready(function () {
    $("body").on("keyup blur change", ".rate", calMain);
    $("body").on("click", ".getChal", GetChalData);
    $("body").on("click", ".fetChal", FetChalData);
    $("body").on("click", ".selall, .Sselall", getAmt);

    $("#Sselall").click(function () {
        $(".selall").prop('checked', $(this).prop('checked'));
    });
    

    $("form").submit(function () {
        var date = $('.vouchdt').val();
        var stdate = $('.zstdate').val();
        var endate = $('.zenddate').val();
        var amount = $('.billamt').val();

        if (amount <= 0) {
            alert("Amount Cannot Be 0 (Zero)");
            return false;
        }
        if (date != '') {
            if (date >= stdate && date <= endate) {
            } else {
                alert("Date Does Not Belong To Finacial Year");
                $(".vouchdt").val("0000-00-00");
                $(".vouchdt").focus();
                return false;
            }
        }
    });

    function getAmt() {
        var tamt = 0;
        var checkedBoxes = $(".selall:checked"); // Get all checked checkboxes
        if (checkedBoxes.length === 0) {
            $(".billamt").val("0.00"); // Set the amount to 0 if no checkbox is checked
            return; // Exit the function
        }
        checkedBoxes.each(function () {
            var amount = $(this).data("amount"); 
            if (!isNaN(amount) && amount.length != 0) {
                tamt += parseFloat(amount);
            }
        });
        $(".billamt").val(tamt.toFixed(2)); // Update the bill amount
    }
    
    // Event listener to call `getAmt` on checkbox change
    $(".selall").on("change", function () {
        getAmt();
    });
    $(".Sselall").on("change", function () {
        getAmt();
    });

    GetChalData();
    function GetChalData() {
        // var vouchdt = $(".vouchdt").val();
        var party = $(".party").val();
        var vessel = $(".vessel").val();
        var vouchno = $(".vouchno").val();
        var vouchdt = $(".vouchdt").val();
        var addflag = $(".addflag").val();
            $.post('get_challan_data.php', {addflag: addflag, party : party, vouchno: vouchno, vouchdt: vouchdt,vessel: vessel}, function (data) {
                $(".tbodyy").html(data);
            });
    }
    function FetChalData() {
        // var vouchdt = $(".vouchdt").val();
        var party = $(".party").val();
        var vessel = $(".vessel").val();
        var vouchno = $(".vouchno").val();
        var vouchdt = $(".vouchdt").val();
        var fetFlag = 1;
        $(".tbodyy").html("");

            $.post('fet_challan_data.php', {fetFlag: fetFlag,party : party, vouchno: vouchno, vouchdt: vouchdt,vessel: vessel}, function (data) {
                $(".tbodyy").html(data);
            });
    }
    

    var addflag = $(".addflag").val(),
            par = $(".par").val();
    if (addflag == 1) {
        if (par == "A") {
            $(".PartySrchTbl").show();
            $(".party").focus();
        }
    }

    $('.srchparty').on('keydown', function (e) {
        var keyCode = e.keyCode || e.which;
        if (keyCode == 40) {
            $(".PTbody tr:visible:first").find(".disblsrchparty").focus();
            e.preventDefault();
            return false;
        }
    });


    $(".PartySrchTbl").hide();
    $('.srchparty').on('keyup', function (e) {
        var value = $(".srchparty").val().toLowerCase();
        $(".PartySrchTbl").show();
        $(".PTbody tr").filter(function () {

            var tr = $(this).closest("tr");
            if (tr.find(".disblsrchparty").val().toLowerCase().substr(0, value.length) == value.substr(0, value.length)) {
                tr.show();
            } else {
                tr.hide();
            }

        });

    });

    $('.PartySrchTbl input').on('keydown', function (e) {

        var keyCode = e.keyCode || e.which;
        if (keyCode == 40) {
            $(this).closest("tr").nextAll().filter(":visible:first").find('.disblsrchparty').focus();
            e.preventDefault();
            return false;
        }

        if (keyCode == 38) {
            $(this).closest("tr").prevAll().filter(":visible:first").find('.disblsrchparty').focus();
            e.preventDefault();
            return false;
        }

        if (keyCode == 13) {
            var tr = $(this).closest("tr"),
                    partyname = tr.find(".disblsrchparty").val();
            $(".PartySrchTbl").hide();

            $(".party").val(partyname);
            e.preventDefault();
            return false;
        }


        if (keyCode != 13 || keyCode != 18 || keyCode != 38) {
            e.preventDefault();
            return false;
        }
    });


    $(".disblsrchparty").click(function () {
        var tr = $(this).closest("tr"),
                partyname = tr.find(".disblsrchparty").val();
        $(".PartySrchTbl").hide();

        $(".party").val(partyname);
    });

    $("#hedit").click(function () {
        $(".pdf").hide();
    });

            function calMain() {
                var rate = parseFloat($('.rate').val()); // Get the rate value
                var nohrs = parseFloat($('.nohrs').val()); // Get the rate value
                var nomin = parseFloat($('.nomin').val()); // Get the rate value
                var totalHours = nohrs + nomin / 60;
                var amount = rate * totalHours;
                $('.amount').val(amount.toFixed(2)); // Set the amount with 2 decimal precision
            }

});