$(document).ready(function () {
    $("body").on("click", ".existuser", itemModalShow);
    $(".iTbody").on("click", ".itemTr", addTrVal);
    $(".custModal").on("keyup", ".isearch", getItemData);
    $(".custModal").on("change", ".isortBy, iorderAS", getItemData);
    $(".inoRecord").hide();

    window.addEventListener('keydown', function (e) {
        if (e.keyIdentifier == 'U+000A' || e.keyIdentifier == 'Enter' || e.keyCode == 13) {
            if (e.target.nodeName == 'INPUT' && e.target.type == 'text' && e.target.className == "iname") {
                addTrVal(e);
            }
        }
    }, true);

    function itemModalShow() {
        var tr = $(this).closest("tr"),
                srno = tr.find(".srno").val(),
                margin = srno * 2.5,
                barcode = tr.find(".barcode").val();
        $(".modal-content").css("margin-top", margin + "%")

        $(".accode").removeClass("dataaccode");
        $(".mobile").removeClass("datamobile");
        $(".suffix").removeClass("datasuffix");
        $(".surname").removeClass("datasurname");
        $(".name").removeClass("dataname");
        $(".midname").removeClass("datamidname");
        $(".datagender").removeClass("datagender");
        $(".address").removeClass("dataaddress");
        $(".city").removeClass("datacity");
        $(".pin").removeClass("datapin");
        $(".email").removeClass("dataemail");

        $(".accode").addClass("dataaccode");
        $(".name").addClass("dataname");
        $(".surname").addClass("datasurname");
        $(".suffix").addClass("datasuffix");
        $(".midname").addClass("datamidname");
        $(".gender").addClass("datagender");
        $(".mobile").addClass("datamobile");
        $(".email").addClass("dataemail");
        $(".city").addClass("datacity");
        $(".pin").addClass("datapin");
        $(".address").addClass("dataaddress");
        $(".custModal").modal("show");
        getItemData();
    }

    $(".custModal").on('shown.bs.modal', function () {
        $(".isearch").focus();
        $(".isearch").val("");
        $(".ifield").val("1");
        getItemData();
    });

    $(".custModal").on('hidden.bs.modal', function () {
        $(".mobile").focus();
        $(".dataQty").select();
    });

    var imrangeFrom = 0,
            imrangeTo = 40;

    function getItemData(pagination) {

        $(".iloader").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(),
                file = $(".file").val(),
                isearch = $(".isearch").val(),
                ifield = $(".ifield").val(),
                ioperator = $(".ioperator").val(),
                sortBy = $(".isortBy").val(),
                orderAs = $(".iorderBy").val(),
                data = {isearch: isearch, ifield: ifield, ioperator: ioperator, sortBy: sortBy, orderAs: orderAs,
                    series: series, vouchno: vouchno, file: file, rangeFrom: imrangeFrom, rangeTo: imrangeTo},
                values = "";
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "existuser_search.php",
            data: data,
            success: function (response) {
                $.each(response, function () {
                    values += "<tr class='itemTr'>" +
                            "<td align='left'><input type='text' class='ifullname'  value='" + this.fullname + "'  readonly style='width:100%'></td>" +
                            "<td align='left'><input type='text' class='isurname'   value='" + this.surname + "'   readonly style='width:100%'tabindex='-1'></td>" +
                            "<td align='left'><input type='text' class='icity'      value='" + this.city + "'      readonly style='width:100%'tabindex='-1'></td>" +
                            "<td align='left'><input type='text' class='imobile'    value='" + this.mobile + "'    readonly style='width:100%'tabindex='-1'></td>" +
                            "<td align='left'><input type='text' class='iaadhaarno' value='" + this.aadhaarno + "' readonly style='width:100%'tabindex='-1'></td>" +
                            "<td align='left'><input type='text' class='ipanno'     value='" + this.panno + "'     readonly style='width:100%'tabindex='-1'></td>" +
                            "<input type='hidden' class='isuffix' value='" + this.suffix + "'>" +
                            "<input type='hidden' class='iname' value='" + this.name + "'>" +
                            "<input type='hidden' class='imidname' value='" + this.midname + "'>" +
                            "<input type='hidden' class='igender' value='" + this.gender + "'>" +
                            "<input type='hidden' class='iaccode' value='" + this.accode + "'>" +
                            "<input type='hidden' class='iaddress' value='" + this.address + "'>" +
                            "<input type='hidden' class='ipin' value='" + this.pin + "'>" +
                            "<input type='hidden' class='iemail' value='" + this.email + "'>" +
                            "</tr>";
                });
                $(".iloader").hide();
                $(".iTbody").empty();
                $(".iTbody").html(values);
                if (response) {
                    $(".search-action-bar").show();
                }
            },
            error: function (request, textStatus, errorThrown) {

            }
        });
    }
    $(".iload").click(function () {
        getItemData();
    });
//    $(".isearch").change(function () {
//        getItemData();
//    });
    $(".iprevious").click(function () {
        getItemData('prev');
    });
    $(".inext").click(function () {
        getItemData('next');
    });

    function addTrVal(e) {
        var tr = "";
        if (e == "undefined") {
            tr = $(this).closest("tr");
        } else {
            tr = $(e.target).closest("tr");
        }

        var isuffix = tr.find(".isuffix").val(),
                iname = tr.find(".iname").val(),
                isurname = tr.find(".isurname").val(),
                imidname = tr.find(".imidname").val(),
                iaccode = tr.find(".iaccode").val(),
                iaddress = tr.find(".iaddress").val(),
                icity = tr.find(".icity").val(),
                igender = tr.find(".igender").val(),
                ipin = tr.find(".ipin").val(),
                iemail = tr.find(".iemail").val(),
                imobile = tr.find(".imobile").val();


        $(".dataname").val(iname);
        $(".dataaccode").val(iaccode);
        $(".datasurname").val(isurname);
        $(".datasuffix").val(isuffix).trigger("change");
        $(".datamidname").val(imidname);
        $(".datagender").val(igender).trigger("change");
        $(".datamobile").val(imobile);
        $(".datacity").val(icity);
        $(".dataaddress").val(iaddress);
        $(".datapin").val(ipin);
        $(".dataemail").val(iemail);

        $(".dataname").removeClass('dataname');
        $(".dataaccode").removeClass('dataaccode');
        $(".datasuffix").removeClass('datasuffix');
        $(".datasurname").removeClass('datasurname');
        $(".datamidname").removeClass("datamidname");
        $(".datamobile").removeClass("datamobile");
        $(".datagender").removeClass("datagender");
        $(".dataemail").removeClass("dataemail");
        $(".datacity").removeClass("datacity");
        $(".datapin").removeClass("datapin");
        $(".dataaddress").removeClass("dataaddress");

        $(".custModal").modal("hide");

    }


//    $(".qty").blur(function () {
//        $(".dataQty").removeClass("dataQty");
//    });
});