import AbstractWidget from './Abstract.js'; import BaseOffcanvas from '../Base/Offcanvas.js'; import ApiAmpeln from '../../api/factory/widget/ampeln.js'; let _idcounter = 0; export default { name: 'WidgetsAmpel', components: { BaseOffcanvas }, data() { return { WIDGET_AMPEL_MAX: 4, filter: '', source: 'offen', allAmpeln:null, activeAmpeln:null, idcounter: this.configMode ? 0 : ++_idcounter }; }, mixins: [ AbstractWidget ], computed: { ampelnComputed() { switch(this.source) { case 'offen': return this.applyFilter(this.activeAmpeln); case 'alle': return this.applyFilter(this.allAmpeln); default: return this.applyFilter(this.activeAmpeln); } }, ampelnOverview () { return this.activeAmpeln?.slice(0, this.WIDGET_AMPEL_MAX); // show only newest 4 active ampeln }, count () { const now = new Date(); let datasource = this.activeAmpeln; if (this.source == 'offen') datasource = this.activeAmpeln; if (this.source == 'alle') datasource = this.allAmpeln; return { verpflichtend: datasource?.filter(item => item.verpflichtend).length, ueberfaellig: datasource?.filter(item => (now > new Date(item.deadline)) && !item.bestaetigt).length, alle: datasource?.length } } }, methods: { applyFilter(data) { switch(this.filter) { case 'verpflichtend': return data?.filter(item => item.verpflichtend); case 'ueberfaellig': const now = new Date(); return data?.filter(item => (now > new Date(item.deadline)) && !item.bestaetigt); default: return data; } }, toggleFilter(value) { this.filter === value ? this.filter = '' : this.filter = value; }, closeOffcanvasAmpeln() { for (let i = 0; i < this.ampelnComputed.length; i++) { let ampelId = this.ampelnComputed[i].ampel_id; if(this.$refs['ampelCollapse_' + ampelId]){ this.$refs['ampelCollapse_' + ampelId][0].classList.remove('show'); } } }, openOffcanvasAmpel(ampelId) { // Close earlier opened Ampeln this.closeOffcanvasAmpeln(); // Show given Ampel this.$refs['ampelCollapse_' + ampelId][0].classList.add('show'); }, closeOffcanvas() { this.closeOffcanvasAmpeln(); this.filter = ''; // maybe we also want to reset the source (open/alle) of the displayed ampeln }, fetchNonConfirmedActiveAmpeln() { this.$api .call(ApiAmpeln.open()) .then(res => { this.activeAmpeln = res.data; }) .catch(error => this.$fhcAlert.handleSystemError); }, fetchAllActiveAmpeln() { this.$api .call(ApiAmpeln.all()) .then(res => { this.allAmpeln = res.data; }) .catch(error => this.$fhcAlert.handleSystemError); }, async confirm(ampelId) { this.$api .call(ApiAmpeln.confirm(ampelId)) .then(() => { this.$fhcAlert.alertSuccess(this.$p.t('ampeln', 'ampelBestaetigt')); // update the ampeln by refetching them this.fetchNonConfirmedActiveAmpeln(); this.fetchAllActiveAmpeln(); }) .catch(this.$fhcAlert.handleSystemError); }, validateBtnTxt(buttontext) { if (buttontext instanceof Array && !buttontext.length) return this.$p.t('ui', 'bestaetigen'); if (!buttontext) return this.$p.t('ui', 'bestaetigen'); return buttontext; } }, created() { this.$emit('setConfig', false); }, async mounted() { if (!this.configMode) { this.fetchNonConfirmedActiveAmpeln(); this.fetchAllActiveAmpeln(); } }, template: /*html*/`
{{$p.t('ampeln', 'newestAmpeln')}}
{{$p.t('ampeln','ampelnDeadline',{value:getDate(ampel.deadline)})}}
{{ ampel.kurzbz }}
{{$p.t('ampeln','super')}}
{{$p.t('ampeln','noOpenAmpeln')}}
{{$p.t('ampeln', 'newestAmpeln')}}
` }