chako / ResetVerifikasiSSCN

// ==UserScript==
// @author chako@PNScoder
// @name ResetVerifikasiSSCN
// @version  2.4.3
// @description Untuk ADMIN SSCN : Helper untuk reset nomor register yang sudah TMS maupun MS
// @copyright perdana1cell@gmail.com 2019
// @license MIT
// @updateURL https://openuserjs.org/meta/chako/ResetVerifikasiSSCN.meta.js
// @downloadURL https://openuserjs.org/install/chako/ResetVerifikasiSSCN.user.js
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_log
// @grant GM_deleteValue
// @grant GM_notification
// @grant GM_openInTab
// @grant GM_setClipboard
// @grant GM_info
// @grant GM_addStyle
// @namespace https://sscnverifikasi.bkn.go.id/resetverifikasi.do
// @match https://sscnverifikasi.bkn.go.id/resetverifikasi.do*
// @require http://userscripts-mirror.org/scripts/source/107941.user.js
// ==/UserScript==

var idRegs = GM_SuperValue.get ('idRegsQueue', []);
var idAcc = GM_SuperValue.get ('idRegsAcc', []);
var idTms = GM_SuperValue.get ('idRegsTms', []);
var resetMS = GM_SuperValue.get ('resetMS', false);
var label = 'Mode reset '+ (resetMS ? 'MS' : 'TMS');
var processReset = GM_SuperValue.get ('processReset', '');

GM_SuperValue.set ('processReset', '');//balikin ke default setelah value disimpan

let e = document.querySelector("#defaultSearchField");
e.focus();
e.select();

var status = $("#myTable > tbody > tr:nth-child(1) > td:nth-child(5)").html();
var noreg = $("#defaultSearchField").val();
var firstreg = $("#myTable > tbody > tr:nth-child(1) > td:nth-child(2)").html();
if (noreg != '' && noreg == firstreg) {//kalo hasil pencarian sama dgn row pertama
    if (idRegs.length > 0 && noreg === $.trim(idRegs[0])){//kalau hasil pencarian sama sama dengan index pertama dari antrian
        if((!resetMS && status == 'Tidak Lulus') || (resetMS && status == 'Lulus')){
            //reset
            GM_SuperValue.set ('processReset', noreg + ' - ' + (resetMS ? 'MS' : 'TMS'));

            $("#myTable > tbody > tr > td:nth-child(7) > a.btn.btn-sm.btn-danger").trigger('click');
        }else if(status == 'Belum diverifikasi'){
            if(idRegs.length > 0 && idRegs.splice( $.inArray(noreg, idRegs), 1 ).length > 0){
                if(processReset !== ''){
                    //push no register yang telah direset
                    idAcc.push(processReset);
                    GM_SuperValue.set ('idRegsAcc', idAcc);
                }

                GM_SuperValue.set ('idRegsQueue', idRegs);

                //load antrian selanjutnya
                noreg = $.trim(idRegs[0]);
                $("#defaultSearchField").val(noreg);
                if(checkRegister(noreg)){
                    $("#searchBar > div:nth-child(5) > div > a").trigger('click');
                }
            }
        } else {
            alert(label + '. Nomor registrasi '+ noreg +' berstatus = ' + status + '. Otomatisasi tidak akan dilanjutkan. Silakan reset antrian.');
        }
    } else {
        //console.log('bukan dari antrian, do nothing');
    }
} else {
    if(noreg != '' && firstreg != null){
        //handle back browser
        $("#searchBar > div:nth-child(5) > div > a").trigger('click');
    } else if (idRegs.length > 0){
        if(noreg !== $.trim(idRegs[0])){
            //load antrian selanjutnya
            noreg = $.trim(idRegs[0]);
            $("#defaultSearchField").val(noreg);

            if(checkRegister(noreg)){
                $("#searchBar > div:nth-child(5) > div > a").trigger('click');
            } else {
                //remove bila nomor register tidak sesuai pattern
                if(idRegs.splice( $.inArray(noreg, idRegs), 1 ).length > 0){
                    GM_SuperValue.set ('idRegsQueue', idRegs);

                    GM_notification ({
                        title: noreg,
                        text: 'Cek kembali nomor register',
                        ondone: () => {
                            location.reload();
                        }
                    });
                }
            }
        } else {
            //remove bila no register sudah diproses
            if(idRegs.splice( $.inArray(noreg, idRegs), 1 ).length > 0){
                GM_SuperValue.set ('idRegsQueue', idRegs);

                GM_notification ( {
                    title: noreg,
                    text: 'Nomor register '+noreg+' tidak ditemukan. Refresh halaman untuk membuka antrian berikutnya',
                    ondone: () => {
                        location.reload();
                    }
                } );
            }
        }

    }
}

