feat: edit report connected to backend added

This commit is contained in:
Angie 2025-07-26 02:06:14 +02:00
parent 8f333ab2cc
commit 09758d74c0
2 changed files with 254 additions and 113 deletions

View File

@ -473,7 +473,148 @@ function viewReport(reportId) {
//create report
// createReport(data)
//edit report
// updateReport(id, data)
async function updateReport(reportId, data) {
const token = localStorage.getItem("token");
try {
const res = await fetch(`${API_BASE_URL}/reports/${reportId}`, {
method: "PUT",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
const result = await res.json();
if (res.ok && result.updated) {
showNotification("Отчет обновлен!", "success");
hideModal("reportViewModal");
await loadReports();
if (typeof updateDashboard === "function") updateDashboard();
return { success: true };
} else {
showNotification(result.error || "Ошибка обновления отчета", "error");
return { success: false, error: result.error };
}
} catch (err) {
showNotification("Ошибка сети. Попробуйте еще раз.", "error");
return { success: false, error: err.message };
}
}
function editReport(report) {
console.log("editReport() called with:", report);
const content = document.getElementById("reportViewContent");
const buttons = document.getElementById("reportModalButtons");
const shopName = (report.storeName || report.storeId || "").replace(
/"/g,
"""
);
content.innerHTML = `
<form id="editReportForm" class="space-y-6">
<!-- Основная информация -->
<div class="report-section">
<h4 class="font-bold text-gray-700 mb-3"><i class="fas fa-info-circle mr-2"></i>Основная информация</h4>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-sm font-bold text-gray-700 mb-1">Магазин</label>
<input
type="text"
value="${shopName}"
class="form-input w-full px-3 py-2 rounded-lg bg-gray-100 text-gray-900 cursor-not-allowed"
readonly
disabled
>
<input type="hidden" id="editStoreSelect" value="${report.storeId}">
</div>
<div>
<label class="block text-sm font-bold text-gray-700 mb-1">Дата</label>
<input
type="date"
id="editDate"
value="${report.reportDate || report.date || ""}"
class="form-input w-full px-3 py-2 rounded-lg bg-gray-100 text-gray-900 cursor-not-allowed"
readonly
disabled
>
</div>
</div>
</div>
<!-- Доходы -->
<div class="report-section income">
<h4 class="font-bold text-green-700 mb-3"><i class="fas fa-arrow-up mr-2"></i>Доходы</h4>
<div class="grid grid-cols-3 gap-4">
<div>
<label class="block text-sm font-bold text-gray-700 mb-1">Income</label>
<input type="number" id="editIncome" value="${
report.income || ""
}" step="0.01" class="form-input w-full px-3 py-2 rounded-lg">
</div>
<div>
<label class="block text-sm font-bold text-gray-700 mb-1">Caja inicial</label>
<input type="number" id="editCajaInicial" value="${
report.initialCash || report.cajaInicial || ""
}" step="0.01" class="form-input w-full px-3 py-2 rounded-lg">
</div>
<div>
<label class="block text-sm font-bold text-gray-700 mb-1">Envelope</label>
<input type="number" id="editEnvelope" value="${
report.envelope || ""
}" step="0.01" class="form-input w-full px-3 py-2 rounded-lg">
</div>
</div>
</div>
<!-- Статус проверки -->
<div class="report-section">
<h4 class="font-bold text-gray-700 mb-3"><i class="fas fa-check mr-2"></i>Статус</h4>
<label class="flex items-center">
<input type="checkbox" id="editVerified" ${
report.isVerified || report.verified ? "checked" : ""
} class="mr-2">
<span>Отчет проверен</span>
</label>
</div>
</form>
`;
buttons.innerHTML = `
<button id="saveReportBtn" class="btn-success text-white px-4 py-2 rounded-lg">
<i class="fas fa-save mr-2"></i>Сохранить
</button>
<button id="cancelEditBtn" class="bg-gray-500 text-white px-4 py-2 rounded-lg">
<i class="fas fa-times mr-2"></i>Отмена
</button>
`;
document
.getElementById("saveReportBtn")
.addEventListener("click", async (e) => {
e.preventDefault();
await saveEditedReport(report.id);
});
document.getElementById("cancelEditBtn").addEventListener("click", (e) => {
e.preventDefault();
showReportModal(report, true);
});
}
async function saveEditedReport(reportId) {
const data = {
storeId: parseInt(document.getElementById("editStoreSelect").value, 10),
reportDate: document.getElementById("editDate").value,
income: parseFloat(document.getElementById("editIncome").value),
initialCash: parseFloat(document.getElementById("editCajaInicial").value),
envelope: parseFloat(document.getElementById("editEnvelope").value),
isVerified: document.getElementById("editVerified").checked ? 1 : 0,
};
data.totalIncome = data.income + data.initialCash;
await updateReport(reportId, data);
}
//accept report (admin only)
async function verifyReport(reportId) {
const token = localStorage.getItem("token");

View File

@ -697,132 +697,132 @@ function showReportModal(report, isAdmin = false) {
}
// Редактирование отчета администратором
function editReport(report) {
const content = document.getElementById("reportViewContent");
const buttons = document.getElementById("reportModalButtons");
// function editReport(report) {
// const content = document.getElementById("reportViewContent");
// const buttons = document.getElementById("reportModalButtons");
// Преобразуем просмотр в форму редактирования
content.innerHTML = `
<form id="editReportForm" class="space-y-6">
<!-- Основная информация -->
<div class="report-section">
<h4 class="font-bold text-gray-700 mb-3"><i class="fas fa-info-circle mr-2"></i>Основная информация</h4>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-sm font-bold text-gray-700 mb-1">Магазин</label>
<select id="editStoreSelect" class="form-input w-full px-3 py-2 rounded-lg">
${database.stores
.map(
(s) => `
<option value="${s.id}" ${
s.id === report.storeId
? "selected"
: ""
}>${s.name}</option>
`
)
.join("")}
</select>
</div>
<div>
<label class="block text-sm font-bold text-gray-700 mb-1">Дата</label>
<input type="date" id="editDate" value="${
report.date
}" class="form-input w-full px-3 py-2 rounded-lg">
</div>
</div>
</div>
// // Преобразуем просмотр в форму редактирования
// content.innerHTML = `
// <form id="editReportForm" class="space-y-6">
// <!-- Основная информация -->
// <div class="report-section">
// <h4 class="font-bold text-gray-700 mb-3"><i class="fas fa-info-circle mr-2"></i>Основная информация</h4>
// <div class="grid grid-cols-2 gap-4">
// <div>
// <label class="block text-sm font-bold text-gray-700 mb-1">Магазин</label>
// <select id="editStoreSelect" class="form-input w-full px-3 py-2 rounded-lg">
// ${database.stores
// .map(
// (s) => `
// <option value="${s.id}" ${
// s.id === report.storeId
// ? "selected"
// : ""
// }>${s.name}</option>
// `
// )
// .join("")}
// </select>
// </div>
// <div>
// <label class="block text-sm font-bold text-gray-700 mb-1">Дата</label>
// <input type="date" id="editDate" value="${
// report.date
// }" class="form-input w-full px-3 py-2 rounded-lg">
// </div>
// </div>
// </div>
<!-- Доходы -->
<div class="report-section income">
<h4 class="font-bold text-green-700 mb-3"><i class="fas fa-arrow-up mr-2"></i>Доходы</h4>
<div class="grid grid-cols-3 gap-4">
<div>
<label class="block text-sm font-bold text-gray-700 mb-1">Income</label>
<input type="number" id="editIncome" value="${
report.income
}" step="0.01" class="form-input w-full px-3 py-2 rounded-lg">
</div>
<div>
<label class="block text-sm font-bold text-gray-700 mb-1">Caja inicial</label>
<input type="number" id="editCajaInicial" value="${
report.cajaInicial
}" step="0.01" class="form-input w-full px-3 py-2 rounded-lg">
</div>
<div>
<label class="block text-sm font-bold text-gray-700 mb-1">Envelope</label>
<input type="number" id="editEnvelope" value="${
report.envelope
}" step="0.01" class="form-input w-full px-3 py-2 rounded-lg">
</div>
</div>
</div>
// <!-- Доходы -->
// <div class="report-section income">
// <h4 class="font-bold text-green-700 mb-3"><i class="fas fa-arrow-up mr-2"></i>Доходы</h4>
// <div class="grid grid-cols-3 gap-4">
// <div>
// <label class="block text-sm font-bold text-gray-700 mb-1">Income</label>
// <input type="number" id="editIncome" value="${
// report.income
// }" step="0.01" class="form-input w-full px-3 py-2 rounded-lg">
// </div>
// <div>
// <label class="block text-sm font-bold text-gray-700 mb-1">Caja inicial</label>
// <input type="number" id="editCajaInicial" value="${
// report.cajaInicial
// }" step="0.01" class="form-input w-full px-3 py-2 rounded-lg">
// </div>
// <div>
// <label class="block text-sm font-bold text-gray-700 mb-1">Envelope</label>
// <input type="number" id="editEnvelope" value="${
// report.envelope
// }" step="0.01" class="form-input w-full px-3 py-2 rounded-lg">
// </div>
// </div>
// </div>
<!-- Статус проверки -->
<div class="report-section">
<h4 class="font-bold text-gray-700 mb-3"><i class="fas fa-check mr-2"></i>Статус</h4>
<label class="flex items-center">
<input type="checkbox" id="editVerified" ${
report.verified ? "checked" : ""
} class="mr-2">
<span>Отчет проверен</span>
</label>
</div>
</form>
`;
// <!-- Статус проверки -->
// <div class="report-section">
// <h4 class="font-bold text-gray-700 mb-3"><i class="fas fa-check mr-2"></i>Статус</h4>
// <label class="flex items-center">
// <input type="checkbox" id="editVerified" ${
// report.verified ? "checked" : ""
// } class="mr-2">
// <span>Отчет проверен</span>
// </label>
// </div>
// </form>
// `;
// Изменяем кнопки на режим редактирования
buttons.innerHTML = `
<button id="saveReportBtn" class="btn-success text-white px-4 py-2 rounded-lg">
<i class="fas fa-save mr-2"></i>Сохранить
</button>
<button id="cancelEditBtn" class="bg-gray-500 text-white px-4 py-2 rounded-lg">
<i class="fas fa-times mr-2"></i>Отмена
</button>
`;
// // Изменяем кнопки на режим редактирования
// buttons.innerHTML = `
// <button id="saveReportBtn" class="btn-success text-white px-4 py-2 rounded-lg">
// <i class="fas fa-save mr-2"></i>Сохранить
// </button>
// <button id="cancelEditBtn" class="bg-gray-500 text-white px-4 py-2 rounded-lg">
// <i class="fas fa-times mr-2"></i>Отмена
// </button>
// `;
document.getElementById("saveReportBtn").addEventListener("click", () => {
saveEditedReport(report.id);
});
// document.getElementById("saveReportBtn").addEventListener("click", () => {
// saveEditedReport(report.id);
// });
document.getElementById("cancelEditBtn").addEventListener("click", () => {
showReportModal(report, true); // Вернуться к просмотру
});
}
// document.getElementById("cancelEditBtn").addEventListener("click", () => {
// showReportModal(report, true); // Вернуться к просмотру
// });
// }
// Сохранение отредактированного отчета
function saveEditedReport(reportId) {
const reportIndex = database.reports.findIndex((r) => r.id === reportId);
if (reportIndex === -1) return;
// function saveEditedReport(reportId) {
// const reportIndex = database.reports.findIndex((r) => r.id === reportId);
// if (reportIndex === -1) return;
const report = database.reports[reportIndex];
// const report = database.reports[reportIndex];
// Обновляем данные
report.storeId = parseInt(document.getElementById("editStoreSelect").value);
report.date = document.getElementById("editDate").value;
report.income = parseFloat(document.getElementById("editIncome").value);
report.cajaInicial = parseFloat(
document.getElementById("editCajaInicial").value
);
report.envelope = parseFloat(document.getElementById("editEnvelope").value);
report.verified = document.getElementById("editVerified").checked;
// // Обновляем данные
// report.storeId = parseInt(document.getElementById("editStoreSelect").value);
// report.date = document.getElementById("editDate").value;
// report.income = parseFloat(document.getElementById("editIncome").value);
// report.cajaInicial = parseFloat(
// document.getElementById("editCajaInicial").value
// );
// report.envelope = parseFloat(document.getElementById("editEnvelope").value);
// report.verified = document.getElementById("editVerified").checked;
// Пересчитываем итоги
report.totalIncome = report.income + report.cajaInicial;
report.cajaFinal =
report.totalIncome - report.totalExpenses - report.envelope;
// // Пересчитываем итоги
// report.totalIncome = report.income + report.cajaInicial;
// report.cajaFinal =
// report.totalIncome - report.totalExpenses - report.envelope;
database.reports[reportIndex] = report;
// database.reports[reportIndex] = report;
showNotification("Отчет успешно обновлен!");
hideModal("reportViewModal");
// showNotification("Отчет успешно обновлен!");
// hideModal("reportViewModal");
// Обновляем таблицу отчетов если мы в админке
if (currentUser.role === "admin") {
loadReports();
updateDashboard();
}
}
// // Обновляем таблицу отчетов если мы в админке
// if (currentUser.role === "admin") {
// loadReports();
// updateDashboard();
// }
// }
// Подтверждение отчета
// function verifyReport(reportId) {