// WorkorderCompany.js Vue.component('workorder-company', { template: `

Auftrag: #{{ rescheduleModalData.workorder.id }}

`, data() { return { window, navigator, rescheduleModalData: null, editingAdditionalInfoId: null, tempAdditionalInfo: '', expandedNotes: [], crudConfig: { ...window.TT_CONFIG.CRUD_CONFIG, expandable: true, customRowClass: (row) => { if (['completed', 'new', 'cancelled'].includes(row.status)) return 'tt-rml-workorder-irrelevant'; if (['correction_requested', 'intervention_required'].includes(row.status)) return 'tt-rml-workorder-high'; const deadlineDate = moment.unix(row.deadlineDate); if (!deadlineDate.isValid()) return 'tt-rml-workorder-irrelevant'; const daysLeft = deadlineDate.diff(moment(), 'days'); if (daysLeft <= 7) return 'tt-rml-workorder-urgent'; if (daysLeft <= 21) return 'tt-rml-workorder-medium'; return 'tt-rml-workorder-ontrack'; }, additionalActions: [] } } }, computed: { editTextareaRows() { const lines = (this.tempAdditionalInfo || '').split('\n').length; return String(Math.max(4, lines + 1)); }, }, methods: { getStatusColumn(status) { const column = this.crudConfig.columns.find(c => c.key === 'status'); return column.table.filterOptions.find(opt => opt.value === status) || {}; }, formatDate(timestamp, withTime = false) { if (!timestamp) return '–'; return window.moment.unix(timestamp).format(withTime ? 'DD.MM.YYYY HH:mm' : 'DD.MM.YYYY'); }, getCalendarType(networkOwnerName) { if (!networkOwnerName) return '1'; const name = networkOwnerName.toLowerCase(); if (name.includes('xinon')) return '2'; if (name.includes('sbidi')) return '7'; if (name.includes('estmk')) return '3'; return '1'; }, getCampaignName(preordercampaignId) { const col = this.crudConfig.columns.find(c => c.key === 'preordercampaign_id'); const opt = col?.table?.filterOptions?.find(o => o.value == preordercampaignId); return opt?.text || ''; }, openCalendarWithPrefill(workorder, dateUnix, win) { const m = window.moment.unix(dateUnix); const zeitraum = m.hour() < 12 ? 'VM' : 'NM'; const campaignName = this.getCampaignName(workorder.preordercampaign_id); const locationParts = [workorder.street, workorder.hausnummer]; if (workorder.stiege) locationParts.push('/' + workorder.stiege); const location = locationParts.join(' ') + ', ' + workorder.plz + ' ' + workorder.city; const descLines = []; if (workorder.oaid || campaignName || workorder.city) { descLines.push([workorder.oaid, campaignName, workorder.city].filter(Boolean).join(' - ')); } descLines.push('Zeitraum: ' + zeitraum); if (workorder.phone) descLines.push('Tel.: ' + workorder.phone); if (workorder.additionalInfo) descLines.push(workorder.additionalInfo); const calendarData = { type: this.getCalendarType(workorder.networkOwnerName), subject: workorder.customerCompany || workorder.customerName || '', location: location, cstart: m.format('DD.MM.YYYY HH:mm'), cend: m.clone().add(90, 'minutes').format('DD.MM.YYYY HH:mm'), description: descLines.join('
'), customer_phone: workorder.phone || null, customer_email: workorder.email || null, calendar_user_name: 'Pusnik', attendee_names: ['Ziga Harc'], }; localStorage.setItem('Calendar_create', JSON.stringify(calendarData)); win.location.href = '/Calendar/View'; }, async setAppointment(workorder, date) { if (!date) return; if (moment.unix(date).hour() >= 23 || moment.unix(date).hour() < 1) { this.$refs.table.$refs.table.refreshTable(); return window.notify('error', 'Bitte Uhrzeit angeben!'); } const calWin = window.open('about:blank', '_blank'); const {data} = await axios.post(`${window.TT_CONFIG.BASE_PATH}/WorkorderCompany/scheduleAppointment`, { workorderId: workorder.id, appointmentDate: date }); if (data.success) { window.notify('success', data.message); this.$refs.table.$refs.table.refreshTable(); this.openCalendarWithPrefill(workorder, date, calWin); } else { if (calWin) calWin.close(); window.notify('error', data.message || 'Ein Fehler ist aufgetreten.'); } }, openRescheduleModal(row) { this.rescheduleModalData = { workorder: row, newDate: row.appointmentDate, reason: '' }; }, async rescheduleAppointment() { const { workorder, newDate, reason } = this.rescheduleModalData; if (!newDate || !reason) return window.notify('error', 'Bitte geben Sie ein neues Datum und einen Grund an.'); if (moment.unix(newDate).hour() >= 23 || moment.unix(newDate).hour() < 1) return window.notify('error', 'Bitte Uhrzeit angeben!'); const calWin = window.open('about:blank', '_blank'); const {data} = await axios.post(`${window.TT_CONFIG.BASE_PATH}/WorkorderCompany/rescheduleAppointment`, { workorderId: workorder.id, appointmentDate: newDate, reason: reason }); if (data.success) { window.notify('success', data.message); this.$refs.table.$refs.table.refreshTable(); this.openCalendarWithPrefill(workorder, newDate, calWin); this.rescheduleModalData = null; } else { if (calWin) calWin.close(); window.notify('error', data.message || 'Ein Fehler ist aufgetreten.'); } }, async clearAppointment(workorder) { if (!confirm('Möchten Sie den Termin wirklich löschen?')) return; const {data} = await axios.post(`${window.TT_CONFIG.BASE_PATH}/WorkorderCompany/clearAppointment`, { workorderId: workorder.id }); if (data.success) { window.notify('success', data.message); this.$refs.table.$refs.table.refreshTable(); } else window.notify('error', data.message || 'Ein Fehler ist aufgetreten.'); }, isNoteLong(text) { if (!text) return false; return text.split('\n').length > 4 || text.length > 180; }, toggleNoteExpand(rowId) { const idx = this.expandedNotes.indexOf(rowId); if (idx === -1) this.expandedNotes.push(rowId); else this.expandedNotes.splice(idx, 1); }, startAdditionalInfoEdit(row) { this.editingAdditionalInfoId = row.id; this.tempAdditionalInfo = row.additionalInfo || ''; this.$nextTick(() => this.$refs.editTextarea?.$el.querySelector('textarea').focus()); }, cancelEdit() { this.editingAdditionalInfoId = null; this.tempAdditionalInfo = ''; }, async updateAdditionalInfo(row) { if (row.additionalInfo === this.tempAdditionalInfo) { this.cancelEdit(); return; } const {data} = await axios.post(`${window.TT_CONFIG.BASE_PATH}/WorkorderCompany/updateAdditionalInfo`, { workorderId: row.id, additionalInfo: this.tempAdditionalInfo }); if (data.success) { window.notify('success', data.message); row.additionalInfo = data.newInfo; } else window.notify('error', data.message || 'Update fehlgeschlagen.'); this.cancelEdit(); }, } });