//header buttons
let lblMode = '<span class="h5 text-danger">'+label+'</span>  ';
let btnAddQueue = ' <button type="button" id="btnAddQueue" class="btn btn-sm btn-success">Tambah Antrian</button> ';
let btnViewQueue = ' <button type="button" id="btnViewQueue" class="btn btn-sm btn-info">Lihat Antrian</button> ';
let btnViewAcc = ' <button type="button" id="btnViewAcc" class="btn btn-sm btn-info">Lihat Hasil Reset</button> ';
let btnResetQueue = ' <button type="button" id="btnResetQueue" class="btn btn-sm btn-danger">Reset Antrian</button> ';
let btnViewInfo = ' <button type="button" id="btnViewInfo" class="btn btn-sm btn-primary">Informasi</button> ';
let info = ' ' + GM_info.script.name + ' v.' + GM_info.script.version + '. copyright ' + GM_info.script.author + ' ('+GM_info.script.copyright+')';
$('body').prepend('<div class="alert alert-success text-center" role="alert">' + lblMode + btnAddQueue + btnViewQueue + btnViewAcc + btnResetQueue + btnViewInfo + '</div>');
$('body').append('<footer class="footer-copyright"><strong>'+info+'</strong></footer>');

let ta = '<div class="row col-md-12"><textarea id="taRegister" rows="20" cols="20" style="width: 100%; margin: 10px;" required></textarea></div>';
let divBtn = '<div class="row col-md-12" style="margin-left: 10px;"><button type="button" id="btnSaveRegTMS" class="btn btn-success" style="margin-right: 3px;">Reset TMS</button> <button type="button" id="btnSaveRegMS" class="btn btn-primary"style="margin-right: 3px;">Reset MS</button> <button type="button" id="btnClose" class="btn btn-warning"style="margin-right: 3px;">Batal</button> </div>';
$('body').append('<div id="myCustomDialog" style="overflow-x: hidden; width: auto; min-height: 0px; height: 853.249px;" class="ui-dialog-content ui-widget-content" scrolltop="0" scrollleft="0">'+ta+divBtn+'</div>');

GM_addStyle('.footer-copyright {position: fixed; left: 0px; bottom: 0px; vertical-align: center; width: 100%; padding: .75rem 1.25rem; text-align: center; color: #721c24; background-color: #f8d7da; border-color: #f5c6cb;}');

/*LOGIC MENU COMMAND*/
GM_registerMenuCommand ("Tambah Antrian", addRegisterNumbers, "A");
GM_registerMenuCommand ("Lihat Antrian", viewQueue, "Q");
GM_registerMenuCommand ("Lihat Hasil Reset", viewAcc, "Y");
GM_registerMenuCommand ("Reset Antrian", resetQueue, "X");
GM_registerMenuCommand ("Informasi", viewInfo, "I");

function addRegisterNumbers () {
    $( "#myCustomDialog" ).dialog({ title: "Tambah Nomor Register" });
    $('#myCustomDialog').dialog('open');
    $('#btnSaveRegTMS').show();
    $('#btnSaveRegMS').show();

    $('textarea#taRegister').val('');
    $('textarea#taRegister').prop('disabled',false);
}

function viewQueue () {
    $( "#myCustomDialog" ).dialog({ title: "Daftar Antrian" });
    $('#myCustomDialog').dialog('open');
    $('#btnSaveRegTMS').hide();
    $('#btnSaveRegMS').hide();

    $('textarea#taRegister').val('');

    let idRegs = GM_SuperValue.get ('idRegsQueue', []);
    if(idRegs !== null){
        $('textarea#taRegister').val(idRegs.join('\n'));
    }
    $('textarea#taRegister').prop('disabled',true);
}

