$(document).ready(function () {
    $("body").on("keyup", ".ssearch", getSearchData);
    $("body").on("click", ".sload", getSearchData);
    $("body").on("click", "#search", getSearchData);
    $("body").on("keyup change blur", ".purqty, .purrate, .salerate", calData);

    function calData() {
        var purqty = $(".purqty").val(),
                purrate = $(".purrate").val(),
                salerate = $(".salerate").val(),
                puramt = purqty * purrate,
                saleamt = purqty * salerate,
                diffamt = saleamt - puramt;
        $(".puramt").val(puramt);
        $(".saleamt").val(saleamt);
        $(".diffamt").val(diffamt);

    }



//////////////////////////////////////////////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(),
                soperator = $(".soperator").val(),
                data = {ssearch: ssearch, sfield: sfield, soperator: soperator, para: para,
                    series: series, rangeFrom: smrangeFrom, rangeTo: smrangeTo},
                values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "purchsale_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.vouchdt + "</td>" +
                            "<td align='left'>" + this.suppname + "</td>" +
                            "<td align='left'>" + this.suppamt + "</td>" +
                            "<td align='left'>" + this.custname + "</td>" +
                            "<td align='left'>" + this.custamt + "</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();
    });
    $(".sprev").click(function () {
        getSearchData('prev');
    });
    $(".sprev").click(function () {
        getSearchData('next');
    });


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

});