$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    $("body").on("keyup change blur focus", ".qty, .rate", calDetail);
    $("body").on("blur", ".invno", chkinvno);
    $("body").on("blur ", ".vatperc", autoadd);
    $("body").on("blur", ".jobgiven", getJbgvDet);
    $("body").on("click", ".recvmat", getRawMatForProd);
    $("body").on("keyup blur focus", ".tr_qty,.meters,.yard, .used_tr_qty, .used_meters, .used_yard, .bal_tr_qty, .bal_meters, .bal_yard", calTran);

function calTran() {
        var tr = $(this).closest("tr"),
            val = 0, val1 = 0;

        var trqty = tr.find(".tr_qty").val();
        var used_tr_qty = tr.find(".used_tr_qty").val();
        var bal_tr_qty = trqty - used_tr_qty;
        tr.find(".bal_tr_qty").val(bal_tr_qty);

        var meters = tr.find(".meters").val();
        var used_meters = tr.find(".used_meters").val();
        var bal_meters = meters - used_meters;
        tr.find(".bal_meters").val(bal_meters);

        var yard = tr.find(".yard").val();
        var used_yard = tr.find(".used_yard").val();
        var bal_yard = yard - used_yard;
        tr.find(".bal_yard").val(bal_yard);

    }

function getJbgvDet() {
        var jobgiven = $(".jobgiven").val(),
                data = {jobgiven: jobgiven};

        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "getJbgvDet.php",
            data: data,
            success: function (response) {
                
                var m = response;
                $(".saleorder").val(m.saleorder);
                $(".party").val(m.party).trigger("change");
                $(".saleorddate").val(m.saleorddate);
                $(".prodcode").val(m.prodcode).trigger("change");
                $(".colorname").val(m.colorname).trigger("change");
                $(".process").val(m.process).trigger("change");
                $(".karigar").val(m.karigar).trigger("change");
                $(".expectdate").val(m.expectdate);
                $(".qty").val(m.qty);
                $(".rate").val(m.rate);
                $(".remark1").val(m.remark1);
                $(".remark2").val(m.remark2);
                $(".rate").focus();
            }
        });
    }



    function getRawMatForProd() {
    var prodcode = $(".prodcode").val();
    var jobgiven = $(".jobgiven").val();

            if (prodcode != '' && jobgiven !='') {
                $.get('getRawMatForProdRecv.php', {prodcode: prodcode, jobgiven: jobgiven}, function (data) {
                    $("#tabledata").html(data);
                });
            }
}
    
    $("form").submit(function () {

        var party = $('.cmbparty').val(),
                totbillamt = $('.totbillamt').val(),
                valid = true;


        if (party == '') {
            var message = "Party Cannot Be Empty";
            alert(message);
            return false;
            valid = false;
        }

        if (parseFloat(totbillamt) <= 0 || totbillamt == '') {
            var message = "Netamt Cannot zero";
            alert(message);
            return false;
            valid = false;
        }



        if (valid) {
            var isValid = true;

            if (isValid) {
                $("#submit").hide();
                $("#cancel").hide();

            }
        }
    });

    $(".party").change(function () {
        var invno = $(".invno").val(),
                file = $(".file").val(),
                party = $(".party").val();
        $.post('chk_invo.php', {invno: invno, party: party, file: file}, function (data) {
            if (data.s == 1) {
                alert('These Invoice Already Exist For Voucher  ' + data.vouchno);
                $(".invno").val('');
                $(".invno").focus();

            }
        }, 'json');
    });

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

    $('.invno').blur(function () {
        $('.party').select2("open");
    });


    function calDetail() {
            var qty = $(".qty").val(),
                rate = $(".rate").val(),
                billamt = 0;
        billamt = qty * rate;
        $(".billamt").val(billamt);
    }


    function chkinvno() {
        var invno = $('.invno').val();
        var party = $(".party").val();
        if (invno.length > 0) {
            $.post('check_inv_dtl.php', {invno: invno, party: party}, function (data) {
                if (data == 'success') {
                    alert('This Bill No. For This Party Already Exist');
                    $('.invno').val('');
                    $('.invno').focus();
                }
            });
        }
    }

    $('#addnew').click(function () {
        AddRows();
//        caltran();
    });
    function remove() {
        $(this).closest("tr").remove();
//        caltran();
calDetail();
    }
    

    function AddRows() {
        var ctr = $('#items').val();
        var prodtype = $('.prodtype').val();
        var hide_check = "";
        if ($("#del_all").prop("checked")) {
            hide_check = 0;
        } else {
            hide_check = 1;
        }
//        var detail = ".fetchpart_" + ctr;
        var focusFld = ".focusFld_" + ctr;
        ctr++;
        $('#items').val(ctr);
        $.post('purchase_add.php', {ctr: ctr, hide: hide_check, prodtype: prodtype}, function (data) {
            $(data).appendTo('.tbody');

//            $(detail).select2();
            $(focusFld).focus();
//            caltran();
        });
    }




});