';
return employees;
}
var html = '';
employees.forEach(function(emp) {
var ini = initials(emp.name);
var statusBadge = emp.is_active
? 'Activo'
: 'Inactivo';
html += '
'
+ '
'
+ '
' + ini + '
'
+ '' + escHtml(emp.name) + ''
+ '
'
+ '
' + escHtml(emp.email || '-') + '
'
+ '
' + roleBadge(emp.role) + '
'
+ '
' + escHtml(emp.branch_name || 'Todas') + '
'
+ '
' + statusBadge + '
'
+ '
' + (emp.max_discount_pct || 0) + '%
'
+ '
'
+ '
';
});
tbody.innerHTML = html;
return employees;
}
async function saveEmployee(data) {
var res = await fetch(API + '/employees', {
method: 'POST',
headers: headers(),
body: JSON.stringify(data)
});
if (!res.ok) {
var err = await res.json().catch(function() { return { error: res.statusText }; });
throw new Error(err.error || 'Save failed');
}
return res.json();
}
// -------------------------------------------------------------------------
// Business data (read-only)
// -------------------------------------------------------------------------
async function loadBusiness() {
try {
var res = await fetch(API + '/business', { headers: headers() });
if (!res.ok) return;
var d = await res.json();
setVal('biz-razon-social', d.razon_social);
setVal('biz-nombre', d.nombre);
setVal('biz-rfc', d.rfc);
setVal('biz-regimen', d.regimen_fiscal);
setVal('biz-direccion', d.direccion);
setVal('biz-telefono', d.telefono);
setVal('biz-email', d.email);
} catch (e) {
console.error('Config.loadBusiness:', e);
}
}
function setVal(id, v) {
var el = document.getElementById(id);
if (el) el.value = v || '';
}
// -------------------------------------------------------------------------
// Event bindings
// -------------------------------------------------------------------------
function bindEvents() {
// New Branch modal
var btnNewBranch = document.querySelector('#branches-grid');
// The "Agregar Sucursal" card is rendered dynamically, handled via onclick
// Save Branch
var btnSaveBranch = document.getElementById('btn-save-branch');
if (btnSaveBranch) {
btnSaveBranch.addEventListener('click', async function() {
var name = document.getElementById('branch-name').value.trim();
if (!name) { toast('Nombre de sucursal requerido', 'error'); return; }
btnSaveBranch.disabled = true;
btnSaveBranch.textContent = 'Guardando...';
try {
await saveBranch({
name: name,
address: document.getElementById('branch-address').value.trim(),
phone: document.getElementById('branch-phone').value.trim()
});
toast('Sucursal creada');
closeModal('modal-branch');
document.getElementById('branch-name').value = '';
document.getElementById('branch-address').value = '';
document.getElementById('branch-phone').value = '';
await loadBranches();
} catch (e) {
toast(e.message, 'error');
} finally {
btnSaveBranch.disabled = false;
btnSaveBranch.textContent = 'Guardar Sucursal';
}
});
}
// New Employee modal
var btnNewEmp = document.getElementById('btn-new-employee');
if (btnNewEmp) {
btnNewEmp.addEventListener('click', function() {
openModal('modal-employee');
});
}
// Save Employee
var btnSaveEmp = document.getElementById('btn-save-employee');
if (btnSaveEmp) {
btnSaveEmp.addEventListener('click', async function() {
var name = document.getElementById('emp-name').value.trim();
var role = document.getElementById('emp-role').value;
var pin = document.getElementById('emp-pin').value.trim();
if (!name) { toast('Nombre requerido', 'error'); return; }
if (!role) { toast('Selecciona un rol', 'error'); return; }
if (!pin || pin.length !== 4 || !/^\d{4}$/.test(pin)) {
toast('PIN debe ser 4 digitos', 'error');
return;
}
var branchId = document.getElementById('emp-branch').value;
btnSaveEmp.disabled = true;
btnSaveEmp.textContent = 'Guardando...';
try {
await saveEmployee({
name: name,
email: document.getElementById('emp-email').value.trim() || null,
phone: document.getElementById('emp-phone').value.trim() || null,
role: role,
pin: pin,
branch_id: branchId ? parseInt(branchId, 10) : null,
max_discount_pct: parseFloat(document.getElementById('emp-discount').value) || 0
});
toast('Empleado creado');
closeModal('modal-employee');
// Reset form
document.getElementById('emp-name').value = '';
document.getElementById('emp-email').value = '';
document.getElementById('emp-phone').value = '';
document.getElementById('emp-role').value = '';
document.getElementById('emp-pin').value = '';
document.getElementById('emp-branch').value = '';
document.getElementById('emp-discount').value = '0';
await loadEmployees();
} catch (e) {
toast(e.message, 'error');
} finally {
btnSaveEmp.disabled = false;
btnSaveEmp.textContent = 'Guardar Empleado';
}
});
}
// Close modals on overlay click
document.querySelectorAll('.cfg-modal-overlay').forEach(function(overlay) {
overlay.addEventListener('click', function(ev) {
if (ev.target === overlay) {
overlay.style.display = 'none';
}
});
});
// Close modals on Escape key
document.addEventListener('keydown', function(ev) {
if (ev.key === 'Escape') {
document.querySelectorAll('.cfg-modal-overlay').forEach(function(o) {
o.style.display = 'none';
});
}
});
}
// -------------------------------------------------------------------------
// Init
// -------------------------------------------------------------------------
function init() {
if (!checkAuth()) return;
// Restore theme
try {
var saved = localStorage.getItem('nexus-theme');
if (saved === 'industrial' || saved === 'modern') {
setTheme(saved);
}
} catch(e) {}
// Start clock
updateClock();
setInterval(updateClock, 30000);
// Bind UI events
bindEvents();
// Load real data in parallel
loadBranches();
loadEmployees();
loadBusiness();
}
document.addEventListener('DOMContentLoaded', init);
return {
init, setTheme, selectThemeOption,
loadBranches, loadEmployees, saveBranch, saveEmployee,
openModal, closeModal
};
})();