Admin Inbox Admin

function getCookie(n){for(var c=document.cookie.split(";"),i=0;inull);t&&t.email&&(document.getElementById("headerEmail").textContent=t.email),document.getElementById("authGate").style.display="none",document.getElementById("mainContent").style.display="block",await y()}catch{v()}}function v(){document.getElementById("authGate").style.display="block",document.getElementById("mainContent").style.display="none"}async function h(t,e={}){if(x==="__cookie__"){return fetch(t,{...e,credentials:"include",headers:{"Content-Type":"application/json",...e.headers||{}}}).then(function(r){return r.json()}).then(function(n){if(!n.success)throw new Error(n.message||"API error");return n})}return fetch(t,{...e,headers:{...e.headers||{},"Content-Type":"application/json",Authorization:"Bearer "+x}}).then(function(r){return r.json()}).then(function(n){if(!n.success)throw new Error(n.message||"API error");return n})}async function y(t=!1){t&&(i=[],l=0),document.getElementById("inboxList").innerHTML='
Loading messages\u2026
';try{const e=new URLSearchParams({limit:500,offset:0,type:$,status:B}),n=await h("/api/admin/inbox?"+e);i=n.messages||[],f=n.total||0,d=n.unread||0,m=n.new_count||0,w=i.filter(s=>s.status==="resolved").length,k(),l=0,b()}catch(e){document.getElementById("inboxList").innerHTML=`

Failed to load inbox: ${e.message}

`}}function k(){document.getElementById("statTotal").textContent=f,document.getElementById("statUnread").textContent=d,document.getElementById("statNew").textContent=m,document.getElementById("statResolved").textContent=w;const t=document.getElementById("headerUnread");d>0?(t.textContent=d,t.style.display="inline-block"):t.style.display="none"}function b(){const t=document.getElementById("inboxList");if(i.length===0){t.innerHTML=`

No messages yet

`,document.getElementById("loadMoreWrap").style.display="none",document.getElementById("resultCount").textContent="";return}const e=i.slice(l,l+L);l+=e.length,l===e.length&&(t.innerHTML=""),e.forEach(s=>{t.appendChild(M(s))});const n=i.length-l;document.getElementById("loadMoreWrap").style.display=n>0?"block":"none",document.getElementById("resultCount").textContent=`Showing ${l} of ${i.length} messages`}function S(){b()}function M(t){const e=document.createElement("div"),n=!t.is_read,s=t.status==="resolved";e.className=`msg-card source-${t.source} ${n?"unread":""} ${s?"resolved":""}`,e.dataset.id=t.id,e.dataset.source=t.source;const r=t.sender_name||"",o=t.sender_email||"Anonymous",c=_(t.created_at),a=t.subject||"",p=t.message||"",T=p.length>200?p.slice(0,200)+"\u2026":p,H=t.source==="contact"?'Support':'Feedback',u=t.message_type||"";let E="";u&&u!=="feedback"&&(E=`${u==="bug_report"?' Bug':u==="support"?' Support':u}`);const A={new:' New',read:' Read',resolved:' Resolved'},N=`${A[t.status]||' New'}`,R=t.rating?`${t.rating>=4?'':t.rating>=2?'':''} ${t.rating}/5`:"",U=n?'\u25CF Unread':"";return e.innerHTML=`
${g(r)} ${g(o)}
${c}
${a?`
${g(a)}
`:""}
${g(T)}
${g(p)}
`,e.addEventListener("click",z=>{z.target.tagName!=="BUTTON"&&(document.getElementById(`body-${t.source}-${t.id}`).classList.toggle("open"),t.is_read||C(null,t.source,t.id,null))}),e}async function C(t,e,n,s){t&&t.stopPropagation();try{await h(`/api/admin/inbox/${e}/${n}/read`,{method:"PATCH"});const r=i.find(c=>c.source===e&&c.id===n);r&&(r.is_read=!0);const o=document.querySelector(`.msg-card[data-id="${n}"][data-source="${e}"]`);o&&o.classList.remove("unread"),s&&s.remove(),d=Math.max(0,d-1),k()}catch(r){console.error("markRead error",r)}}async function j(t,e,n,s,r){t&&t.stopPropagation();try{await h(`/api/admin/inbox/${e}/${n}/status`,{method:"PATCH",body:JSON.stringify({status:s})});const o=i.find(a=>a.source===e&&a.id===n);o&&(o.status=s,o.is_read=s!=="new");const c=document.querySelector(`.msg-card[data-id="${n}"][data-source="${e}"]`);if(c&&o){const a=M(o);c.replaceWith(a)}w=i.filter(a=>a.status==="resolved").length,m=i.filter(a=>a.status==="new").length,d=i.filter(a=>!a.is_read).length,k()}catch(o){console.error("setStatus error",o)}}window.setTypeFilter=function(t,e){$=e,document.querySelectorAll("[data-type]").forEach(n=>n.classList.remove("active")),t.classList.add("active"),y(!0)},window.setStatusFilter=function(t,e){B=e,document.querySelectorAll("[data-status]").forEach(n=>n.classList.remove("active")),t.classList.add("active"),y(!0)},window.loadMore=S,window.markRead=C,window.setStatus=j;function _(t){if(!t)return"";const e=new Date(t),n=new Date-e,s=Math.floor(n/6e4),r=Math.floor(n/36e5),o=Math.floor(n/864e5);return s<1?"just now":s<60?`${s}m ago`:r<24?`${r}h ago`:o<7?`${o}d ago`:e.toLocaleDateString("en-US",{month:"short",day:"numeric",year:o>365?"numeric":void 0})}function g(t){return t?String(t).replace(/&/g,"&").replace(//g,">").replace(/"/g,"""):""}I()})();