$(document).ready(function () {

    getdataheader();
   

});
let publink = "";
let pubdayvalue = "";
let pubitemvalue = "";
let pubmonth="";
let pubyear="";
function getdataheader() {


    let str_para = getUrlVars()["para"];
    publink = str_para;
    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 linegroup = arr_para[0];
        let linename = arr_para[1];
        let partnumber = arr_para[2];
        let machinename = arr_para[3];
        pubmonth = arr_para[4];
        pubyear = arr_para[5];
        pubdayvalue = arr_para[6];
        pubitemvalue = arr_para[7];
        let lamp = arr_para[8];
        let model = arr_para[9];
        let userid = arr_para[10];

        $("#MainContent_txt_linegroup").html(linegroup);
        $("#MainContent_txt_linename").html(linename);
        $("#MainContent_txt_partnumber").html(partnumber);
        $("#MainContent_txt_lamp").html(lamp);
        $("#MainContent_txt_model").html(model);
        $("#MainContent_txt_machine").html(machinename);
        getinspectionmaster(1, 0, 0, '');
        //getdata_tbl_header(pubyear, pubmonth);
    }






}



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').find('input').each(function () {


        if ($(this).attr('id').indexOf('Seq') != -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 id='rowd_" + inputrow.toString() + "'>" + newrow + "</tr>");


    let iseq = 1;
    $('#tbldt tbody>tr input[type="text"]').each(function () {
        let ctl = $(this).attr('id');
        if (ctl.indexOf("Seq") != -1) {
            if ($(this).is(':hidden')) {

            } else {
                $(this).val(iseq.toString());
                iseq++;

            }
        }
    });


    $('#tbldt tbody>tr input[type="text"]').each(function () {

        $(this).addClass("fonttextcontent");
        $(this).css({ 'border-style': 'none' });
        $(this).prop('readonly', true);


    });







}


let pubirow = 0;
let pubfilter = "";
let pubiseqno = 1;
let pubcurrentpage_checkitem = 1;

function getnext_item() {


    var elmnt = document.getElementById("g2");
    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 = Math.ceil((len - 1) / 10);
                  pubcurrentpage_checkitem = pubcurrentpage_checkitem + 1;
                  getinspectionmaster(pubcurrentpage_checkitem, pubirow, parseInt(y, 0), pubfilter);



              }, 500);



        } catch (err) { };



    }

}

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

    let iseqno = irow + 1;
    pubfilter = filter;
    let userid = $("#lbluserid").html();
    $.ajax({
        type: "POST",
        url: "Machine_inspection_machine_sub_confirm.aspx/getinspection",
        data: '{parahd:"' + filter + '",currentpage:"' + currentpage + '",dayvalue:"'+pubdayvalue+'",itemvalue:"'+pubitemvalue+'",month:"'+pubmonth+'",year:"'+pubyear+'",userid:"'+userid+'"}',
        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_Item_' + irow.toString()).html($(this).find("itemname").text() + "<span id='inspid_"+irow.toString()+"' style='font-size:1px;color:white;'>" + $(this).find("inspidid").text() + '</span>');
                $('#MainContent_txt_Date_' + irow.toString()).html($(this).find("recdate").text());
                $('#MainContent_txt_Comment_' + irow.toString()).html("<a href='#' onclick=\"javascript:showdetail(\'" + $(this).find("notice").text() + "\');return false;\"  style='text-decoration:none;font-size:16px;'>" + $(this).find("noticeshort").text() + "</a>");
                $('#MainContent_txt_RejectComment_' + irow.toString()).html("<a href='#' onclick=\"javascript:showdetail(\'" + $(this).find("confirm_notice").text() + "\');return false;\"  style='text-decoration:none;font-size:16px;'>" + $(this).find("confirm_noticeshort").text() + "</a>");
                if ($(this).find("confirm_status").text() == "Y")
                {
                    $("#MainContent_btn_check_" + irow.toString()).css({ 'background-color': '#77A9F9' });

                }
                else if($(this).find("confirm_status").text() == "N")
                {
                    $("#MainContent_btn_reject_" + irow.toString()).css({ 'background-color': '#F8183D' });

                }

                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_Item_") != -1) {

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

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

                    }


                });
            }, 500);


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


}

function showdetail(detail)
{


                       swal(
                       'Notice',
                       detail,
                       'info'
                       );




}

function confirm(ctl,resultconfirm)
{

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


                if (resultconfirm == "N") {

                    opennotice(ctl);

                }
                else
                {
                    confirm_save(ctl);

                }
       




            } else {
                return false;
            }
        })
       

}
function confirm_save(ctl)
{

    let ctlid = $(ctl).attr('id');
    let arr = ctlid.split('_');
    let irow = arr[3];
    let result = "";

    if (ctlid.indexOf("btn_check_") != -1)
    {
        $("#MainContent_btn_check_" + irow).css({ 'background-color': '#77A9F9' });
        result = "Y";
    }
    else
    {
        $("#MainContent_btn_reject_" + irow).css({ 'background-color': '#F8183D' });
        result = "N";

    }
    


    let spectid = $("#inspid_" + irow).html();
    let notice = $('#txt_notice').val();
    let userid = $("#lbluserid").html();

    $.ajax({
        type: "POST",
        url: "ConfirmMessage.aspx/saveconfirm",
        data: '{id:"' + spectid + '",status:"' + result + '", notice:"' + notice + '",userid:"' + userid + '",insp_type:"1"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {

            if (response.d == "") {

                swal({
                    title: "Success!",
                    type: 'success',
                    text: "Data saved successfully.",
                    timer: 1000,
                    showConfirmButton: false,
                    timerProgressBar: true
                })

    .then(() => {
        swal({ 
            title: 'Finished!',
            type: 'info',
            timer: 1000,
            showConfirmButton: false
        })
    })


            }
            else {
                swal(
                    'Error',
                    response.d,
                    'error'
                    );


            }


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


   



}
let irow_confirm = "";
function opennotice(ctl) {
    let ctlid = $(ctl).attr('id');
    let arr = ctlid.split('_');
    irow_confirm = arr[3];

    $('#txt_notice').val("");
    var modal = document.getElementById('NoticeModal');
    modal.style.display = "block";


}
function savenotice() {

  



            let notice = $('#txt_notice').val();
            if (notice.length > 15) {
                let subnotice = notice.substring(0, 14);
                $("#MainContent_txt_RejectComment_" + irow_confirm).html(subnotice);

            }
            else {

                $("#MainContent_txt_RejectComment_" + irow_confirm).html(notice);

            }
            $("#MainContent_btn_reject_" + irow_confirm).css({ 'background-color': '#F8183D' });

            confirm_save($("#MainContent_btn_reject_" + irow_confirm));
            var modal = document.getElementById('NoticeModal');
            modal.style.display = "none";




}

function back() {
    
 window.location.href = "Machine_inspection_sub_result?para=" + publink;
    

}