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

    $("body").on('blur', '.nomonths, .frodate', fillToDate);


    function fillToDate() {

        var date = new Date($(".frodate").val()),
                days = parseInt($(".nomonths").val());


        var newDate = new Date(date.setMonth(date.getMonth() + days));

        var d = new Date(newDate),
                month = '' + (d.getMonth() + 1),
                day = '' + d.getDate(),
                year = d.getFullYear();

        if (month.length < 2)
            month = '0' + month;
        if (day.length < 2)
            day = '0' + day;

        var toodate = year + '-' + month + '-' + day;
        $(".toodate").val(toodate);

    }

    $("form").submit(function (event) {

        var tenant = $(".tenant").val(),
//        monthno = $(".monthno").val(),
                total = $(".taxable").val(),
                a = 1;

        if (tenant.length <= 0 || tenant == '') {
            alert('Tenant Name Cannot Be Empty');
            a = 0;
        }
//        if (monthno.length <= 0 || monthno == '') {
//            alert('Month Cannot Be Empty');
//            a = 0;
//        }
        if (total == '' || total <= 0) {
            alert('Amount Cannot Be Zero');
            a = 0;
        }

        if (a == 0) {
            return false;
            event.preventDefault();
        }
    });

    function gettotal() {

        var nomonths = parseFloat($('.nomonths').val()),
                rent = parseFloat($('.monrent').val()) * nomonths;
//                proptax = parseFloat($('.proptax').val()),
//                repairtax = parseFloat($('.repairtax').val()),
//                othertax = parseFloat($('.othertax').val()),
//                taxable = rent + proptax + repairtax + othertax;

        $('.rent').val(rent);
//        $('.proptax').val(proptax);
//        $('.repairtax').val(repairtax);
//        $('.othertax').val(othertax);
//        $('.taxable').val(taxable);
    }

    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;
                $(".name").val(m.name);
                $(".block").val(m.block);
                $(".wingno").val(m.wing);
                $(".floorno").val(m.floor);
                $(".flatno").val(m.flatno);
                $(".rentfor").val(m.rentfor);


                $(".monrent").val(m.rent);
//                $(".proptax").val(m.proptax);
//                $(".repairtax").val(m.repairtax);
//                $(".othertax").val(m.othertax);
//                $(".taxable").val(m.total);
//                $(".area").val(m.area);
//                $(".unit").val(m.unit);
//                $(".mobile").val(m.mobile);

            },
            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: "manrecpres_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  //////////////////////////////////////////////

});  