let publevel = 0;
function getdocapprove(docno)
{
    cleartbl();
    $("#loader").show();
    setTimeout(function () {
        $("#MainContent_txt_docno").html(docno);
        getData();
    }, 500);

    setTimeout(function () {
        $("#MainContent_txt_docno").html(docno);
        getDataTable();
        $("#loader").hide();
    }, 1000);

}
function cleartbl() {


    $('#tbldt tbody>tr span').each(function () {


        var ctl = $(this).attr('id');
        if (ctl.indexOf("SeqNo") != -1) {
            var arr = ctl.split('_');

            if (arr[3] != "0") {

                $("#" + ctl).parents("tr").remove();


            }
           

        }

    });

    setTimeout(function () {
        $('#tbldt tbody>tr span').each(function () {


            $(this).html("");
           

        });
    }, 300);


}

$(document).ready(function () {


    
    try {

        let user = getUrlVars()["user"];
        cleartbl();
        getDoc(user);


    } catch (err) { };
 

    try {
        $("#MainContent_Panel1").css('height', $(window).height() - 20);
    } catch (err) { }




    


    $("#MainContent_txt_SeqNo_0").html("1");

    try {
        $("#g2").css('height', $(window).height() - 320);
    } catch (err) { }



    


    $('#MainContent_ddl_section').on('change', function (event) {
        getapproveflow(1, $("#lblskill"));
        getapproveflow(2, $("#lbleng"));
        getapproveflow(3, $("#lblmgr"));
        getapproveflow(4, $("#lblmgrdm"));

    });

    $(document).on("input", "textarea", function () {
        autosize(this);
    });
    $("textarea").each(function () {
        autosize(this);
    });

});
function newrowsform_enter(ctl) {
    inputrow = 0;
    var resindex = ctl.id.split("_");
    var irow = resindex[3].toString();

    irow = irow + 1;
    if (irow >= inputrow) {

        addrow("Y");

    }

}
function newrows(ctl) {
    inputrow = 0;
    var resindex = ctl.id.split("_");
    var irow = resindex[2].toString();

    irow = irow + 1;
    if (irow >= inputrow) {

        addrow("Y");

    }
}

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[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>");

    controlpage();




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

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

            }
        }
    });
}





