$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    $("body").on("click", '.proceed', shfunc);
    $("body").on('dblclick', '.schltab', tableval);
    $("body").on('focus', '.custcode', showschl);
    $("body").on('keyup change', '.tsearch', searchschl);
    $("body").on("keyup change blur", '.first,.second,.third,.fourth,.fifth,.sixth,.seventh,.eight,.ninth,.tenth,.pqrelm', calpack);
    $("body").on("keyup change blur", '.amount,.amount2,.amount3,.nopack,.stamt,.otheramt,.billamt,.first,.second,.third,.fourth,.fifth,.sixth,.seventh,.eight,.ninth,.tenth,.pqrelm,.advance,.balamt', cal);
    $("body").on("change", '.exam,.subject,.subject2,.subject3', getrate);
    $("body").on("focus", '.third', autoaddpri);
    $("body").on("focus", '.ninth', autoaddsec);
    $("body").on("focus", '.srno3', autoaddpre);


    $('.emailbut').click(function () {

        if (confirm("Are you sure you want to send an E-mail")) {

            var scode = $('.scode').val(),
                vouch = $('.vouchno').val(),
                    data = {scode: scode, vouch: vouch};

            $.ajax({
                type: 'GET',
                dataType: 'text',
                url: "send_email.php",
                data: data,
                success: function (response) {
                   alert(response);
                },
                error: function (request, textStatus, errorThrown) {
                }
            });
//            var vouchno = $('.vouchno').val();
//            $.post('invoice_email_print.php', {vouchno: vouchno, email: values}, function (data) {
//            });
        }else {
            alert("Mail Not Send");
        }
    });


    function autoaddpri() {
        if ($(this).closest('tr').is(":last-child")) {
            AddRows();
        }
    }

    function autoaddsec() {
        if ($(this).closest('tr').is(":last-child")) {
            AddRows2();
        }
    }

    function autoaddpre() {
        if ($(this).closest('tr').is(":last-child")) {
            AddRows3();
        }
    }

    function cal() {
        var tr = $(this).closest('tr');
        var first = tr.find('.first').val(),
                second = tr.find('.second').val(),
                third = tr.find('.third').val(),
                fourth = tr.find('.fourth').val(),
                fifth = tr.find('.fifth').val(),
                sixth = tr.find('.sixth').val(),
                seven = tr.find('.seventh').val(),
                eight = tr.find('.eight').val(),
                ninth = tr.find('.ninth').val(),
                tenth = tr.find('.tenth').val(),
                pqrelm = tr.find('.pqrelm').val(),
                rt1 = tr.find('.rt1').val(),
                rt2 = tr.find('.rt2').val(),
                rt3 = tr.find('.rt3').val(),
                rt4 = tr.find('.rt4').val(),
                rt5 = tr.find('.rt5').val(),
                rt6 = tr.find('.rt6').val(),
                rt7 = tr.find('.rt7').val(),
                rt8 = tr.find('.rt8').val(),
                rt9 = tr.find('.rt9').val(),
                rt10 = tr.find('.rt10').val(),
                rtprelm = tr.find('.rtprelm').val(),
                famt = first * rt1,
                samt = second * rt2,
                thamt = third * rt3,
                foamt = fourth * rt4,
                fiveamt = fifth * rt5,
                sixamt = sixth * rt6,
                sevamt = seven * rt7,
                eigamt = eight * rt8,
                nineamt = ninth * rt9,
                tenamt = tenth * rt10,
                pqrelmamt = pqrelm * rtprelm,
                pamt = parseFloat(famt) + parseFloat(samt) + parseFloat(thamt) + parseFloat(foamt),
                secondamt = parseFloat(fiveamt) + parseFloat(sixamt) + parseFloat(sevamt) + parseFloat(eigamt) + parseFloat(nineamt) + parseFloat(tenamt);


        tr.find('.amount').val(pamt);
        tr.find('.amount2').val(secondamt);
        tr.find('.amount3').val(pqrelmamt);


        var amt = 0,
                amt2 = 0,
                amt3 = 0;
        $('.amount').each(function () {
            amt += parseFloat(this.value);
        });
        $('.amount2').each(function () {
            amt2 += parseFloat(this.value);
        });
        $('.amount3').each(function () {
            amt3 += parseFloat(this.value);
        });

        if (amt != 0) {
            $('.subtotal').val(amt);
        } else if (amt2 != 0) {
            $('.subtotal').val(amt2);
        } else if (amt3 != 0) {
            $('.subtotal').val(amt3);
        }


        var stamt = $('.stamt').val(),
                otheramt = $('.otheramt').val(),
                nopck = $('.nopack').val(),
                packrt = $('.packrate').val(),
                packamt = nopck * packrt,
                totamt = Math.round(parseFloat(amt) + parseFloat(amt2) + parseFloat(amt3) + parseFloat(stamt) + parseFloat(otheramt) + parseFloat(packamt));

        $('.billamt').val(totamt);
        var advance = $(".advance").val();
        var balamt = totamt - advance;
        $('.balamt').val(balamt);
        $('.packamt').val(packamt);


    }

    function getrate() {
        var exam = $('.exam').val(),
                tr = $(this).closest('tr'),
                subj = tr.find('.subject').val(),
                subj1 = tr.find('.subject2').val(),
                subj2 = tr.find('.subject3').val();

        $.post('get_rate.php', {exam: exam, subj: subj, subj1: subj1, subj2: subj2}, function (data) {
            if (data.sec == 'P') {
                tr.find('.rt1').val(data.rt1);
                tr.find('.rt2').val(data.rt2);
                tr.find('.rt3').val(data.rt3);
                tr.find('.rt4').val(data.rt4);
                $('.packrate').val(data.ppackrate);
            } else if (data.sec == 'S') {
                tr.find('.rt5').val(data.rt5);
                tr.find('.rt6').val(data.rt6);
                tr.find('.rt7').val(data.rt7);
                tr.find('.rt8').val(data.rt8);
                tr.find('.rt9').val(data.rt9);
                tr.find('.rt10').val(data.rt10);
                $('.packrate').val(data.spackrate);
            } else {
                tr.find('.rtprelm').val(data.rtprelm);
                $('.packrate').val(data.mpackrate);
            }

        }, 'json');

    }

    var section = $('.section').val();
    if (section == '') {
        $('.primary').hide();
        $('.secondary').hide();
        $('.prelium').hide();
        $('.cal').hide();
    } else if (section == 'P') {
        $('.primary').show();
        $('.secondary').hide();
        $('.prelium').hide();
        $('.cal').show();
    } else if (section == 'S') {
        $('.secondary').show();
        $('.prelium').hide();
        $('.primary').hide();
        $('.cal').show();
    } else if (section == 'M') {
        $('.prelium').show();
        $('.primary').hide();
        $('.secondary').hide();
        $('.cal').show();
    }

    function shfunc() {
        var section = $('.section').val(),
                session = $('.session').val(),
                exam = $('.exam').val(),
                medium = $('.medium').val();
        if (section != '' && session != '' && medium != '' && exam != '') {

            if (section == 'P') {
                $('.primary').show();
                $('.cal').show();
            } else {
                $('.primary').hide();
            }
            if (section == 'S') {
                $('.secondary').show();
                $('.cal').show();
            } else {
                $('.secondary').hide();
                $('.cal').show();
            }
            if (section == 'M') {
                $('.prelium').show();
                $('.cal').show();
            } else {
                $('.prelium').hide();
            }
        }
    }

    $("#schlmodal").on('shown.bs.modal', function () {
        $(".tsearch").val("");
        $(".tsearch").focus();
        searchschl();
    });
    function showschl() {
        var code = $('.custcode').val();
        if (code == '') {
            $(".tsearch").val("");
            $(".tsearch").focus();
            $("#schlmodal").modal("show");
        }

    }

    function tableval() {
        var tr = $(this).closest("tr");
        var cust = tr.find(".scust").val(),
                accode = tr.find(".accode").val(),
                med = tr.find(".medium").val(),
                add1 = tr.find(".sadd1").val(),
                add2 = tr.find(".sadd2").val(),
                add3 = tr.find(".sadd3").val();
        $(".custcode").val(cust);
        $(".school").val(accode).trigger('change');
        $(".medium").val(med).trigger('change');
        $(".add1").val(add1);
        $(".add2").val(add2);
        $(".add3").val(add3);
        $("#schlmodal").modal("hide");
        $(".medium").focus();
    }

    function searchschl() {
        var search = $(".tsearch").val();
        $.post('get_school.php', {search: search}, function (data) {
            $(".myTable").html(data);
        });
    }


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



    $('#addnew').click(function () {
        AddRows();
    });
