$(document).ready(function () {
    $("body").on("dblclick", ".clickable-row", clickRow);
    $(".searchModal").on("dblclick", ".clickable-row", clickRow);
    $("body").on("keyup", ".ssearch", getSearchData);
    $("body").on("change", ".soperator", getSearchData);
    $("body").on("change", ".sdelv", getSearchData);
    $("body").on("click", ".sload", getSearchData);
    $("body").on("click", "#search", getSearchData);
    $("body").on("click", ".imgchkbox", ChkBoxFill);

    function ChkBoxFill() {
        var tr = $(this).closest("tr"),
                mvouch = tr.find(".srchvouchchkbox").val();
        if (tr.find(".imgchkbox").prop("checked") == true) {
            $(".imgdata option[value='" + mvouch + "']").prop("selected", true);
        } else {
            $(".imgdata option[value='" + mvouch + "']").prop("selected", false);
        }
    }

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


                    $.each(response, function () {
                        if (this.usertype == 1) {
                            var valdel = "<td align='left'>" +
                                    "<a href=\"?delete=delete&vouchno=" + this.srno + "\" onClick=\"return confirm('Confrim Delete?');\"><i class='fa fa-trash-o' style='padding: 2px 10px;font-size:1.3em;color:red;cursor:pointer;'></i></a>" +
                                    "</td>";
                        } else {
                            var valdel = "";
                        }

                        if (jQuery.inArray(this.vouchno, imgdata) !== -1) {
                            var selectchkbox = "checked";
                        } else {
                            var selectchkbox = "";
                        }

                        values += "<tr class='clickable-row' style='font-size: 11px;padding:0px;' data-href='" + this.action + "'>" +
                                "<td align='left'><input type='checkbox' name='imgchk_" + this.vouchno + "' class='form-control imgchkbox' value='1' " + selectchkbox + " >" +
                                "<input type='hidden' name='srchvouchchkbox_" + this.vouchno + "' class='form-control srchvouchchkbox' value='" + this.vouchno + "' >" + "</td>" +
                                "<td align='left'>" + this.srno + "</td>" +
                                "<td align='left'>" + this.mark + "</td>" +
                                "<td align='left' class='cvouchdt'>" + this.vouchdt + "</td>" +
                                "<td align='left'>" + this.delv + "</td>" +
                                "<td align='left'>" + this.delvdate + "</td>" +
                                "<td align='left' class='partynametd'>" + this.partyname + "</td>" +
                                "<td align='left' class='text-right'>" + this.nag + "</td>" +
                                "<td align='left' class='text-right'>" + this.sizemm + "</td>" +
                                "<td align='left' class='text-right'>" + this.guagemm + "</td>" +
                                "<td align='left' class='text-right'>" + this.makemm + "</td>" +
                                "<td align='left'>" + this.dia + "</td>" +
                                "<td align='left'>" + this.ap + "</td>" +
                                "<td align='left'>" + this.weld + "</td>" +
                                "<td align='left'>" + this.straight + "</td>" +
                                "<td align='left' class='text-right'>" + this.foot + "</td>" +
                                "<td align='left' class='text-right'>" + this.totfeet + "</td>" +
                                "<td align='left' class='text-right'>" + this.weight + "</td>" +
                                "<td align='left' class='text-right'>" + this.rate + "</td>" +
                                "<td align='left' class='text-right'>" + this.amount + "</td>" +
                                "<td align='left'>" + this.descrip + "</td>" +
                                "<td align='left'>" + this.cashcred + "</td>" +
                                "<td align='left'>" + this.cashvouch + "</td>" +
                                "<td align='left' class='gstperctd'>" + Math.round(this.gstperc) + "</td>" + valdel +
                                "<td>" +
                                "<a href=\"labelprin.php?vouchno=" + this.vouchno + "\" target='_blank'><i class='fas fa-square' style='font-weight: 800;padding: 2px 10px;font-size:1.3em;color:blue;cursor:pointer;'></i></a>" +
                                "</td>" +
                                "<td class='copydata'>" + 
                                "<a href='#'><i class='fa fa-files-o' style='padding: 2px 10px;font-size:1.3em;color:red;cursor:pointer;'></i></a>" +
                                "<input type='hidden' name='copyaccode' class='copyaccode' value='" + this.accode + "'>" +
                                "<input type='date' name='copyvouchdt' class='copyvouchdt' style='display: none' value='" + this.mvdate + "'>" +
                                "</td>" +
                                "</tr>";
//                                "<td align='left'><a href='" + this.action + "' ><i class='fa fa-pencil'></i></a></td>" +
                        $(".chalcount").html("(" + this.numrows + ")")
                    });
                    $(".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  //////////////////////////////////////////////
});