var DashGrafico = SuperWidget.extend({

chart:null,

init:function(){

var self=this;
this.renderAmostra();

window.addEventListener("dashboardData",function(e){

self.render(e.detail);

});

},

render:function(dados){

var conforme=dados.filter(function(i){
return normalizaStatus(i)=="CONFORME";
}).length;

var nao=dados.filter(function(i){
return normalizaStatus(i)=="NAO_CONFORME";
}).length;

var ctx=document.getElementById("grafico_"+this.instanceId);

if(this.chart){
this.chart.destroy();
}

this.chart=new Chart(ctx,{

type:'doughnut',

data:{
labels:["Conforme ("+conforme+")","Não Conforme ("+nao+")"],
datasets:[{
data:[conforme,nao],
backgroundColor:["#2ecc71","#e74c3c"]
}]
},
options:{
responsive:true,
maintainAspectRatio:false,
cutout:"48%"
}

});

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

},

renderAmostra:function(){
var ctx=document.getElementById("grafico_"+this.instanceId);
if(!ctx){
return;
}

if(this.chart){
this.chart.destroy();
}

this.chart=new Chart(ctx,{
type:'doughnut',
data:{
labels:["Conforme (0)","Não Conforme (0)"],
datasets:[{
data:[1,0],
backgroundColor:["#cfd8dc","#eceff1"]
}]
},
options:{
responsive:true,
maintainAspectRatio:false,
cutout:"48%",
plugins:{
tooltip:{enabled:false}
}
}
});

}

});
