$(document).ready(function () {

    $("#loader").hide();

    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;

    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 matchind = arr_para[3];
        pubmonth = arr_para[4];
        pubyear = arr_para[5];
        let lamp = arr_para[8];
        let model = arr_para[9];

       

        $("#MainContent_txt_linegroup").val(linegroup);
        $("#MainContent_txt_linename").val(linename);
        $("#MainContent_txt_partnumber").val(partnumber);
        $("#MainContent_txt_checkmonth").val(pubmonth);
        $("#MainContent_txt_checkyear").val(pubyear);
        $("#MainContent_txt_lamp").val(lamp);
        $("#MainContent_txt_model").val(model);



        getdata_tbl_header(pubyear, pubmonth);
        $("#MainContent_lblhd").html(pubyear + "/" + pubmonth);

    }
});
function getdate() {

    let month = $("#MainContent_txt_checkmonth").val();
    let year = $("#MainContent_txt_checkyear").val();
    getdata_tbl_header(year, month);
    $("#MainContent_lblhd").html(year + "/" + month);


}

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


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

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

            inputrow = parseFloat(arrctl[1].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 span').each(function () {
        let ctl = $(this).attr('id');
        if (ctl.indexOf("seqno_") != -1) {
            if ($(this).is(':hidden')) {

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

            }
        }
    });






}


