
$(document).ready(function () {


    getdata(1, 0, 0, '');

});




function newrows(ctl) {

    $('#tbldt tr').find('input').each(function () {


        if ($(this).attr('id').indexOf('Seq') != -1) {

            var ctlname = $(this).attr('id');
            var arrctl = ctlname.split('_');

            let irow = parseFloat(arrctl[3].toString());
            irow = irow + 1;
            addrow("Y");
            return false;



        }
    });

}

let inputrow = 0;
function addrow(news) {



    $('#tbldt tr td').find('span').each(function () {


        if ($(this).attr('id').indexOf('_SeqNo_') != -1) {

            var ctlname = $(this).attr('id');
            var arrctl = ctlname.split('_');

            inputrow = parseFloat(arrctl[3].toString());



        }
    });

    var aClone = $('#tbldt tbody>tr:last').clone(true)
    var regex = new RegExp("_" + inputrow.toString(), "g");
    var newrow = aClone.html().replace(regex, "_" + (inputrow + 1).toString());
    inputrow++;
    $("#tbldt > tbody:last").append("<tr  onclick=\"javascript:edit('_" + inputrow.toString() + "');return false;\"> id='rowd_" + inputrow.toString() + "'>" + newrow + "</tr>");






    controlpage();





}



let pubirow = 0;
let pubfilter = "";
let pubiseqno = 1;
let pubcurrentpage_checkitem = 1;
let publastpage = 1;
function getnext_item() {


    var elmnt = document.getElementById("gtbl");
    var y = elmnt.scrollTop;
    var x = elmnt.scrollHeight;
    var n = elmnt.clientHeight;
    //$("#MainContent_txt_linegroup").val(parseInt(y, 0).toString() + "," + (x - n).toString());
    if (parseInt(y, 0) == (x - n) - 1 || parseInt(y, 0) == (x - n)) {


        try {

            setTimeout(
              function () {
                  let len = $("#tbldt tbody").find('tr').length;

                  pubcurrentpage_checkitem = ((len - 1) / 10);
                  pubcurrentpage_checkitem = pubcurrentpage_checkitem + 1;
                  if (publastpage == pubcurrentpage_checkitem) {
                      pubcurrentpage_checkitem = pubcurrentpage_checkitem + 1;
                      publastpage = pubcurrentpage_checkitem;
                  }

                  publastpage = pubcurrentpage_checkitem;


                  getdata(pubcurrentpage_checkitem, pubirow, parseInt(y, 0), pubfilter);



              }, 500);



        } catch (err) { };



    }

}

let iseqno = 1;
let ipub = 0;
function getdata(currentpage, irow, scposstock, filter) {




    iseqno = irow + 1;
    pubfilter = filter;

    $.ajax({
        type: "POST",
        url: "Register_operator.aspx/getdata",
        data: '{parahd:"' + filter + '",currentpage:"' + currentpage + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var customers = xml.find("Table");
            if (customers.length == 0) {



            }

            $.each(customers, function () {
                var customer = $(this);

                addrow("Y");


                $("#MainContent_txt_SeqNo_" + irow.toString()).html(iseqno.toString());
                $("#MainContent_txt_empid_" + irow.toString()).html($(this).find("approve_userid").text());
                $("#MainContent_txt_empname_" + irow.toString()).html($(this).find("approve_username").text());
                $("#MainContent_txt_approvelevel_" + irow.toString()).html($(this).find("approve_position").text());
                $("#approvelogid_" + irow.toString()).html($(this).find("approve_id").text());


                irow++;
                iseqno++;
            });

            pubirow = irow;
            if (currentpage > 1) {
                $("#tbdy").animate({ scrollTop: (scposstock + 100) }, "fast");
            }



            setTimeout(function () {

                $('#tbldt tbody>tr span').each(function () {
                    var ctl = $(this).attr('id');
                    if (ctl.indexOf("_txt_empid_") != -1) {

                        if ($(this).html() == "") {

                            $(this).closest('tr').remove();
                        }

                    }


                });
            }, 500);


        },
        failure: function (response) {
            alert(response.d);
        },
        error: function (response) {
            alert(response.d);
        }
    });


}


