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


    getdata_tbl_header(year, month);
    $("#MainContent_lblhd").html(year + "/" + month);


});
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: "XR_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: "XR_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: "XR_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(
     'Success',
     'Data saved successfully',
     'success'
     );


}