function viewAcc () {
    $( "#myCustomDialog" ).dialog({ title: "Daftar SESUAI" });
    $('#myCustomDialog').dialog('open');
    $('#btnSaveRegTMS').hide();
    $('#btnSaveRegMS').hide();

    $('textarea#taRegister').val('');

    let idRegs = GM_SuperValue.get ('idRegsAcc', []);
    if(idRegs !== null){
        $('textarea#taRegister').val(idRegs.reverse().join('\n'));
    }
    $('textarea#taRegister').prop('disabled',true);
}

function resetQueue () {
    GM_deleteValue('idRegsQueue');
    GM_notification ( {title: 'Berhasil', text: 'Antrian berhasil direset'} );
}

function viewInfo () {
    GM_notification ({title: $('#header > div > div > div.col-sm-9.new-btn-header > div:nth-child(1) > div > div > button.btn.btn-sm.btn-primary > b').html(), text: '\
Program hanyalah sebuah tools pembantu, ketelitian dalam memverifikasi berkas tetap harus dijaga. Ganbatte!'});
}

function checkRegister(noreg){
    var pattern = /^\d{17}$/;
    return pattern.test(noreg);
}

function addQueue(resetMS){
    var val = $('textarea#taRegister').val();
    if(val.length > 0){
        if(GM_SuperValue.get('resetMS', false) != resetMS){
            //kalo ganti antrian
            GM_deleteValue('idRegsQueue');
            GM_SuperValue.set('resetMS', resetMS);
        }

        var arr = val.split(/\s+/g);

        var initArr = GM_SuperValue.get ('idRegsQueue', []);
        $.each(arr, function( index, reg ) {
            initArr.push(reg);
        });
        GM_SuperValue.set ('idRegsQueue', initArr);

        $('#myCustomDialog').dialog('close');

        $("#defaultSearchField").val($.trim(initArr[0]));

        let reg = $.trim(initArr[0]);
        if(checkRegister(reg)){
            $("#searchBar > div:nth-child(5) > div > a").trigger('click');
            GM_notification ( {title: 'Berhasil', text: 'Antrian berhasil ditambah'} );
        } else {
            if(initArr.splice( $.inArray(reg, initArr), 1 ).length > 0){
                GM_SuperValue.set ('idRegsQueue', initArr);

                GM_notification ({
                    title: reg,
                    text: 'Cek kembali nomor register',
                    ondone: () => {
                        location.reload();
                    }
                });
            }
        }
    } else {
        GM_notification ( {title: 'Tidak Ada Data', text: 'Silakan isi nomor register yang akan dimasukkan ke dalam antrian'} );
    }
}

let w = $(window).width();
let h = $(window).height();

$("#myCustomDialog").dialog({
    autoOpen: false,
    height: h,
    width: 1120,
    position: {
        my: "center",
        at: "top",
        of: window
    },
    modal: true,
    resizable: true,
    open: function(){
        $('.ui-widget-overlay').bind('click',function(){
            $('#myCustomDialog').dialog('close');
        });

        //action close button
        document.getElementById('btnClose').addEventListener('click', function() {
            $('#myCustomDialog').dialog('close');
        });
    }
});

document.getElementById('btnAddQueue').addEventListener('click', addRegisterNumbers);
document.getElementById('btnViewQueue').addEventListener('click', viewQueue);
document.getElementById('btnViewAcc').addEventListener('click', viewAcc);
document.getElementById('btnResetQueue').addEventListener('click', resetQueue);
document.getElementById('btnViewInfo').addEventListener('click', viewInfo);

//btn tambah antrian TMS
document.getElementById('btnSaveRegTMS').addEventListener('click', function() {
    addQueue(false);
});

//btn tambah antrian OK
document.getElementById('btnSaveRegMS').addEventListener('click', function() {
    addQueue(true);
});

//paste no register
$('#defaultSearchField').on('paste', function(elm){
    var e = $(this);
    setTimeout(function(){
        if(checkRegister($.trim(e.val()))){
            $("#searchBar > div:nth-child(5) > div > a").trigger('click');
        } else {
            GM_notification ( {title: e.val(), text: 'Cek kembali nomor register'} );
        }
    }, 0);
});

$('#defaultSearchField').on('change', function(elm){
    if(checkRegister($.trim($(this).val()))){
        $("#searchBar > div:nth-child(5) > div > a").trigger('click');
    } else {
        GM_notification ( {title: $(this).val(), text: 'Cek kembali nomor register'} );
    }
});