$(document).ready(function () {
    $("body").on("click", '.proceed', checkvalues);
    $("body").on("click", '#submit', insert);
    $("body").on('keyup change click', '.tsearch, .proceed', searchschl);
    $("body").on('change click', '.allchk', chkUncheck);



    function insert() {
//        $(".myTable").find('input[type="checkbox"]:checked').each(function(){
        $(".myTable").find('tr').each(function () {
            var row = $(this);
            if (row.find('input[type="checkbox"]').is(':checked')) {
                var accode = row.find('.accode').val(),
                        ncust = row.find('.scust').val();
                row.find('.saccode').val(accode);
                row.find('.ncust').val(ncust);
            }
        });
    }


    function checkvalues() {
        var exam = $('.exam').val(),
                section = $('.section').val(),
                session = $('.session').val();

        if (exam != '' && section != '' && session != '') {

            $.post('checkinv.php', {exam: exam, section: section, session: session}, function (data) {
                if (data.exam == 1) {
                    alert('This Selected Exam + Section + Session Already Exist' + data.exam);
                } else if (data.rates == '') {
                    alert('Rates For This Exam + Section Not Exist' + data.rates);
                } else if (data.demnd == '') {
                    alert('Record Not found' + data.demnd);
                } else {
                    
                    $.post('gendmnd_school.php', {fexam: exam, section: section, session: session}, function (data) {
                        $(".mySearchBody").empty();
                        $(".mySearchBody").html(data);
                        var ctr = $('.myTable').find('.scount').val();
                        $('#items').val(ctr);
                    });
                    
                    $('#custmodal').modal('show');
                    $("#custmodal").on('shown.bs.modal', function () {
                        $(".tsearch").val("");
                        $(".tsearch").focus();

                    });
                }
            }, 'json');
        }
    }

    function searchschl() {
        var search = $(".tsearch").val(),
                section = $(".section").val(),
                session = $(".session").val();
        $.post('schl_srch.php', {search: search, section: section, session: session}, function (data) {
            $(".mySearchBody").html(data);
        });
    }


    function chkUncheck() {
        $(".mcheck").prop('checked', $(this).prop('checked'));
    }
});