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

This commit is contained in:
dev
2026-11-28 12:34:15 +03:00
parent cde281fd89
commit c9d477b6c5
8 changed files with 79 additions and 14 deletions
+18 -1
View File
@@ -5,6 +5,9 @@ async function init() {
document.getElementById('addUserBtn').addEventListener('click', () => openModal(null));
document.getElementById('uCancel').addEventListener('click', closeModal);
document.getElementById('uSave').addEventListener('click', saveUser);
document.getElementById('uColorRandom').addEventListener('click', () => {
document.getElementById('uColor').value = randomUserColor();
});
document.getElementById('uRole').addEventListener('change', () => {
document.getElementById('uBranchesField').style.display = document.getElementById('uRole').value === 'admin' ? 'none' : '';
});
@@ -13,6 +16,16 @@ async function init() {
let branches = [];
let editingId = null;
const USER_COLORS = ['#2563eb', '#7c3aed', '#db2777', '#dc2626', '#ea580c', '#d97706', '#16a34a', '#0d9488', '#0891b2', '#4f46e5', '#9333ea', '#475569'];
function userColor(value) {
const raw = String(value || '').trim();
return /^#[0-9a-fA-F]{6}$/.test(raw) ? raw.toLowerCase() : USER_COLORS[0];
}
function randomUserColor() {
return USER_COLORS[Math.floor(Math.random() * USER_COLORS.length)];
}
async function loadUsers() {
const [uRes, bRes] = await Promise.all([
@@ -37,11 +50,13 @@ function renderUsers(users) {
const roleCls = u.role === 'admin' ? 'badge-admin' : 'badge-tutor';
const roleTxt = u.role === 'admin' ? 'Администратор' : 'Тутор';
const status = u.is_active ? '<span class="status-ok">Активен</span>' : '<span class="status-off">Заблокирован</span>';
const color = userColor(u.color);
const delBtn = u.id === currentUser.id
? ''
: `<button class="row-btn del" data-del="${u.id}" title="Удалить">&times;</button>`;
tr.innerHTML = `
<td>${u.id}</td>
<td><span class="user-color-swatch" style="background:${color}" title="${color}"></span></td>
<td>${esc(u.username)}</td>
<td>${esc(u.name || '')}</td>
<td><span class="${roleCls}">${roleTxt}</span></td>
@@ -66,6 +81,7 @@ function openModal(user) {
document.getElementById('userModalTitle').textContent = user ? `Пользователь: ${user.username}` : 'Новый пользователь';
document.getElementById('uUsername').value = user ? user.username : '';
document.getElementById('uName').value = user ? (user.name || '') : '';
document.getElementById('uColor').value = user ? userColor(user.color) : randomUserColor();
document.getElementById('uRole').value = user ? user.role : 'tutor';
document.getElementById('uPassword').value = '';
document.getElementById('uActive').checked = user ? user.is_active : true;
@@ -92,13 +108,14 @@ async function saveUser() {
const username = document.getElementById('uUsername').value.trim();
const name = document.getElementById('uName').value.trim() || null;
const role = document.getElementById('uRole').value;
const color = userColor(document.getElementById('uColor').value);
const password = document.getElementById('uPassword').value;
const is_active = document.getElementById('uActive').checked;
const branchSel = document.getElementById('uBranches');
const branch_ids = role === 'admin' ? [] : [...branchSel.selectedOptions].map(o => Number(o.value));
if (!username) { showToast('Укажите логин'); return; }
if (!editingId && password.length < 6) { showToast('Пароль должен быть не короче 6 символов'); return; }
const payload = { username, name, role, is_active, branch_ids };
const payload = { username, name, role, is_active, branch_ids, color };
if (password) payload.password = password;
const res = await fetch(editingId ? `${API}/api/users/${editingId}` : `${API}/api/users`, {
method: editingId ? 'PUT' : 'POST',