Филиалы: цвет при создании и редактировании

This commit is contained in:
dev
2026-11-28 12:41:49 +03:00
parent c9d477b6c5
commit 468ab0a5fd
7 changed files with 73 additions and 12 deletions
+18 -2
View File
@@ -1,4 +1,14 @@
let branches = [];
const BRANCH_COLORS = ['#2563eb', '#7c3aed', '#db2777', '#dc2626', '#ea580c', '#d97706', '#16a34a', '#0d9488', '#0891b2', '#4f46e5', '#9333ea', '#475569'];
function branchColor(value) {
const raw = String(value || '').trim();
return /^#[0-9a-fA-F]{6}$/.test(raw) ? raw.toLowerCase() : BRANCH_COLORS[0];
}
function randomBranchColor() {
return BRANCH_COLORS[Math.floor(Math.random() * BRANCH_COLORS.length)];
}
async function loadBranches() {
const res = await fetch(`${API}/api/branches`, { headers: hdr() });
@@ -16,7 +26,7 @@ function renderBranches() {
el.innerHTML = branches.map(b => `
<div class="branch-card" data-id="${b.id}">
<div class="branch-header">
<span class="branch-name">${esc(b.name)}</span>
<span class="branch-name"><span class="branch-color-swatch" style="background:${branchColor(b.color)}" title="${branchColor(b.color)}"></span>${esc(b.name)}</span>
<span class="branch-badge">${b.groups_count} ${plural(b.groups_count, 'группа', 'группы', 'групп')}</span>
</div>
${b.address ? `<div class="branch-meta">📍 ${esc(b.address)}</div>` : ''}
@@ -52,10 +62,12 @@ function openBranchModal(id = null) {
form.name.value = b.name;
form.address.value = b.address || '';
form.phone.value = b.phone || '';
document.getElementById('branchColor').value = branchColor(b.color);
document.getElementById('branchId').value = b.id;
} else {
title.textContent = 'Создать филиал';
submitBtn.textContent = 'Создать';
document.getElementById('branchColor').value = randomBranchColor();
}
modal.classList.add('open');
setTimeout(() => form.name.focus(), 100);
@@ -72,6 +84,7 @@ document.getElementById('branchForm').addEventListener('submit', async (e) => {
const name = fd.get('name').trim();
const address = fd.get('address').trim();
const phone = fd.get('phone').trim();
const color = branchColor(fd.get('color'));
if (!name) { alert('Название обязательно'); return; }
const url = id ? `${API}/api/branches/${id}` : `${API}/api/branches`;
@@ -79,7 +92,7 @@ document.getElementById('branchForm').addEventListener('submit', async (e) => {
const res = await fetch(url, {
method,
headers: hdrJson(),
body: JSON.stringify({ name, address: address || null, phone: phone || null })
body: JSON.stringify({ name, address: address || null, phone: phone || null, color })
});
if (res.ok) {
showToast(id ? 'Филиал обновлён' : 'Филиал создан');
@@ -98,6 +111,9 @@ async function deleteBranch(id) {
}
document.getElementById('addBranchBtn').addEventListener('click', () => openBranchModal());
document.getElementById('branchColorRandom').addEventListener('click', () => {
document.getElementById('branchColor').value = randomBranchColor();
});
document.getElementById('branchModal').addEventListener('click', e => { if (e.target === e.currentTarget) closeBranchModal(); });
document.getElementById('branchCancelBtn').addEventListener('click', closeBranchModal);
document.getElementById('imgModal').addEventListener('click', e => e.currentTarget.classList.remove('open'));