var DashKPI = SuperWidget.extend({

    init: function () {
        var self = this;

        window.addEventListener("dashboardData", function (e) {
            self.calcular(e.detail || []);
        });
    },

    calcular: function (dados) {
        var total = dados.length;
        var conformes = dados.filter(function (i) {
            return normalizaStatus(i) === "CONFORME";
        }).length;

        var pct = total ? (conformes / total) * 100 : 0;
        var pctTxt = pct.toFixed(2).replace(".", ",") + "%";

        $("#kpi_total_" + this.instanceId).text(total);
        $("#kpi_pct_" + this.instanceId).text(pctTxt);

        var largura = total ? Math.max(12, Math.min(100, pct)) : 0;
        $("#kpi_total_bar_" + this.instanceId).css("width", largura + "%");
    }

});

function normalizaStatus(item) {
    var raw = item.saidaAnalise || item.status || "";
    return String(raw).trim().toUpperCase();
}
