$(document).ready(function () {
    $("body").off('click').on("click", '.remove', remove);
    $(".tbody").on("keyup blur focus", ".qty, .rate, .amount, .discperc, .discount, .total", calDetail);
    $("body").on("keyup", ".ssearch", getSearchData);
    $("body").on("click", ".sload", getSearchData);

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

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

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

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

    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;
        $.post('owtward_add.php', {ctr: ctr, hide: hide_check, prodtype: prodtype}, function (data) {
            $(data).appendTo('.tbody');
            ctr++;
            $('#items').val(ctr);
            $(detail).select2();
        });
    }
    
    
    
    //////////////////////////////////////////////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 ssearch     = $(".ssearch").val(),
            series      = $(".series").val(),
            sfield      = $(".sfield").val(),
            soperator   = $(".soperator").val(),
            data        = {ssearch: ssearch, sfield: sfield, soperator: soperator, series: series,
                           rangeFrom: smrangeFrom, rangeTo: smrangeTo},
            values      = "";
        $.ajax({
            type    : 'GET',
            dataType: 'json',
            url     : "outward_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.from+ "</td>" +
                              "<td align='left'>" + this.to+ "</td>" +
                              "<td align='left'>" + this.chalno+ "</td>" +
                              "<td align='left'>" + this.totqty+ "</td>" +
                              "<td align='left'>" + this.rem1+ "</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();
                }
            },
            error: function (request, textStatus, errorThrown) {
                
            }
        });
    }
    $("#search").click(function () {
        getSearchData();
    });
//    $(".ssearch").change(function () {
//        getSearchData();
//    });
    $(".sprevious").click(function () {
        getSearchData('prev');
    });
    $(".snext").click(function () {
        getSearchData('next');
    });
    
    
    //////////////////////////////////////////////Search Part - End  //////////////////////////////////////////////
    

});