let tbl0 = "";
let tbl1 = "";
let tbl2 = "";
function getdata_tbl_header(year, month) {

    $("#loader").show();
    tbl0 = "<table class='tbl' id='tbldt' style='width:100%;'>";


    tbl1 = "<thead><tr><th style='width:60px;'><span style='font-size:12px;' id='sun'>Sun</span></th>" +
        "<th style='width:60px;'><span style='font-size:12px;' id='mon'>Mon</span></th>" +
        "<th style='width:60px;'><span style='font-size:12px;' id='tue'>Tue</span></th>" +
        "<th style='width:60px;'><span style='font-size:12px;' id='wed'>Wed</span></th>" +
        "<th style='width:60px;'><span style='font-size:12px;' id='tur'>Tur</span></th>" +
        "<th style='width:60px;'><span style='font-size:12px;' id='fri'>Fri</span></th>" +
        "<th style='width:60px;'><span style='font-size:12px;' id='sat'>Sat</span></th>";



    tbl2 = "<tbody id='tbdy'>";
    //<tr><td id='sun_0'></td>" +
    //"<td id='mon_0'></td>" +
    //"<td id='tue_0'></td>" +
    //"<td id='wed_0'></td>" +
    //"<td id='tur_0'></td>" +
    //"<td id='fri_0'></td>" +
    //"<td id='sat_0'></td>";



    $.ajax({
        type: "POST",
        url: "Jig_checklist_create.aspx/GetAllDay",
        data: '{year: "' + year + '",month:"' + month + '"}',
        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 dayname = $(this).find("listdayname").text();
                let dayseq = $(this).find("listnumday").text();

                if (dayseq == "1") {

                    if (dayname.indexOf("Saturday") != -1) {

                        tbl2 = tbl2 + "<td></td><td></td><td></td><td></td><td></td><td></td><td style='max-width:60px' id='Saturday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Saturday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'><span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td></tr>";

                    }
                    if (dayname.indexOf("Sunday") != -1) {

                        tbl2 = tbl2 + "<tr><td  style='max-width:60px' id='Sunday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Sunday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'><span style='width:100%;'><span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td>";

                    }
                    if (dayname.indexOf("Monday") != -1) {

                        tbl2 = tbl2 + "<td></td><td  style='max-width:60px'  id='Monday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Monday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'><span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td>";
                    }
                    if (dayname.indexOf("Tuesday") != -1) {

                        tbl2 = tbl2 + "<td></td><td></td><td   style='max-width:60px'  id='Tuesday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Tuesday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'> <span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td>";

                    }
                    if (dayname.indexOf("Wednesday") != -1) {


                        tbl2 = tbl2 + "<td></td><td></td><td></td><td  style='max-width:60px'  id='Wednesday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Wednesday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'><span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td>";

                    }
                    if (dayname.indexOf("Thursday") != -1) {


                        tbl2 = tbl2 + "<td></td><td></td><td></td><td></td><td  style='max-width:60px'  id='Thursday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Thursday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'> <span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td>";

                    }
                    if (dayname.indexOf("Friday") != -1) {


                        tbl2 = tbl2 + "<td></td><td></td><td></td><td></td><td></td><td  style='max-width:60px'  id='Friday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Friday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'> <span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td>";
                    }



                }
                else {
                    if (dayname.indexOf("Saturday") != -1) {

                        tbl2 = tbl2 + "<td style='max-width:60px' id='Saturday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Saturday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'> <span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td></tr>";

                    }
                    if (dayname.indexOf("Sunday") != -1) {

                        tbl2 = tbl2 + "<tr><td  style='max-width:60px' id='Sunday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Sunday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'> <span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td>";

                    }
                    if (dayname.indexOf("Monday") != -1) {

                        tbl2 = tbl2 + "<td  style='max-width:60px'  id='Monday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Monday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'> <span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td>";
                    }
                    if (dayname.indexOf("Tuesday") != -1) {

                        tbl2 = tbl2 + "<td   style='max-width:60px'  id='Tuesday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Tuesday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'> <span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td>";

                    }
                    if (dayname.indexOf("Wednesday") != -1) {


                        tbl2 = tbl2 + "<td  style='max-width:60px'  id='Wednesday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Wednesday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'> <span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td>";

                    }
                    if (dayname.indexOf("Thursday") != -1) {


                        tbl2 = tbl2 + "<td  style='max-width:60px'  id='Thursday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Thursday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'> <span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td>";

                    }
                    if (dayname.indexOf("Friday") != -1) {


                        tbl2 = tbl2 + "<td  style='max-width:60px'  id='Friday" + $(this).find("listnumday").text() + "_0'><a href='#' onclick='javascript:setdate(\"" + $(this).find("listday").text() + "\",\"Friday" + $(this).find("listnumday").text() + "_0\");return false;' style='text-decoration: none;'> <span style='width:40px;display: block;text-align: center;margin:auto;line-height:40px; vertical-align: middle;'> " + $(this).find("listnumday").text() + "</span></a></td>";
                    }

                }


            });


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


    setTimeout(function () {
        tbl1 = tbl1 + "</tr></thead>";
        let strtbl = tbl0 + tbl1 + tbl2 + "</tr></tbody></table>";

        $("#g2").html(strtbl);
        $("#loader").hide();



        let lineid = $("#MainContent_txt_linegroup").val();
        let partnumber = $("#MainContent_txt_partnumber").val();
        let user = 'admin';


        $.ajax({
            type: "POST",
            url: "Jig_checklist_create.aspx/GetDayOff",
            data: "{year:'" + year + "',month:'" + month + "',lineid:'" + lineid + "',partnumber:'" + partnumber + "',user:'" + user + "'}",
            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);


                    $('#' + $(this).find("listnumday").text() + '_0').css({ 'background-color': '#84B7EE' });

                });


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



    }, 1500);


}
function setdate(valdate, ctl) {

    if ($("#" + ctl).css("background-color") == "rgb(132, 183, 238)") {

        $("#" + ctl).css({ 'background-color': '#FFFFFF' });
        save("del", valdate, ctl);

    }
    else {

        $("#" + ctl).css({ 'background-color': '#84B7EE' });
        save("add", valdate, ctl);
    }


    //alert(valdate);
    //alert(ctl);

}
function save(type, dayoff, ctl) {

    let month = $("#MainContent_txt_checkmonth").val();
    let year = $("#MainContent_txt_checkyear").val();
    let lineid = $("#MainContent_txt_linegroup").val();
    let partnumber = $("#MainContent_txt_partnumber").val();
    let user = "admin";

    $.ajax({
        type: "POST",
        url: "Jig_checklist_create.aspx/save",
        data: "{dayoff:'" + dayoff + "',type:'" + type + "',month:'" + month + "',year:'" + year + "',lineid:'" + lineid + "',partnumber:'" + partnumber + "',user:'" + user + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {

            if (response.d.length > 0) {


                swal(
                   'Error',
                   response.d,
                   'error'
                 );
                $("#" + ctl).css({ 'background-color': '#FFFFFF' });

            }


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


}

function saveall() {
    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
         })
     })

}


function back() {


    window.location.href = "Jig_inspection_result?para=" + publink;


}
