Files
PPve/admin-dashboard.html
T
Tomas Dvorak 7b9cafeed2 r
2025-05-28 12:53:23 +02:00

1676 lines
58 KiB
HTML

<!DOCTYPE html>
<html lang="cs">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Dashboard - PP Kunovice</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
:root {
--primary-color: #4a6cf7;
--primary-hover: #3a56d4;
--secondary-color: #6c757d;
--success-color: #28a745;
--danger-color: #dc3545;
--warning-color: #ffc107;
--info-color: #17a2b8;
--light-color: #f8f9fa;
--dark-color: #212529;
--border-color: #e9ecef;
--border-radius: 8px;
--box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05), 0 1px 3px rgba(0, 0, 0, 0.1);
--transition: all 0.3s ease;
}
* {
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Roboto, -apple-system, BlinkMacSystemFont, sans-serif;
margin: 0;
padding: 0;
background-color: #f8f9fa;
color: #333;
line-height: 1.6;
}
.header {
background-color: #fff;
color: var(--dark-color);
padding: 1rem 2rem;
display: flex;
justify-content: space-between;
align-items: center;
box-shadow: var(--box-shadow);
position: sticky;
top: 0;
z-index: 1000;
}
.header h1 {
margin: 0;
font-size: 1.5rem;
font-weight: 600;
color: var(--primary-color);
}
.logout-btn {
background-color: transparent;
border: 1px solid var(--primary-color);
color: var(--primary-color);
padding: 0.5rem 1.2rem;
border-radius: var(--border-radius);
cursor: pointer;
font-weight: 500;
transition: var(--transition);
}
.logout-btn:hover {
background-color: var(--primary-color);
color: white;
}
.container {
max-width: 1200px;
margin: 2rem auto;
padding: 0 1.5rem;
}
.dashboard-cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 1.5rem;
margin-top: 2rem;
}
.card {
background-color: white;
border-radius: var(--border-radius);
padding: 1.75rem;
box-shadow: var(--box-shadow);
transition: var(--transition);
border: 1px solid var(--border-color);
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
}
.card h3 {
margin-top: 0;
color: var(--dark-color);
font-weight: 600;
font-size: 1.25rem;
margin-bottom: 1rem;
padding-bottom: 0.75rem;
border-bottom: 1px solid var(--border-color);
}
.form-group {
margin-bottom: 1.5rem;
}
.form-group label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
color: var(--dark-color);
font-size: 0.95rem;
}
.form-group input[type="text"],
.form-group input[type="url"],
.form-group input[type="number"],
.form-group textarea,
.form-group select {
width: 100%;
padding: 0.75rem 1rem;
border: 1px solid var(--border-color);
border-radius: var(--border-radius);
font-size: 1rem;
transition: var(--transition);
box-shadow: inset 0 1px 2px rgba(0,0,0,0.05);
}
.form-group input[type="text"]:focus,
.form-group input[type="url"]:focus,
.form-group input[type="number"]:focus,
.form-group textarea:focus,
.form-group select:focus {
border-color: var(--primary-color);
outline: none;
box-shadow: 0 0 0 3px rgba(74, 108, 247, 0.15);
}
.form-group textarea {
min-height: 120px;
resize: vertical;
line-height: 1.5;
}
.color-preview {
display: inline-block;
width: 30px;
height: 30px;
border: 1px solid var(--border-color);
border-radius: 50%;
vertical-align: middle;
margin-left: 10px;
box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
}
.color-picker {
margin-left: 10px;
vertical-align: middle;
height: 30px;
padding: 0;
border: 1px solid var(--border-color);
border-radius: 4px;
cursor: pointer;
}
.style-presets {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin: 15px 0;
}
.style-preset {
padding: 8px 16px;
border: 1px solid var(--border-color);
border-radius: var(--border-radius);
cursor: pointer;
font-size: 0.9rem;
transition: var(--transition);
background-color: white;
font-weight: 500;
}
.style-preset:hover {
background-color: var(--light-color);
border-color: var(--secondary-color);
transform: translateY(-2px);
}
.banner-preview {
margin: 2rem 0;
padding: 0;
border: 2px dashed var(--border-color);
border-radius: var(--border-radius);
background-color: #fff;
display: none;
position: relative;
overflow: hidden;
min-height: 180px;
transition: var(--transition);
box-shadow: var(--box-shadow);
width: 100%;
max-width: 100%;
}
.banner-preview:hover {
border-color: var(--primary-color);
box-shadow: 0 8px 16px rgba(0,0,0,0.1);
}
.banner-preview::before {
content: 'Náhled banneru';
position: absolute;
top: 10px;
right: 10px;
background-color: rgba(255,255,255,0.8);
padding: 4px 10px;
border-radius: 4px;
font-size: 0.8rem;
color: var(--secondary-color);
z-index: 3;
font-weight: 500;
}
.banner-preview img {
max-width: 100%;
max-height: 300px;
object-fit: contain;
display: block;
margin: 0 auto;
}
.banner-preview.with-image {
min-height: 220px;
}
.banner-preview-content {
position: relative;
z-index: 2;
text-align: center;
padding: 20px;
margin: 0;
width: 100%;
box-sizing: border-box;
display: block;
}
.banner-preview-bg {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
opacity: 0.7;
z-index: 1;
transition: var(--transition);
}
.banner-preview-text {
position: relative;
z-index: 2;
margin: 0;
padding: 0;
line-height: 1.5;
word-wrap: break-word;
}
.color-picker-container {
display: flex;
align-items: center;
gap: 10px;
}
.color-picker {
vertical-align: middle;
margin-right: 8px;
cursor: pointer;
width: 40px;
height: 40px;
border: none;
border-radius: 8px;
overflow: hidden;
padding: 0;
background: none;
}
.image-upload-container {
margin: 15px 0;
padding: 15px;
border: 1px dashed #ddd;
border-radius: 4px;
text-align: center;
}
.image-preview {
max-width: 200px;
max-height: 150px;
margin: 10px auto;
display: block;
}
.card p {
color: #666;
margin-bottom: 0;
}
/* Form Actions */
.form-actions {
margin-top: 2.5rem;
padding-top: 1.5rem;
border-top: 1px solid var(--border-color);
display: flex;
justify-content: flex-end;
gap: 1rem;
}
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
padding: 0.75rem 1.5rem;
border: 1px solid transparent;
border-radius: var(--border-radius);
font-size: 1rem;
font-weight: 500;
cursor: pointer;
transition: var(--transition);
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
position: relative;
overflow: hidden;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.btn:active {
transform: translateY(0);
}
.btn i {
margin-right: 0.5rem;
font-size: 0.9em;
}
.btn-primary {
background-color: var(--primary-color);
color: white;
border-color: var(--primary-color);
}
.btn-primary:hover {
background-color: var(--primary-hover);
border-color: var(--primary-hover);
}
.btn-secondary {
background-color: var(--secondary-color);
color: white;
border-color: var(--secondary-color);
}
.btn-secondary:hover {
background-color: #5a6268;
border-color: #545b62;
}
.btn-danger {
background-color: var(--danger-color);
color: white;
border-color: var(--danger-color);
}
.btn-danger:hover {
background-color: #c82333;
border-color: #bd2130;
}
.btn:disabled {
opacity: 0.65;
cursor: not-allowed;
transform: none;
}
/* Notifications */
.notification {
position: fixed;
top: 20px;
right: 20px;
padding: 16px 24px;
border-radius: var(--border-radius);
color: white;
display: flex;
align-items: center;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
z-index: 1000;
animation: fadeIn 0.3s, fadeOut 0.3s 2.7s forwards;
min-width: 300px;
max-width: 450px;
}
.notification i {
margin-right: 12px;
font-size: 1.2rem;
}
.notification.info {
background-color: var(--info-color);
border-left: 4px solid #117a8b;
}
.notification.success {
background-color: var(--success-color);
border-left: 4px solid #1e7e34;
}
.notification.warning {
background-color: var(--warning-color);
color: #212529;
border-left: 4px solid #d39e00;
}
.notification.error {
background-color: var(--danger-color);
border-left: 4px solid #bd2130;
}
.notification.fade-out {
animation: fadeOut 0.3s forwards;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(-20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes fadeOut {
to {
opacity: 0;
transform: translateY(-20px);
}
}
/* Additional responsive styles */
@media (max-width: 768px) {
.container {
padding: 0 1rem;
}
.card {
padding: 1.25rem;
}
.notification {
left: 20px;
right: 20px;
max-width: calc(100% - 40px);
}
.form-actions {
flex-direction: column;
}
.form-actions .btn {
width: 100%;
margin-bottom: 0.5rem;
}
}
.drag-drop-area {
border: 2px dashed var(--border-color);
border-radius: var(--border-radius);
padding: 30px 20px;
text-align: center;
background-color: #f8f9fa;
cursor: pointer;
transition: var(--transition);
margin-bottom: 15px;
position: relative;
}
.drag-drop-area:hover, .drag-drop-area.dragover {
border-color: var(--primary-color);
background-color: rgba(74, 108, 247, 0.05);
}
.drag-drop-message {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: var(--secondary-color);
}
.drag-drop-message i {
font-size: 2.5rem;
margin-bottom: 15px;
color: var(--primary-color);
}
.drag-drop-message p {
margin: 0;
font-size: 1rem;
}
.image-actions {
display: flex;
gap: 10px;
}
.image-preview {
max-width: 100%;
max-height: 200px;
border-radius: 4px;
margin-top: 10px;
display: none;
}
.banner-preview.with-image {
min-height: 220px;
}
.image-position-options {
display: flex;
gap: 8px;
margin-bottom: 10px;
}
.image-position-btn {
padding: 6px 12px;
background-color: #f8f9fa;
border: 1px solid var(--border-color);
border-radius: var(--border-radius);
font-size: 0.9rem;
cursor: pointer;
transition: var(--transition);
}
.image-position-btn:hover {
background-color: #e9ecef;
}
.image-position-btn.active {
background-color: var(--primary-color);
color: white;
border-color: var(--primary-color);
}
.draggable-image {
cursor: move;
position: relative;
z-index: 10;
transition: none;
user-select: none;
}
.draggable-image.dragging {
opacity: 0.8;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
}
</style>
</head>
<body>
<div class="header">
<h1>Admin Dashboard</h1>
<button class="logout-btn" id="logoutBtn">Odhlásit se</button>
</div>
<div class="container">
<h2>Vítejte v administraci</h2>
<div class="card" style="margin: 2rem auto; max-width: 1000px;">
<h3>Správa banneru</h3>
<form id="bannerForm" enctype="multipart/form-data" onsubmit="saveBanner(event)">
<div class="form-group">
<label for="bannerText">Text banneru</label>
<textarea id="bannerText" name="text" placeholder="Zadejte text banneru" required></textarea>
</div>
<div class="form-group">
<label for="bannerLink">Odkaz (volitelný)</label>
<input type="url" id="bannerLink" name="link" placeholder="https://example.com">
</div>
<div class="form-group">
<label>Nahrát obrázek</label>
<input type="file" id="bannerImage" name="image" accept="image/*" style="display: none;" onchange="handleImageUpload(event)">
<input type="hidden" id="removeImage" name="removeImage" value="false">
<div id="dragDropArea" class="drag-drop-area">
<div class="drag-drop-message">
<i class="fas fa-cloud-upload-alt"></i>
<p>Přetáhněte obrázek sem nebo klikněte pro výběr</p>
</div>
<div id="imagePreviewContainer" style="display: none; margin-top: 15px; text-align: center;">
<img id="imagePreview" style="max-width: 100%; max-height: 200px; border-radius: 4px;" />
</div>
</div>
<div class="image-actions" style="margin-top: 10px;">
<button type="button" class="btn btn-secondary" id="uploadImageBtn">
<i class="fas fa-upload"></i> Vybrat obrázek
</button>
<button type="button" id="removeImageBtn" class="btn btn-danger" style="display: none; margin-left: 10px;" onclick="removeImage()">
<i class="fas fa-trash"></i> Odstranit obrázek
</button>
</div>
<p class="help-text" style="margin-top: 10px;">
Doporučený poměr stran: 3:1 (např. 1200x400px)
</p>
</div>
<div class="form-group">
<label>
<input type="checkbox" id="bannerVisible" name="style[isVisible]" value="true" checked> Zobrazit banner
</label>
</div>
<h4>Styl</h4>
<div class="form-group">
<label for="bannerBgColor">Barva pozadí</label>
<input type="text" id="bannerBgColor" name="style[backgroundColor]" value="#f8d7da" class="color-input">
<input type="color" id="bannerBgColorPicker" value="#f8d7da" class="color-picker">
<div id="bgColorPreview" class="color-preview"></div>
</div>
<div class="form-group">
<label for="bannerTextColor">Barva textu</label>
<input type="text" id="bannerTextColor" name="style[textColor]" value="#721c24" class="color-input">
<input type="color" id="bannerTextColorPicker" value="#721c24" class="color-picker">
<div id="textColorPreview" class="color-preview"></div>
</div>
<div class="form-group">
<label for="bannerTextAlign">Zarovnání textu</label>
<select id="bannerTextAlign" name="style[textAlign]">
<option value="left">Vlevo</option>
<option value="center" selected>Na střed</option>
<option value="right">Vpravo</option>
</select>
</div>
<div class="form-group">
<label for="bannerFontSize">Velikost písma (px)</label>
<input type="number" id="bannerFontSize" name="style[fontSize]" value="18" min="10" max="50">
</div>
<div class="form-group">
<label for="bannerPadding">Odsazení (px)</label>
<input type="number" id="bannerPadding" name="style[padding]" value="20" min="0" max="100">
</div>
<div class="form-group">
<label for="bannerMargin">Okraj (px)</label>
<input type="number" id="bannerMargin" name="style[margin]" value="20" min="0" max="100">
</div>
<div class="form-group">
<label for="bannerBorderRadius">Zaoblení rohů (px)</label>
<input type="number" id="bannerBorderRadius" name="style[borderRadius]" value="8" min="0" max="50">
</div>
<h4>Předvolby stylů</h4>
<div class="style-presets">
<div class="style-preset" data-preset="info">Info</div>
<div class="style-preset" data-preset="warning">Upozornění</div>
<div class="style-preset" data-preset="success">Úspěch</div>
<div class="style-preset" data-preset="error">Chyba</div>
</div>
<div class="banner-preview" id="bannerPreview" style="display: none;">
<div class="banner-preview-bg"></div>
<div class="banner-preview-content">Náhled banneru se zde zobrazí</div>
</div>
<div id="imagePositionControls" style="display: none; margin-top: 15px;">
<p style="margin-bottom: 8px;"><strong>Pozice obrázku:</strong></p>
<div class="image-position-options">
<button type="button" class="image-position-btn" data-position="left">Vlevo</button>
<button type="button" class="image-position-btn" data-position="center">Na střed</button>
<button type="button" class="image-position-btn" data-position="right">Vpravo</button>
</div>
<div class="form-group" style="margin-top: 15px;">
<label for="bannerImageWidth">Šířka obrázku (px)</label>
<input type="number" id="bannerImageWidth" name="style[imageWidth]" value="200" min="50" max="1000">
</div>
<div class="form-group">
<label for="bannerImageHeight">Výška obrázku (px)</label>
<input type="number" id="bannerImageHeight" name="style[imageHeight]" value="200" min="50" max="500">
</div>
<!-- Hidden fields for image position data -->
<input type="hidden" id="imagePosition" name="style[imagePosition]" value="center">
</div>
<div id="imagePreviewContainer" style="display: none; margin-top: 15px; text-align: center;">
<img id="imagePreview" style="max-width: 100%; max-height: 200px; border-radius: 4px;" />
</div>
<div class="form-actions">
<button type="submit" id="saveBannerBtn" class="btn btn-primary">
<i class="fas fa-save"></i> Uložit banner
</button>
</div>
</form>
<div class="dashboard-cards">
<div class="card">
<h3>Uživatelé</h3>
<p>Správa uživatelských účtů</p>
</div>
<div class="card">
<h3>Nastavení</h3>
<p>Konfigurace systému</p>
</div>
<div class="card">
<h3>Statistiky</h3>
<p>Přehled aktivit</p>
</div>
</div>
</div>
<div class="card">
<h3>Uživatelé</h3>
<p>Správa uživatelských účtů</p>
</div>
<div class="card">
<h3>Nastavení</h3>
<p>Konfigurace systému</p>
</div>
<div class="card">
<h3>Statistiky</h3>
<p>Přehled aktivit</p>
</div>
</div>
</div>
<script>
// Get token and check authentication
const token = localStorage.getItem('token');
if (!token) {
window.location.href = '/login.html';
}
// Show notification to user
function showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.className = `notification ${type}`;
// Set icon based on notification type
let icon = 'info-circle';
if (type === 'success') icon = 'check-circle';
else if (type === 'error') icon = 'exclamation-circle';
else if (type === 'warning') icon = 'exclamation-triangle';
notification.innerHTML = `
<i class="fas fa-${icon}"></i>
<span>${message}</span>
`;
document.body.appendChild(notification);
// Auto-remove notification after delay
const delay = type === 'error' ? 5000 : 3000;
setTimeout(() => {
notification.classList.add('fade-out');
setTimeout(() => notification.remove(), 300);
}, delay);
}
// Override fetch to include token (but NOT for FormData requests)
const originalFetch = window.fetch;
window.fetch = async function(resource, init = {}) {
// Add token to headers if it's an API request
if (typeof resource === 'string' && resource.startsWith('/api/')) {
const headers = new Headers(init.headers || {});
if (token) {
headers.set('Authorization', `Bearer ${token}`);
}
// Only set content type if not FormData (FormData sets its own)
if (!headers.has('Content-Type') && init.body && !(init.body instanceof FormData)) {
headers.set('Content-Type', 'application/json');
}
init.headers = headers;
}
return originalFetch(resource, init);
};
// Image handling - Drag and Drop functionality
const dragDropArea = document.getElementById('dragDropArea');
const uploadImageBtn = document.getElementById('uploadImageBtn');
const bannerImage = document.getElementById('bannerImage');
// Click on drag area to select file
dragDropArea.addEventListener('click', function() {
bannerImage.click();
});
// Click on upload button to select file
uploadImageBtn.addEventListener('click', function() {
bannerImage.click();
});
// Prevent default behavior for drag events
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dragDropArea.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
// Visual feedback when dragging over the area
['dragenter', 'dragover'].forEach(eventName => {
dragDropArea.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
dragDropArea.addEventListener(eventName, unhighlight, false);
});
function highlight() {
dragDropArea.classList.add('dragover');
}
function unhighlight() {
dragDropArea.classList.remove('dragover');
}
// Handle dropped files
dragDropArea.addEventListener('drop', handleDrop, false);
function handleDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
if (files.length) {
bannerImage.files = files;
const event = new Event('change');
bannerImage.dispatchEvent(event);
}
}
// Handle image upload
function handleImageUpload(event) {
const fileInput = event.target;
const file = fileInput.files[0];
if (!file) return;
// Check file type
const validImageTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'];
if (!validImageTypes.includes(file.type)) {
showNotification('Vyberte prosím soubor obrázku (JPG, PNG, GIF, SVG)', 'warning');
fileInput.value = ''; // Reset file input
return;
}
// Check file size (max 5MB)
const maxSize = 5 * 1024 * 1024; // 5MB
if (file.size > maxSize) {
showNotification('Maximální velikost souboru je 5MB', 'warning');
fileInput.value = ''; // Reset file input
return;
}
// Show preview
const reader = new FileReader();
reader.onload = function(e) {
const preview = document.getElementById('imagePreview');
const previewContainer = document.getElementById('imagePreviewContainer');
const removeBtn = document.getElementById('removeImageBtn');
const dragDropMessage = document.querySelector('.drag-drop-message');
// Update preview elements if they exist
if (preview) {
preview.src = e.target.result;
preview.alt = 'Náhled obrázku';
}
if (previewContainer) {
previewContainer.style.display = 'block';
}
if (removeBtn) {
removeBtn.style.display = 'inline-block';
}
// Hide the drag & drop message when showing preview
if (dragDropMessage) {
dragDropMessage.style.display = 'none';
}
// Update hidden input
const removeImageInput = document.getElementById('removeImage');
if (removeImageInput) {
removeImageInput.value = 'false';
}
// Update banner preview with the new image
currentImage = e.target.result;
updateBannerPreview();
};
reader.onerror = function() {
showNotification('Při načítání obrázku došlo k chybě. Zkuste to prosím znovu.', 'error');
fileInput.value = ''; // Reset file input
};
reader.readAsDataURL(file);
}
// Logout functionality
document.getElementById('logoutBtn').addEventListener('click', function() {
localStorage.removeItem('token');
window.location.href = '/';
});
// DOM Elements
const bannerText = document.getElementById('bannerText');
const bannerVisible = document.getElementById('bannerVisible');
const bannerBgColor = document.getElementById('bannerBgColor');
const bannerTextColor = document.getElementById('bannerTextColor');
const bannerTextAlign = document.getElementById('bannerTextAlign');
const bannerFontSize = document.getElementById('bannerFontSize');
const bannerPadding = document.getElementById('bannerPadding');
const bannerMargin = document.getElementById('bannerMargin');
const bannerBorderRadius = document.getElementById('bannerBorderRadius');
const bannerPreview = document.getElementById('bannerPreview');
const bannerPreviewContent = bannerPreview.querySelector('.banner-preview-content');
const bannerPreviewText = bannerPreview.querySelector('.banner-preview-text');
const bannerPreviewBg = bannerPreview.querySelector('.banner-preview-bg');
const bgColorPreview = document.getElementById('bgColorPreview');
const textColorPreview = document.getElementById('textColorPreview');
const saveBannerBtn = document.getElementById('saveBannerBtn');
const stylePresets = document.querySelectorAll('.style-preset');
let currentImage = null;
// Preset styles
const presets = {
info: {
backgroundColor: '#cce5ff',
textColor: '#004085',
textAlign: 'left'
},
warning: {
backgroundColor: '#fff3cd',
textColor: '#856404',
textAlign: 'center'
},
success: {
backgroundColor: '#d4edda',
textColor: '#155724',
textAlign: 'center'
},
error: {
backgroundColor: '#f8d7da',
textColor: '#721c24',
textAlign: 'center'
}
};
// Variables for image positioning
let currentImagePosition = 'center'; // default position
let currentImageX = '0';
let currentImageY = '0';
// Load current banner
async function loadBanner() {
try {
const response = await fetch('/api/banner');
if (!response.ok) {
throw new Error('Nepodařilo se načíst banner');
}
const data = await response.json();
console.log('Loaded banner data:', data);
if (data) {
// Update form fields
document.getElementById('bannerText').value = data.text || '';
document.getElementById('bannerBgColor').value = data.style?.backgroundColor || '#f8d7da';
document.getElementById('bannerTextColor').value = data.style?.color || '#721c24';
document.getElementById('bannerTextAlign').value = data.style?.textAlign || 'center';
document.getElementById('bannerFontSize').value = data.style?.fontSize || '18';
document.getElementById('bannerPadding').value = data.style?.padding || '20';
document.getElementById('bannerMargin').value = data.style?.margin || '20';
document.getElementById('bannerBorderRadius').value = data.style?.borderRadius || '8';
// Initialize image position variables once
currentImagePosition = data.style?.imagePosition || 'center';
currentImageX = data.style?.imageX || '0';
currentImageY = data.style?.imageY || '0';
// Update image position controls if they exist
const imagePosition = document.getElementById('imagePosition');
if (imagePosition) {
// Set the value only if the element exists
imagePosition.value = currentImagePosition;
// Update active button
const positionButtons = document.querySelectorAll('.image-position-btn');
positionButtons.forEach(btn => {
btn.classList.toggle('active', btn.dataset.position === currentImagePosition);
});
}
// Update active button
const positionButtons = document.querySelectorAll('.image-position-btn');
positionButtons.forEach(btn => {
btn.classList.toggle('active', btn.dataset.position === currentImagePosition);
});
// Handle image
const imagePreview = document.getElementById('imagePreview');
const imagePreviewContainer = document.getElementById('imagePreviewContainer');
const removeBtn = document.getElementById('removeImageBtn');
const removeImageInput = document.getElementById('removeImage');
if (data.image) {
currentImage = data.image;
if (imagePreview) {
imagePreview.src = data.image;
imagePreview.alt = 'Nahraný obrázek banneru';
}
if (imagePreviewContainer) {
imagePreviewContainer.style.display = 'block';
}
if (removeBtn) {
removeBtn.style.display = 'inline-block';
}
if (removeImageInput) {
removeImageInput.value = 'false';
}
} else {
// No image in the saved banner
currentImage = null;
if (imagePreview) {
imagePreview.removeAttribute('src');
}
if (imagePreviewContainer) {
imagePreviewContainer.style.display = 'none';
}
if (removeBtn) {
removeBtn.style.display = 'none';
}
if (removeImageInput) {
removeImageInput.value = 'true';
}
}
// Update previews
updateColorPreviews();
updateBannerPreview();
}
} catch (error) {
console.error('Chyba při načítání banneru:', error);
showNotification('Chyba při načítání banneru', 'error');
}
}
// Add submission flag at the top of the script
let isSubmitting = false;
async function saveBanner(event) {
event.preventDefault();
// Prevent multiple submissions
if (isSubmitting) {
console.log('Form submission already in progress');
return;
}
isSubmitting = true;
const form = document.getElementById('bannerForm');
const formData = new FormData(form);
const submitButton = form.querySelector('button[type="submit"]');
const originalButtonText = submitButton ? submitButton.innerHTML : '';
try {
// Show loading state
if (submitButton) {
submitButton.disabled = true;
submitButton.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Ukládám...';
}
// Add text and link to form data
formData.append('text', document.getElementById('bannerText').value);
formData.append('link', document.getElementById('bannerLink').value);
// Append style properties individually
formData.append('style[backgroundColor]', document.getElementById('bannerBgColor').value);
formData.append('style[textColor]', document.getElementById('bannerTextColor').value);
formData.append('style[textAlign]', document.getElementById('bannerTextAlign').value);
formData.append('style[fontSize]', document.getElementById('bannerFontSize').value || '16px');
formData.append('style[padding]', document.getElementById('bannerPadding').value || '0px');
formData.append('style[margin]', document.getElementById('bannerMargin').value || '0px');
formData.append('style[borderRadius]', document.getElementById('bannerBorderRadius').value || '0px');
formData.append('style[isVisible]', document.getElementById('bannerVisible').checked);
// Append image position data using our variables
formData.append('style[imagePosition]', currentImagePosition);
formData.append('style[imageX]', currentImageX);
formData.append('style[imageY]', currentImageY);
console.log('Sending image position:',
currentImagePosition,
currentImageX,
currentImageY);
// Log form data for debugging
console.log('Odesílám data:');
for (let [key, value] of formData.entries()) {
console.log(key, value);
}
// Create headers object - DO NOT set Content-Type for FormData!
const headers = {};
// Add Authorization header if token exists
const token = localStorage.getItem('token');
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
// Send request with FormData (browser will set correct Content-Type with boundary)
const response = await fetch('/api/banner/update', {
method: 'POST',
headers: headers, // No Content-Type header!
body: formData
});
if (!response.ok) {
const errorText = await response.text().catch(() => 'Neznámá chyba serveru');
console.error('Server error:', errorText);
let errorMessage = 'Chyba při ukládání banneru';
try {
const errorData = JSON.parse(errorText);
errorMessage = errorData.message || errorMessage;
} catch (e) {
errorMessage = errorText || errorMessage;
}
throw new Error(errorMessage);
}
const result = await response.json().catch(() => ({}));
// Show success message using the showNotification function
showNotification('Banner byl úspěšně uložen', 'success');
// Update the preview with the new banner data
if (result.image) {
currentImage = result.image;
const imagePreview = document.getElementById('imagePreview');
const imagePreviewContainer = document.getElementById('imagePreviewContainer');
const removeBtn = document.getElementById('removeImageBtn');
if (imagePreview) imagePreview.src = currentImage;
if (imagePreviewContainer) imagePreviewContainer.style.display = 'block';
if (removeBtn) removeBtn.style.display = 'inline-block';
// Update the hidden input if the image was changed
const removeImageInput = document.getElementById('removeImage');
if (removeImageInput) removeImageInput.value = 'false';
}
updateBannerPreview();
} catch (error) {
console.error('Chyba při ukládání banneru:', error);
showNotification(error.message || 'Nepodařilo se uložit banner', 'error');
} finally {
// Reset button state
if (submitButton) {
submitButton.disabled = false;
submitButton.innerHTML = originalButtonText;
}
isSubmitting = false;
}
}
// Update color previews
function updateColorPreviews() {
const bgColorPreview = document.getElementById('bgColorPreview');
const textColorPreview = document.getElementById('textColorPreview');
const bgColorPicker = document.getElementById('bannerBgColorPicker');
const textColorPicker = document.getElementById('bannerTextColorPicker');
const bgColorInput = document.getElementById('bannerBgColor');
const textColorInput = document.getElementById('bannerTextColor');
if (bgColorPreview && bgColorInput) {
bgColorPreview.style.backgroundColor = bgColorInput.value;
}
if (textColorPreview && textColorInput) {
textColorPreview.style.backgroundColor = textColorInput.value;
}
if (bgColorPicker && bgColorInput) {
bgColorPicker.value = bgColorInput.value;
}
if (textColorPicker && textColorInput) {
textColorPicker.value = textColorInput.value;
}
}
// Remove image
function removeImage() {
const bannerImage = document.getElementById('bannerImage');
const imagePreview = document.getElementById('imagePreview');
const imagePreviewContainer = document.getElementById('imagePreviewContainer');
const removeBtn = document.getElementById('removeImageBtn');
const removeImageInput = document.getElementById('removeImage');
const dragDropMessage = document.querySelector('.drag-drop-message');
// Reset file input
if (bannerImage) bannerImage.value = '';
// Reset preview image
if (imagePreview) {
imagePreview.src = '';
imagePreview.removeAttribute('src');
}
// Hide preview container
if (imagePreviewContainer) {
imagePreviewContainer.style.display = 'none';
}
// Show drag & drop message again
if (dragDropMessage) {
dragDropMessage.style.display = 'flex';
}
// Hide remove button
if (removeBtn) {
removeBtn.style.display = 'none';
}
// Update hidden input to indicate image removal
if (removeImageInput) {
removeImageInput.value = 'true';
}
// Clear the current image and update preview
currentImage = null;
updateBannerPreview();
// Show success notification
showNotification('Obrázek byl odstraněn', 'info');
// Trigger a change event on the file input in case any other code is listening
if (bannerImage) {
const event = new Event('change');
bannerImage.dispatchEvent(event);
}
}
// Update banner preview
function updateBannerPreview() {
const bannerPreview = document.getElementById('bannerPreview');
const bannerPreviewContent = bannerPreview?.querySelector('.banner-preview-content');
const imagePreview = document.getElementById('imagePreview');
const imagePreviewContainer = document.getElementById('imagePreviewContainer');
const bannerLink = document.getElementById('bannerLink')?.value || '';
const imagePositionControls = document.getElementById('imagePositionControls');
if (!bannerPreview || !bannerPreviewContent) {
return; // Elements not found
}
// Get current values
const bannerText = document.getElementById('bannerText')?.value || '';
const bannerBgColor = document.getElementById('bannerBgColor')?.value || '#f8d7da';
const bannerTextColor = document.getElementById('bannerTextColor')?.value || '#721c24';
const bannerTextAlign = document.getElementById('bannerTextAlign')?.value || 'center';
const bannerFontSize = parseInt(document.getElementById('bannerFontSize')?.value || '18');
const bannerPadding = parseInt(document.getElementById('bannerPadding')?.value || '20');
const bannerMargin = parseInt(document.getElementById('bannerMargin')?.value || '20');
const bannerBorderRadius = parseInt(document.getElementById('bannerBorderRadius')?.value || '8');
// Get image position (default to center if not set)
const imagePosition = currentImagePosition || 'center';
const imageX = currentImageX || '0';
const imageY = currentImageY || '0';
// Get image dimensions
const imageWidth = parseInt(document.getElementById('bannerImageWidth')?.value || '200');
const imageHeight = parseInt(document.getElementById('bannerImageHeight')?.value || '200');
// Update banner container styles to exactly match index.html
bannerPreview.style.display = 'block';
bannerPreview.style.width = '100%';
bannerPreview.style.transition = 'all 0.3s ease';
bannerPreview.style.margin = '0 auto';
bannerPreview.style.maxWidth = '1200px';
bannerPreview.style.padding = '0 1rem';
bannerPreview.style.position = 'relative'; // Ensure proper positioning context
// Update banner content styles to exactly match index.html
Object.assign(bannerPreviewContent.style, {
backgroundColor: bannerBgColor,
color: bannerTextColor,
textAlign: bannerTextAlign,
fontSize: `${bannerFontSize}px`,
padding: `${bannerPadding}px`,
margin: `${bannerMargin}px 0`,
borderRadius: `${bannerBorderRadius}px`,
boxShadow: '0 2px 4px rgba(0,0,0,0.1)',
display: 'block'
});
// Handle content and image like in index.html
let content = bannerText || 'Náhled banneru';
const bannerImage = document.getElementById('bannerImage');
const hasImage = currentImage || (bannerImage && bannerImage.files.length > 0);
if (hasImage && currentImage) {
// Show image position controls
if (imagePositionControls) {
imagePositionControls.style.display = 'block';
// Update active button
const positionButtons = imagePositionControls.querySelectorAll('.image-position-btn');
positionButtons.forEach(btn => {
btn.classList.toggle('active', btn.dataset.position === imagePosition);
});
}
// Apply the same border radius to the image as to the container
const imageRadius = Math.max(bannerBorderRadius, 0);
// Determine image style based on position - exactly match index.html
let imageStyle = `max-width: ${imageWidth}px; max-height: ${imageHeight}px; border-radius: ${imageRadius}px;`;
let containerStyle = 'margin-bottom: 15px;';
switch(imagePosition) {
case 'left':
containerStyle += 'text-align: left; float: left; margin-right: 15px;';
break;
case 'right':
containerStyle += 'text-align: right; float: right; margin-left: 15px;';
break;
case 'center':
containerStyle += 'text-align: center; display: block; margin: 0 auto;';
break;
case 'custom':
containerStyle += 'position: relative;';
// For custom position, use absolute positioning to ensure exact placement
imageStyle += `position: absolute; left: ${imageX}px; top: ${imageY}px;`;
break;
}
// Format content with image exactly like in index.html
content = `
<div style="${containerStyle}" class="banner-image-container">
<img src="${currentImage}"
style="${imageStyle}"
class="${imagePosition === 'custom' ? 'draggable-image' : ''}"
data-position="${imagePosition}"
data-x="${imageX}"
data-y="${imageY}">
</div>
${content}
`;
// Show the image preview in the container
if (imagePreview && imagePreviewContainer) {
imagePreview.src = currentImage;
imagePreviewContainer.style.display = 'block';
}
// Add the with-image class to the banner preview for proper spacing
bannerPreview.classList.add('with-image');
} else {
// Hide the image preview container if no image
if (imagePreviewContainer) {
imagePreviewContainer.style.display = 'none';
}
// Hide image position controls
if (imagePositionControls) {
imagePositionControls.style.display = 'none';
}
// Remove the with-image class from the banner preview
bannerPreview.classList.remove('with-image');
}
// Update the preview content
bannerPreviewContent.innerHTML = content;
// Add event listeners for width/height changes
const imageWidthInput = document.getElementById('bannerImageWidth');
const imageHeightInput = document.getElementById('bannerImageHeight');
if (imageWidthInput) {
imageWidthInput.addEventListener('input', updateBannerPreview);
}
if (imageHeightInput) {
imageHeightInput.addEventListener('input', updateBannerPreview);
}
// Wrap in link if provided - exactly match index.html but prevent clicks
if (bannerLink) {
content = `<a href="${bannerLink}"
style="color: inherit; text-decoration: none; display: block; pointer-events: none; cursor: default;"
onclick="event.preventDefault(); return false;">${content}</a>`;
}
// Update content
bannerPreviewContent.innerHTML = content;
// Make sure the preview is visible
bannerPreview.style.visibility = 'visible';
// Setup drag functionality for the image if in custom position mode
if (hasImage && imagePosition === 'custom') {
setupDraggableImage();
}
}
// Apply preset
function applyPreset(preset) {
const style = presets[preset];
if (!style) return;
bannerBgColor.value = style.backgroundColor;
bannerTextColor.value = style.textColor;
bannerTextAlign.value = style.textAlign;
// Update color pickers to match input fields
bannerBgColorPicker.value = style.backgroundColor;
bannerTextColorPicker.value = style.textColor;
updateColorPreviews();
updateBannerPreview();
}
// Event Listeners
// Debounced update for text inputs
const debouncedUpdatePreview = debounce(() => {
updateColorPreviews();
updateBannerPreview();
}, 300);
bannerBgColor.addEventListener('input', () => {
// Update color preview immediately
const bgColorPreview = document.getElementById('bgColorPreview');
if (bgColorPreview) {
bgColorPreview.style.backgroundColor = bannerBgColor.value;
}
// Debounce the full preview update
debouncedUpdatePreview();
});
bannerTextColor.addEventListener('input', () => {
// Update color preview immediately
const textColorPreview = document.getElementById('textColorPreview');
if (textColorPreview) {
textColorPreview.style.backgroundColor = bannerTextColor.value;
}
// Debounce the full preview update
debouncedUpdatePreview();
});
// Connect color pickers to input fields
const bannerBgColorPicker = document.getElementById('bannerBgColorPicker');
const bannerTextColorPicker = document.getElementById('bannerTextColorPicker');
// Debounce function to improve performance
function debounce(func, wait) {
let timeout;
return function(...args) {
const context = this;
clearTimeout(timeout);
timeout = setTimeout(() => func.apply(context, args), wait);
};
}
// Update only the color preview during dragging (lightweight)
bannerBgColorPicker.addEventListener('input', () => {
bannerBgColor.value = bannerBgColorPicker.value;
// Only update the color preview (lightweight operation)
const bgColorPreview = document.getElementById('bgColorPreview');
if (bgColorPreview) {
bgColorPreview.style.backgroundColor = bannerBgColorPicker.value;
}
});
// Update the full preview only when the user has finished selecting a color
bannerBgColorPicker.addEventListener('change', () => {
updateColorPreviews();
updateBannerPreview();
});
// Same for text color picker
bannerTextColorPicker.addEventListener('input', () => {
bannerTextColor.value = bannerTextColorPicker.value;
// Only update the color preview (lightweight operation)
const textColorPreview = document.getElementById('textColorPreview');
if (textColorPreview) {
textColorPreview.style.backgroundColor = bannerTextColorPicker.value;
}
});
// Update the full preview only when the user has finished selecting a color
bannerTextColorPicker.addEventListener('change', () => {
updateColorPreviews();
updateBannerPreview();
});
// For other form controls, use debounced updates on input
[bannerText, bannerTextAlign, bannerFontSize, bannerPadding, bannerMargin, bannerBorderRadius, bannerVisible].forEach(el => {
el.addEventListener('change', updateBannerPreview);
el.addEventListener('input', debouncedUpdatePreview);
});
stylePresets.forEach(preset => {
preset.addEventListener('click', () => applyPreset(preset.dataset.preset));
});
saveBannerBtn.addEventListener('click', saveBanner);
// Setup draggable image functionality
function setupDraggableImage() {
const draggableImage = document.querySelector('.draggable-image');
if (!draggableImage) return;
// Remove any existing event listeners to prevent duplicates
const newDraggable = draggableImage.cloneNode(true);
draggableImage.parentNode.replaceChild(newDraggable, draggableImage);
let isDragging = false;
let startX, startY;
let originalX = parseInt(currentImageX) || 0;
let originalY = parseInt(currentImageY) || 0;
// Mouse events for desktop
newDraggable.addEventListener('mousedown', startDrag);
document.addEventListener('mousemove', drag);
document.addEventListener('mouseup', endDrag);
// Touch events for mobile
newDraggable.addEventListener('touchstart', startDragTouch);
document.addEventListener('touchmove', dragTouch);
document.addEventListener('touchend', endDrag);
function startDrag(e) {
e.preventDefault();
isDragging = true;
startX = e.clientX;
startY = e.clientY;
newDraggable.classList.add('dragging');
console.log('Started dragging at', startX, startY);
}
function startDragTouch(e) {
if (e.touches.length === 1) {
isDragging = true;
startX = e.touches[0].clientX;
startY = e.touches[0].clientY;
newDraggable.classList.add('dragging');
}
}
function drag(e) {
if (!isDragging) return;
const deltaX = e.clientX - startX;
const deltaY = e.clientY - startY;
const newX = originalX + deltaX;
const newY = originalY + deltaY;
newDraggable.style.left = `${newX}px`;
newDraggable.style.top = `${newY}px`;
// Update current position values
currentImageX = newX.toString();
currentImageY = newY.toString();
console.log('Dragging to', newX, newY);
}
function dragTouch(e) {
if (!isDragging || e.touches.length !== 1) return;
const deltaX = e.touches[0].clientX - startX;
const deltaY = e.touches[0].clientY - startY;
const newX = originalX + deltaX;
const newY = originalY + deltaY;
newDraggable.style.left = `${newX}px`;
newDraggable.style.top = `${newY}px`;
// Update current position values
currentImageX = newX.toString();
currentImageY = newY.toString();
}
function endDrag() {
if (!isDragging) return;
isDragging = false;
originalX = parseInt(currentImageX);
originalY = parseInt(currentImageY);
newDraggable.classList.remove('dragging');
console.log('Finished dragging at', originalX, originalY);
// Force a save of the current position
document.getElementById('imagePositionX').value = currentImageX;
document.getElementById('imagePositionY').value = currentImageY;
}
}
// Setup image position buttons
document.addEventListener('DOMContentLoaded', () => {
// Add event listeners to image position buttons
const positionButtons = document.querySelectorAll('.image-position-btn');
positionButtons.forEach(btn => {
btn.addEventListener('click', () => {
const position = btn.dataset.position;
currentImagePosition = position;
// Reset position values if not custom
if (position !== 'custom') {
currentImageX = '0';
currentImageY = '0';
}
// Update active button styling
positionButtons.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
// Update preview
updateBannerPreview();
});
});
loadBanner();
});
</script>
</body>
</html>