import"./hoisted.yFz1BYXO.js";const V=window.__PW_API||"https://api.performancewest.net",O="pw_admin_token",U="pw_admin_user",Y=document.getElementById("login-screen"),W=document.getElementById("dashboard-screen"),le=document.getElementById("login-form"),k=document.getElementById("login-error"),f=document.getElementById("login-btn"),ce=document.getElementById("logout-btn"),me=document.getElementById("refresh-btn"),G=document.getElementById("refresh-indicator"),ge=document.getElementById("admin-user"),ue=document.getElementById("stats-bar"),p=document.getElementById("orders-tbody"),S=document.getElementById("orders-loading"),x=document.getElementById("orders-empty"),K=document.getElementById("pagination"),pe=document.getElementById("pagination-info"),Q=document.getElementById("prev-btn"),X=document.getElementById("next-btn"),$=document.getElementById("detail-panel"),ye=document.getElementById("detail-title"),T=document.getElementById("detail-loading"),z=document.getElementById("detail-content"),be=document.getElementById("detail-close"),u=document.getElementById("action-feedback"),C=document.getElementById("filter-status"),P=document.getElementById("filter-automation"),N=document.getElementById("filter-priority"),M=document.getElementById("filter-assigned"),fe=document.getElementById("apply-filters-btn"),xe=document.getElementById("clear-filters-btn"),Z=document.getElementById("action-status"),ee=document.getElementById("action-automation"),te=document.getElementById("action-priority"),F=document.getElementById("action-note"),he=document.getElementById("action-assign"),H=document.getElementById("action-standalone-note"),ve=document.getElementById("action-add-note");let c=1;const h=25;let v=0,i=null,ne=Date.now(),j=null,D=null;function ae(){return localStorage.getItem(O)}function Ee(e,t){localStorage.setItem(O,e),localStorage.setItem(U,t)}function oe(){localStorage.removeItem(O),localStorage.removeItem(U)}function re(){return localStorage.getItem(U)||"admin"}async function y(e,t={}){const o=ae(),n={"Content-Type":"application/json",...t.headers||{}};o&&(n.Authorization=`Bearer ${o}`);const a=await fetch(`${V}${e}`,{...t,headers:n});if(a.status===401)throw oe(),q(),new Error("Session expired. Please log in again.");if(!a.ok){const d=await a.json().catch(()=>({}));throw new Error(d.error||d.message||`API error: ${a.status}`)}return a.json()}function s(e){const t=document.createElement("div");return t.textContent=e,t.innerHTML}function $e(e){return e?new Date(e).toLocaleDateString("en-US",{month:"short",day:"numeric",year:"numeric"}):"—"}function Ie(e){return e?new Date(e).toLocaleString("en-US",{month:"short",day:"numeric",hour:"numeric",minute:"2-digit"}):"—"}function Le(e){const t=Math.floor((Date.now()-e)/1e3);if(t<5)return"just now";if(t<60)return`${t}s ago`;const o=Math.floor(t/60);return o<60?`${o}m ago`:`${Math.floor(o/60)}h ${o%60}m ago`}const Be={received:"bg-gray-100 text-gray-700",processing:"bg-blue-100 text-blue-700",submitted:"bg-indigo-100 text-indigo-700",filed:"bg-green-100 text-green-700",delivered:"bg-emerald-100 text-emerald-800",cancelled:"bg-red-100 text-red-700"},we={pending:"bg-gray-100 text-gray-700",running:"bg-blue-100 text-blue-700",succeeded:"bg-green-100 text-green-700",failed:"bg-red-100 text-red-700",manual:"bg-amber-100 text-amber-700"},_e={low:"bg-gray-100 text-gray-600",normal:"bg-blue-100 text-blue-700",high:"bg-amber-100 text-amber-700",urgent:"bg-red-100 text-red-700"};function A(e,t){return`${s(e)}`}function q(){Y.classList.remove("hidden"),W.classList.add("hidden"),$.classList.add("hidden"),i=null,j&&clearInterval(j),D&&clearInterval(D)}function se(){Y.classList.add("hidden"),W.classList.remove("hidden"),ge.textContent=re(),I(),g(),j=setInterval(()=>{I()},3e4),D=setInterval(()=>{G.textContent=`Last refreshed: ${Le(ne)}`},5e3)}le.addEventListener("submit",async e=>{e.preventDefault(),k.classList.add("hidden"),f.disabled=!0,f.textContent="Signing in...";const t=document.getElementById("login-username").value.trim(),o=document.getElementById("login-password").value;try{const n=await fetch(`${V}/api/v1/admin/login`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({username:t,password:o})});if(!n.ok){const d=await n.json().catch(()=>({}));throw new Error(d.error||d.message||"Invalid credentials")}const a=await n.json();Ee(a.token,a.user?.username||t),se()}catch(n){k.textContent=n.message||"Login failed",k.classList.remove("hidden")}finally{f.disabled=!1,f.textContent="Sign In"}});ce.addEventListener("click",()=>{oe(),q()});async function I(){try{const e=await y("/api/v1/admin/stats");ne=Date.now(),G.textContent="Last refreshed: just now";const t=[{key:"received",label:"Received",color:"border-gray-300 bg-white"},{key:"processing",label:"Processing",color:"border-blue-300 bg-blue-50"},{key:"submitted",label:"Submitted",color:"border-indigo-300 bg-indigo-50"},{key:"filed",label:"Filed",color:"border-green-300 bg-green-50"},{key:"delivered",label:"Delivered",color:"border-emerald-300 bg-emerald-50"},{key:"automation_failed",label:"Auto Failed",color:"border-red-300 bg-red-50"},{key:"manual_required",label:"Manual Req",color:"border-amber-300 bg-amber-50"},{key:"urgent",label:"Urgent",color:"border-red-400 bg-red-50"},{key:"unassigned",label:"Unassigned",color:"border-red-300 bg-red-50"}];ue.innerHTML=t.map(o=>{const n=e[o.key]??0,a=["automation_failed","urgent","unassigned"].includes(o.key)&&n>0?"text-red-700 font-bold":["filed","delivered"].includes(o.key)?"text-green-700":["processing"].includes(o.key)?"text-blue-700":"text-gray-900";return`
Name: ${s(a.name||n.customer_name||"—")}
Email: ${s(a.email||n.customer_email||"—")}
Phone: ${s(a.phone||n.customer_phone||"—")}
`,document.getElementById("detail-entity").innerHTML=`Name: ${s(n.entity_name||"—")}
Type: ${s(n.entity_type||"—")}
State: ${s(n.state||"—")}
Purpose: ${s(n.purpose||n.entity?.purpose||"—")}
`;const d=n.principal_address||n.principal||{},w=n.mailing_address||n.mailing||{};document.getElementById("detail-addresses").innerHTML=`Principal: ${o(d)}
Mailing: ${o(w)}
`;const b=n.members||[];document.getElementById("detail-members").innerHTML=b.length===0?"None listed":b.map((r,_)=>`${r.role||`Member ${_+1}`}: ${s(r.name||"—")} ${r.title?`(${s(r.title)})`:""}
`).join("");const R=n.addons||[],m=n.pricing||{};let l="";m.base_price!=null&&(l+=`Base Price: $${Number(m.base_price).toFixed(2)}
`),m.state_fee!=null&&(l+=`State Fee: $${Number(m.state_fee).toFixed(2)}
`),R.length>0&&(l+='Add-Ons:
Total: $${Number(m.total).toFixed(2)}
`),m.stripe_payment_id&&(l+=`Stripe: ${s(m.stripe_payment_id)}
`),l||(l="No pricing data"),document.getElementById("detail-pricing").innerHTML=l;const de=n.audit_log||[],ie=n.notes||[],J=[...de.map(r=>({ts:r.timestamp||"",text:`[${r.action||"action"}] ${r.details||""}`,user:r.user||""})),...ie.map(r=>({ts:r.timestamp||"",text:r.text||"",user:r.user||""}))].sort((r,_)=>new Date(_.ts).getTime()-new Date(r.ts).getTime());document.getElementById("detail-audit").innerHTML=J.length===0?"No audit entries":J.map(r=>`