feat: add photos to daily reports

This commit is contained in:
Арсентьев Александр
2026-10-15 14:42:04 +03:00
parent 1af2a70406
commit 3a3e481750
14 changed files with 1765 additions and 20 deletions
+93 -3
View File
@@ -18,12 +18,13 @@
<h1>{{ 'Редактировать Daily Report' if report else 'Новый Daily Report' }}</h1>
{% for error in errors %}<div class="alert alert-danger">{{ error }}</div>{% endfor %}
<form method="post" action="{% if report %}/projects/{{ project.id }}/daily-reports/{{ report.id }}/edit{% else %}/projects/{{ project.id }}/daily-reports{% endif %}">
<form method="post" enctype="multipart/form-data"
action="{% if report %}/projects/{{ project.id }}/daily-reports/{{ report.id }}/edit{% else %}/projects/{{ project.id }}/daily-reports{% endif %}">
<input type="hidden" name="system_id" id="hidden-system-id" value="{{ selected_system.id if selected_system else ('unclassified' if unclassified_selected else '') }}">
<div class="card card-body mb-3">
<div class="row g-3">
<div class="col-md-4"><label for="report-date" class="form-label">Дата отчёта</label><input id="report-date" class="form-control" type="date" name="report_date" required value="{{ report.report_date.isoformat() if report else '' }}"></div>
<div class="col-md-8"><label for="notes" class="form-label">Комментарий</label><input id="notes" class="form-control" name="notes" value="{{ report.notes if report and report.notes else '' }}"></div>
<div class="col-md-4"><label for="report-date" class="form-label">Дата отчёта</label><input id="report-date" class="form-control" type="date" name="report_date" required value="{{ form_values.get('report_date', report.report_date.isoformat() if report else '') }}"></div>
<div class="col-md-8"><label for="notes" class="form-label">Комментарий</label><input id="notes" class="form-control" name="notes" value="{{ form_values.get('notes', report.notes if report and report.notes else '') }}"></div>
</div>
</div>
@@ -88,6 +89,50 @@
</table>
</div>
<div id="selected-empty" class="card-footer text-muted" hidden>Пока ничего не добавлено.</div>
<!-- ============ Фотоотчёт ============ -->
<div class="card-footer border-top" id="photo-zone">
<div class="d-flex justify-content-between align-items-center mb-2">
<strong>Фотоотчёт</strong>
<span class="text-muted small">до {{ photo_limit }} фото · до {{ (photo_max_bytes / 1024 / 1024)|round(0)|int }} МБ · {{ photo_formats }}</span>
</div>
<div class="form-text mb-2">Фото привязываются к отчёту целиком. Можно выбрать файлы на компьютере или телефоне — из галереи или камерой, если это поддерживает устройство.</div>
{% if existing_photos %}
<div class="mb-3">
<div class="form-label">Сохранённые фотографии</div>
<div class="row g-2" id="existing-photos">
{% for photo in existing_photos %}
<div class="col-4 col-md-2">
<div class="border rounded p-1">
<img class="img-fluid rounded w-100"
style="aspect-ratio: 4 / 3; object-fit: cover;"
src="/projects/{{ project.id }}/daily-reports/{{ report.id }}/photos/{{ photo.id }}"
alt="{{ photo.original_name or ('Фото ' ~ photo.id) }}">
<div class="form-check mt-1">
<input class="form-check-input" type="checkbox"
name="delete_photo_{{ photo.id }}" value="1"
id="delete_photo_{{ photo.id }}">
<label class="form-check-label small text-danger"
for="delete_photo_{{ photo.id }}">Удалить</label>
</div>
</div>
</div>
{% endfor %}
</div>
<div class="form-text">Существующие фото удаляются только по явной отметке «Удалить» и сохранению формы.</div>
</div>
{% endif %}
<div class="mb-2">
<label for="photo-input" class="form-label">Добавить фотографии</label>
<input class="form-control" type="file" id="photo-input" name="files"
accept="image/jpeg,image/png,image/webp,image/*" multiple>
</div>
<div class="row g-2" id="photo-preview"></div>
<div id="photo-empty" class="form-text">Новые файлы будут показаны миниатюрами здесь.</div>
</div>
<div class="card-footer d-flex justify-content-between">
<a class="btn btn-outline-secondary" href="/projects/{{ project.id }}/daily-reports">Отмена</a>
<button class="btn btn-primary" type="submit">{{ 'Сохранить изменения' if report else 'Создать отчёт' }}</button>
@@ -213,6 +258,51 @@
rebuildSelected();
applyPickerFilter();
// ---------- Фотоотчёт: миниатюры до сохранения ----------
const photoInput = document.getElementById('photo-input');
const photoPreview = document.getElementById('photo-preview');
const photoEmpty = document.getElementById('photo-empty');
if (photoInput && photoPreview) {
const renderPhotoPreview = () => {
photoPreview.textContent = '';
const files = [...photoInput.files];
if (photoEmpty) photoEmpty.hidden = files.length !== 0;
files.forEach((file, index) => {
const col = document.createElement('div');
col.className = 'col-4 col-md-2';
const box = document.createElement('div');
box.className = 'border rounded p-1';
const img = document.createElement('img');
img.className = 'img-fluid rounded w-100';
img.style.aspectRatio = '4 / 3';
img.style.objectFit = 'cover';
img.alt = file.name;
try {
img.src = URL.createObjectURL(file);
img.onload = () => URL.revokeObjectURL(img.src);
} catch { /* preview is best-effort */ }
const name = document.createElement('div');
name.className = 'small text-muted text-truncate';
name.textContent = file.name;
name.title = file.name;
const remove = document.createElement('button');
remove.type = 'button';
remove.className = 'btn btn-sm btn-outline-danger w-100 mt-1';
remove.textContent = 'Убрать';
remove.addEventListener('click', () => {
const dt = new DataTransfer();
[...photoInput.files].forEach((f, i) => { if (i !== index) dt.items.add(f); });
photoInput.files = dt.files;
renderPhotoPreview();
});
box.append(img, name, remove);
col.appendChild(box);
photoPreview.appendChild(col);
});
};
photoInput.addEventListener('change', renderPhotoPreview);
}
})();
</script>
</main>