Vue.component('preorder-logistics', { template: `

Filter

`, data() { return { window: window, loading: false, isPrinting: false, isExporting: false, isExportingAndMarking: false, lastFcpInfoTime: null, campaigns: [{value: '', text: 'Alle'}], fcps: [], sentStatusOptions: [ {value: 'no', text: 'Noch nicht versendet'}, {value: 'yes', text: 'Versendet'}, {value: 'all', text: 'Alle'} ], filters: { preordercampaign_id: '', ucode: '', oaid: '', sent: 'no', address: '', kunde: '', fcp: [], sent_date: null }, tableConfig: { key: 'preorderlogistics', tableHeader: 'Bestellungen', headers: [ {key: 'actions', text: '', sortable: false, filter: false}, {key: 'sent', text: 'Versandt', sortable: false, filter: false}, {key: 'status_code', text: 'Status', sortable: false, filter: false}, {key: 'shipping_address', text: 'Versandadresse', sortable: false, filter: false}, {key: 'oaid', text: 'OAID', sortable: false, filter: false}, {key: 'ucode', text: 'Bestellcode', sortable: false, filter: false}, ] } } }, async mounted() { this.campaigns.push(...window.TT_CONFIG.CRUD_CONFIG.CAMPAIGNS); if (this.campaigns.length === 2) { // 'Alle' + one campaign this.filters.preordercampaign_id = this.campaigns[1].value; } await this.fetchFcps(); }, watch: { 'filters.preordercampaign_id': 'fetchFcps' }, methods: { async fetchFcps() { if (!this.filters.preordercampaign_id) { this.fcps = []; this.filters.fcp = []; return; } if (Array.isArray(this.filters.preordercampaign_id) && this.filters.preordercampaign_id.length !== 1) { this.fcps = []; this.filters.fcp = []; const now = new Date().getTime(); if (!this.lastFcpInfoTime || now - this.lastFcpInfoTime > 7000) { this.lastFcpInfoTime = now; } else { return; } window.notify('info', 'Bitte wählen Sie genau eine Kampagne aus, um die FCPs zu laden.'); return; } try { const response = await axios.get(`${window.TT_CONFIG.BASE_PATH}/Preorderlogistics/getFCPsForCampaign`, { params: {campaign_id: this.filters.preordercampaign_id} }); this.fcps = response.data; } catch (e) { console.error("Could not fetch FCPs", e); this.fcps = []; } }, applyFilters() { this.$refs.table.filters = {...this.filters}; this.$refs.table.refreshTable(); }, resetFilters() { this.filters = { preordercampaign_id: '', ucode: '', oaid: '', sent: 'no', address: '', kunde: '', fcp: [], sent_date: null }; this.applyFilters(); }, async saveSent(id, sent) { try { const response = await axios.post(`${window.TT_CONFIG.BASE_PATH}/Preorderlogistics/saveSentAction`, { id, sent }); if (response.data.success) { window.notify('success', 'Status gespeichert.'); this.$refs.table.refreshTable(); } else { window.notify('error', 'Fehler beim Speichern.'); } } catch (e) { console.error("Error saving sent status:", e); window.notify('error', 'Ein Fehler ist aufgetreten.'); this.$refs.table.refreshTable(); } }, printSlip(row) { const w = window.open(`${window.TT_CONFIG.BASE_PATH}/Preorderlogistics/print?id=${row.id}`, "_blank", "popup=yes"); w.addEventListener('load', () => w.print()); }, async printAll() { this.isPrinting = true; try { const response = await axios.post(`${window.TT_CONFIG.BASE_PATH}/Preorderlogistics/printAllAction`, { filters: this.filters }, {responseType: 'text'}); const printWindow = window.open("", "_blank"); printWindow.document.write(response.data); printWindow.document.close(); setTimeout(() => { printWindow.focus(); printWindow.print(); }, 500); } catch (e) { console.error("Error printing all slips:", e); window.notify('error', 'Fehler beim Drucken.'); } finally { this.isPrinting = false; } }, async exportCsv(markAsSent) { const action = markAsSent ? 'csvExportAndMarkAsSentAction' : 'csvExportAction'; if (markAsSent) this.isExportingAndMarking = true; else this.isExporting = true; try { const response = await axios.post(`${window.TT_CONFIG.BASE_PATH}/Preorderlogistics/${action}`, { filters: this.filters }, {responseType: 'blob'}); const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', markAsSent ? 'export-versand-markiert.csv' : 'export-versand.csv'); document.body.appendChild(link); link.click(); document.body.removeChild(link); window.URL.revokeObjectURL(url); if (markAsSent) { window.notify('success', 'CSV exportiert und Einträge als versendet markiert.'); this.$refs.table.refreshTable(); } else { window.notify('success', 'CSV erfolgreich exportiert.'); } } catch (e) { console.error("Error exporting CSV:", e); window.notify('error', 'Fehler beim CSV-Export.'); } finally { if (markAsSent) this.isExportingAndMarking = false; else this.isExporting = false; } } } });