function save() {

    if (checkreq().length == 0) {

      
        let inspectionid = $("#MainContent_txt_inspectionid").val();
        let inspectionname = $("#MainContent_txt_inspectionname").val();
        let linegroupid = $("#MainContent_txt_linegroup").val();
        let linename  = $("#MainContent_txt_linename").val();
        let partnumber = $("#MainContent_txt_partnumber").val();
        let empid = $("#MainContent_txt_empid").val();
        let empname = $("#MainContent_txt_empname").val();
        let approveid = $("#MainContent_txt_authorityid").val();
        let approvename = $("#MainContent_txt_authorityname").val();
        let userid = $("#lbluserid").html();


        let strsend = inspectionid + '|' + inspectionname + '|' + linegroupid + '|' + linename + '|' + partnumber + '|' + empid + '|' + empname + '|' + approveid + '|' + approvename + '|' + userid + '|';
        
        let Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function (e) { var t = ""; var n, r, i, s, o, u, a; var f = 0; e = Base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++); r = e.charCodeAt(f++); i = e.charCodeAt(f++); s = n >> 2; o = (n & 3) << 4 | r >> 4; u = (r & 15) << 2 | i >> 6; a = i & 63; if (isNaN(r)) { u = a = 64 } else if (isNaN(i)) { a = 64 } t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a) } return t }, decode: function (e) { var t = ""; var n, r, i; var s, o, u, a; var f = 0; e = e.replace(/[^A-Za-z0-9+/=]/g, ""); while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++)); o = this._keyStr.indexOf(e.charAt(f++)); u = this._keyStr.indexOf(e.charAt(f++)); a = this._keyStr.indexOf(e.charAt(f++)); n = s << 2 | o >> 4; r = (o & 15) << 4 | u >> 2; i = (u & 3) << 6 | a; t = t + String.fromCharCode(n); if (u != 64) { t = t + String.fromCharCode(r) } if (a != 64) { t = t + String.fromCharCode(i) } } t = Base64._utf8_decode(t); return t }, _utf8_encode: function (e) { e = e.replace(/rn/g, "n"); var t = ""; for (var n = 0; n < e.length; n++) { var r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) { t += String.fromCharCode(r >> 6 | 192); t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224); t += String.fromCharCode(r >> 6 & 63 | 128); t += String.fromCharCode(r & 63 | 128) } } return t }, _utf8_decode: function (e) { var t = ""; var n = 0; var r = c1 = c2 = 0; while (n < e.length) { r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r); n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1); t += String.fromCharCode((r & 31) << 6 | c2 & 63); n += 2 } else { c2 = e.charCodeAt(n + 1); c3 = e.charCodeAt(n + 2); t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63); n += 3 } } return t } }
        let parahd = Base64.encode(strsend);


        $.ajax({
            type: "POST",
            url: "Register_operator.aspx/save",
            data: '{parahd:"' + parahd + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                let res = response.d;
                if (res.length == 0) {
                    swal(
                     'Success',
                     'Data saved successfully',
                     'success'
                     );

                    setTimeout(function () {
                        location.reload();
                    }, 1000);


                }
                else {
                    swal(
                     'Error',
                      res,
                     'error'
                      );
                }


            },
            failure: function (response) {
                alert(response.d);
            },
            error: function (response) {
                alert(response.d);
            }
        });

    }
}


function edit(irow)
{


    let approvelogid = $("#approvelogid" + irow.toString()).html();
    $.ajax({
        type: "POST",
        url: "Register_operator.aspx/getdataedit",
        data: '{approvelogid:"' + approvelogid+ '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var customers = xml.find("Table");
            if (customers.length == 0)
            {



            }

            $.each(customers, function () {
                var customer = $(this);

              
                $("#MainContent_txt_inspectionid").val($(this).find("inspectionid").text());
                $("#MainContent_txt_inspectionname").val($(this).find("inspectionname").text());
                $("#MainContent_txt_linegroup").val($(this).find("linegroupid").text());
                $("#MainContent_txt_linename").val($(this).find("linename").text());
                $("#MainContent_txt_partnumber").val($(this).find("partnumber").text());
                $("#MainContent_txt_empid").val($(this).find("empid").text());
                $("#MainContent_txt_empname").val($(this).find("empname").text());
                $("#MainContent_txt_authorityid").val($(this).find("approveid").text());
                $("#MainContent_txt_authorityname").val($(this).find("approvename").text());          



               



            });

           


        },
        failure: function (response) {
            alert(response.d);
        },
        error: function (response) {
            alert(response.d);
        }
    });


}

function gvdelrowid(ctl, msg) {

    let resindex = ctl.id.split("_");
    let irow = resindex[2].toString();
    let rowCount = $('#tbldt tr').length;
    rowCount = rowCount - 1;

    swal({
        title: 'Are you sure?',
        text: "Do you want delete this data!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'
    }).then(function (result) {
        if (result.value) {


            let approvelogid = $('#approvelogid_' + irow.toString()).html();

            $.ajax({
                type: "POST",
                url: "Register_operator.aspx/DeleteRow",
                data: "{approvelogid:'" + approvelogid + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {


                },
                failure: function (response) {
                    alert(response.d);
                },
                error: function (response) {
                    alert(response.d);
                }
            });


            if (rowCount > 1) {
                $(ctl).closest('tr').remove();

            }
            else {
                if (irow != "0") {

                    $(ctl).closest('tr').remove();
                }
                else {
                    $('#tbldt tbody>tr span').each(function () {
                        $(this).html("");
                    });

                }
            }





            swal(
              'Deleted!',
              'Your file has been deleted.',
              'success'
            );
        } else {
            return false;
        }
    })








}