let vacations = [];
document.getElementById('vacationForm').addEventListener('submit', function(e) {
e.preventDefault();
const vacation = {
id: Date.now(),
country: document.getElementById('country').value,
city: document.getElementById('city').value,
dateFrom: document.getElementById('dateFrom').value,
dateTo: document.getElementById('dateTo').value,
budget: document.getElementById('budget').value,
hotel: document.getElementById('hotel').value,
tickets: document.getElementById('tickets').value
};
vacations.push(vacation);
renderTable();
this.reset();
});
function renderTable() {
const tbody = document.getElementById('tableBody');
const emptyMessage = document.getElementById('emptyMessage');
const totalCount = document.getElementById('totalCount');
totalCount.textContent = vacations.length;
if (vacations.length === 0) {
tbody.innerHTML = '';
emptyMessage.style.display = 'block';
return;
}
emptyMessage.style.display = 'none';
tbody.innerHTML = vacations.map(v => {
const dates = formatDate(v.dateFrom) + ' — ' + formatDate(v.dateTo);
return `
| ${escapeHtml(v.country)} |
${escapeHtml(v.city)} |
${dates} |
$${escapeHtml(v.budget)} |
${escapeHtml(v.hotel) || '—'} |
${escapeHtml(v.tickets) || '—'} |
|
`;
}).join('');
}
function deleteVacation(id) {
if (confirm('Удалить этот вариант отпуска?')) {
vacations = vacations.filter(v => v.id !== id);
renderTable();
}
}
function formatDate(dateStr) {
const date = new Date(dateStr);
return date.toLocaleDateString('ru-RU', { day: 'numeric', month: 'short', year: 'numeric' });
}
function escapeHtml(text) {
if (!text) return '';
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function exportToExcel() {
if (vacations.length === 0) {
alert('Сначала добавьте варианты отпуска');
return;
}
const headers = ['Страна', 'Город', 'Дата вылета', 'Дата возвращения', 'Бюджет ($)', 'Отель', 'Билеты'];
const rows = vacations.map(v => [
v.country,
v.city,
v.dateFrom,
v.dateTo,
v.budget,
v.hotel,
v.tickets
]);
const csvContent = '\uFEFF' + [
headers.join(';'),
...rows.map(row => row.map(cell => '"' + String(cell || '').replace(/"/g, '""') + '"').join(';'))
].join('\n');
const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'otpusk.csv';
link.click();
}
renderTable();