/**
 * TEMPLATE: afterProcessFinish-base.js
 * =============================================
 * Eventos de encerramento do processo.
 * Inclui afterProcessFinish e afterCancelProcess.
 *
 * PADRAO DE PRODUCAO (VIPal, BOMAG):
 * - Gravar data/hora de encerramento
 * - Atualizar campo situacao
 * - Gravar usuario que cancelou (se cancelamento)
 */

// =============================================
// FINALIZACAO DO PROCESSO
// =============================================
function afterProcessFinish(processId) {

    log.info("=== ${COD_PROCESSO} afterProcessFinish - processId: " + processId);

    var dataBR = fnBuscarDataHora("data");
    var partes = dataBR.split("/");
    var dataEN = partes[2] + "-" + partes[1] + "-" + partes[0];

    hAPI.setCardValue("situacaoSolic", "Finalizado");
    hAPI.setCardValue("dtFimSolic", dataBR);
    hAPI.setCardValue("dtFimSolic_EN", dataEN);
    hAPI.setCardValue("hrFimSolic", fnBuscarDataHora("hora"));
}

// =============================================
// CANCELAMENTO DO PROCESSO
// =============================================
function afterCancelProcess(colleagueId, processId) {

    log.info("=== ${COD_PROCESSO} afterCancelProcess - processId: " + processId + " user: " + colleagueId);

    var dataBR = fnBuscarDataHora("data");
    var partes = dataBR.split("/");
    var dataEN = partes[2] + "-" + partes[1] + "-" + partes[0];

    hAPI.setCardValue("situacaoSolic", "Cancelado");
    hAPI.setCardValue("dtFimSolic", dataBR);
    hAPI.setCardValue("dtFimSolic_EN", dataEN);
    hAPI.setCardValue("hrFimSolic", fnBuscarDataHora("hora"));
    hAPI.setCardValue("userCancelamento", colleagueId);
}

// =============================================
// HELPER
// =============================================
function fnBuscarDataHora(tipo) {
    var fullDate = new java.util.Date();
    var sdf;
    if (tipo == "data") {
        sdf = new java.text.SimpleDateFormat("dd/MM/yyyy");
    } else {
        sdf = new java.text.SimpleDateFormat("HH:mm");
    }
    return sdf.format(fullDate);
}
