/*! phoscon-app 2023-07-31 */

export const getRlFunctionListView=c=>{const ItemView=Backbone.View.extend({tagName:"div",className:"func list-group-item",initialize:function({model:model,template:template}){this.template=_.template(document.getElementById(template).innerHTML),this.el.innerHTML=this.template(model),this.el.setAttribute("data-func",model.get("class"))},render:function(){return this}}),ListView=Backbone.View.extend({el:"#rlFunctionList",initialize:function(){this.views=[],this.service=null,c.rlFunctionMap.forEach(func=>{let templ=func.templ,mapItem=c.ClassIds.find(cl=>cl.class===func.class);if(!mapItem)return;let model={class:func.class,name:translate(mapItem.translateId),icon:mapItem.iconId};var funcModel=new Backbone.Model(model);this.addFunc(funcModel,templ)}),this.el.setAttribute("data-fid","")},events:{"click [data-btn-ev]:not(.disabled)":function(ev){var elem=ev.currentTarget,dataEv=elem.getAttribute("data-btn-ev"),id=null;elem.hasAttribute("data-oid")&&(id=elem.getAttribute("data-oid")),this.service.send({type:dataEv,id:id})}},render:function(rl){},addFunc:function(model,templ){let view=this.views.find(v=>v.model===model);view||(view=new ItemView({model:model,template:templ}),this.el.appendChild(view.el),this.views.push(view))},showFunc:function(rlid){let rlClass=app.resourcelinks.get(rlid).get("classid"),func=c.classIdFunctionMap[rlClass],clMapItem=c.ClassIds.find(cl=>-1!==cl.options.indexOf(rlClass));clMapItem&&(this.views.forEach(v=>{v.el.querySelectorAll(".radio").forEach(el=>el.classList.remove("active-radio"))}),this.el.setAttribute("data-fid",clMapItem.class),this.el.querySelector(`[data-oid="${func}"] .radio`).classList.add("active-radio"))},setFuncActive:function(fid,oid){this.views.find(v=>v.el.getAttribute("data-func")==fid).el.querySelectorAll(".subfunc").forEach(el=>{let elOid=el.getAttribute("data-oid");oid==elOid?el.querySelector(".radio").classList.add("active-radio"):el.querySelector(".radio").classList.remove("active-radio")})}});return new(Backbone.View.extend({el:"#rlFunction",template:_.template(document.getElementById("rlfunction-template").innerHTML),initialize:function(){this.el.innerHTML=this.template(),this.service=null,this.c=c,this.elHead=this.el.querySelector("#rlFunctionHead"),this.subView=new ListView},setActiveResource:function(rlid){let rl=app.resourcelinks.get(rlid),clMapItem=c.ClassIds.find(cl=>-1!==cl.options.indexOf(rl.get("classid")));clMapItem&&(this.elHead.querySelector(".item-name").innerText=translate(clMapItem.translateId)||"",this.elHead.querySelector("svg use").setAttribute("xlink:href",clMapItem.iconId))},show:function(){this.el.classList.remove("hidden")},hide:function(){this.el.classList.add("hidden")}}))};