Supabase: регистрация, аудиты и синхронизация между ПК

This commit is contained in:
Dauren777 2026-06-05 10:46:15 +00:00
parent cd3d1176ed
commit 2415e0ceab
2 changed files with 34 additions and 7 deletions

View File

@ -4,6 +4,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>ПАБ — Система</title>
<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2/dist/umd/supabase.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.7/dist/chart.umd.min.js"></script>
<style>
*{box-sizing:border-box;margin:0;padding:0}
@ -86,15 +87,37 @@ td{padding:8px 12px;border-bottom:1px solid #F2F4F7}tr:hover td{background:#F2F4
</div>
<script>
var U,editId,lastSubmitted,vrc=6;
var SB=supabase.createClient("https://znexbjafkvyjffffbhlf.supabase.co","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InpuZXhiamFma3Z5amZmZmZiaGhsZiIsInJvbGUiOiJhbm9uIiwiaWF0IjoxNzgwNjQ0MTgyLCJleHAiOjIwOTYyMjAxODJ9.fhKixmi5Tx6yB-rRkGqHzOdepbXq3BoETdh7s__aDRk");
var U,editId,lastSubmitted;
try{U=JSON.parse(sessionStorage.getItem("pab_user"));if(!U)location.href="index.html"}catch(e){location.href="index.html"}
document.getElementById("dn").textContent=U.login;
function isA(){return U&&U.login==="admin"}
function getU(){try{return JSON.parse(localStorage.getItem("pab_users")||"{}")}catch(e){return{}}}
// ===== DATA: Supabase + localStorage =====
function getU(){return JSON.parse(localStorage.getItem("pab_users")||"{}")}
function saveU(d){localStorage.setItem("pab_users",JSON.stringify(d))}
function allU(){var r=getU();r.admin={pass:"admin",name:"Администратор",role:"Руководитель",email:"admin@telecom.kz",branch:"АО «Казахтелеком»",dept:"ЦА",region:"Центральный",oblast:"—",city:"г. Астана"};return r}
function getA(){try{return JSON.parse(localStorage.getItem("pab_audits")||"[]")}catch(e){return[]}}
function getA(){return JSON.parse(localStorage.getItem("pab_audits")||"[]")}
function saveA(d){localStorage.setItem("pab_audits",JSON.stringify(d))}
function gq(role){var q={c:1,p:"month",l:"1 раз в месяц"};return q}
// Sync from Supabase to localStorage
async function syncFromSB(){
try{
var u=await SB.from("users").select("*");if(u.data){var um={};u.data.forEach(function(x){um[x.login]={pass:x.pass,name:x.name,email:x.email,role:x.role,freq:x.freq,branch:x.branch,dept:x.dept,region:x.region,oblast:x.oblast,city:x.city}});saveU(um)}
var a=await SB.from("audits").select("*").order("created_at",{ascending:false});if(a.data){var am=a.data.map(function(x){return{id:x.id,number:x.number,date:x.date,location:x.location,region:x.region,workType:x.work_type,workerCount:x.worker_count,observer:x.observer,observerRole:x.observer_role,overallSafe:x.overall_safe,categories:x.categories,totalViolations:x.total_violations,dialogue:x.dialogue,docs:x.docs,createdBy:x.created_by,createdAt:x.created_at}});saveA(am)}
console.log("Synced from Supabase")
}catch(e){console.log("Supabase sync failed, using localStorage")}
}
// Write to Supabase
async function sbUpsert(table,data){try{await SB.from(table).upsert(data)}catch(e){}}
async function sbDelete(table,id){try{await SB.from(table).delete().eq("id",id)}catch(e){}}
// ===== SYNC ON LOAD =====
syncFromSB().then(function(){rHS()});
// ===== QUOTA =====
function gq(role){return{c:1,p:"month",l:"1 раз в месяц"}}
function getUserQuota(u){if(u.freq){var p=u.freq.split(",");if(p.length===2)return{c:parseInt(p[0]),p:p[1],l:parseInt(p[0])+" раз(а) в "+(p[1]==="month"?"месяц":p[1]==="quarter"?"квартал":"полгода")}}return gq(u.role)}
function gp(p){var n=new Date();if(p==="month")return{s:new Date(n.getFullYear(),n.getMonth(),1),l:n.toLocaleString("ru",{month:"long",year:"numeric"})};if(p==="quarter"){var q=Math.floor(n.getMonth()/3);return{s:new Date(n.getFullYear(),q*3,1),l:(q+1)+"-й квартал "+n.getFullYear()}}return{s:new Date(n.getFullYear(),n.getMonth()<6?0:6,1),l:(n.getMonth()<6?1:2)+"-е полугодие "+n.getFullYear()}}
@ -163,7 +186,8 @@ function submitAudit(){
var cats={},tv=0;CATS.forEach(function(cat){var ch=[];cat.items.forEach(function(item,i){var cb=document.getElementById("cb-"+cat.id+"-"+i);if(cb&&cb.checked)ch.push({item:item})});cats[cat.id]={items:ch,allSafe:ch.length===0};tv+=ch.length});
var dl=[];if(document.getElementById("d0").checked)dl.push("Работник привёл примеры безопасных действий");if(document.getElementById("d1").checked)dl.push("Были обсуждены риски / проблемы");if(document.getElementById("d2").checked)dl.push("Определены корректирующие меры");if(document.getElementById("d3").checked)dl.push("Предложения работника зафиксированы");
var e={id:editId||Date.now(),number:document.getElementById("pn").value.trim(),date:document.getElementById("pd").value,location:loc,region:document.getElementById("pr").value,workType:document.getElementById("pw").value.trim(),workerCount:parseInt(document.getElementById("pc").value)||1,observer:document.getElementById("po").value.trim()||U.name,observerRole:document.getElementById("por").value.trim(),overallSafe:document.getElementById("os").classList.contains("sf"),categories:cats,totalViolations:tv,dialogue:dl,createdBy:U.login,createdAt:new Date().toISOString()};
var audits=getA();if(editId){audits=audits.map(function(a){return a.id===editId?e:a});editId=null}else{audits.unshift(e)}saveA(audits);lastSubmitted=e;
var audits=getA();if(editId){audits=audits.map(function(a){return a.id===editId?e:a});editId=null}else{audits.unshift(e)}saveA(audits);
sbUpsert("audits",{id:e.id,number:e.number,date:e.date,location:e.location,region:e.region,work_type:e.workType,worker_count:e.workerCount,observer:e.observer,observer_role:e.observerRole,overall_safe:e.overallSafe,categories:e.categories,total_violations:e.totalViolations,dialogue:e.dialogue,docs:e.docs,created_by:e.createdBy,created_at:e.createdAt});lastSubmitted=e;
document.getElementById("sd").innerHTML="<b>Бланк №"+(e.number||"—")+"</b> | "+e.date+" | "+(e.overallSafe?"БЕЗОПАСНО":"НАРУШЕНИЙ: "+e.totalViolations);document.getElementById("fs").style.display="block";setTimeout(function(){document.getElementById("fs").style.display="none"},20000);
resetF();
rVL();
@ -178,7 +202,7 @@ function resetF(){
function exportCSV(){var a=getA();if(a.length===0){alert("Нет данных");return}var all=allU(),h="Бланк №;Дата;Место;Наблюдатель;Должность;Филиал;Регион;Город;Статус;Нарушений",rs=a.map(function(x){var u=all[x.createdBy]||{};return(x.number||"")+";"+x.date+";"+x.location+";"+x.observer+";"+(x.observerRole||"")+";"+(u.branch||"")+";"+(u.region||"")+";"+(u.city||"")+";"+(x.overallSafe?"Безопасно":"Нарушения")+";"+(x.totalViolations||0)}),csv="\uFEFF"+h+"\n"+rs.join("\n"),bl=new Blob([csv],{type:"text/csv"}),ur=URL.createObjectURL(bl),dl=document.createElement("a");dl.href=ur;dl.download="pab.csv";dl.click();URL.revokeObjectURL(ur)}
function editA(id){if(!isA()){alert("Только админ");return}alert("Редактирование: аудит #"+id)}
function delA(id){if(!isA()){alert("Только админ");return}if(!confirm("Удалить?"))return;saveA(getA().filter(function(a){return a.id!==id}));rHS()}
function delA(id){if(!isA()){alert("Только админ");return}if(!confirm("Удалить?"))return;saveA(getA().filter(function(a){return a.id!==id}));sbDelete("audits",id);rHS()}
function downloadFullCSV(){var a=getA();if(a.length===0){alert("Нет данных");return}var all=allU(),h="Бланк №;Дата;Место;Наблюдатель;Филиал;Регион;Статус;Нарушений",rs=a.map(function(x){var u=all[x.createdBy]||{};return(x.number||"")+";"+x.date+";"+x.location+";"+x.observer+";"+(u.branch||"")+";"+(u.region||"")+";"+(x.overallSafe?"Безопасно":"Нарушения")+";"+(x.totalViolations||0)}),csv="\uFEFF"+h+"\n"+rs.join("\n"),bl=new Blob([csv],{type:"text/csv"}),ur=URL.createObjectURL(bl),dl=document.createElement("a");dl.href=ur;dl.download="pab-full.csv";dl.click();URL.revokeObjectURL(ur)}
function showAllUsers(){if(!isA())return;var all=allU(),h="<h2>👥 Пользователи</h2><table style=\"width:100%;border-collapse:collapse;font-size:13px\"><tr style=\"background:#0F1218;color:#fff\"><th>Логин</th><th>ФИО</th><th>Должность</th><th>Филиал</th><th>Регион</th><th>Город</th></tr>";for(var k in all){var u=all[k];h+="<tr><td>"+k+(k==="admin"?" ⭐":"")+"</td><td>"+u.name+"</td><td>"+u.role+"</td><td>"+(u.branch||"—")+"</td><td>"+(u.region||"—")+"</td><td>"+(u.city||"—")+"</td></tr>"}h+="</table>";var w=window.open("","_blank","width=800,height=500");w.document.write("<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Пользователи</title><style>body{font:14px/1.5 Arial;max-width:800px;margin:20px auto;padding:20px}</style></head><body>"+h+"</body></html>");w.document.close()}
function downloadSummaryHTML(){var a=getA(),all=allU(),t=a.length;var w=window.open("","_blank","width=800,height=600");w.document.write("<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Отчёт ПАБ</title><style>body{font:14px/1.5 Arial;max-width:900px;margin:20px auto;padding:20px}h1{font-size:22px}h2{font-size:16px;margin-top:20px}table{width:100%;border-collapse:collapse;font-size:12px}th{background:#0F1218;color:#fff;padding:8px}td{padding:6px 8px;border-bottom:1px solid #E2E6EB}@media print{button{display:none}}</style></head><body><button onclick=\"window.print()\" style=\"padding:8px 16px;margin-bottom:16px\">🖨️ Печать</button><h1>📊 Сводный отчёт ПАБ</h1><p>Сформирован: "+new Date().toLocaleString("ru")+" | Всего аудитов: "+t+"</p><h2>📋 Аудиты</h2><table><tr><th></th><th>Дата</th><th>Место</th><th>Наблюдатель</th><th>Статус</th><th>Нарушений</th></tr>"+a.map(function(x){return"<tr><td>"+(x.number||"—")+"</td><td>"+x.date+"</td><td>"+x.location+"</td><td>"+x.observer+"</td><td>"+(x.overallSafe?"Безопасно":"Нарушения")+"</td><td>"+(x.totalViolations||0)+"</td></tr>"}).join("")+"</table></body></html>");w.document.close()}

View File

@ -4,6 +4,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Вход — ПАБ</title>
<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2/dist/umd/supabase.min.js"></script>
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{font:16px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;display:flex;align-items:center;justify-content:center;min-height:100vh;background:linear-gradient(135deg,#0F1218 0%,#1a2332 100%)}
@ -64,7 +65,9 @@ document.getElementById('lp').onkeydown=function(e){if(e.key==='Enter')document.
document.getElementById('rbb').onclick=function(){
var l=document.getElementById('rl').value.trim().toLowerCase(),p=document.getElementById('rp').value.trim(),n=document.getElementById('rn').value.trim(),em=document.getElementById('re').value.trim(),r=document.getElementById('rr').value,fr=document.getElementById('rf').value,br=document.getElementById('rb').value.trim(),dp=document.getElementById('rd').value.trim(),rg=document.getElementById('rg').value.trim(),ob=document.getElementById('ro').value.trim(),ct=document.getElementById('rc').value.trim();
var e=document.getElementById('rerr'),ok=document.getElementById('rok');ok.style.display='none';if(!l||l.length<2){e.textContent='Логин мин. 2 символа';e.style.display='block';return}if(!p||p.length<3){e.textContent='Пароль мин. 3 символа';e.style.display='block';return}if(!n){e.textContent='Укажите ФИО';e.style.display='block';return}if(!r){e.textContent='Выберите должность';e.style.display='block';return}if(!em||em.indexOf('@')<0){e.textContent='Укажите Email';e.style.display='block';return}if(!br){e.textContent='Укажите филиал';e.style.display='block';return}if(!rg){e.textContent='Укажите регион';e.style.display='block';return}if(!ct){e.textContent='Укажите город';e.style.display='block';return}if(!dp){e.textContent='Укажите подразделение';e.style.display='block';return}if(!fr){e.textContent='Укажите периодичность ПАБ';e.style.display='block';return}
var users={};try{users=JSON.parse(localStorage.getItem('pab_users')||'{}')}catch(e){}if(users[l]){e.textContent='Логин занят';e.style.display='block';return}e.style.display='none';users[l]={pass:p,name:n,email:em,role:r,freq:fr,branch:br,dept:dp,region:rg,oblast:ob,city:ct};localStorage.setItem('pab_users',JSON.stringify(users));ok.style.display='block';document.getElementById('lu').value=l;setTimeout(function(){ok.style.display='none';document.getElementById('tL').onclick()},2000)
var users={};try{users=JSON.parse(localStorage.getItem('pab_users')||'{}')}catch(e){}if(users[l]){e.textContent='Логин занят';e.style.display='block';return}e.style.display='none';users[l]={pass:p,name:n,email:em,role:r,freq:fr,branch:br,dept:dp,region:rg,oblast:ob,city:ct};localStorage.setItem('pab_users',JSON.stringify(users));
try{supabase.createClient("https://znexbjafkvyjffffbhlf.supabase.co","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InpuZXhiamFma3Z5amZmZmZiaGhsZiIsInJvbGUiOiJhbm9uIiwiaWF0IjoxNzgwNjQ0MTgyLCJleHAiOjIwOTYyMjAxODJ9.fhKixmi5Tx6yB-rRkGqHzOdepbXq3BoETdh7s__aDRk").from("users").upsert({login:l,pass:p,name:n,email:em,role:r,freq:fr,branch:br,dept:dp,region:rg,oblast:ob,city:ct})}catch(e){}
ok.style.display='block';document.getElementById('lu').value=l;setTimeout(function(){ok.style.display='none';document.getElementById('tL').onclick()},2000)
};
</script>
</body>