// CORE64 Records - Admin Panel Logic
let currentSection = 'dashboard';
let editingId = null;
let editingType = null;
// Initialize
document.addEventListener('DOMContentLoaded', () => {
checkAuth();
loadDashboard();
});
// Check authentication
function checkAuth() {
const isAuth = sessionStorage.getItem('core64_admin_auth');
const loginScreen = document.getElementById('login-screen');
if (isAuth === 'true') {
if (loginScreen) loginScreen.classList.add('hidden');
} else {
if (loginScreen) loginScreen.classList.remove('hidden');
}
}
// Handle login
function handleLogin(e) {
e.preventDefault();
const password = document.getElementById('admin-password').value;
const data = getData();
if (password === data.settings.password) {
sessionStorage.setItem('core64_admin_auth', 'true');
document.getElementById('login-screen').classList.add('hidden');
document.getElementById('login-error').classList.add('hidden');
loadDashboard();
} else {
document.getElementById('login-error').classList.remove('hidden');
}
}
// Logout
function logout() {
sessionStorage.removeItem('core64_admin_auth');
location.reload();
}
// Get data
function getData() {
return JSON.parse(localStorage.getItem('core64_data')) || {};
}
// Save data
function saveData(data) {
localStorage.setItem('core64_data', JSON.stringify(data));
// Sync with main site
window.dispatchEvent(new Event('storage'));
}
// Show section
function showSection(section) {
// Hide all sections
document.querySelectorAll('.section-content').forEach(el => {
el.classList.add('hidden');
});
// Show selected
document.getElementById(`section-${section}`).classList.remove('hidden');
// Update nav
document.querySelectorAll('.nav-item').forEach(el => {
el.classList.remove('active');
});
event.target.closest('.nav-item').classList.add('active');
currentSection = section;
// Load data for section
if (section === 'dashboard') loadDashboard();
if (section === 'releases') loadReleases();
if (section === 'artists') loadArtists();
if (section === 'events') loadEvents();
if (section === 'settings') loadSettings();
}
// Load dashboard
function loadDashboard() {
const data = getData();
document.getElementById('dash-releases').textContent = data.releases?.length || 0;
document.getElementById('dash-artists').textContent = data.artists?.length || 0;
document.getElementById('dash-events').textContent = data.events?.length || 0;
}
// Load releases
function loadReleases() {
const data = getData();
const container = document.getElementById('releases-list');
container.innerHTML = data.releases.map(release => `
${release.title}
${release.artist}
${release.genre} • ${release.year}
`).join('');
lucide.createIcons();
}
// Load artists
function loadArtists() {
const data = getData();
const container = document.getElementById('artists-list');
container.innerHTML = data.artists.map(artist => `
${artist.name}
${artist.genre}
${artist.bio}
`).join('');
lucide.createIcons();
}
// Load events
function loadEvents() {
const data = getData();
const container = document.getElementById('events-list-admin');
const sortedEvents = [...(data.events || [])].sort((a, b) => new Date(a.date) - new Date(b.date));
container.innerHTML = sortedEvents.map(event => `
${event.title}
${event.date} • ${event.time}
${event.venue}
`).join('');
lucide.createIcons();
}
// Load settings
function loadSettings() {
const data = getData();
document.getElementById('setting-title').value = data.settings.title || '';
document.getElementById('setting-about').value = data.settings.about || '';
document.getElementById('setting-mission').value = data.settings.mission || '';
document.getElementById('setting-email').value = data.settings.email || '';
}
// Open modal
function openModal(type, id = null) {
const modal = document.getElementById('modal');
const title = document.getElementById('modal-title');
const fields = document.getElementById('modal-fields');
editingId = id;
editingType = type;
const data = getData();
let item = {};
if (id) {
item = data[type + 's'].find(x => x.id === id) || {};
title.textContent = 'Редагувати ' + getTypeName(type);
} else {
title.textContent = 'Додати ' + getTypeName(type);
}
// Generate fields based on type
fields.innerHTML = generateFields(type, item);
modal.classList.remove('hidden');
lucide.createIcons();
}
// Close modal
function closeModal() {
document.getElementById('modal').classList.add('hidden');
editingId = null;
editingType = null;
}
// Handle file upload and convert to base64
function handleFileUpload(input, type) {
const file = input.files[0];
if (!file) return;
// Check file size (max 2MB)
if (file.size > 2 * 1024 * 1024) {
alert('Файл занадто великий. Максимальний розмір: 2MB');
input.value = '';
return;
}
const reader = new FileReader();
reader.onload = function(e) {
const base64 = e.target.result;
// Find the container div (parent of the label which contains the file input)
const container = input.closest('div');
const imageInput = container.querySelector('input[name="image"]');
const preview = container.parentElement.querySelector('.image-preview');
if (imageInput) {
imageInput.value = base64;
console.log('Image input updated, length:', base64.length);
}
if (preview) {
preview.src = base64;
preview.classList.remove('hidden');
console.log('Preview updated');
}
};
reader.onerror = function(e) {
alert('Помилка читання файлу: ' + e.target.error);
};
reader.readAsDataURL(file);
}
// Generate form fields
function generateFields(type, item) {
const imagePreview = item.image ? `
` : '
';
const fields = {
release: `
`,
artist: `
`,
event: `
`
};
return fields[type] || '';
}
function getTypeName(type) {
const names = { release: 'реліз', artist: 'артиста', event: 'подію' };
return names[type] || type;
}
// Handle form submit
document.getElementById('modal-form').addEventListener('submit', (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const data = getData();
const type = editingType;
const collection = type + 's';
const item = {
id: editingId || Date.now()
};
// Collect all fields
formData.forEach((value, key) => {
item[key] = value;
});
// Update or add
if (editingId) {
const index = data[collection].findIndex(x => x.id === editingId);
if (index !== -1) {
data[collection][index] = item;
}
} else {
if (!data[collection]) data[collection] = [];
data[collection].push(item);
}
saveData(data);
closeModal();
// Reload current section
if (type === 'release') loadReleases();
if (type === 'artist') loadArtists();
if (type === 'event') loadEvents();
loadDashboard();
// Add to activity log
addActivity(`${editingId ? 'Оновлено' : 'Додано'} ${getTypeName(type)}: ${item.title || item.name}`);
});
// Edit item
function editItem(type, id) {
openModal(type, id);
}
// Delete item
function deleteItem(type, id) {
if (!confirm('Ви впевнені, що хочете видалити цей запис?')) return;
const data = getData();
const collection = type + 's';
data[collection] = data[collection].filter(x => x.id !== id);
saveData(data);
if (type === 'release') loadReleases();
if (type === 'artist') loadArtists();
if (type === 'event') loadEvents();
loadDashboard();
addActivity(`Видалено ${getTypeName(type)} #${id}`);
}
// Save settings
function saveSettings() {
const data = getData();
data.settings.title = document.getElementById('setting-title').value;
data.settings.about = document.getElementById('setting-about').value;
data.settings.mission = document.getElementById('setting-mission').value;
data.settings.email = document.getElementById('setting-email').value;
saveData(data);
addActivity('Оновлено налаштування сайту');
alert('Налаштування збережено!');
}
// Reset data
function resetData() {
if (!confirm('УВАГА! Це видалить ВСІ дані та поверне демо-контент. Продовжити?')) return;
localStorage.removeItem('core64_data');
location.reload();
}
// Add activity log
function addActivity(text) {
const log = document.getElementById('activity-log');
const time = new Date().toLocaleTimeString('uk-UA');
const entry = document.createElement('div');
entry.className = 'flex gap-2 text-sm';
entry.innerHTML = `[${time}] ${text}`;
log.insertBefore(entry, log.firstChild);
}
// Close modal on backdrop click
document.getElementById('modal').addEventListener('click', (e) => {
if (e.target.id === 'modal') closeModal();
});