var interval;
var interval1;
$(document).ready(function () {

    getdataheader();
    try {
        $("#MainContent_txt_empid").val($("#lbluserid").html());
        getemp();
    } catch (err) { }


    $('#MainContent_txt_partnumber').on('input', function (event) {

        let partnumber = $("#MainContent_txt_partnumber").val();
        $("#piz").attr('src', null);
        $.ajax({
            type: "POST",
            url: "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);
            }
        });





    });

    $('#MainContent_txt_empid').on('input', function (event) {

        let empid = $("#MainContent_txt_empid").val();
        $.ajax({
            type: "POST",
            url: "Machine_condition_check.aspx/getempid",
            data: '{empid: "' + empid + '"}',
            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) {

                    $("#MainContent_txt_empname").val("");

                }

                $.each(customers, function () {
                    var customer = $(this);
                    $("#MainContent_txt_empid").val($(this).find("empid").text());
                    $("#MainContent_txt_empname").val($(this).find("empname").text());


                });


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

        if (empid.length == 1) {
            let partnumber = $("#MainContent_txt_partnumber").val();
            $("#piz").attr('src', null);
            $.ajax({
                type: "POST",
                url: "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);
                }
            });

        }



    });

    
});

function calldata_other() {

    let partnumber = $("#MainContent_txt_partnumber").val();
    $("#piz").attr('src', null);
    $.ajax({
        type: "POST",
        url: "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);
        }
    });

}


function getemp() {
    let empid = $("#MainContent_txt_empid").val();
    $.ajax({
        type: "POST",
        url: "Machine_condition_check.aspx/getempid",
        data: '{empid: "' + empid + '"}',
        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) {

                $("#MainContent_txt_empname").val("");

            }

            $.each(customers, function () {
                var customer = $(this);
                $("#MainContent_txt_empid").val($(this).find("empid").text());
                $("#MainContent_txt_empname").val($(this).find("empname").text());


            });


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


function getdataheader() {

    const today = new Date();
    let day = String(today.getDate()).padStart(2, '0');
    let month = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
    let year = today.getFullYear();
    let hh = String(today.getHours()).padStart(2, '0');
    let mm = String(today.getMinutes()).padStart(2, '0');


    let currentDate = day + '-' + month + '-' + year + "  " + hh + ":" + mm;

    $("#MainContent_txt_datetime").val(currentDate);




}

function checkscanempid() {
    let qrcode_data = getCookie("qrcode");
    if (qrcode_data.length > 0) {

        setCookie("qrcode", "", 5);
        clearInterval(interval);

        $.ajax({
            type: "POST",
            url: "Machine_condition_check.aspx/getempid",
            data: '{empid: "' + qrcode_data + '"}',
            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 empid = $(this).find("usercode").text();
                    let empname = $(this).find("firstname").text() + "  " + $(this).find("lastname").text();
                    $("#MainContent_txt_empid").val(empid);
                    $("#MainContent_txt_empname").val(empname);
                });


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

       


    }



}
function scanempid() {
    setCookie("qrcode", "", 5);
    $("#MainContent_txt_empid").val("");
    $("#MainContent_txt_empname").val("");
    interval = setInterval(checkscanempid, 1000);

    window.open(
  "Opencamera",
  '_blank' // <- This is what makes it open in a new window.
 );

}





function inspection() {
    if (checkreq().length == 0) {
        let ddate = $("#MainContent_txt_datetime").val();
        let partnumber = $("#MainContent_txt_partnumber").val();
        let empid = $("#MainContent_txt_empid").val();
        let empname = $("#MainContent_txt_empname").val();



        let para = ddate + "|" + partnumber + "|" + empid + "|" + empname + "|";
        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);

        window.location.href = "4M1E_input_check?para=" + str_para;
    }


}
