// IpNetwork.js Vue.component('IpNetwork', { template: `
/
`, data() { return { window: window, apiUrl: window.TT_CONFIG.API_BASE_URL, currentNetworkData: null, modalData: null, globalSearchTerm: '', isNavigating: false, searchDebounce: null, statusOptions: [ {value: 'active', text: 'Aktiv'}, {value: 'inactive', text: 'Inaktiv'}, {value: 'reserved', text: 'Reserviert'} ], IpNetworkTableConfig: { defaultPageSize: 50, customRowClass: row => (row.cidr < 31 ? 'tt-pointer' : ''), expandCondition: row => !!row.description, headers: [ {text: 'Netzwerkadresse', key: 'network_address_str', sortable: true}, {text: 'Name', key: 'name', sortable: true}, {text: 'Status', key: 'status', filter: 'iconSelect', sortable: true, filterOptions: [ {value: 'active', text: 'Aktiv', icon: 'fas fa-check text-success'}, {value: 'inactive', text: 'Inaktiv', icon: 'fas fa-times text-danger'}, {value: 'reserved', text: 'Reserviert', icon: 'fas fa-lock text-warning'} ] }, {text: 'Subnetze', key: 'children', filter: 'numberRange', sortable: true}, {text: 'Standort', key: 'location', sortable: true}, {text: 'Aktionen', key: 'actions', sortable: false, class: 'text-center'}, ], tableHeader: 'IPAM - Stamm', key: 'IpNetwork' } } }, watch: { globalSearchTerm(newValue) { if (this.isNavigating) { this.isNavigating = false; return; } clearTimeout(this.searchDebounce); this.searchDebounce = setTimeout(() => { this.$refs.table.filters.globalSearch = this.globalSearchTerm; }, 300); } }, mounted() { window.addEventListener('popstate', this.handlePopState); this.handlePopState(); // Initial load based on URL }, beforeDestroy() { window.removeEventListener('popstate', this.handlePopState); }, methods: { async handleGlobalSearchSelect(selectedNetworkString) { if (!selectedNetworkString || typeof selectedNetworkString !== 'string') { this.globalSearchTerm = selectedNetworkString; return; } this.globalSearchTerm = selectedNetworkString; try { const response = await axios.get(`${this.apiUrl}/findNetworkByString`, { params: { network_string: selectedNetworkString } }); if (response.data.success) { this.switchCurrentNetwork(response.data.navigateToId); this.isNavigating = true; this.globalSearchTerm = ''; } else { window.notify('error', response.data.message || 'Netzwerk konnte nicht gefunden werden.'); } } catch (e) { window.notify('error', e.response?.data?.message || 'Fehler bei der Netzwerksuche.'); } }, handlePopState() { const params = new URLSearchParams(window.location.search); const parentId = params.get('parent_network_id') || null; this.switchCurrentNetwork(parentId, false); // false to not push state }, openModal(row = null) { if (row) { this.modalData = { id: row.id, network_address: row.network_address_str.split('/')[0], cidr: row.cidr, status: row.status, name: row.name, description: row.description, location: row.location, }; } else { this.modalData = { network_address: '', cidr: '', status: 'active', name: '', description: '', location: '', }; } }, closeModal() { this.modalData = null; }, async switchCurrentNetwork(networkId = null, pushState = true) { this.$refs.table.filters = { ...this.$refs.table.filters, parent_network_id: networkId || undefined }; if (pushState) { const url = new URL(window.location); if (networkId) { url.searchParams.set('parent_network_id', networkId); } else { url.searchParams.delete('parent_network_id'); } window.history.pushState({path: url.href}, '', url.href); } if (networkId) { try { const response = await axios.get(`${this.apiUrl}/getById?id=${networkId}`); this.currentNetworkData = response.data.network; this.IpNetworkTableConfig.tableHeader = `IPAM - ${this.currentNetworkData.network_address_str} ${this.currentNetworkData.name ? '- ' + this.currentNetworkData.name : ''}`; } catch (e) { window.notify('error', 'Details des übergeordneten Netzwerks konnten nicht geladen werden.'); this.currentNetworkData = null; this.IpNetworkTableConfig.tableHeader = 'IPAM - Stamm'; } } else { this.currentNetworkData = null; this.IpNetworkTableConfig.tableHeader = 'IPAM - Stamm'; } }, resetFiltersAndSwitch() { this.globalSearchTerm = ''; this.switchCurrentNetwork(null); }, navigateBack() { window.history.back(); }, async submitModal() { const isUpdate = !!this.modalData.id; const url = isUpdate ? `${this.apiUrl}/update` : `${this.apiUrl}/create`; const payload = { ...this.modalData, parent_network_id: this.currentNetworkData?.id || null }; try { const response = await axios.post(url, payload); if (response.data.success) { window.notify('success', response.data.message); this.closeModal(); this.$refs.table.refreshTable(); } else { window.notify('error', response.data.message || 'Ein Fehler ist aufgetreten.'); } } catch (e) { window.notify('error', e.response?.data?.message || 'Ein Netzwerkfehler ist aufgetreten.'); } }, async deleteNetwork() { if (!confirm('Sind Sie sicher, dass Sie dieses Netzwerk und alle untergeordneten Netzwerke löschen möchten?')) return; try { const response = await axios.post(`${this.apiUrl}/delete`, { id: this.modalData.id }); if (response.data.success) { window.notify('success', response.data.message); this.closeModal(); this.$refs.table.refreshTable(); } else { window.notify('error', response.data.message || 'Ein Fehler ist aufgetreten.'); } } catch (e) { window.notify('error', e.response?.data?.message || 'Ein Netzwerkfehler ist aufgetreten.'); } } } })