$(document).ready(function () {
    $("body").on("click", ".clickable-row", clickRow);
    $(".searchModal").on("click", ".clickable-row", clickRow);
    $("body").on("keyup", ".ssearch", getSearchData);
    $("body").on("change", ".soperator", getSearchData);
    $("body").on("click", ".sload", getSearchData);
    $("body").on("click", "#search", getSearchData);
    $("body").on("click", ".deldata", DeleWind);
    $("body").on("click", ".contordtd", OpenKOT);
    $("body").on('dblclick', '.getdtl', getDetails);
    $("body").on('dblclick', '.dtlhide1', hideDetails);

    $(".dtlsale").hide();


    function OpenKOT() {
        var tr = $(this).closest("tr");
        var mvouch = tr.find(".vouchno").val();
        var windip = tr.find(".kotdirect").val();
        if (windip == "I") {
            window.parent.location = "kotent.php?mode=edit&vouchno=" + mvouch;
        } else {
            window.parent.location = "kotpent.php?mode=edit&vouchno=" + mvouch;
        }
    }

    function DeleWind() {
        var tr = $(this).closest("tr");
        if (confirm('Delete Confirm?')) {
            var mvouch = tr.find(".vouchno").val(),
                    mtable = tr.find(".trtable").text();
            window.document.location = "pendkot.php?delete&vouchno=" + mvouch + "&tableno=" + mtable;
        }
    }


    function hideDetails() {
        var trid = $(this).closest('tr').attr('id');
        var idval = trid.split('_')[1];
        $("#dtlsale_" + idval).hide();
    }

    function getDetails() {
        var trid = $(this).closest('tr').attr('id');
        var idval = trid.split('_')[1];
        $("#dtlsale_" + idval).show();
        var tr = $(this).closest("tr"),
                vouch = tr.find(".vouchno").val();
        $.post('get_order.php', {vouchno: vouch}, function (data) {
            $("#dtlsale_" + idval).html(data);
        });
    }
    //////////////////////////////////////////////Search Part - Start//////////////////////////////////////////////
    function clickRow() {
        window.document.location = $(this).data("href");
    }
    $(".snoRecord").hide();
    $(".sloader").hide();

    var imrangeFrom = 0,
            imrangeTo = 40;

    $(".ssearch").focus();
//    $(".ssearch").val("");
//    $(".sfield").val("1");
    getSearchData();

    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(),
                type = $(".type").val(),
                manual = $(".manual").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, type: type, manual: manual},
                values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "pendkot_search.php",
            data: data,
            success: function (response) {
                if (response == "") {
                    $(".snoRecord").show();
                    $(".snext").hide();
                    $(".ssload").hide();
                } else {
                    $(".snext").show();
                    $(".snoRecord").hide();
                    $(".ssload").show();

                    $.each(response, function () {
                        values +=
                                "<tr class='getdtl' id='getdtl_" + this.value + " ' >" +
//                                "<td align='left'><img style='height:25px;width:50px;' src='" + this.parcel + "' alt=''></td>" +
                                "<td align='left'>" + this.vouchno + "<input type='hidden' class='vouchno' value='" + this.vouchno + "'</td>" +
                                "<td align='left'>" + this.vouchdt + "</td>" +
                                "<td align='left'>" + this.vouchtime + "</td>" +
                                "<td align='left' class='trtable'>" + this.tablename + "</td>" +
                                "<td align='left'>" + this.user + "</td>" +
                                "<td align='right'>" + this.totitems + "</td>" +
                                "<td align='right'>" + this.totqty + "</td>" +
                                "<td align='center'><a href='pendkot_print.php?vouchno=" + this.vouchno + "' target='_blank'><i class='fa fa-print' aria-hidden='true'></i></a></td>" +
                                this.del + "</tr>" +
                                "<tr class='dtlhide1' id='dtlsale_" + this.value + "' style='font-size:18px;'>" +
                                "</tr>";
                    });
                    $(".sloader").hide();
                    $(".sTbody").empty();
                    $(".sTbody").html(values);
                    if (response) {
                        $(".search-action-bar").show();
                    }
                }
            },
            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  //////////////////////////////////////////////
});