$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    $(".tbody").on("keyup blur focus", ".qty, .rate, .amount,.igstrate,.cgstrate", calDetail);
    $("body").on("change keyup blur", ".qty,.rate,.amount,.igstrate,.cgstrate", calMain);
    $("body").on("keyup", ".ssearch", getSearchData);
    $("body").on("click", ".sload", getSearchData);
    $("body").on("change", ".orderAs, .sortBy", getSearchData);
    $("body").on("focus", ".qty", gethsn);
    $("body").on("click change", ".oms", gsttr);
    $("body").on("focus blur change", ".vouchdt", getvoucher);
    $("body").on("change blur", ".cmbparty", getgst);
    $("body").on("change keyup blur", ".rate,.qty,.amount", caltran);

    var lrno = $('.lrno').val();

    if (lrno != '') {
        $(".lrdetail").show();
    } else {
        $(".lrdetail").hide();
    }

    function gsttr() {
        $('.tbody').find('tr').each(function () {
            var row = $(this),
                    cgstrate = row.find('.cgstrate').val(),
                    igstrate = row.find('.igstrate').val(),
                    igstamt = row.find('.igstamt').val(),
                    cgstamt = row.find('.cgstamt').val();
            if ($('.oms').prop("checked") == true) {
                if (igstrate == 0) {
                    var nigstrt = cgstrate * 2,
                            nigstamt = cgstamt * 2;
                    row.find('.igstrate').val(nigstrt);
                    row.find('.igstamt').val(nigstamt);
                    row.find('.cgstrate').val(0);
                    row.find('.cgstamt').val(0);
                    row.find('.sgstrate').val(0);
                    row.find('.sgstamt').val(0);
                }


            } else {
                if (cgstrate == 0) {
                    var ncgstrt = igstrate / 2,
                            ncgstamt = igstamt / 2;
                    row.find('.cgstrate').val(ncgstrt);
                    row.find('.cgstamt').val(ncgstamt);
                    row.find('.sgstrate').val(ncgstrt);
                    row.find('.sgstamt').val(ncgstamt);
                    row.find('.igstrate').val(0);
                    row.find('.igstamt').val(0);

                }

            }
        });
        caltran();
    }


    function getvoucher() {
        var date = new Date($(".vouchdt").val()),
                month = (date.getMonth() + 1).toString(),
                vouchno = $('.vouchno').val(),
                voucher = '0' + month + vouchno;

        $('.monno').val(month);
        $('.voucher').val(voucher);

    }


    $(".lrtab").click(function () {
        $(".lrdetail").animate({
            height: 'toggle'
        });
    });

    $('#hedit').click(function () {
        var addflag = $('.addflag').val();
        if (addflag != 1) {
            $('.oms').prop("disabled", true);
        } else {
            $('.oms').prop("disabled", false);
        }
    });

    function getgst() {
        var party = $('.cmbparty').val();
        $.post('getcustgst.php', {party: party}, function (data) {
            if (data.gstno != 0) {
                $('.gstno').val(data.gstno);
            }
            $('.mobile').val(data.mobile);
        }, 'json');
    }

    function gethsn() {
        var tr = $(this).closest("tr");
        var barcode = tr.find('.barcode').val();
        $.post('get_salegst.php', {barcode: barcode}, function (data) {
            tr.find('.discperc').val(data.discperc);
            if ($(".oms").prop('checked')) {
                tr.find('.igstrate').val(data.igst);
                tr.find('.cgstrate').val(0);
                tr.find('.sgstrate').val(0);
            } else {
                tr.find('.cgstrate').val(data.cgst);
                tr.find('.sgstrate').val(data.sgst);
                tr.find('.igstrate').val(0);
            }
        }, 'json');
    }

    function caltran() {
        var tr = $(this).closest("tr");
        var amt = tr.find('.amount').val(),
                discperc = tr.find('.discperc').val(),
                cgst5 = 0,
                cgst12 = 0,
                cgst18 = 0,
                cgst28 = 0,
                igst5 = 0,
                igst12 = 0,
                igst18 = 0,
                igst28 = 0,
                cgst5a = 0,
                cgst12a = 0,
                cgst18a = 0,
                cgst28a = 0,
                igst5a = 0,
                igst12a = 0,
                igst18a = 0,
                igst28a = 0,
                totgst5 = 0,
                totgst12 = 0,
                totgst18 = 0,
                totgst28 = 0,
                totbillamt = 0,
                discount = (discperc * amt) / 100,
                rdisc = Math.round(discount * 100) / 100;
        tr.find('.discamt').val(rdisc);
        var taxval = parseFloat(amt) - parseFloat(discount),
                rtaxval = Math.round(taxval * 100) / 100;
        tr.find('.subtot').val(rtaxval);

        var cgstrate = tr.find('.cgstrate').val(),
                sgstrate = tr.find('.sgstrate').val(),
                igstrate = tr.find('.igstrate').val(),
                cgstamnt = parseFloat(taxval) * parseFloat(cgstrate) / 100,
                sgstamnt = parseFloat(taxval) * parseFloat(sgstrate) / 100,
                igstamnt = parseFloat(taxval) * parseFloat(igstrate) / 100,
                total = parseFloat(cgstamnt) + parseFloat(sgstamnt) + parseFloat(igstamnt) + parseFloat(taxval),
                rtotal = Math.round(total * 100) / 100;
        tr.find('.cgstamt').val(cgstamnt.toFixed(2));
        tr.find('.sgstamt').val(sgstamnt.toFixed(2));
        tr.find('.igstamt').val(igstamnt.toFixed(2));
        tr.find('.total').val(rtotal);

        var grosstot = 0,
                discamt = 0,
                subtot = 0;


        $(".amount").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                grosstot += parseFloat(this.value);
            }
        });
        $(".grossamt").val(grosstot.toFixed(2));

        $(".discamt").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                discamt += parseFloat(this.value);
            }
        });

        $('.totdiscamt').val(discamt.toFixed(2));

        $(".subtot").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                subtot += parseFloat(this.value);
            }
        });
        $('.taxableamt').val(subtot.toFixed(2));


        $('.tab').find('tr').each(function () {
            var row = $(this),
                    crate = row.find('.cgstrate').val(),
                    camt = row.find('.cgstamt').val(),
                    irate = row.find('.igstrate').val(),
                    iamt = row.find('.igstamt').val();


            if (crate < 3) {
                cgst5a = parseFloat(camt);
                cgst5 += parseFloat(cgst5a);
                igst5a = 0;
            } else if (crate < 7) {
                cgst12a = parseFloat(camt);
                cgst12 += parseFloat(cgst12a);
                igst12a = 0;
            } else if (crate < 10) {
                cgst18a = parseFloat(camt);
                cgst18 += parseFloat(cgst18a);
                igst18a = 0;
            } else if (crate < 15) {
                cgst28a = parseFloat(camt);
                cgst28 += parseFloat(cgst28a);
                igst28a = 0;
            }

            if (irate < 7) {
                igst5a = parseFloat(iamt);
                igst5 += parseFloat(igst5a);
                cgst5a = 0;
            } else if (irate < 13) {
                igst12a = parseFloat(iamt);
                igst12 += parseFloat(igst12a);
                cgst12a = 0;
            } else if (irate < 19) {
                igst18a = parseFloat(iamt);
                igst18 += parseFloat(igst18a);
                cgst12a = 0;
            } else if (irate < 29) {
                igst28a = parseFloat(iamt);
                igst28 += parseFloat(igst28a);
                cgst28a = 0;
            }

        });

        totgst5 = parseFloat(cgst5) + parseFloat(cgst5) + parseFloat(igst5);
        totgst12 = parseFloat(cgst12) + parseFloat(cgst12) + parseFloat(igst12);
        totgst18 = parseFloat(cgst18) + parseFloat(cgst18) + parseFloat(igst18);
        totgst28 = parseFloat(cgst28) + parseFloat(cgst28) + parseFloat(igst28);

        $('.cgst5').val(cgst5.toFixed(2));
        $('.sgst5').val(cgst5.toFixed(2));
        $('.igst5').val(igst5.toFixed(2));

        $('.cgst12').val(cgst12.toFixed(2));
        $('.sgst12').val(cgst12.toFixed(2));
        $('.igst12').val(igst12.toFixed(2));



        $('.cgst18').val(cgst18.toFixed(2));
        $('.sgst18').val(cgst18.toFixed(2));
        $('.igst18').val(igst18.toFixed(2));

        $('.cgst28').val(cgst28.toFixed(2));
        $('.sgst28').val(cgst28.toFixed(2));
        $('.igst28').val(igst28.toFixed(2));

        $('.totgst5').val(totgst5.toFixed(2));
        $('.totgst12').val(totgst12.toFixed(2));
        $('.totgst18').val(totgst18.toFixed(2));
        $('.totgst28').val(totgst28.toFixed(2));


        totbillamt = parseFloat(subtot) + parseFloat(totgst5) + parseFloat(totgst12) + parseFloat(totgst18) + parseFloat(totgst28);
        var rtotbillamt = Math.round(totbillamt * 100) / 100;

        $(".totbillamt").val(rtotbillamt);
    }


    function calDetail() {
        var tr = $(this).closest("tr"),
                qty = tr.find(".qty").val(),
                rate = tr.find(".rate").val(),
                amount = parseFloat(rate) * parseFloat(qty),
                subtotal = 0;

        tr.find(".amount").val(amount);

        $(".amount").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                subtotal += parseFloat(this.value);
            }
        });
        $(".subtotal").val(subtotal);

    }

    function calMain() {
        var tr = $(this).closest("tr");
        var amt = tr.find('.amount').val(),
                discperc = tr.find('.discperc').val(),
                discount = (discperc * amt) / 100;
        tr.find('.discamt').val(discount);
        var taxval = parseFloat(amt) - parseFloat(discount);
        tr.find('.subtot').val(taxval);

        var cgstrate = tr.find('.cgstrate').val(),
                sgstrate = tr.find('.sgstrate').val(),
                cgstamnt = parseFloat(taxval) * parseFloat(cgstrate) / 100,
                sgstamnt = parseFloat(taxval) * parseFloat(sgstrate) / 100,
                total = Math.round(parseFloat(cgstamnt) + parseFloat(sgstamnt) + parseFloat(taxval));

        tr.find('.cgstamt').val(cgstamnt);
        tr.find('.sgstamt').val(sgstamnt);
        tr.find('.total').val(total);

        var grosstot = 0,
                discamt = 0,
                subtot = 0,
                cgstamt = 0,
                sgstamt = 0;


        $(".amount").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                grosstot += parseFloat(this.value);
            }
        });
        $(".grossamt").val(grosstot.toFixed(2));

        $(".discamt").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                discamt += parseFloat(this.value);
            }
        });
        $('.totdiscamt').val(discamt.toFixed(2));

        $(".subtot").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                subtot += parseFloat(this.value);
            }
        });
        $('.taxableamt').val(subtot.toFixed(2));

        $(".cgstamt").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                cgstamt += parseFloat(this.value);
            }
        });
        $('.totcgst').val(cgstamt.toFixed(2));

        $(".sgstamt").each(function () {
            if (!isNaN(this.value) && this.value.lenght != 0) {
                sgstamt += parseFloat(this.value);
            }
        });
        $('.totsgst').val(sgstamt.toFixed(2));




        var totbillamt = Math.round(parseFloat(subtot) + parseFloat(cgstamt) + parseFloat(sgstamt));


        $(".totbillamt").val(totbillamt);
    }

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

    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;
        $.post('sale_ex_add.php', {ctr: ctr, hide: hide_check, prodtype: prodtype}, function (data) {
            $(data).appendTo('.tbody');
            ctr++;
            $('#items').val(ctr);
//            $(detail).select2();
            $(focusFld).focus();
        });
    }



    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    $(".snoRecord").hide();

    $(".searchModal").on('shown.bs.modal', function () {
        $(".ssearch").focus();
        $(".ssearch").val("");
        $(".sfield").val("1");
        getSearchData();
    });

    var smrangeFrom = 0,
            smrangeTo = 40;

    function getSearchData(pagination) {

        $(".sloader").show();

        if (pagination == 'prev') {
            if (smrangeFrom > 0) {
                smrangeFrom = (smrangeFrom - 40);
                smrangeTo = (smrangeTo - 40);
            }
        }
        if (pagination == 'next') {
            smrangeFrom = (smrangeFrom + 40);
            smrangeTo = (smrangeTo + 40);
        }

        var series = $(".series").val(),
                para = $(".para").val(),
                ssearch = $(".ssearch").val(),
                sfield = $(".sfield").val(),
                sortBy = $(".sortBy").val(),
                orderAs = $(".orderAs").val(),
                ioperator = $(".soperator").val(),
                data = {ssearch: ssearch, sfield: sfield, sortBy: sortBy, orderAs: orderAs, ioperator: ioperator, para: para,
                    series: series, rangeFrom: smrangeFrom, rangeTo: smrangeTo},
        values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "sale_ex_search.php",
            data: data,
            success: function (response) {
                $.each(response, function () {
                    values += "<tr class='clickable-row' data-href='" + this.action + "'>" +
                            "<td align='left'>" + this.vouchno + "</td>" +
                            "<td align='left'>" + this.name + "</td>" +
                            "<td align='left'>" + this.mobile + "</td>" +
                            "<td align='left'>" + this.billno + "</td>" +
                            "<td align='left'>" + this.vouchdt + "</td>" +
                            "<td align='left'>" + this.billamt + "</td>" +
                            "<td align='left'>" + this.rem1 + "</td>" +
                            "<td align='left'><a href='" + this.action + "'><i class='fa fa-pencil' style='font-size:1.3em;cursor:pointer;'></i></a></td>" +
                            "</tr>";
                });
                $(".sloader").hide();
                $(".sTbody").empty();
                $(".sTbody").html(values);
                if (response) {
                    $(".search-action-bar").show();
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }
    $("#search").click(function () {
        getSearchData();
    });
    $(".sprevious").click(function () {
        getSearchData('prev');
    });
    $(".snext").click(function () {
        getSearchData('next');
    });


    //////////////////////////////////////////////Search Part - End  //////////////////////////////////////////////


});