//    $('.third').focus(function () {
//        if ($(this).closest('tr').is(":last-child")) {
//            AddRows();
//        }
//    });
    function AddRows() {
        var ctr = $('#items').val();
        var medium = $('.medium').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        var detail = ".fetchpart_" + ctr;
        $.post('invprim_add.php', {ctr: ctr, hide: hide_check, medium: medium}, function (data) {
            $(data).appendTo('.tbody');
            ctr++;
            $('#items').val(ctr);
            $(detail).select2();
        });
    }
    $('#addnew2').click(function () {
        AddRows2();
    });
//    $('.ninth').focus(function () {
//        if ($(this).closest("tr").is(":last-child")) {
//            AddRows2();
//        }
//    });
    function AddRows2() {
        var ctr = $('#items2').val();
        var medium = $('.medium').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        var detail = ".fetchpart_" + ctr;
        $.post('invsec_add.php', {ctr: ctr, hide: hide_check, medium: medium}, function (data) {
            $(data).appendTo('.tbody2');
            ctr++;
            $('#items2').val(ctr);
            $(detail).select2();
        });
    }

    $('#addnew3').click(function () {
        AddRows3();
    });

    function AddRows3() {
        var ctr = $('#items3').val();
        var medium = $('.medium').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
        var detail = ".fetchpart_" + ctr;
        $.post('invpril_add.php', {ctr: ctr, hide: hide_check, medium: medium}, function (data) {
            $(data).appendTo('.tbody3');
            ctr++;
            $('#items3').val(ctr);
            $(detail).select2();
        });
    }

    function calpack() {
        var section = $('.section').val();
        if (section == 'P') {
            var frs = 0,
                    sc = 0,
                    th = 0,
                    four = 0,
                    totpack = 0;
            $(".first").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    frs += 1;
                }
            });
            $(".second").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    sc += 1;
                }
            });
            $(".third").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    th += 1;
                }
            });
            $(".fourth").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    four += 1;
                }
            });

            totpack = parseFloat(frs) + parseFloat(sc) + parseFloat(th) + parseFloat(four);
            $('.nopack').val(totpack);

        } else if (section == 'S') {

            var fifth = 0,
                    six = 0,
                    sevn = 0,
                    eight = 0,
                    nine = 0,
                    ten = 0,
                    totpack2 = 0;
            $(".fifth").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    fifth += 1;
                }
            });
            $(".sixth").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    six += 1;
                }
            });
            $(".seventh").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    sevn += 1;
                }
            });
            $(".eight").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    eight += 1;
                }
            });
            $(".ninth").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    nine += 1;
                }
            });
            $(".tenth").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    ten += 1;
                }
            });
            totpack2 = parseFloat(fifth) + parseFloat(six) + parseFloat(sevn) + parseFloat(eight) + parseFloat(nine) + parseFloat(ten);
            $('.nopack').val(totpack2);
        } else {
            var prel = 0;
            $(".pqrelm").each(function () {
                if (!isNaN(this.value) && this.value.length !== 0 && (this.value) > 1) {
                    prel += 1;
                }
                var tpck = parseFloat(prel);
                $('.nopack').val(tpck);
            });
        }
    }



});