$(document).ready(function () {
    $("body").on("keyup", ".csearch", getSearchData);
    $("body").on("click", ".cload", getSearchData);
    $("body").on("click", "#search", getSearchData);
    $("body").on("change", ".state", getStateCode);
//    $("body").on("change", ".openbal, .opendc", calOpen);
    $("body").off('click').on("click", '.remove', remove);


//    function calOpen() {
//        var totamt = 0;
//        
//    }

    $("form").submit(function () {
        var maindc = $('.maindc').val();
        var mainbal = $('.mainbal').val();

        var mtot = 0;
        $(".tbody").find('tr').each(function () {
            var row = $(this);
            var dc = row.find('.opendc').val();
            var openbal = row.find('.openbal').val();
            if (dc == "D") {
                openbal = openbal * -1;
            }
            mtot = mtot + parseFloat(openbal);
//                    alert(openbal);

        });
        if (maindc == "D") {
            mainbal = mainbal * -1;
        }
//        alert(mainbal);
        if (parseFloat(mainbal) != parseFloat(mtot)) {
            alert("Main Balance & Salesman Balance Not Tally");
            return false;
        }

    });
    function getStateCode() {
        var state = $(".state").val();
        $.get('get_stcode.php', {state: state}, function (data) {
            $(".statecode").val(data);

            if (data != 27) {
                $(".oms").prop("checked", true);
            }else{
                $(".oms").prop("checked", false);
            }
        });
    }


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

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

            $(focusFld).focus();
        });
    }


    var accode = $(".vouchno").val();
    if (accode == 49) {
        $("#hdelete").hide();
    }

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

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

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".cloader").show();

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

        var series = $(".series").val(),
                vouchno = $(".vouchno").val(),
                csearch = $(".csearch").val(),
                cfield = $(".cfield").val(),
                coperator = $(".coperator").val(),
                data = {csearch: csearch, cfield: cfield, coperator: coperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
                values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "cust_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".cnoRecord").show();
                    $(".cnext").hide();
                } else {
                    $(".cnext").show();
                    $(".cnoRecord").hide();

                    $.each(response, function () {
                        values += "<tr class='clickable-row' data-href='" + this.action + "'>" +
                                "<td align='left'>" + this.name + "</td>" +
                                "<td align='left'>" + this.add1 + "</td>" +
                                "<td align='left'>" + this.city + "</td>" +
                                "<td align='left'>" + this.mobile + " </td>" +
                                "<td align='left'>" + this.email + "</td>" +
                                "<td align='left'><a href='" + this.action + "' ><i class='fa fa-pencil'></i></a></td>" +
                                "</tr>";
                    });
                    $(".cloader").hide();
                    $(".cTbody").empty();
                    $(".cTbody").html(values);
                    if (response) {
                        $(".search-action-bar").show();
                    }
                }
            },
            error: function (request, textStatus, errorThrown) {

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


    //////////////////////////////////////////////Search Part - End  //////////////////////////////////////////////
});