

$(document).ready(function () {
    

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

});


function save() {

    if (checkreq().length == 0) {
       
        let userid = $("#lbluserid").html();
        let linegroup = $("#MainContent_txt_linegroup").val();
        let linename = $("#MainContent_txt_linename").val();
        let partnumber = $("#MainContent_txt_partnumber").val();

        let piz = getCookie("pizinfo");


        let strsend = linegroup + '|' + linename + '|' + partnumber + '|' + userid + '|' + piz + '|';



        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: "RegisterLine.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 linegroup = $("#MainContent_txt_LineGroup" + irow).html();
    let linename = $("#MainContent_txt_LineName" + irow).html();
    let partnumber = $("#MainContent_txt_PartNumber" + irow).html();




    $("#MainContent_txt_linegroup").val(linegroup);
    $("#MainContent_txt_linename").val(linename);
    $("#MainContent_txt_partnumber").val(partnumber);
    getpiz(partnumber);
}


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();





}
function getpiz(partnumber)
{

    $("#piz").attr('src', null);
    $.ajax({
        type: "POST",
        url: "../checkinput/Jig_check.aspx/getequ",
        data: '{partnumber: "' + partnumber + '"}',
        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);
                let piz = $(this).find("equ_piz").text();


                if (piz.length > 0) {
                    $("#piz").attr('src', piz);

                }
            });


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

}


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: "RegisterLine.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_LineGroup_" + irow.toString()).html($(this).find("linegroupid").text());
                $("#MainContent_txt_LineName_" + irow.toString()).html($(this).find("linename").text());
                $("#MainContent_txt_PartNumber_" + irow.toString()).html($(this).find("partnumber").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_LineGroup_") != -1) {

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

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

                    }


                });
            }, 500);


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


}



function gvedit(ctl) {
    let ctlname = $(ctl).attr('id');
    let arrctl = ctlname.split('_');
    let ctlrow = parseFloat(arrctl[2].toString());

    $('#tbldt tbody>tr input[type="text"]').each(function () {
        let ctlid = $(this).attr('id');
        if (ctlid.indexOf("_" + ctlrow.toString()) != -1) {
            $(this).css({ 'border-style': 'solid' });
            $(this).prop('readonly', false);

        }
    });


}






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 linegroupid = $('#MainContent_txt_LineGroup_' + irow.toString()).html();
            let partnumber = $('#MainContent_txt_PartNumber_' + irow.toString()).html();
      
            $.ajax({
                type: "POST",
                url: "RegisterLine.aspx/DeleteRow",
                data: "{linegroupid:'" + linegroupid + "',partnumber:'" + partnumber + "'}",
                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;
        }
    })








}



var interval;
function takephoto() {

    let linegroup = $("#MainContent_txt_linegroup").val();
    let partnumber = $("#MainContent_txt_partnumber").val();


    let para = linegroup + "|" + partnumber + "|equ|";
    //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 str_para = Base64.encode(para);
    setCookie("pizinfo", "", 5);

    let str_para = para;
    interval = setInterval(checkscan, 1000);


    window.open(
"Opencamera_takephoto?para=" + str_para,
'_blank' // <- This is what makes it open in a new window.
);




}
function checkscan() {
    let piz_data = getCookie("pizinfo");
    if (piz_data.length > 0) {

        //setCookie("pizinfo", "", 5);
        clearInterval(interval);
        $("#piz").attr('src', piz_data);


    }

}
