/*! phoscon-app 2025-05-20 */

export const getBlindSliderView=(constants,service)=>{const BlindSliderItemView=Backbone.View.extend({tagName:"li",className:"blindSliderItemView",template:_.template($("#blindSliderViewTemplate").html()),initialize:function(data){this.state=data.state||"light",this.$el.html(this.template(this.model)),this.slider=this.el.querySelector(".blindSlider"),this.value=this.el.querySelector(".blindSliderValue"),this.render()},events:{"input .blindSlider":"eventUpdateValue"},eventUpdateValue:function(e){let value=e.target.value;this.value.innerHTML=value<=0?translate("txt_close"):value+"% "+translate("text_isopen");let data={lift:100-value};this.sendData(data)},sendData:async function(data){$.ajax({url:`/api/${app.apikey}/lights/${this.model.id}/state`,method:"PUT",contentType:"application/json",data:JSON.stringify(data)})},render:function(){let displayValue=100-this.model.attributes.state.lift;return displayValue<=0?this.value.innerHTML=translate("txt_close"):(this.value.innerHTML=displayValue+"% "+translate("text_isopen"),this.$el[0].firstElementChild.innerHTML=this.model.attributes.name.slice(0,50)),this.slider.value=displayValue,this}}),blindSliderListView=new(Backbone.View.extend({el:"#blindSliderContainer",initialize:function(){this.views=[],this.blind=null,document.getElementById("closeBlindSlider").addEventListener("click",this.eventClose.bind(this)),this.listenTo(app.lights,"reset",this.addSliders)},render:function(){if(this.$el.empty(),this.blind){let view=new BlindSliderItemView({model:this.blind});this.views.push(view),this.$el.append(view.el)}const blindSliderContainer=document.querySelector("#sliderWrapperBlinds");blindSliderContainer.style.left="50%",blindSliderContainer.style.top="50%",blindSliderContainer.style.transform="translate(-50%, -50%)"},selectBlind:function(model){document.querySelector("#sliderWrapperBlinds").classList.remove("hidden"),this.blind=model,this.render()},eventClose:function(){document.getElementById("sliderWrapperBlinds").classList.add("hidden"),this.render()}}));return{view:blindSliderListView,selectBlind:function(blindId){const blind=app.lights.get(blindId);blind?blindSliderListView.selectBlind(blind):console.error(`Blind with ID ${blindId} not found`)}}};