$(document).ready(function () {
    $("body").on("change", ".empname", GetDeptnDiv);
    $('body').on('change blur', '.monname, .yearno', getMonDate);

    getMonDate();
    function getMonDate() {
        var month = parseInt($(".monname").val());
        var year = parseInt($(".yearno").val());
        if (month >= 1 && month <= 12) {
            var lastDate = new Date(year, month, 0); // 0 gets last day of given month

            var yyyy = lastDate.getFullYear();
            var mm = String(lastDate.getMonth() + 1).padStart(2, '0');
            var dd = String(lastDate.getDate()).padStart(2, '0');

            var formattedDate = yyyy + '-' + mm + '-' + dd;
            $(".procdt").val(formattedDate);
        }
    }


    function GetDeptnDiv() {
        var empcode = $(".empname").val(),
            data = { empcode: empcode };

        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "get_deptndiv.php",
            data: data,
            success: function (response) {
                var m = response;
                $(".depart").val(m.deptcode).trigger('change');
                $(".process").val(m.process).trigger('change');
            },
            error: function (request, textStatus, errorThrown) {

            }
        });

    }

});

