$(document).ready(function () {


    getdata(0);

});



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


    });


    controlpage();





}

function gvedit(ctl) {
    let ctlname = $(ctl).attr('id');
    let arrctl = ctlname.split('_');
    let ctlrow = parseFloat(arrctl[2].toString());

    $('#tbldt tbody>tr input[type="text"]').each(function () {
        let ctlid = $(this).attr('id');
        if (ctlid.indexOf("_" + ctlrow.toString()) != -1) {
            $(this).css({ 'border-style': 'solid' });
            $(this).prop('readonly', false);

        }
    });


}

function getnext() {


    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)) {

        getdata(1);

        //pubcurrentpagestock = pubcurrentpagestock + 1;
        //try {

        //    setTimeout(
        //      function () {
        //          getdata(pubcurrentpagestock, pubirow, pubfilter, parseInt(y, 0));



        //      }, 500);



        //} catch (err) { };



    }

}
let iseqno = 1;
let ipub = 0;
function getdata(page) {

    if (page == 0) {
        for (let i = 0; i < 10; i++) {

            addrow("Y");
            $("#MainContent_txt_SeqNo_" + ipub.toString()).html(iseqno.toString());
            $("#MainContent_txt_LineGroup_" + ipub.toString()).html("000" + iseqno.toString());
            $("#MainContent_txt_LineName_" + ipub.toString()).html("XXX" + iseqno.toString());
            $("#MainContent_txt_JigName_" + ipub.toString()).html("JIG" + iseqno.toString());
            $("#MainContent_txt_Serial_" + ipub.toString()).html("serial_" + iseqno.toString());





            ipub++;
            iseqno++;



        }
    }
    else {

        for (var i = ipub; i < 20; i++) {

            addrow("Y");
            $("#MainContent_txt_SeqNo_" + ipub.toString()).html(iseqno.toString());
            $("#MainContent_txt_LineGroup_" + ipub.toString()).html("000" + iseqno.toString());
            $("#MainContent_txt_LineName_" + ipub.toString()).html("XXX" + iseqno.toString());
            $("#MainContent_txt_JigName_" + ipub.toString()).html("JIG" + iseqno.toString());
            $("#MainContent_txt_Serial_" + ipub.toString()).html("serial_" + iseqno.toString());




            ipub++;
            iseqno++;



        }



    }




}


function gvdelrowid(ctl, msg) {

    let resindex = ctl.id.split("_");
    let irow = resindex[2].toString();
    let rowCount = $('#tbldt tr').length;
    rowCount = rowCount - 1;

    swal({
        title: 'Are you sure?',
        text: "Do you want delete this data!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'
    }).then(function (result) {
        if (result.value) {
            //let itemno = $('#MainContent_txt_Item_' + irow.toString()).val();
            //let who = $('#MainContent_txt_Who_' + irow.toString()).val();
            //let freq = $('#MainContent_txt_Freq_' + irow.toString()).val();
            //let how = $('#MainContent_txt_How_' + irow.toString()).val();
            //let crit = $('#MainContent_txt_Crit_' + irow.toString()).val();
            //let unit = $('#MainContent_txt_Unit_' + irow.toString()).val();


            //$.ajax({
            //    type: "POST",
            //    url: "ListItemDetail.aspx/DeleteRow",
            //    data: "{docno:'" + docno + "',itemno:'" + itemno + "',drawing:'" + drawing + "',modelcode:'" + modelcode + "'}",
            //    contentType: "application/json; charset=utf-8",
            //    dataType: "json",
            //    success: function (response) {


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


            if (rowCount > 1) {
                $(ctl).closest('tr').remove();

            }
            else {
                if (irow != "0") {

                    $(ctl).closest('tr').remove();
                }
                else {
                    $('#tbldt tbody>tr input[type="text"]').each(function () {
                        $(this).val("");
                    });
                    $('#tbldt tbody>tr select').each(function () {
                        $(this).val("");
                    });

                }
            }




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

            });

            swal(
              'Deleted!',
              'Your file has been deleted.',
              'success'
            );
        } else {
            return false;
        }
    })








}