var TF_ADMIN_EMAIL=’lizbethflores.9823@hotmail.com’;
var TF_PASS_HASH=’6410917fb752cc2c9fce16f0f41f9f435115d29abe499c3d04994072432b699a’;
async function tfLogin(){
var email=document.getElementById(‘tf-admin-email’).value.trim();
var pass=document.getElementById(‘tf-admin-pass’).value;
var err=document.getElementById(‘tf-login-err’);
err.style.display=’none’;
try{
var buf=await crypto.subtle.digest(‘SHA-256′,new TextEncoder().encode(pass));
var hash=Array.from(new Uint8Array(buf)).map(b=>b.toString(16).padStart(2,’0’)).join(»);
if(email===TF_ADMIN_EMAIL&&hash===TF_PASS_HASH){
sessionStorage.setItem(‘tf_admin’,’1′);
document.getElementById(‘tf-login-wrap’).style.display=’none’;
document.getElementById(‘tf-dashboard-wrap’).style.display=’flex’;
document.getElementById(‘tf-dashboard-wrap’).style.flexDirection=’column’;
tfRenderProds();
}else{err.style.display=’block’;}
}catch(e){err.style.display=’block’;}
}
function tfLogout(){sessionStorage.removeItem(‘tf_admin’);location.reload();}
function tfTab(t){
[‘productos’,’agregar’,’stats’].forEach(function(n){
var c=document.getElementById(‘tf-tab-‘+n);
var b=document.getElementById(‘tab-btn-‘+n);
if(c)c.style.display=n===t?’block’:’none’;
if(b){b.classList.remove(‘active’);if(n===t)b.classList.add(‘active’);}
});
if(t===’stats’)tfRenderStats();
if(t===’productos’)tfRenderProds();
}
function tfGetProds(){try{return JSON.parse(localStorage.getItem(‘tf_productos’)||'[]’);}catch(e){return[];}}
function tfSaveProds(arr){localStorage.setItem(‘tf_productos’,JSON.stringify(arr));}
function tfRenderProds(){
var prods=tfGetProds();
var wrap=document.getElementById(‘tf-prod-list-wrap’);
if(!wrap)return;
if(prods.length===0){
wrap.innerHTML=’
📦
No hay productos aún. ¡Agrega el primero haciendo click en Agregar!
‘;
return;
}
var rows=prods.map(function(p,i){
return ‘
‘+(p.img?’‘:»)+’ ‘+p.name+’
‘+p.cat+’
$’+p.price+’
‘+p.desc+’
‘;
}).join(»);
wrap.innerHTML=’
Producto
Categoría
Precio
Descripción
Acciones
‘+rows+’
‘;
}
function tfSaveProd(){
var idx=parseInt(document.getElementById(‘tf-edit-idx’).value);
var prod={
name:document.getElementById(‘tf-p-name’).value.trim(),
cat:document.getElementById(‘tf-p-cat’).value,
price:parseFloat(document.getElementById(‘tf-p-price’).value)||0,
img:document.getElementById(‘tf-p-img’).value.trim(),
desc:document.getElementById(‘tf-p-desc’).value.trim()
};
if(!prod.name){alert(‘El nombre es obligatorio’);return;}
var prods=tfGetProds();
if(idx>=0){prods[idx]=prod;}else{prods.push(prod);}
tfSaveProds(prods);
document.getElementById(‘tf-success-msg’).style.display=’block’;
setTimeout(function(){document.getElementById(‘tf-success-msg’).style.display=’none’;},3000);
tfCancelEdit();
}
function tfEditProd(i){
var p=tfGetProds()[i];
document.getElementById(‘tf-edit-idx’).value=i;
document.getElementById(‘tf-p-name’).value=p.name;
document.getElementById(‘tf-p-cat’).value=p.cat;
document.getElementById(‘tf-p-price’).value=p.price;
document.getElementById(‘tf-p-img’).value=p.img||»;
document.getElementById(‘tf-p-desc’).value=p.desc||»;
document.getElementById(‘tf-form-title’).textContent=’Editar Producto’;
tfTab(‘agregar’);
}
function tfDelProd(i){
if(!confirm(‘¿Eliminar este producto?’))return;
var prods=tfGetProds();prods.splice(i,1);tfSaveProds(prods);tfRenderProds();
}
function tfCancelEdit(){
document.getElementById(‘tf-edit-idx’).value=’-1′;
document.getElementById(‘tf-p-name’).value=»;
document.getElementById(‘tf-p-price’).value=»;
document.getElementById(‘tf-p-img’).value=»;
document.getElementById(‘tf-p-desc’).value=»;
document.getElementById(‘tf-form-title’).textContent=’Agregar Producto’;
tfTab(‘productos’);
}
function tfRenderStats(){
var cot=parseInt(localStorage.getItem(‘tf_cotizaciones’)||’0′);
var prods=tfGetProds().length;
var cart=0;try{cart=JSON.parse(localStorage.getItem(‘tf_carrito’)||'[]’).reduce(function(s,i){return s+i.qty;},0);}catch(e){}
document.getElementById(‘tf-s-cot’).textContent=cot;
document.getElementById(‘tf-s-prod’).textContent=prods;
document.getElementById(‘tf-s-cart’).textContent=cart;
}
function tfResetCot(){if(confirm(‘¿Resetear el contador a 0?’)){localStorage.setItem(‘tf_cotizaciones’,’0′);tfRenderStats();}}
// Auto-check session
if(sessionStorage.getItem(‘tf_admin’)===’1′){
document.getElementById(‘tf-login-wrap’).style.display=’none’;
document.getElementById(‘tf-dashboard-wrap’).style.display=’flex’;
document.getElementById(‘tf-dashboard-wrap’).style.flexDirection=’column’;
tfRenderProds();
}