"use strict"; /* CyberReport: детализация атаки в drawer: добавление, редактирование, удаление. */ CR.Form = {}; CR.Form.canEdit = (a) => !a.demo; CR.Form.canDelete = (a) => !a.demo; function drawerShell(title, bodyHtml, actionsHtml) { return '
' + "

" + title + "

" + '" + "
" + bodyHtml + (actionsHtml ? '
' + actionsHtml + "
" : ""); } CR.Form.attachClose = () => { const el = document.getElementById("cr-drawer-close"); if (el) el.addEventListener("click", () => CR.closeDrawer()); document.getElementById("drawer").addEventListener("click", (e) => { if (e.target === e.currentTarget) CR.closeDrawer(); }, { once: false }); }; /* ---------- Добавление / редактирование ---------- */ CR.Form.openCreate = function () { CR.Form.formView(null, "Добавить атаку"); }; CR.Form.openEdit = function (id) { const a = CR.state.attacks.find(x => x.id === id); if (!a) return CR.toast("Атака не найдена", "warn"); if (a.demo) return CR.toast("Демо-запись: создайте свою запись, чтобы редактировать её", "warn"); CR.Form.formView(a, "Редактировать атаку"); }; CR.Form.formView = function (a, title) { const isEdit = !!a; const opts = (list, sel) => list.map(x => '" ).join(""); const level = a ? a.level : ""; const typeT = a ? a.type : ""; const st = a ? a.status : ""; const body = '
' + '
' + '
' + '
' + '
" + '
" + '
" + "
" + '
" + '
" + "
" + (isEdit && a.id ? '" : ""); const actions = '" + ''; CR.openDrawer(drawerShell(title, body, actions)); CR.Form.attachClose(); document.getElementById("cr-form-cancel").addEventListener("click", () => CR.closeDrawer()); document.getElementById("cr-form-save").addEventListener("click", () => CR.Form.submit(a)); const del = document.getElementById("cr-form-delete"); if (del) del.addEventListener("click", () => CR.Form.confirmDelete(a.id)); const t = document.getElementById("cr-form").elements.title; if (t) t.focus(); }; CR.Form.submit = async function (a) { const form = document.getElementById("cr-form"); const title = form.elements.title.value.trim(); const date = form.elements.date.value; if (!title) { CR.toast("Укажите название атаки", "warn"); form.elements.title.focus(); return; } if (!date) { CR.toast("Укажите дату атаки", "warn"); form.elements.date.focus(); return; } const payload = { title, date, type: form.elements.type.value, level: form.elements.level.value, status: form.elements.status.value, description: form.elements.description.value.trim(), measures: form.elements.measures.value.trim() }; const btn = document.getElementById("cr-form-save"); if (btn) { btn.disabled = true; } try { await CR.saveAttack(a ? a.id : null, payload); CR.closeDrawer(); CR.toast(a ? "Изменения сохранены" : "Атака добавлена"); } catch (e2) { CR.toast("Не удалось сохранить: " + e2.message, "risk"); if (btn) { btn.disabled = false; } } }; /* ---------- Удаление ---------- */ CR.Form.confirmDelete = function (id) { const a = CR.state.attacks.find(x => x.id === id); if (!a) return; CR.openDrawer(drawerShell("Удалить атаку?", '

' + CR.esc(a.title) + " (" + CR.fmtDate(a.date) + ") будет удалена безвозвратно.

" + (a.demo ? '

Это демо-запись.

' : ""), '" + '')); CR.Form.attachClose(); document.getElementById("cr-del-yes").addEventListener("click", async () => { try { await CR.api("attacks/" + id, { method: "DELETE" }); await CR.loadAttacks(); CR.closeDrawer(); CR.toast("Атака удалена"); } catch (e2) { CR.toast("Не удалось удалить: " + e2.message, "risk"); } }); document.getElementById("cr-del-no").addEventListener("click", () => CR.closeDrawer()); };