/* ============================================ ClickTracker — логика страницы статистики ============================================ */ (function () { 'use strict'; // ===== DEMO-ДАННЫЕ ===== const demoStats = [ { date: '17.11.2026', link: 'https://short.link/abc123', clicks: 5 }, { date: '18.11.2026', link: 'https://short.link/abc123', clicks: 12 }, { date: '19.11.2026', link: 'https://short.link/xyz789', clicks: 3 }, { date: '20.11.2026', link: 'https://short.link/abc123', clicks: 8 } ]; // ===== DOM ===== const tabBtns = document.querySelectorAll('.tab-btn'); const mainTab = document.getElementById('mainTab'); const chartTab = document.getElementById('chartTab'); const statsBody = document.getElementById('statsBody'); const refreshBtn = document.getElementById('refreshBtn'); const exportBtn = document.getElementById('exportBtn'); const linkSelect = document.getElementById('linkSelect'); const dateFrom = document.getElementById('dateFrom'); const dateTo = document.getElementById('dateTo'); const authLink = document.getElementById('authLink'); // ============================================ // ПЛАВАЮЩИЕ ЛЕЙБЛЫ — синхронизация класса .filled // (без этого лейбл в модалке авторизации не останется // поднятым после заполнения поля и потери фокуса) // ============================================ function updateFilledState(input) { const wrapper = input.closest('.floating-input'); if (!wrapper) return; if (input.value && input.value.trim() !== '') { wrapper.classList.add('filled'); } else { wrapper.classList.remove('filled'); } } function initFloatingLabels(root) { root = root || document; const inputs = root.querySelectorAll('.floating-input input'); inputs.forEach(function (input) { updateFilledState(input); input.addEventListener('input', function () { updateFilledState(this); }); input.addEventListener('change', function () { updateFilledState(this); }); }); } // ============================================ // МОДАЛЬНЫЕ ОКНА // ============================================ const Modal = { show: function (text, type) { type = type || 'info'; const overlay = document.getElementById('messageModal'); const iconEl = document.getElementById('msgIcon'); const textEl = document.getElementById('msgText'); const iconMap = { success: '✓', error: '✕', warning: '⚠', info: 'ℹ' }; iconEl.textContent = iconMap[type] || iconMap.info; iconEl.className = 'modal-message-icon ' + type; textEl.textContent = text; overlay.classList.add('active'); const okBtn = document.getElementById('msgOkBtn'); const closeBtn = overlay.querySelector('[data-close]'); function close() { overlay.classList.remove('active'); okBtn.removeEventListener('click', close); closeBtn.removeEventListener('click', close); overlay.removeEventListener('click', outsideClick); } function outsideClick(e) { if (e.target === overlay) close(); } okBtn.addEventListener('click', close); closeBtn.addEventListener('click', close); overlay.addEventListener('click', outsideClick); }, openAuth: function () { document.getElementById('authModal').classList.add('active'); }, closeAuth: function () { document.getElementById('authModal').classList.remove('active'); } }; // ===== АВТОРИЗАЦИЯ ===== if (authLink) { authLink.addEventListener('click', function (e) { e.preventDefault(); Modal.openAuth(); }); } const authModal = document.getElementById('authModal'); if (authModal) { authModal.querySelectorAll('[data-close]').forEach(function (btn) { btn.addEventListener('click', function () { Modal.closeAuth(); }); }); authModal.addEventListener('click', function (e) { if (e.target === authModal) Modal.closeAuth(); }); } const authForm = document.getElementById('authForm'); if (authForm) { authForm.addEventListener('submit', function (e) { e.preventDefault(); const email = document.getElementById('authEmail').value.trim(); const password = document.getElementById('authPassword').value; if (!email || !password) { Modal.show('Заполните все поля', 'warning'); return; } Modal.show('Вход выполнен успешно', 'success'); Modal.closeAuth(); authForm.reset(); // После reset() сбрасываем классы .filled у лейблов setTimeout(function () { initFloatingLabels(authForm); }, 0); }); } // ===== ВКЛАДКИ ===== tabBtns.forEach(function (btn) { btn.addEventListener('click', function () { tabBtns.forEach(function (b) { b.classList.remove('active'); }); btn.classList.add('active'); const tab = btn.dataset.tab; if (tab === 'main') { mainTab.style.display = 'flex'; document.querySelector('.export-wrapper').style.display = 'flex'; document.querySelector('.stats-table-wrapper').style.display = 'block'; chartTab.style.display = 'none'; } else { mainTab.style.display = 'none'; document.querySelector('.export-wrapper').style.display = 'none'; document.querySelector('.stats-table-wrapper').style.display = 'none'; chartTab.style.display = 'block'; } }); }); // ===== ТАБЛИЦА ===== function renderStats(data) { statsBody.innerHTML = ''; if (data.length === 0) { statsBody.innerHTML = '