$(document).ready(function () {

    setCookie("pizinfo", "", 5);
    getdataheader();

});
let pubequid = "";
let pubinspectionid = "";
let pubitemname = "";
let pubresult = "";
let publink = "";
function getdataheader() {



    let str_para = getUrlVars()["para"];
    publink = getUrlVars()["link"];
    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 de_para = Base64.decode(str_para);
    if (de_para.indexOf("|") != -1) {
        let arr_para = de_para.split('|');

        let equid = arr_para[0];
        let inspectionid = arr_para[1];

        pubequid = equid;
        pubinspectionid = inspectionid;

        $.ajax({
            type: "POST",
            url: "Machine_input_check_status.aspx/getinspectiondetail",
            data: '{equid:"' + equid + '", inspectionid: "' + inspectionid + '"}',
            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_checkitem").html("<div style='padding-top:6px;font-size:12px;line-height:12px;'>" + $(this).find("itemname").text() + "</div>");
                    $("#MainContent_txt_procedure").html("<div style='line-height:12px;font-size:8px;margin-top:-15px;'>"+$(this).find("howcheck").text()+"</div>");
                    $("#MainContent_txt_criteria").html("<div style='padding-top:6px;font-size:14px;'>" + $(this).find("criteriatext").text() + "</div>");
                    $("#MainContent_txt_method").html("<div style='padding-top:6px;font-size:14px;'>" + $(this).find("input_method").text() + "</div>");
                    pubitemname = $(this).find("itemname").text();


                });


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


        let strhd = equid + '|' + inspectionid + '|';

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

        $.ajax({
            type: "POST",
            url: "Machine_input_check_status.aspx/getdata",
            data: '{parahd:"' + parahd + '"}',
            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 status = $(this).find("resultstatus").text();
                    pubresult = status;
                    if (status == "OK")
                    {
                        $("#MainContent_btnok").css({ 'background-color': '#77A9F9' });

                    }
                    else if (status == "NG")
                    {
                        $("#MainContent_btnng").css({ 'background-color': '#F8183D' });

                    }

                    $("#MainContent_txt_notice").val($(this).find("notice").text());
                    let piz = $(this).find("insp_piz").text();
                    setCookie("pizinfo", piz, 5);

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

                    }

                });


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





    }






}
function btncheck(ctl)
{
   

    let ctlid = $(ctl).attr('id');
   

    if (ctlid.indexOf("btnok") != -1)
    {
        if ($("#" + ctlid).css('background-color') == "rgb(227, 188, 8)")
        {
            
            $("#MainContent_btnok").css('background-color', '#77A9F9');
            $("#MainContent_btnng").css('background-color', '#E3BC08');
            pubresult = "OK";
        }
        else
        {

            $("#MainContent_btnok").css('background-color', '#E3BC08');
            $("#MainContent_btnng").css('background-color', '#E3BC08');
            pubresult = "";

        }

    }
    else
    {
        if ($("#" + ctlid).css('background-color') == "rgb(227, 188, 8)") {

            $("#MainContent_btnng").css('background-color', '#F8183D');
            $("#MainContent_btnok").css('background-color', '#E3BC08');
            pubresult = "NG";
        }
        else {

            $("#MainContent_btnok").css('background-color', '#E3BC08');
            $("#MainContent_btnng").css('background-color', '#E3BC08');
            pubresult = "";

        }

    }

    
}

function save()
{

    let notic = $("#MainContent_txt_notice").val();
    if (pubresult == "")
    {
        swal(
         'Warning',
         'Please select status.',
         'warning'
       );
        
    }
    else if ($("#MainContent_btnng").css('background-color') == "rgb(248, 24, 61)" && notic.length == 0) {

        swal(
          'Warning',
          'Please input notice.',
          'warning'
        );
        $("#MainContent_txt_notice").focus();
        $("#MainContent_txt_notice").select();

    }
    else {

        let strhd = "";
        let equid = pubequid;
        let inspectid = pubinspectionid;
        let itemname = pubitemname;
        let type = "1";
        let result = pubresult;
        let notic = $("#MainContent_txt_notice").val();
        let user = "admin";
        let piz = getCookie("pizinfo");

        let inspectid_id = "";


        strhd = inspectid_id + '|' + equid + '|' + inspectid + '|' + itemname + '|' + type + '|' + result + '|' + notic + '|' + user + '|' + 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(strhd);



        $.ajax({
            type: "POST",
            url: "Machine_input_check_status.aspx/save",
            data: '{parahd:"' + parahd + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                res = response.d;

                if (res.length == 0) {

                    swal(
                    'Success',
                    'Data saved successfully',
                    'success'
                    );
                    
                    setTimeout(function () {
                        window.location.href = "Machine_check_item?para=" + publink;
                    }, 1000);
                    

                }
                else {

                    swal(
                     'Error',
                      res,
                     'error'
                     );

                }


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


    }



}

function checklen(ctl)
{
    let notic = $("#MainContent_txt_notice").val();
    let remain = 100 - notic.length;
    $("#strnotice").html("Input have remaining " + remain.toString() + " character.");
    if(remain<=0)
    {
        $("#MainContent_txt_notice").val(notic.substring(0,99));

    }


}

function scanmachine() {


    setCookie("qrcode", "", 5);
    $("#MainContent_txt_machineid").val("");
    $("#MainContent_txt_machinename").val("");
    interval1 = setInterval(checkscanmachine, 1000);



}
var interval;
function takephoto() {

    let para = pubequid + "|" + pubinspectionid + "|1|";
    //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");
    //alert(piz_data);
    if (piz_data.length > 0) {

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


    }
    
}

