feat: добавлена возможность ручного ввода времени и трафика

- Добавлены custom input поля для expiration (секунды) и traffic (МБ)
- Добавлена функциональность редактирования клиента
- Исправлена migration 007 (AFTER expires_at)
- Удалены дублирующиеся миграции (0025, 0044, 0053, 0057)
- Удалён старый init.sql (заменён на 001_init.sql)
- Добавлены переводы для custom полей на 6 языках
This commit is contained in:
infosave2007
2025-11-08 10:45:05 +03:00
parent b6cf9cbab7
commit bbb0fbeeb9
15 changed files with 305 additions and 816 deletions
+225
View File
@@ -64,6 +64,85 @@
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
<div class="bg-white rounded shadow p-6">
<h3 class="font-bold mb-4">{{ t('clients.expiration') }}</h3>
<form onsubmit="updateExpiration(event, {{ client.id }})" class="space-y-3">
<div>
<label class="block text-sm text-gray-600 mb-1">{{ t('clients.expiration') }}</label>
<select id="expirationSelect" class="w-full px-3 py-2 border rounded mb-2" onchange="toggleExpirationEdit()">
<option value="" selected disabled>-- Select from list --</option>
<option value="never">{{ t('clients.never_expires') }}</option>
<option value="7">7 {{ t('common.days') }}</option>
<option value="30">30 {{ t('common.days') }}</option>
<option value="60">60 {{ t('common.days') }}</option>
<option value="90">90 {{ t('common.days') }}</option>
<option value="180">180 {{ t('common.days') }}</option>
<option value="365">365 {{ t('common.days') }}</option>
<option value="custom">{{ t('clients.custom_seconds') }}</option>
</select>
<input type="number" id="expirationSeconds" class="w-full px-3 py-2 border rounded" placeholder="{{ t('clients.enter_seconds') }}" style="display:none;" min="1">
</div>
<div class="p-3 bg-blue-50 border border-blue-200 rounded">
<div class="text-xs text-blue-600 font-semibold mb-1">CURRENT VALUE:</div>
<div class="text-sm text-blue-900 font-medium" id="currentExpiration">
{% if client.expires_at %}
{% set expires_ts = client.expires_at|date('U') %}
{% set now_ts = "now"|date('U') %}
{% set diff_days = ((expires_ts - now_ts) / 86400)|round %}
{{ client.expires_at|date('Y-m-d H:i:s') }} ({{ diff_days }} {{ t('common.days') }})
{% else %}
{{ t('clients.never_expires') }}
{% endif %}
</div>
</div>
<button type="submit" class="gradient-bg text-white px-4 py-2 rounded w-full">
<i class="fas fa-save"></i> {{ t('form.update') }}
</button>
</form>
</div>
<div class="bg-white rounded shadow p-6">
<h3 class="font-bold mb-4">{{ t('clients.traffic_limit') }}</h3>
<form onsubmit="updateTrafficLimit(event, {{ client.id }})" class="space-y-3">
<div>
<label class="block text-sm text-gray-600 mb-1">{{ t('clients.traffic_limit') }}</label>
<select id="trafficSelect" class="w-full px-3 py-2 border rounded mb-2" onchange="toggleTrafficEdit()">
<option value="" selected disabled>-- Select from list --</option>
<option value="unlimited">{{ t('clients.unlimited') }}</option>
<option value="1">1 GB</option>
<option value="5">5 GB</option>
<option value="10">10 GB</option>
<option value="25">25 GB</option>
<option value="50">50 GB</option>
<option value="100">100 GB</option>
<option value="250">250 GB</option>
<option value="500">500 GB</option>
<option value="1000">1000 GB (1 TB)</option>
<option value="custom">{{ t('clients.custom_mb') }}</option>
</select>
<input type="number" id="trafficMegabytes" class="w-full px-3 py-2 border rounded" placeholder="{{ t('clients.enter_megabytes') }}" style="display:none;" min="1">
</div>
<div class="p-3 bg-blue-50 border border-blue-200 rounded">
<div class="text-xs text-blue-600 font-semibold mb-1">CURRENT VALUE:</div>
<div class="text-sm text-blue-900 font-medium" id="currentTrafficLimit">
{% if client.traffic_limit %}
{% set limit_gb = (client.traffic_limit / 1073741824)|number_format(2) %}
{% set total_traffic = (client.bytes_sent|default(0) + client.bytes_received|default(0)) %}
{% set used_gb = (total_traffic / 1073741824)|number_format(2) %}
{{ limit_gb }} GB (used: {{ used_gb }} GB)
{% else %}
{{ t('clients.unlimited') }}
{% endif %}
</div>
</div>
<button type="submit" class="gradient-bg text-white px-4 py-2 rounded w-full">
<i class="fas fa-save"></i> {{ t('form.update') }}
</button>
</form>
</div>
</div>
{% if client.qr_code %}
<div class="bg-white rounded shadow p-6 text-center">
<h3 class="font-bold mb-4">QR Code</h3>
@@ -74,6 +153,152 @@
</div>
<script>
function toggleExpirationEdit() {
const select = document.getElementById('expirationSelect');
const input = document.getElementById('expirationSeconds');
if (select.value === 'custom') {
input.style.display = 'block';
input.required = true;
input.focus();
} else {
input.style.display = 'none';
input.required = false;
input.value = '';
}
}
function toggleTrafficEdit() {
const select = document.getElementById('trafficSelect');
const input = document.getElementById('trafficMegabytes');
if (select.value === 'custom') {
input.style.display = 'block';
input.required = true;
input.focus();
} else {
input.style.display = 'none';
input.required = false;
input.value = '';
}
}
async function updateExpiration(event, clientId) {
event.preventDefault();
const select = document.getElementById('expirationSelect');
const customInput = document.getElementById('expirationSeconds');
let expiresAt = null;
let displayText = '{{ t("clients.never_expires") }}';
if (select.value === 'custom' && customInput.value) {
// Convert seconds to timestamp
const seconds = parseInt(customInput.value);
const date = new Date(Date.now() + seconds * 1000);
expiresAt = date.toISOString().slice(0, 19).replace('T', ' ');
const days = Math.round(seconds / 86400);
displayText = `${expiresAt} (${days} {{ t("common.days") }})`;
} else if (select.value === 'never') {
// Set to unlimited
expiresAt = null;
displayText = '{{ t("clients.never_expires") }}';
} else if (select.value && select.value !== 'custom' && select.value !== '') {
// Convert days to timestamp
const days = parseInt(select.value);
const date = new Date(Date.now() + days * 86400 * 1000);
expiresAt = date.toISOString().slice(0, 19).replace('T', ' ');
displayText = `${expiresAt} (${days} {{ t("common.days") }})`;
} else {
alert('Please select an option');
return;
}
try {
const response = await fetch(`/api/clients/${clientId}/set-expiration`, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ expires_at: expiresAt })
});
const data = await response.json();
if (data.success !== false) {
alert('Expiration updated successfully');
document.getElementById('currentExpiration').textContent = displayText;
// Reset form
select.value = '';
customInput.value = '';
customInput.style.display = 'none';
} else {
alert('Error: ' + (data.error || 'Unknown error'));
}
} catch (error) {
alert('Error: ' + error.message);
}
}
async function updateTrafficLimit(event, clientId) {
event.preventDefault();
const select = document.getElementById('trafficSelect');
const customInput = document.getElementById('trafficMegabytes');
let limitBytes = null;
let displayText = '{{ t("clients.unlimited") }}';
if (select.value === 'custom' && customInput.value) {
// Convert MB to bytes
limitBytes = parseInt(customInput.value) * 1048576;
const limitGb = (limitBytes / 1073741824).toFixed(2);
displayText = `${limitGb} GB`;
} else if (select.value === 'unlimited') {
// Set to unlimited
limitBytes = null;
displayText = '{{ t("clients.unlimited") }}';
} else if (select.value && select.value !== 'custom' && select.value !== '') {
// Convert GB to bytes
limitBytes = parseFloat(select.value) * 1073741824;
displayText = `${parseFloat(select.value).toFixed(2)} GB`;
} else {
alert('Please select an option');
return;
}
try {
const response = await fetch(`/api/clients/${clientId}/set-traffic-limit`, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ limit_bytes: limitBytes })
});
const data = await response.json();
if (data.success !== false) {
alert('Traffic limit updated successfully');
// Update current value display (keep usage info if available)
const currentDiv = document.getElementById('currentTrafficLimit');
const usageMatch = currentDiv.textContent.match(/used: ([\d.]+) GB/);
if (usageMatch && limitBytes !== null) {
displayText += ` (used: ${usageMatch[1]} GB)`;
}
currentDiv.textContent = displayText;
// Reset form
select.value = '';
customInput.value = '';
customInput.style.display = 'none';
} else {
alert('Error: ' + (data.error || 'Unknown error'));
}
} catch (error) {
alert('Error: ' + error.message);
}
}
async function syncStats(clientId) {
try {
const response = await fetch(`/clients/${clientId}/sync-stats`, {
+34 -2
View File
@@ -18,7 +18,7 @@
<input name="name" placeholder="{{ t('clients.name') }}" required class="w-full px-3 py-2 border rounded" id="clientName">
<div>
<label class="block text-sm text-gray-600 mb-1">{{ t('clients.expiration') }}</label>
<select name="expires_in_days" class="w-full px-3 py-2 border rounded">
<select name="expires_in_days" class="w-full px-3 py-2 border rounded mb-2" id="expirationSelect" onchange="toggleExpirationInput()">
<option value="" selected>{{ t('clients.never_expires') }}</option>
<option value="7">7 {{ t('common.days') }}</option>
<option value="30">30 {{ t('common.days') }}</option>
@@ -26,11 +26,13 @@
<option value="90">90 {{ t('common.days') }}</option>
<option value="180">180 {{ t('common.days') }}</option>
<option value="365">365 {{ t('common.days') }}</option>
<option value="custom">{{ t('clients.custom_seconds') }}</option>
</select>
<input type="number" name="expires_in_seconds" id="expirationSeconds" class="w-full px-3 py-2 border rounded" placeholder="{{ t('clients.enter_seconds') }}" style="display:none;" min="1">
</div>
<div>
<label class="block text-sm text-gray-600 mb-1">{{ t('clients.traffic_limit') }}</label>
<select name="traffic_limit_gb" class="w-full px-3 py-2 border rounded">
<select name="traffic_limit_gb" class="w-full px-3 py-2 border rounded mb-2" id="trafficSelect" onchange="toggleTrafficInput()">
<option value="" selected>{{ t('clients.unlimited') }}</option>
<option value="1">1 GB</option>
<option value="5">5 GB</option>
@@ -41,7 +43,9 @@
<option value="250">250 GB</option>
<option value="500">500 GB</option>
<option value="1000">1000 GB (1 TB)</option>
<option value="custom">{{ t('clients.custom_mb') }}</option>
</select>
<input type="number" name="traffic_limit_mb" id="trafficMegabytes" class="w-full px-3 py-2 border rounded" placeholder="{{ t('clients.enter_megabytes') }}" style="display:none;" min="1">
</div>
<button type="submit" class="gradient-bg text-white px-4 py-2 rounded w-full" id="createClientBtn">
<span id="createClientText">{{ t('form.create') }}</span>
@@ -182,6 +186,34 @@
</div>
<script>
function toggleExpirationInput() {
const select = document.getElementById('expirationSelect');
const input = document.getElementById('expirationSeconds');
if (select.value === 'custom') {
input.style.display = 'block';
input.required = true;
input.focus();
} else {
input.style.display = 'none';
input.required = false;
input.value = '';
}
}
function toggleTrafficInput() {
const select = document.getElementById('trafficSelect');
const input = document.getElementById('trafficMegabytes');
if (select.value === 'custom') {
input.style.display = 'block';
input.required = true;
input.focus();
} else {
input.style.display = 'none';
input.required = false;
input.value = '';
}
}
document.addEventListener('DOMContentLoaded', function() {
const form = document.getElementById('createClientForm');
if (form) {