$(document).ready(function () {
    $("body").on("change", ".tenant", getdata);
    $("body").on("keyup", ".rent, .proptax, .repairtax, .othertax", gettotal);

//    checkGst();
    var rentfor = $(".rentfor").val();
    if (rentfor == 'C') {
        $(".gstpart").show();
    } else {
        $(".gstpart").hide();

    }

    function gettotal() {

        var cgstamt = 0,
                sgstamt = 0,
                netamt = 0,
                rent = parseFloat($('.rent').val()),
                proptax = parseFloat($('.proptax').val()),
                repairtax = parseFloat($('.repairtax').val()),
                othertax = parseFloat($('.othertax').val()),
                cgstperc = parseFloat($('.cgstperc').val()),
                taxable = rent + proptax + repairtax + othertax;
        if (cgstperc > 0) {
            cgstamt = (cgstperc * taxable) / 100;
            sgstamt = (cgstperc * taxable) / 100;
            netamt = parseFloat(taxable) + parseFloat(cgstamt) + parseFloat(sgstamt);
        } else {
            cgstamt = 0;
            sgstamt = 0;
            netamt = parseFloat(taxable);

        }

        $('.cgstamt').val(cgstamt);
        $('.sgstamt').val(sgstamt);
        $('.taxable').val(taxable);
        $('.total').val(netamt);
    }

    function getdata() {
        var tenant = $(".tenant").val();

        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "get_party_dtl.php",
            data: {tenant: tenant},
            success: function (response) {
                var m = response;
                $('.block').val(m.block);
                $('.floortype').val(m.floortype);
                $('.floorno').val(m.floorno);
                $('.room').val(m.room);
                $('.rentfor').val(m.rentfor);
                $('.cgstperc').val(m.cgstperc);
                $('.sgstperc').val(m.sgstperc);
                if (m.rentfor == 'C') {
                    $('.gstpart').show();
                } else {
                    $('.gstpart').hide();
                }
            },
//            error: function (request, textStatus, errorThrown) {
//            }
        });

    }



    $("body").on("keyup", ".ssearch", getSearchData);
    $("body").on("click", ".sload", getSearchData);
    $("body").on("click", "#search", getSearchData);

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


    var imrangeFrom = 0,
            imrangeTo = 40;

    function getSearchData(pagination) {

        $(".sloader").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(),
                ssearch = $(".ssearch").val(),
                sfield = $(".sfield").val(),
                sortBy = $(".sortBy").val(),
                orderAs = $(".orderAs").val(),
                soperator = $(".soperator").val(),
                data = {ssearch: ssearch, sfield: sfield, sortBy: sortBy, orderAs: orderAs, soperator: soperator,
                    series: series, vouchno: vouchno, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
                values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "manrecp_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".snoRecord").show();
                    $(".snext").hide();
                } else {
                    $(".snext").show();
                    $(".snoRecord").hide();

                    $.each(response, function () {
                        values += "<tr class='clickable-row' data-href='" + this.action + "'>" +
                                "<td align='left'>" + this.party + "</td>" +
                                "<td align='left'>" + this.vouchdt + "</td>" +
                                "<td align='left'>" + this.vouchno + "</td>" +
                                "<td align='left'>" + this.monthno + "</td>" +
                                "<td align='left'><a href='" + this.action + "' ><i class='fa fa-pencil'></i></a></td>" +
                                "</tr>";
                    });
                    $(".sloader").hide();
                    $(".sTbody").empty();
                    $(".sTbody").html(values);
                    if (response) {
                        $(".search-action-bar").show();
                    }
                }
                $(".ssearch").focus();

            },
            error: function (request, textStatus, errorThrown) {

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

        getSearchData();
    });
    $(".sprev").click(function () {
        getSearchData('prev');
    });
    $(".snext").click(function () {
        getSearchData('next');
    });


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

});  