// Инициализация даты при загрузке document.addEventListener('DOMContentLoaded', function() { const today = new Date().toISOString().split('T')[0]; document.getElementById('issueDate').value = today; updateCertificate(); }); // Обработка формы document.getElementById('debtForm').addEventListener('submit', function(e) { e.preventDefault(); updateCertificate(); // Плавный скролл к предпросмотру на мобильных if (window.innerWidth <= 1024) { document.querySelector('.preview-section').scrollIntoView({ behavior: 'smooth' }); } }); // Обновление предпросмотра в реальном времени const formInputs = document.querySelectorAll('#debtForm input, #debtForm select, #debtForm textarea'); formInputs.forEach(input => { input.addEventListener('input', updateCertificate); }); function updateCertificate() { // Получаем данные из формы const data = { fio: document.getElementById('fio').value || '___________________________', position: document.getElementById('position').value || '___________________________', department: document.getElementById('department').value, employeeId: document.getElementById('employeeId').value, debtAmount: document.getElementById('debtAmount').value, currency: document.getElementById('currency').value, debtPeriod: document.getElementById('debtPeriod').value || '___________________________', reason: document.getElementById('reason').value || '___________________________', document: document.getElementById('document').value, issueDate: document.getElementById('issueDate').value, issueNumber: document.getElementById('issueNumber').value, director: document.getElementById('director').value }; // Форматируем дату const formattedDate = formatDate(data.issueDate); const signDate = formattedDate.replace('г.', ''); // Заполняем шаблон document.getElementById('certFio').textContent = data.fio; document.getElementById('certCompanyNameInline').textContent = 'ООО «Ваша Компания»'; document.getElementById('certPosition').textContent = data.position; // Показываем/скрываем опциональные поля const deptBlock = document.getElementById('certDepartmentBlock'); if (data.department) { deptBlock.style.display = 'block'; document.getElementById('certDepartment').textContent = data.department; } else { deptBlock.style.display = 'none'; } const empIdBlock = document.getElementById('certEmployeeIdBlock'); if (data.employeeId) { empIdBlock.style.display = 'block'; document.getElementById('certEmployeeId').textContent = data.employeeId; } else { empIdBlock.style.display = 'none'; } // Сумма задолженности const amount = parseFloat(data.debtAmount) || 0; const currencySymbol = getCurrencySymbol(data.currency); const formattedAmount = formatNumber(amount); document.getElementById('certDebtAmount').textContent = `${formattedAmount} ${currencySymbol}`; document.getElementById('certDebtAmountWords').textContent = sumToWords(amount, data.currency); document.getElementById('certDebtPeriod').textContent = data.debtPeriod; document.getElementById('certReason').textContent = data.reason; const docBlock = document.getElementById('certDocumentBlock'); if (data.document) { docBlock.style.display = 'block'; document.getElementById('certDocument').textContent = data.document; } else { docBlock.style.display = 'none'; } // Дата и номер document.getElementById('certIssueDate').textContent = formattedDate; document.getElementById('certIssueNumber').textContent = data.issueNumber ? `№ ${data.issueNumber}` : ''; document.getElementById('certSignDate').textContent = signDate; // Руководитель const directorText = data.director || 'Руководитель _______________'; document.getElementById('certDirector').textContent = directorText; } function formatDate(dateString) { if (!dateString) return '«___» ________ 20__ г.'; const date = new Date(dateString); const day = date.getDate(); const month = date.toLocaleDateString('ru-RU', { month: 'long' }); const year = date.getFullYear(); return `«${day}» ${month} ${year} г.`; } function formatNumber(num) { return num.toLocaleString('ru-RU'); } function getCurrencySymbol(currency) { const symbols = { 'KZT': '₸', 'RUB': '₽', 'USD': '$' }; return symbols[currency] || currency; } // Преобразование суммы в пропись (упрощённая версия) function sumToWords(sum, currency) { if (!sum || sum === 0) return '______________________'; const currencies = { 'KZT': { units: ['тенге', 'тенге', 'тенге'], kopeks: ['тиын', 'тиына', 'тиынов'] }, 'RUB': { units: ['рубль', 'рубля', 'рублей'], kopeks: ['копейка', 'копейки', 'копеек'] }, 'USD': { units: ['доллар', 'доллара', 'долларов'], kopeks: ['цент', 'цента', 'центов'] } }; const curr = currencies[currency] || currencies['KZT']; const wholePart = Math.floor(sum); // Очень упрощённо - для точной реализации нужен полный словарь return `${formatNumber(wholePart)} (${currency})`; } function printCertificate() { window.print(); } function copyToClipboard() { const cert = document.getElementById('certificatePreview'); const text = cert.innerText; navigator.clipboard.writeText(text).then(() => { const btn = document.querySelector('.btn-copy'); const originalText = btn.textContent; btn.textContent = '✅ Скопировано!'; setTimeout(() => { btn.textContent = originalText; }, 2000); }).catch(err => { alert('Не удалось скопировать текст'); }); } function clearForm() { document.getElementById('debtForm').reset(); const today = new Date().toISOString().split('T')[0]; document.getElementById('issueDate').value = today; updateCertificate(); }