$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    // $("body").on('blur', '.mon, .deptcode,.yearno', chkattendance);
    $("body").on('click', '.autoattend', chkattendance);
    $("body").on('click', '.manualattend', getattendanceManual);
    $("body").on('blur', '#sun, #ho', getholiday);
    $("body").on('change keyup ', '#sun, #ho, .ab, .hf, .pr, .ot, .pd, .sun, .ho', caltotdays);
    $("body").on('click', '#hedit', disabled);

    $("#example1").DataTable();

    function getattendanceManual() {
        var deptcode = $('.deptcode').val(),
            vouchno = $('.vouchno').val(),
            addflag = $('.addflag').val(),
            yearno = $('.yearno').val(),
            monname = $('.monname').val();

        if (!deptcode) {
            alert("Please select Department.");
            $('.deptcode').select2('open'); // opens select2 dropdown
            return;
        }
        if (!monname) {
            alert("Please select Month.");
            $('.monname').select2('open');
            return;
        }
        if (!yearno) {
            alert("Please select Year.");
            $('.yearno').focus(); // for text input
            return;
        }

        if (deptcode != '' && monname != '' && yearno != '') {
            $(".tabledata").val("");
            $.post('auto_empmanual_attend.php', { deptcode: deptcode, vouchno: vouchno, addflag: addflag, monname: monname, yearno: yearno }, function (data) {
                $(".tabledata").html(data);

                $('.deptcode, .monname').on('select2:opening select2:unselecting', function (e) {
                    e.preventDefault(); // Block dropdown
                });

                // Optional: style it to look disabled
                $('.deptcode, .monname').next('.select2-container').css('pointer-events', 'none');
                $('.deptcode, .monname').next('.select2-container').find('.select2-selection')
                    .css('background-color', '#e9ecef');

                // IMPORTANT: Keep the select enabled so the value submits
                $('.deptcode, .monname').prop('disabled', false);
                $('.yearno').prop('readonly', true);
                $('.msun').prop('readonly', true);
                $('.mho').prop('readonly', true);

                $(".autoattend").hide();
                $(".manualattend").hide();

                getholiday();
            });
        }
    }

    disabled();
    function disabled() {
        var addflag = $('.addflag').val();
        if (addflag != 1) {
            //            $(".monname").attr("readonly", true);
            $("#sun").attr("readonly", true);
            $("#ho").attr("readonly", true);

            $('.deptcode, .monname').on('select2:opening select2:unselecting', function (e) {
                e.preventDefault(); // Block dropdown
            });

            // Optional: style it to look disabled
            $('.deptcode, .monname').next('.select2-container').css('pointer-events', 'none');
            $('.deptcode, .monname').next('.select2-container').find('.select2-selection')
                .css('background-color', '#e9ecef');

            // IMPORTANT: Keep the select enabled so the value submits
            $('.deptcode, .monname').prop('disabled', false);
            $('.yearno').prop('readonly', true);

        }
    }

    function caltotdays() {
        var tr = $(this).closest("tr"),
            totpr = tr.find(".pr").val() || 0,
            ot = tr.find(".ot").val() || 0,
            ho = tr.find(".ho").val() || 0,
            sun = tr.find(".sun").val() || 0,
            ab = tr.find(".ab").val() || 0,
            hf = tr.find(".hf").val() || 0,
            pd = tr.find(".pd").val() || 0;

        var total = parseFloat(totpr) + parseFloat(ot) + parseFloat(pd) + parseFloat(ho) + parseFloat(sun) - parseFloat(ab);

        //        if (total > 31) {
        //            alert("Total No. Of Days Cannaot Be Greater Than 31");
        //            tr.find(".ot").val();
        //            tr.find(".ot").focus();
        //            tr.find(".ot").select();
        //        } else {
        tr.find(".totdays").val(total);
        //        }

        $(".tbody").find('tr').each(function () {
            var tr = $(this);
                totpr = tr.find(".pr").val() || 0,
                ot = tr.find(".ot").val() || 0,
                ho = tr.find(".ho").val() || 0,
                sun = tr.find(".sun").val() || 0,
                ab = tr.find(".ab").val() || 0,
                hf = tr.find(".hf").val() || 0,
                pd = tr.find(".pd").val() || 0;

            var total = parseFloat(totpr) + parseFloat(ot) + parseFloat(pd) + parseFloat(ho) + parseFloat(sun) - parseFloat(ab);

            //        if (total > 31) {
            //            alert("Total No. Of Days Cannaot Be Greater Than 31");
            //            tr.find(".ot").val();
            //            tr.find(".ot").focus();
            //            tr.find(".ot").select();
            //        } else {
            tr.find(".totdays").val(total);
        });


    }

    $("#submit").hide();
    $("#finishTab").click(function () {
        var valid = true;
        $(".tbody").find('tr').each(function () {
            var row = $(this);
            var pr = row.find('.pr').val();
            if (pr > 31) {
                alert('Present Cannot Be Greater Than 31');
                valid = false;
                row.find('.ho').focus();
                return false;
            }
        });

        if (valid) {
            var isValid = true;
            if (isValid) {
                $("#submit").show();
                $("#submit").click();
            }
        }
    });

    $('.sun').keyup(function () {
        var sun;
        $(".sun").each(function () {
            if (this.value > 5) {
                alert("No. Of Sundays In A Month Cannot Be Greater Than 5");
                $(this).val(0);
            }
        });
    });


    function getholiday() {
        var sun = $("#sun").val();
        var ho = $("#ho").val();

        if (sun > 5) {
            alert("No. Of Sundays In A Month Cannot Be Greater Than 5");
            $("#sun").focus();
            $("#sun").val(0);
            return false;
        }

        $('.tbody tr').each(function () {
            var row = $(this);
            if (sun > 0) {
                $(".sun").val(sun);
            }
            if (ho > 0) {
                $(".ho").val(ho);
            }
        });

        caltotdays();
    }

    function chkattendance() {
        var mon = $(".mon").val(),
            deptcode = $(".deptcode").val(),
            yearno = $(".yearno").val(),
            vouchno = $(".vouchno").val();

        if (!deptcode) {
            alert("Please select Department.");
            $('.deptcode').select2('open'); // opens select2 dropdown
            return;
        }
        if (!mon) {
            alert("Please select Month.");
            $('.monname').select2('open');
            return;
        }
        if (!yearno) {
            alert("Please select Year.");
            $('.yearno').focus(); // for text input
            return;
        }


        if (deptcode != '' && mon != '' && yearno != '') {
            $.post("auto_chkattendance.php", { mon: mon, deptcode: deptcode, yearno: yearno, vouchno: vouchno }, function (data) {
                if (data != '') {
                    alert(data);
                    $('#ssubmit').hide();
                    $('#scancel').hide();
                } else {
                    getdata();
                    $('#ssubmit').show();
                    $('#scancel').show();
                }
            });
        }
    }

    $('.deptname').change(function () {
        var deptname = $(".deptname").val();
        $(".deptcode").val(deptname);
    });



    function getdata() {
        var deptcode = $('.deptcode').val(),
            vouchno = $('.vouchno').val(),
            addflag = $('.addflag').val(),
            yearno = $('.yearno').val(),
            monname = $('.monname').val();

        if (deptcode != '' && monname != '' && yearno != '') {
            $(".tabledata").val("");
            $.post('auto_empdata_attend.php', { deptcode: deptcode, vouchno: vouchno, addflag: addflag, monname: monname, yearno: yearno }, function (data) {
                $(".tabledata").html(data);

                $('.deptcode, .monname').on('select2:opening select2:unselecting', function (e) {
                    e.preventDefault(); // Block dropdown
                });

                // Optional: style it to look disabled
                $('.deptcode, .monname').next('.select2-container').css('pointer-events', 'none');
                $('.deptcode, .monname').next('.select2-container').find('.select2-selection')
                    .css('background-color', '#e9ecef');

                // IMPORTANT: Keep the select enabled so the value submits
                $('.deptcode, .monname').prop('disabled', false);
                $('.yearno').prop('readonly', true);
                $('.msun').prop('readonly', true);
                $('.mho').prop('readonly', true);

                $(".autoattend").hide();
                $(".manualattend").hide();
                
                getholiday();

            });
        }
    }

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

    function AddRows() {
        var ctr = $('#items').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        $.post('attendmonth_add.php', { ctr: ctr, hide: hide_check }, function (data) {
            $(data).appendTo('.tabledata');
            ctr++;
            $('#items').val(ctr);
        });
    }
    $('#addnew').click(function () {
        AddRows();
    });
});


