feat: редактирование и удаление оборудования
This commit is contained in:
parent
602ff94919
commit
bd9aedbdfe
57
script.js
57
script.js
@ -376,6 +376,30 @@ function addEquipment(event) {
|
|||||||
const period = parseInt(formData.get('period'));
|
const period = parseInt(formData.get('period'));
|
||||||
const nextCheck = calculateNextDateValue(lastCheck, period);
|
const nextCheck = calculateNextDateValue(lastCheck, period);
|
||||||
|
|
||||||
|
if (editingId) {
|
||||||
|
// Edit existing item
|
||||||
|
const index = equipment.findIndex(e => e.id === editingId);
|
||||||
|
if (index !== -1) {
|
||||||
|
equipment[index] = {
|
||||||
|
...equipment[index],
|
||||||
|
name: formData.get('name'),
|
||||||
|
category: formData.get('category'),
|
||||||
|
inventoryNumber: formData.get('inventoryNumber'),
|
||||||
|
location: formData.get('location'),
|
||||||
|
responsible: formData.get('responsible'),
|
||||||
|
lastCheck: lastCheck,
|
||||||
|
period: period,
|
||||||
|
nextCheck: nextCheck,
|
||||||
|
note: formData.get('note') || ''
|
||||||
|
};
|
||||||
|
saveEquipment();
|
||||||
|
updateAll();
|
||||||
|
resetForm();
|
||||||
|
switchTab('all');
|
||||||
|
}
|
||||||
|
editingId = null;
|
||||||
|
} else {
|
||||||
|
// Add new item
|
||||||
const newItem = {
|
const newItem = {
|
||||||
id: Date.now(),
|
id: Date.now(),
|
||||||
name: formData.get('name'),
|
name: formData.get('name'),
|
||||||
@ -395,8 +419,7 @@ function addEquipment(event) {
|
|||||||
resetForm();
|
resetForm();
|
||||||
updateAll();
|
updateAll();
|
||||||
switchTab('all');
|
switchTab('all');
|
||||||
|
}
|
||||||
alert('Оборудование успешно добавлено!');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateNextDateValue(lastCheck, periodMonths) {
|
function calculateNextDateValue(lastCheck, periodMonths) {
|
||||||
@ -419,6 +442,11 @@ function calculateNextDate() {
|
|||||||
function resetForm() {
|
function resetForm() {
|
||||||
document.getElementById('add-form').reset();
|
document.getElementById('add-form').reset();
|
||||||
document.getElementById('next-check').value = '';
|
document.getElementById('next-check').value = '';
|
||||||
|
editingId = null;
|
||||||
|
|
||||||
|
// Reset button text
|
||||||
|
const submitBtn = document.getElementById('add-form').querySelector('button[type="submit"]');
|
||||||
|
submitBtn.innerHTML = 'Сохранить';
|
||||||
}
|
}
|
||||||
|
|
||||||
function openDrawer(id) {
|
function openDrawer(id) {
|
||||||
@ -492,10 +520,16 @@ function openDrawer(id) {
|
|||||||
`;
|
`;
|
||||||
|
|
||||||
actions.innerHTML = `
|
actions.innerHTML = `
|
||||||
|
<div style="display: flex; gap: var(--kt-ai-space-3);">
|
||||||
<button class="kt-ai-btn" data-variant="primary" onclick="editEquipment(${item.id})">
|
<button class="kt-ai-btn" data-variant="primary" onclick="editEquipment(${item.id})">
|
||||||
<svg class="kt-icon kt-icon-sm" aria-hidden="true"><use href="#pencil"></use></svg>
|
<svg class="kt-icon kt-icon-sm" aria-hidden="true"><use href="#pencil"></use></svg>
|
||||||
Редактировать
|
Редактировать
|
||||||
</button>
|
</button>
|
||||||
|
<button class="kt-ai-btn" data-variant="danger" onclick="deleteEquipment(${item.id})">
|
||||||
|
<svg class="kt-icon kt-icon-sm" aria-hidden="true"><use href="#trash"></use></svg>
|
||||||
|
Удалить
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
<button class="kt-ai-btn" data-variant="ghost" onclick="closeDrawer()">Закрыть</button>
|
<button class="kt-ai-btn" data-variant="ghost" onclick="closeDrawer()">Закрыть</button>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
@ -506,10 +540,13 @@ function closeDrawer() {
|
|||||||
document.getElementById('equipment-drawer').hidden = true;
|
document.getElementById('equipment-drawer').hidden = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let editingId = null;
|
||||||
|
|
||||||
function editEquipment(id) {
|
function editEquipment(id) {
|
||||||
const item = equipment.find(e => e.id === id);
|
const item = equipment.find(e => e.id === id);
|
||||||
if (!item) return;
|
if (!item) return;
|
||||||
|
|
||||||
|
editingId = id;
|
||||||
closeDrawer();
|
closeDrawer();
|
||||||
switchTab('add');
|
switchTab('add');
|
||||||
|
|
||||||
@ -524,8 +561,9 @@ function editEquipment(id) {
|
|||||||
document.getElementById('next-check').value = item.nextCheck;
|
document.getElementById('next-check').value = item.nextCheck;
|
||||||
form.note.value = item.note || '';
|
form.note.value = item.note || '';
|
||||||
|
|
||||||
// Remove old item
|
// Change button text
|
||||||
equipment = equipment.filter(e => e.id !== id);
|
const submitBtn = form.querySelector('button[type="submit"]');
|
||||||
|
submitBtn.innerHTML = '<svg class="kt-icon kt-icon-sm" aria-hidden="true"><use href="#check"></use></svg> Сохранить изменения';
|
||||||
}
|
}
|
||||||
|
|
||||||
function exportToExcel() {
|
function exportToExcel() {
|
||||||
@ -657,7 +695,7 @@ function escapeHtml(text) {
|
|||||||
.replace(/"/g, '"');
|
.replace(/"/g, '"');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function saveEquipment() {
|
async async function saveEquipment() {
|
||||||
try {
|
try {
|
||||||
await fetch('api/items', {
|
await fetch('api/items', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@ -669,6 +707,15 @@ async function saveEquipment() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deleteEquipment(id) {
|
||||||
|
if (!confirm('Вы уверены, что хотите удалить это оборудование?')) return;
|
||||||
|
|
||||||
|
equipment = equipment.filter(e => e.id !== id);
|
||||||
|
saveEquipment();
|
||||||
|
closeDrawer();
|
||||||
|
updateAll();
|
||||||
|
}
|
||||||
|
|
||||||
// Navigation
|
// Navigation
|
||||||
document.querySelectorAll('[data-tab]').forEach(link => {
|
document.querySelectorAll('[data-tab]').forEach(link => {
|
||||||
link.addEventListener('click', (e) => {
|
link.addEventListener('click', (e) => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user