Files
ip-manager/webapp/index.php
2026-01-16 19:48:04 +00:00

1198 lines
41 KiB
PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Geofeed Manager</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=SF+Pro+Display:wght@300;400;500;600;700&family=SF+Pro+Text:wght@400;500;600&display=swap" rel="stylesheet">
<style>
:root {
--bg-primary: #f5f5f7;
--bg-secondary: #ffffff;
--bg-tertiary: #fbfbfd;
--text-primary: #1d1d1f;
--text-secondary: #86868b;
--text-tertiary: #6e6e73;
--accent: #0071e3;
--accent-hover: #0077ed;
--accent-light: rgba(0, 113, 227, 0.08);
--border: rgba(0, 0, 0, 0.08);
--border-strong: rgba(0, 0, 0, 0.12);
--shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.04), 0 1px 2px rgba(0, 0, 0, 0.06);
--shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
--shadow-lg: 0 10px 25px -5px rgba(0, 0, 0, 0.08), 0 8px 10px -6px rgba(0, 0, 0, 0.04);
--shadow-xl: 0 25px 50px -12px rgba(0, 0, 0, 0.15);
--radius-sm: 8px;
--radius-md: 12px;
--radius-lg: 16px;
--radius-xl: 20px;
--success: #34c759;
--warning: #ff9500;
--error: #ff3b30;
--transition: all 0.2s cubic-bezier(0.25, 0.1, 0.25, 1);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'SF Pro Display', 'Helvetica Neue', Helvetica, Arial, sans-serif;
background: var(--bg-primary);
color: var(--text-primary);
line-height: 1.47059;
letter-spacing: -0.022em;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
min-height: 100vh;
}
/* Header */
.header {
background: rgba(255, 255, 255, 0.72);
backdrop-filter: saturate(180%) blur(20px);
-webkit-backdrop-filter: saturate(180%) blur(20px);
border-bottom: 1px solid var(--border);
position: sticky;
top: 0;
z-index: 100;
}
.header-content {
max-width: 1200px;
margin: 0 auto;
padding: 16px 24px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
}
.logo {
display: flex;
align-items: center;
gap: 12px;
}
.logo-icon {
width: 40px;
height: 40px;
background: linear-gradient(135deg, var(--accent) 0%, #5856d6 100%);
border-radius: var(--radius-md);
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: 600;
font-size: 18px;
}
.logo-text {
font-size: 21px;
font-weight: 600;
letter-spacing: -0.03em;
}
.header-actions {
display: flex;
align-items: center;
gap: 12px;
}
/* Buttons */
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
padding: 10px 20px;
font-size: 14px;
font-weight: 500;
border-radius: 980px;
border: none;
cursor: pointer;
transition: var(--transition);
text-decoration: none;
white-space: nowrap;
}
.btn-primary {
background: var(--accent);
color: white;
}
.btn-primary:hover {
background: var(--accent-hover);
transform: scale(1.02);
}
.btn-secondary {
background: var(--bg-secondary);
color: var(--text-primary);
border: 1px solid var(--border-strong);
}
.btn-secondary:hover {
background: var(--bg-tertiary);
border-color: var(--border-strong);
}
.btn-ghost {
background: transparent;
color: var(--accent);
padding: 8px 16px;
}
.btn-ghost:hover {
background: var(--accent-light);
}
.btn-danger {
background: var(--error);
color: white;
}
.btn-danger:hover {
background: #e8342a;
}
.btn-sm {
padding: 6px 14px;
font-size: 13px;
}
.btn-icon {
width: 36px;
height: 36px;
padding: 0;
border-radius: 50%;
}
/* Main Content */
.main {
max-width: 1200px;
margin: 0 auto;
padding: 32px 24px;
}
/* Stats Cards */
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 16px;
margin-bottom: 32px;
}
.stat-card {
background: var(--bg-secondary);
border-radius: var(--radius-lg);
padding: 24px;
box-shadow: var(--shadow-sm);
border: 1px solid var(--border);
}
.stat-label {
font-size: 13px;
color: var(--text-secondary);
text-transform: uppercase;
letter-spacing: 0.02em;
margin-bottom: 8px;
}
.stat-value {
font-size: 32px;
font-weight: 600;
letter-spacing: -0.03em;
color: var(--text-primary);
}
.stat-change {
font-size: 13px;
margin-top: 4px;
}
.stat-change.positive { color: var(--success); }
.stat-change.negative { color: var(--error); }
/* Search Bar */
.search-container {
margin-bottom: 24px;
}
.search-bar {
display: flex;
align-items: center;
gap: 12px;
background: var(--bg-secondary);
border: 1px solid var(--border);
border-radius: var(--radius-lg);
padding: 12px 20px;
transition: var(--transition);
}
.search-bar:focus-within {
border-color: var(--accent);
box-shadow: 0 0 0 3px var(--accent-light);
}
.search-icon {
color: var(--text-secondary);
flex-shrink: 0;
}
.search-input {
flex: 1;
border: none;
background: transparent;
font-size: 17px;
color: var(--text-primary);
outline: none;
}
.search-input::placeholder {
color: var(--text-secondary);
}
/* Table */
.table-container {
background: var(--bg-secondary);
border-radius: var(--radius-xl);
box-shadow: var(--shadow-md);
border: 1px solid var(--border);
overflow: hidden;
}
.table-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 20px 24px;
border-bottom: 1px solid var(--border);
background: var(--bg-tertiary);
}
.table-title {
font-size: 17px;
font-weight: 600;
}
.table-actions {
display: flex;
gap: 8px;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
text-align: left;
padding: 14px 20px;
}
th {
font-size: 12px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.04em;
color: var(--text-secondary);
background: var(--bg-tertiary);
border-bottom: 1px solid var(--border);
}
td {
font-size: 14px;
border-bottom: 1px solid var(--border);
vertical-align: middle;
}
tr:last-child td {
border-bottom: none;
}
tr:hover td {
background: var(--bg-tertiary);
}
.ip-prefix {
font-family: 'SF Mono', SFMono-Regular, ui-monospace, Menlo, Monaco, monospace;
font-size: 13px;
font-weight: 500;
color: var(--accent);
background: var(--accent-light);
padding: 4px 10px;
border-radius: 6px;
display: inline-block;
}
.country-badge {
display: inline-flex;
align-items: center;
gap: 6px;
font-size: 13px;
font-weight: 500;
}
.country-flag {
font-size: 16px;
}
.row-actions {
display: flex;
gap: 4px;
opacity: 0.5;
transition: var(--transition);
}
tr:hover .row-actions {
opacity: 1;
}
/* Pagination */
.pagination {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
padding: 20px;
border-top: 1px solid var(--border);
background: var(--bg-tertiary);
}
.pagination-btn {
width: 36px;
height: 36px;
display: flex;
align-items: center;
justify-content: center;
border: none;
background: transparent;
color: var(--text-secondary);
border-radius: var(--radius-sm);
cursor: pointer;
font-size: 14px;
font-weight: 500;
transition: var(--transition);
}
.pagination-btn:hover:not(:disabled) {
background: var(--bg-secondary);
color: var(--text-primary);
}
.pagination-btn.active {
background: var(--accent);
color: white;
}
.pagination-btn:disabled {
opacity: 0.3;
cursor: not-allowed;
}
.pagination-info {
color: var(--text-secondary);
font-size: 13px;
margin: 0 16px;
}
/* Modal */
.modal-overlay {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.4);
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
opacity: 0;
visibility: hidden;
transition: var(--transition);
}
.modal-overlay.active {
opacity: 1;
visibility: visible;
}
.modal {
background: var(--bg-secondary);
border-radius: var(--radius-xl);
box-shadow: var(--shadow-xl);
width: 100%;
max-width: 520px;
max-height: 90vh;
overflow-y: auto;
transform: scale(0.95) translateY(20px);
transition: var(--transition);
}
.modal-overlay.active .modal {
transform: scale(1) translateY(0);
}
.modal-header {
padding: 24px 24px 0;
}
.modal-title {
font-size: 24px;
font-weight: 600;
letter-spacing: -0.03em;
margin-bottom: 8px;
}
.modal-subtitle {
font-size: 14px;
color: var(--text-secondary);
}
.modal-body {
padding: 24px;
}
.modal-footer {
padding: 0 24px 24px;
display: flex;
justify-content: flex-end;
gap: 12px;
}
/* Form */
.form-group {
margin-bottom: 20px;
}
.form-label {
display: block;
font-size: 13px;
font-weight: 600;
color: var(--text-primary);
margin-bottom: 8px;
}
.form-label .required {
color: var(--error);
}
.form-input {
width: 100%;
padding: 12px 16px;
font-size: 17px;
border: 1px solid var(--border-strong);
border-radius: var(--radius-md);
background: var(--bg-tertiary);
color: var(--text-primary);
transition: var(--transition);
}
.form-input:focus {
outline: none;
border-color: var(--accent);
box-shadow: 0 0 0 3px var(--accent-light);
background: var(--bg-secondary);
}
.form-input::placeholder {
color: var(--text-secondary);
}
.form-hint {
font-size: 12px;
color: var(--text-secondary);
margin-top: 6px;
}
.form-error {
font-size: 12px;
color: var(--error);
margin-top: 6px;
}
.form-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 16px;
}
/* Toast */
.toast-container {
position: fixed;
top: 24px;
right: 24px;
z-index: 2000;
display: flex;
flex-direction: column;
gap: 12px;
}
.toast {
background: var(--bg-secondary);
border-radius: var(--radius-md);
padding: 16px 20px;
box-shadow: var(--shadow-lg);
border: 1px solid var(--border);
display: flex;
align-items: center;
gap: 12px;
min-width: 320px;
animation: slideIn 0.3s ease;
}
@keyframes slideIn {
from {
opacity: 0;
transform: translateX(100%);
}
to {
opacity: 1;
transform: translateX(0);
}
}
.toast-icon {
width: 24px;
height: 24px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.toast.success .toast-icon {
background: rgba(52, 199, 89, 0.15);
color: var(--success);
}
.toast.error .toast-icon {
background: rgba(255, 59, 48, 0.15);
color: var(--error);
}
.toast-message {
flex: 1;
font-size: 14px;
font-weight: 500;
}
.toast-close {
background: none;
border: none;
cursor: pointer;
color: var(--text-secondary);
padding: 4px;
}
/* Empty State */
.empty-state {
text-align: center;
padding: 60px 24px;
}
.empty-icon {
width: 64px;
height: 64px;
background: var(--bg-tertiary);
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 20px;
color: var(--text-secondary);
}
.empty-title {
font-size: 17px;
font-weight: 600;
margin-bottom: 8px;
}
.empty-text {
font-size: 14px;
color: var(--text-secondary);
max-width: 400px;
margin: 0 auto 24px;
}
/* Loading */
.loading {
display: flex;
align-items: center;
justify-content: center;
padding: 60px;
}
.spinner {
width: 32px;
height: 32px;
border: 3px solid var(--border);
border-top-color: var(--accent);
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
/* Responsive */
@media (max-width: 768px) {
.header-content {
flex-wrap: wrap;
}
.form-row {
grid-template-columns: 1fr;
}
.stats-grid {
grid-template-columns: repeat(2, 1fr);
}
table {
font-size: 13px;
}
th, td {
padding: 12px;
}
.modal {
margin: 16px;
}
}
/* Delete Confirmation */
.confirm-delete-text {
font-size: 15px;
line-height: 1.5;
color: var(--text-primary);
}
.confirm-delete-prefix {
font-family: 'SF Mono', monospace;
background: var(--bg-tertiary);
padding: 2px 8px;
border-radius: 4px;
color: var(--error);
}
</style>
</head>
<body>
<header class="header">
<div class="header-content">
<div class="logo">
<div class="logo-icon">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="10"/>
<line x1="2" y1="12" x2="22" y2="12"/>
<path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/>
</svg>
</div>
<span class="logo-text">Geofeed Manager</span>
</div>
<div class="header-actions">
<button class="btn btn-secondary" onclick="exportCSV()">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/>
<polyline points="7 10 12 15 17 10"/>
<line x1="12" y1="15" x2="12" y2="3"/>
</svg>
Export CSV
</button>
<button class="btn btn-primary" onclick="openModal()">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<line x1="12" y1="5" x2="12" y2="19"/>
<line x1="5" y1="12" x2="19" y2="12"/>
</svg>
Add Entry
</button>
</div>
</div>
</header>
<main class="main">
<!-- Stats -->
<div class="stats-grid" id="statsGrid">
<div class="stat-card">
<div class="stat-label">Total Entries</div>
<div class="stat-value" id="statTotal">-</div>
</div>
<div class="stat-card">
<div class="stat-label">IPv4 Prefixes</div>
<div class="stat-value" id="statIPv4">-</div>
</div>
<div class="stat-card">
<div class="stat-label">IPv6 Prefixes</div>
<div class="stat-value" id="statIPv6">-</div>
</div>
<div class="stat-card">
<div class="stat-label">Countries</div>
<div class="stat-value" id="statCountries">-</div>
</div>
</div>
<!-- Search -->
<div class="search-container">
<div class="search-bar">
<svg class="search-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="11" cy="11" r="8"/>
<line x1="21" y1="21" x2="16.65" y2="16.65"/>
</svg>
<input type="text" class="search-input" id="searchInput" placeholder="Search IP prefixes, cities, regions..." oninput="debounceSearch()">
</div>
</div>
<!-- Table -->
<div class="table-container">
<div class="table-header">
<h2 class="table-title">Geofeed Entries</h2>
<div class="table-actions">
<button class="btn btn-ghost btn-sm" onclick="loadEntries()">
<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="23 4 23 10 17 10"/>
<path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/>
</svg>
Refresh
</button>
</div>
</div>
<div id="tableContent">
<div class="loading">
<div class="spinner"></div>
</div>
</div>
</div>
</main>
<!-- Add/Edit Modal -->
<div class="modal-overlay" id="entryModal">
<div class="modal">
<div class="modal-header">
<h2 class="modal-title" id="modalTitle">Add Entry</h2>
<p class="modal-subtitle">Enter the geofeed entry details below</p>
</div>
<form id="entryForm" onsubmit="saveEntry(event)">
<input type="hidden" id="entryId" name="id">
<div class="modal-body">
<div class="form-group">
<label class="form-label">IP Prefix <span class="required">*</span></label>
<input type="text" class="form-input" id="ipPrefix" name="ip_prefix" placeholder="e.g., 192.168.1.0/24 or 2001:db8::/32" required>
<div class="form-hint">IPv4 or IPv6 address in CIDR notation</div>
</div>
<div class="form-row">
<div class="form-group">
<label class="form-label">Country Code</label>
<input type="text" class="form-input" id="countryCode" name="country_code" placeholder="e.g., GB" maxlength="2" style="text-transform: uppercase;">
<div class="form-hint">ISO 3166-1 alpha-2 code</div>
</div>
<div class="form-group">
<label class="form-label">Region Code</label>
<input type="text" class="form-input" id="regionCode" name="region_code" placeholder="e.g., GB-ENG" style="text-transform: uppercase;">
<div class="form-hint">ISO 3166-2 code</div>
</div>
</div>
<div class="form-row">
<div class="form-group">
<label class="form-label">City</label>
<input type="text" class="form-input" id="city" name="city" placeholder="e.g., London">
</div>
<div class="form-group">
<label class="form-label">Postal Code</label>
<input type="text" class="form-input" id="postalCode" name="postal_code" placeholder="e.g., EC1A 1BB">
</div>
</div>
<div class="form-group">
<label class="form-label">Notes</label>
<input type="text" class="form-input" id="notes" name="notes" placeholder="Optional notes for internal use">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" onclick="closeModal()">Cancel</button>
<button type="submit" class="btn btn-primary" id="saveBtn">Save Entry</button>
</div>
</form>
</div>
</div>
<!-- Delete Confirmation Modal -->
<div class="modal-overlay" id="deleteModal">
<div class="modal" style="max-width: 420px;">
<div class="modal-header">
<h2 class="modal-title">Delete Entry</h2>
</div>
<div class="modal-body">
<p class="confirm-delete-text">
Are you sure you want to delete the entry for <span class="confirm-delete-prefix" id="deletePrefix"></span>? This action cannot be undone.
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" onclick="closeDeleteModal()">Cancel</button>
<button type="button" class="btn btn-danger" onclick="confirmDelete()">Delete</button>
</div>
</div>
</div>
<!-- Toast Container -->
<div class="toast-container" id="toastContainer"></div>
<script>
// State
let currentPage = 1;
let totalPages = 1;
let searchTimeout = null;
let deleteEntryId = null;
const csrfToken = '<?php echo generateCSRFToken(); ?>';
// Initialize
document.addEventListener('DOMContentLoaded', () => {
loadEntries();
loadStats();
});
// API Helper
async function api(action, params = {}, method = 'GET', body = null) {
const url = new URL('api.php', window.location.href);
url.searchParams.set('action', action);
Object.entries(params).forEach(([k, v]) => url.searchParams.set(k, v));
const options = { method };
if (body) {
options.headers = { 'Content-Type': 'application/json' };
options.body = JSON.stringify({ ...body, csrf_token: csrfToken });
}
const response = await fetch(url, options);
return response.json();
}
// Load entries
async function loadEntries(page = 1) {
currentPage = page;
const searchQuery = document.getElementById('searchInput').value;
document.getElementById('tableContent').innerHTML = '<div class="loading"><div class="spinner"></div></div>';
try {
const result = await api('list', { page, search: searchQuery });
if (result.success) {
renderTable(result.data, result.pagination);
} else {
showToast(result.error || 'Failed to load entries', 'error');
}
} catch (error) {
showToast('Network error', 'error');
}
}
// Render table
function renderTable(entries, pagination) {
totalPages = pagination.pages;
if (entries.length === 0) {
document.getElementById('tableContent').innerHTML = `
<div class="empty-state">
<div class="empty-icon">
<svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="10"/>
<line x1="2" y1="12" x2="22" y2="12"/>
<path d="M12 2a15.3 15.3 0 0 1 4 10 15.3 15.3 0 0 1-4 10 15.3 15.3 0 0 1-4-10 15.3 15.3 0 0 1 4-10z"/>
</svg>
</div>
<h3 class="empty-title">No entries found</h3>
<p class="empty-text">Get started by adding your first geofeed entry.</p>
<button class="btn btn-primary" onclick="openModal()">Add Entry</button>
</div>
`;
return;
}
const tableHTML = `
<table>
<thead>
<tr>
<th>IP Prefix</th>
<th>Country</th>
<th>Region</th>
<th>City</th>
<th>Postal</th>
<th style="width: 100px;">Actions</th>
</tr>
</thead>
<tbody>
${entries.map(entry => `
<tr>
<td><span class="ip-prefix">${escapeHtml(entry.ip_prefix)}</span></td>
<td>
${entry.country_code ? `
<span class="country-badge">
<span class="country-flag">${getFlagEmoji(entry.country_code)}</span>
${escapeHtml(entry.country_code)}
</span>
` : '<span style="color: var(--text-secondary)">—</span>'}
</td>
<td>${entry.region_code ? escapeHtml(entry.region_code) : '<span style="color: var(--text-secondary)">—</span>'}</td>
<td>${entry.city ? escapeHtml(entry.city) : '<span style="color: var(--text-secondary)">—</span>'}</td>
<td>${entry.postal_code ? escapeHtml(entry.postal_code) : '<span style="color: var(--text-secondary)">—</span>'}</td>
<td>
<div class="row-actions">
<button class="btn btn-ghost btn-icon" onclick="editEntry(${entry.id})" title="Edit">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M11 4H4a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/>
<path d="M18.5 2.5a2.121 2.121 0 0 1 3 3L12 15l-4 1 1-4 9.5-9.5z"/>
</svg>
</button>
<button class="btn btn-ghost btn-icon" onclick="deleteEntry(${entry.id}, '${escapeHtml(entry.ip_prefix)}')" title="Delete" style="color: var(--error);">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="3 6 5 6 21 6"/>
<path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/>
</svg>
</button>
</div>
</td>
</tr>
`).join('')}
</tbody>
</table>
<div class="pagination">
<button class="pagination-btn" onclick="loadEntries(1)" ${currentPage === 1 ? 'disabled' : ''}>
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="11 17 6 12 11 7"/>
<polyline points="18 17 13 12 18 7"/>
</svg>
</button>
<button class="pagination-btn" onclick="loadEntries(${currentPage - 1})" ${currentPage === 1 ? 'disabled' : ''}>
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="15 18 9 12 15 6"/>
</svg>
</button>
<span class="pagination-info">Page ${currentPage} of ${totalPages || 1} (${pagination.total} entries)</span>
<button class="pagination-btn" onclick="loadEntries(${currentPage + 1})" ${currentPage >= totalPages ? 'disabled' : ''}>
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="9 18 15 12 9 6"/>
</svg>
</button>
<button class="pagination-btn" onclick="loadEntries(${totalPages})" ${currentPage >= totalPages ? 'disabled' : ''}>
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="13 17 18 12 13 7"/>
<polyline points="6 17 11 12 6 7"/>
</svg>
</button>
</div>
`;
document.getElementById('tableContent').innerHTML = tableHTML;
}
// Load stats
async function loadStats() {
try {
const result = await api('stats');
if (result.success) {
document.getElementById('statTotal').textContent = result.data.total_entries.toLocaleString();
document.getElementById('statIPv4').textContent = (result.data.ip_versions?.ipv4 || 0).toLocaleString();
document.getElementById('statIPv6').textContent = (result.data.ip_versions?.ipv6 || 0).toLocaleString();
document.getElementById('statCountries').textContent = result.data.by_country?.length || 0;
}
} catch (error) {
console.error('Failed to load stats:', error);
}
}
// Search with debounce
function debounceSearch() {
clearTimeout(searchTimeout);
searchTimeout = setTimeout(() => loadEntries(1), 300);
}
// Modal functions
function openModal(entry = null) {
const modal = document.getElementById('entryModal');
const form = document.getElementById('entryForm');
const title = document.getElementById('modalTitle');
form.reset();
if (entry) {
title.textContent = 'Edit Entry';
document.getElementById('entryId').value = entry.id;
document.getElementById('ipPrefix').value = entry.ip_prefix || '';
document.getElementById('countryCode').value = entry.country_code || '';
document.getElementById('regionCode').value = entry.region_code || '';
document.getElementById('city').value = entry.city || '';
document.getElementById('postalCode').value = entry.postal_code || '';
document.getElementById('notes').value = entry.notes || '';
} else {
title.textContent = 'Add Entry';
document.getElementById('entryId').value = '';
}
modal.classList.add('active');
document.getElementById('ipPrefix').focus();
}
function closeModal() {
document.getElementById('entryModal').classList.remove('active');
}
// Edit entry
async function editEntry(id) {
try {
const result = await api('get', { id });
if (result.success) {
openModal(result.data);
} else {
showToast(result.error || 'Failed to load entry', 'error');
}
} catch (error) {
showToast('Network error', 'error');
}
}
// Save entry
async function saveEntry(event) {
event.preventDefault();
const id = document.getElementById('entryId').value;
const action = id ? 'update' : 'create';
const data = {
id: id || undefined,
ip_prefix: document.getElementById('ipPrefix').value,
country_code: document.getElementById('countryCode').value.toUpperCase(),
region_code: document.getElementById('regionCode').value.toUpperCase(),
city: document.getElementById('city').value,
postal_code: document.getElementById('postalCode').value,
notes: document.getElementById('notes').value
};
try {
const result = await api(action, {}, 'POST', data);
if (result.success) {
showToast(result.message || 'Entry saved successfully', 'success');
closeModal();
loadEntries(currentPage);
loadStats();
} else {
showToast(result.error || 'Failed to save entry', 'error');
}
} catch (error) {
showToast('Network error', 'error');
}
}
// Delete entry
function deleteEntry(id, prefix) {
deleteEntryId = id;
document.getElementById('deletePrefix').textContent = prefix;
document.getElementById('deleteModal').classList.add('active');
}
function closeDeleteModal() {
document.getElementById('deleteModal').classList.remove('active');
deleteEntryId = null;
}
async function confirmDelete() {
if (!deleteEntryId) return;
try {
const result = await api('delete', {}, 'POST', { id: deleteEntryId });
if (result.success) {
showToast('Entry deleted successfully', 'success');
closeDeleteModal();
loadEntries(currentPage);
loadStats();
} else {
showToast(result.error || 'Failed to delete entry', 'error');
}
} catch (error) {
showToast('Network error', 'error');
}
}
// Export CSV
function exportCSV() {
window.location.href = 'api.php?action=export&format=download';
}
// Toast notifications
function showToast(message, type = 'success') {
const container = document.getElementById('toastContainer');
const toast = document.createElement('div');
toast.className = `toast ${type}`;
const icon = type === 'success'
? '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="20 6 9 17 4 12"/></svg>'
: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>';
toast.innerHTML = `
<div class="toast-icon">${icon}</div>
<span class="toast-message">${escapeHtml(message)}</span>
<button class="toast-close" onclick="this.parentElement.remove()">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<line x1="18" y1="6" x2="6" y2="18"/>
<line x1="6" y1="6" x2="18" y2="18"/>
</svg>
</button>
`;
container.appendChild(toast);
setTimeout(() => {
toast.style.animation = 'slideIn 0.3s ease reverse';
setTimeout(() => toast.remove(), 300);
}, 4000);
}
// Helpers
function escapeHtml(text) {
if (!text) return '';
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function getFlagEmoji(countryCode) {
if (!countryCode || countryCode.length !== 2) return '';
const codePoints = countryCode
.toUpperCase()
.split('')
.map(char => 127397 + char.charCodeAt());
return String.fromCodePoint(...codePoints);
}
// Close modals on escape key
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
closeModal();
closeDeleteModal();
}
});
// Close modals on overlay click
document.querySelectorAll('.modal-overlay').forEach(overlay => {
overlay.addEventListener('click', (e) => {
if (e.target === overlay) {
closeModal();
closeDeleteModal();
}
});
});
</script>
</body>
</html>