function getData() {
    let docno = $("#MainContent_txt_docno").html();


    $.ajax({
        type: "POST",
        url: "Approve.aspx/GetDataHeader",
        data: '{docno: "' + docno + '"}',
        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_Panel1 span").each(function () {
                    $("#" + $(this).attr('id')).html("");
                });


            }
            $.each(customers, function () {
                var customer = $(this);





                $('#MainContent_txt_docno').html($(this).find("DocNo").text());
                $('#MainContent_ddl_section').html($(this).find("Section").text());
                $('#MainContent_txt_shop').html($(this).find("Shop").text());
                $('#MainContent_ddl_scraptype').html($(this).find("ScrapType").text());
                $('#MainContent_ddl_ngfrom').html($(this).find("NGFrom").text());
                publevel = $(this).find("ApproveLevel").text()

                var d = $(this).find("DocDate").text().slice(0, 10).split('-');
                var year = parseInt(d[0].toString());
                var rdate = d[2] + "/" + d[1] + "/" + year.toString();
                $('#MainContent_txt_date').html(rdate);

                $("#lblfullname").html($(this).find("ApproveName").text());
                $("#lblDept").html($(this).find("ApprovePosition").text());

                $("#lblfullname").css({ 'margin-left': '-20px' });
                $("#lblDept").css({ 'margin-left': '90px' });
                

                setstatusflow($(this).find("DocStatus").text(), $(this).find("ApproveStatus").text(), $(this).find("ApproveName1").text(), $(this).find("ApproveName2").text(), $(this).find("ApproveName3").text(), $(this).find("ApproveName4").text());


                //$("#lblskill").html($(this).find("ApproveName1").text());
                //$("#lbleng").html($(this).find("ApproveName2").text());
                //$("#lblmgr").html($(this).find("ApproveName3").text());
                //$("#lblmgrdm").html($(this).find("ApproveName4").text());

               



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


}

function setstatusflow(docstatus,approvestatus,approvename1,approvename2,approvename3,approvename4)
{
    let app1 = "";
    let app2 = "";
    let app3 = "";
    let app4 = "";

    let flow = " <div class=\"stepper-wrapper\"> " +
" <div class=\"stepper-item completed\"> " +
" <div class=\"step-counter\">1</div> " +
" <div class=\"step-name\">Scrap issued</div> " +
" </div> ";
    //alert(docstatus + "," + approvestatus);
    if (docstatus == "1" && approvestatus == "Y") {

        app1 = " <div class=\"stepper-item active completed\"> " +
" <div class=\"step-counter\">2</div> " +
" <div class=\"step-name\"><span id=\"lblskill\" style=\"width:150px;\"/>" + approvename1 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Skill sign</div> " +
"  </div>";

    }
    else if (docstatus == "1" && approvestatus == "N") {
        app1 = " <div class=\"stepper-item active reject\"> " +
" <div class=\"step-counter\">2</div> " +
" <div class=\"step-name\"><span id=\"lblskill\" style=\"width:150px;\"/>" + approvename1 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Skill sign</div> " +
"  </div>";

    }
    else {
        app1 = " <div class=\"stepper-item active\"> " +
" <div class=\"step-counter\">2</div> " +
" <div class=\"step-name\"><span id=\"lblskill\" style=\"width:150px;\"/>" + approvename1 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Skill sign</div> " +
"  </div>";

    }


    if (docstatus == "2" && approvestatus == "Y") {
        app2 = " <div class=\"stepper-item active completed\"> " +
  " <div class=\"step-counter\">3</div> " +
  " <div class=\"step-name\"><span id=\"lbleng\" style=\"width:150px;\"/>" + approvename2 + "</div> " +
  " <div class=\"step-name\" style=\"margin-top:5px;\">Eng. sign</div></div> ";
        app1 = " <div class=\"stepper-item active completed\"> " +
" <div class=\"step-counter\">2</div> " +
" <div class=\"step-name\"><span id=\"lblskill\" style=\"width:150px;\"/>" + approvename1 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Skill sign</div> " +
"  </div>";
    }
    else if (docstatus == "2" && approvestatus == "N") {
        app2 = " <div class=\"stepper-item active reject\"> " +
   " <div class=\"step-counter\">3</div> " +
   " <div class=\"step-name\"><span id=\"lbleng\" style=\"width:150px;\"/>" + approvename2 + "</div> " +
   " <div class=\"step-name\" style=\"margin-top:5px;\">Eng. sign</div></div> ";

    }
    else {
        app2 = " <div class=\"stepper-item active\"> " +
" <div class=\"step-counter\">3</div> " +
" <div class=\"step-name\"><span id=\"lbleng\" style=\"width:150px;\"/>" + approvename2 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Eng. sign</div></div> ";
 

    }


    if (docstatus == "3" && approvestatus == "Y") {
        app3 = " <div class=\"stepper-item active completed\"> " +
" <div class=\"step-counter\">4</div> " +
" <div class=\"step-name\"><span id=\"lblmgr\" style=\"width:150px;\"/>" + approvename3 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Mgr. sign</div></div> ";
        app2 = " <div class=\"stepper-item active completed\"> " +
" <div class=\"step-counter\">3</div> " +
" <div class=\"step-name\"><span id=\"lbleng\" style=\"width:150px;\"/>" + approvename2 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Eng. sign</div></div> ";
        app1 = " <div class=\"stepper-item active completed\"> " +
" <div class=\"step-counter\">2</div> " +
" <div class=\"step-name\"><span id=\"lblskill\" style=\"width:150px;\"/>" + approvename1 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Skill sign</div> " +
"  </div>";
    }
    else if (docstatus == "3" && approvestatus == "N") {
        app3 = " <div class=\"stepper-item active reject\"> " +
   " <div class=\"step-counter\">4</div> " +
" <div class=\"step-name\"><span id=\"lblmgr\" style=\"width:150px;\"/>" + approvename3 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Mgr. sign</div></div> ";

    }
    else {
        app3 = " <div class=\"stepper-item active\"> " +
" <div class=\"step-counter\">4</div> " +
" <div class=\"step-name\"><span id=\"lblmgr\" style=\"width:150px;\"/>" + approvename3 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Mgr. sign</div></div> ";


    }


    if (docstatus == "4" && approvestatus == "Y") {
     
        app4 = " <div class=\"stepper-item active completed\"> " +
" <div class=\"step-counter\">5</div> " +
" <div class=\"step-name\"><span id=\"lblmgrdm\" style=\"width:150px;\"/>" + approvename4 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Mgr.Dm.</div></div> ";
        app3 = " <div class=\"stepper-item active completed\"> " +
  " <div class=\"step-counter\">4</div> " +
  " <div class=\"step-name\"><span id=\"lblmgr\" style=\"width:150px;\"/>" + approvename3 + "</div> " +
  " <div class=\"step-name\" style=\"margin-top:5px;\">Mgr. sign</div></div> ";
        app2 = " <div class=\"stepper-item active completed\"> " +
" <div class=\"step-counter\">3</div> " +
" <div class=\"step-name\"><span id=\"lbleng\" style=\"width:150px;\"/>" + approvename2 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Eng. sign</div></div> ";
        app1 = " <div class=\"stepper-item active completed\"> " +
" <div class=\"step-counter\">2</div> " +
" <div class=\"step-name\"><span id=\"lblskill\" style=\"width:150px;\"/>" + approvename1 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Skill sign</div> " +
"  </div>";
    }
    else if (docstatus == "4" && approvestatus == "N") {
        app4 = " <div class=\"stepper-item active reject\"> " +
" <div class=\"step-counter\">5</div> " +
" <div class=\"step-name\"><span id=\"lblmgrdm\" style=\"width:150px;\"/>" + approvename4 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Mgr.Dm.</div></div> ";

    }
    else {
        app4 = " <div class=\"stepper-item active\"> " +
" <div class=\"step-counter\">5</div> " +
" <div class=\"step-name\"><span id=\"lblmgrdm\" style=\"width:150px;\"/>" + approvename4 + "</div> " +
" <div class=\"step-name\" style=\"margin-top:5px;\">Mgr.Dm.</div></div> ";
     

    }


    let flowprint = " <div class=\"stepper-item\"> " +
   " <div class=\"step-counter\">6</div> " +
   " <div class=\"step-name\">Printed out</div> " +
   "  </div>";
    $("#flow").html(flow + app1 + app2 + app3 + app4 + flowprint);

}

function getDataTable() {

    let docno = $("#MainContent_txt_docno").html();
    let i = 0;

    $.ajax({
        type: "POST",
        url: "Approve.aspx/GetDataDetail",
        data: '{docno:"' + docno + '"}',
        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 (response.d == "<NewDataSet />") {


                controlpage();
                var iseq = 1;

                setTimeout(function () {

                    $('#tbldt tbody>tr span').each(function () {
                        var ctl = $(this).attr('id');
                        if (ctl.indexOf("Seq") != -1) {

                            $(this).html(iseq.toString());
                            iseq++;

                        }
                        if (ctl.indexOf("Model") != -1) {
                            if ($(this).html() == "") {

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


                        }

                    });
                }, 500);



                response = "";
                xmlDoc = "";
                xml = "";
                customers = "";

            } else {



                $.each(customers, function () {
                    var customer = $(this);

                    try {
                        addrow("N");
                     
                        $('#MainContent_txt_SeqNo_' + i.toString()).html($(this).find("ItemNo").text());
                        $('#MainContent_txt_PartName_' + i.toString()).html($(this).find("PartName").text());
                        $('#MainContent_txt_ModelCode_' + i.toString()).html($(this).find("ModelCode").text());
                        $('#MainContent_txt_ModelName_' + i.toString()).html($(this).find("ModelName").text());
                        $('#MainContent_txt_Drwaing_' + i.toString()).html($(this).find("Drawing").text());
                        $('#MainContent_ddl_Boi_' + i.toString()).html($(this).find("PartBoi").text());
                        $('#MainContent_txt_Defect_' + i.toString()).html($(this).find("PartDefect").text());
                        $('#MainContent_txt_DefectName_' + i.toString()).html($(this).find("PartDefectName").text());
                        $('#MainContent_txt_Qty_' + i.toString()).html($(this).find("PartQty").text());
                        $('#MainContent_txt_PricePerUnit_' + i.toString()).html($(this).find("PartPircePerUnit").text());
                        $('#MainContent_txt_PricePerGroup_' + i.toString()).html($(this).find("PartPriceScrap").text());
                        $('#MainContent_txt_DefectName_' + i.toString()).html($(this).find("PartDefectDetail").text());

                        $('#MainContent_txt_Line_' + i.toString()).html($(this).find("PartLine").text());
                        $('#MainContent_txt_Machine_' + i.toString()).html($(this).find("PartMachine").text());
                        $('#MainContent_txt_Shift_' + i.toString()).html($(this).find("PartShift").text());


                        $("#MainContent_ddl_Unit_" + i.toString()).html($(this).find("PartPriceUnit").text());
                        $("#MainContent_ddl_UnitGroup_" + i.toString()).html($(this).find("PartPriceUnit").text());

                        $("#MainContent_ddl_PM_" + i.toString()).html($(this).find("PartPmWscd").text());
                    


                        $('#MainContent_txt_Remark_' + i.toString()).html($(this).find("PartRemark").text());


                        i++;

                    } catch (err) { } finally { };

                });

                response = "";
                xmlDoc = "";
                xml = "";
                customers = "";
                setTimeout(function () {
                    var iseq = 1;
                    $('#tbldt tbody>tr span').each(function () {
                        var ctl = $(this).attr('id');

                        if (ctl.indexOf("Seq") != -1) {

                            $(this).html(iseq.toString());
                            iseq++;

                        }
                        if (ctl.indexOf("Model") != -1) {
                            if ($(this).html() == "") {

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


                        }

                    });


                }, 500);

            }
        },
        failure: function (response) {

        },
        error: function (response) {

        }
    });


}


function getDoc(user) {


 
    $("#tdocbody").html("");
    $.ajax({
        type: "POST",
        url: "Approve.aspx/GetDataForApprove",
        data: '{user:"' + user + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {

            if (response.d.indexOf('|') != -1) {

                let arr = response.d.split('|');
                $("#tdocbody").append(arr[1]);


              

                setTimeout(function () {
                    $("#MainContent_txt_docno").html(arr[0]);
                    getData();
                }, 500);

                setTimeout(function () {
                    $("#MainContent_txt_docno").html(arr[0]);
                    getDataTable();
                }, 1000);


            }

          
           
            
        },
        failure: function (response) {

        },
        error: function (response) {

        }
    });


   



}



function getapproveflow(level, ctl) {

    $(ctl).html("");
    let sectionname = $('#MainContent_ddl_section').html();
    let scrapttype = $('#MainContent_ddl_scraptype').html();


    $.ajax({
        type: "POST",
        url: "ApproveFlowData.aspx/getApproveFlow",
        data: '{section:"' + sectionname + '",level:"' + level + '",scrapttype:"' + scrapttype + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var xmlDoc = $.parseXML(response.d);
            var xml = $(xmlDoc);
            var customers = xml.find("Table");
            $.each(customers, function () {
                var customer = $(this);


                $(ctl).append($(this).find("ApproveName").text() + '<br/>');


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

}


function autosize(textarea) {
    $(textarea).height(1); // temporarily shrink textarea so that scrollHeight returns content height when content does not fill textarea
    $(textarea).height($(textarea).prop("scrollHeight"));
}

function savereject()
{
    
    if($('#MainContent_txt_remark').val().length==0)
    {

        alert("Please input infomation remark.");
        $('#MainContent_txt_remark').focus();


    }
    else
    {
        $("#loader").show();
        let section = $('#MainContent_ddl_section').html();
        let docno = $('#MainContent_txt_docno').html();
        let userid = $('#lbluserdetail').html();
        let username = $('#lblfullname').html();
        let dept = $('#lblDept').html();
        let remark = $('#MainContent_txt_remark').val();
        let scraptype = $('#MainContent_ddl_scraptype').html();

        $.ajax({
            type: "POST",
            url: "Approve.aspx/SaveApprove",
            data: '{section:"' + section + '", docno:"' + docno + '",userid:"' + userid + '",username:"' + username + '",dept:"' + dept + '",level:"' + publevel + '",approvestatus:"N",remark:"' + remark + '",scraptype:"' + scraptype + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {

                if (response.d.length == 0) {

                  


                    setstatusflow(publevel, 'N', $("#lblskill").html(), $("#lbleng").html(), $("#lblmgr").html(), $("#lblmgrdm").html());


                    alert("Successfully recorded.");
                    $("#loader").hide();
                    location.reload();


                }

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

    }

}

function saveapprove() {
    $("#loader").show();
    let section = $('#MainContent_ddl_section').html();
    let docno = $('#MainContent_txt_docno').html();
    let userid = $('#lbluserdetail').html();
    let username = $('#lblfullname').html();
    let dept = $('#lblDept').html();
    let remark = $('#MainContent_txt_remark').val();
    let scraptype = $('#MainContent_ddl_scraptype').html();

    $.ajax({
        type: "POST",
        url: "Approve.aspx/SaveApprove",
        data: '{section:"' + section + '", docno:"' + docno + '",userid:"' + userid + '",username:"' + username + '",dept:"' + dept + '",level:"' + publevel + '",approvestatus:"Y",remark:"' + remark + '",scraptype:"' + scraptype + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {

            if(response.d.length==0)
            {
             

                setstatusflow(publevel, 'Y', $("#lblskill").html(), $("#lbleng").html(), $("#lblmgr").html(), $("#lblmgrdm").html());


                alert("Successfully recorded.");

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

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



