PageSourceSearch

https://hunters.net/assets/admin-dafkTIVf.js

js hunters.net collected 2026-10-06 04:37:28 UTC 76,293 bytes, 4 lines download raw bytes

1import{r,j as e}from"./vendor-query-DTH5f3nY.js";import{s as D,t as ye,u as F,a as q,v as G,w as oe,p as H,x as K,F as de,y as ce,z as fe,X,D as je,E as V,I as xe,J as me,K as pe,O as Ne,W as we,Q as ve,l as Y,o as W,b as re,V as ke,Y as ie,_ as Se,$ as ne,a0 as ue,M as le,e as Ce,a1 as Ae,N as $e,a2 as Z,a3 as Te,a4 as Pe}from"./vendor-ui-A_clRq2m.js";function Ee(t){return t?new Date(t).toLocaleDateString("lt-LT",{year:"numeric",month:"short",day:"numeric"}):"—"}function Le(t){if(!t)return"—";const o=new Date(t);return o.toLocaleDateString("lt-LT",{year:"numeric",month:"short",day:"numeric"})+" "+o.toLocaleTimeString("lt-LT",{hour:"2-digit",minute:"2-digit"})}function he(t){return t?t<1024?`${t} B`:t<1024*1024?`${(t/1024).toFixed(0)} KB`:`${(t/1024/1024).toFixed(1)} MB`:""}function De({kind:t}){const o=t==="link"?ce:t==="html"?fe:de;return e.jsx(o,{className:"w-4 h-4 text-primary"})}function Ie({adminKey:t}){const[o,f]=r.useState(null),[b,m]=r.useState(null),[h,w]=r.useState(null),[v,i]=r.useState(new Set),[d,u]=r.useState(!1),[j,g]=r.useState(""),[N,S]=r.useState(""),[$,k]=r.useState(!1),[T,y]=r.useState(""),[C,p]=r.useState(""),[n,x]=r.useState(!1),P={Authorization:`Bearer ${t}`},E=async()=>{try{const[a,s]=await Promise.all([fetch("/api/admin/clients",{headers:P}),fetch("/api/admin/resources",{headers:P})]);a.ok&&f(await a.json()),s.ok&&m(await s.json())}catch(a){console.error(a)}};r.useEffect(()=>{E()},[]);const U=async a=>{u(!0);try{const s=await fetch(`/api/admin/clients/${a}/resources`,{headers:P});if(s.ok){const l=await s.json();i(new Set(l))}}catch{}u(!1)};r.useEffect(()=>{h?U(h):i(new Set)},[h]);const _=async a=>{a.preventDefault(),k(!0),y(""),p("");try{const s=await fetch("/api/admin/clients",{method:"POST",headers:{...P,"Content-Type":"application/json"},body:JSON.stringify({name:j.trim(),email:N.trim()})}),l=await s.json();s.ok?(p(`Klientas sukurtas. Jis gali prisijungti per /demos su ${N.trim()}`),g(""),S(""),await E(),setTimeout(()=>p(""),6e3)):y(l.error||"Nepavyko sukurti.")}catch{y("Tinklo klaida.")}k(!1)},B=async(a,s)=>{confirm(`IÅ¡trinti klientą „${s}"? IÅ¡trinami ir visi priskyrimai.`)&&(await fetch(`/api/admin/clients/${a}`,{method:"DELETE",headers:P}),h===a&&w(null),await E())},R=async(a,s)=>{const l=await fetch(`/api/admin/clients/${a}/resend-invite`,{method:"POST",headers:P});if(l.ok)alert(`Naujas pakvietimas iÅ¡siųstas į ${s}`),await E();else{const A=await l.json().catch(()=>({}));alert(`Klaida: ${A.error||"nepavyko iÅ¡siųsti"}`)}},M=async a=>{if(!h)return;const s=v.has(a),l=new Set(v);s?l.delete(a):l.add(a),i(l);try{await fetch(`/api/admin/clients/${h}/resources/${a}`,{method:s?"DELETE":"POST",headers:P})}catch{U(h)}},z=async(a,s)=>{confirm(`IÅ¡trinti iÅ¡teklių „${s}"? Bus paÅ¡alintas iÅ¡ visų klientų.`)&&(await fetch(`/api/admin/resources/${a}`,{method:"DELETE",headers:P}),await E())},c=o?.find(a=>a.id===h)??null;return!o||!b?e.jsx("div",{className:"flex justify-center py-12",children:e.jsx(D,{className:"w-5 h-5 text-primary animate-spin"})}):e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"bg-white rounded-xl border border-gray-200 p-5",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-3",children:[e.jsx(ye,{className:"w-4 h-4 text-primary"}),e.jsx("h3",{className:"font-semibold text-gray-900 text-sm",children:"Sukurti naują klientą"})]}),e.jsxs("form",{onSubmit:_,className:"grid sm:grid-cols-[1fr_1fr_auto] gap-2",children:[e.jsx("input",{value:j,onChange:a=>{g(a.target.value),y("")},placeholder:"Vardas Pavardė",required:!0,minLength:2,"data-testid":"input-new-client-name",className:"px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/30 focus:border-primary transition"}),e.jsx("input",{type:"email",value:N,onChange:a=>{S(a.target.value),y("")},placeholder:"[email protected]",required:!0,"data-testid":"input-new-client-email",className:"px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/30 focus:border-primary transition"}),e.jsxs("button",{type:"submit",disabled:$||!j.trim()||!N.trim(),"data-testid":"button-add-client",className:"px-4 py-2 bg-primary text-white text-sm font-semibold rounded-lg hover:bg-primary/90 transition disabled:opacity-50 flex items-center gap-1.5 justify-center",children:[$?e.jsx(D,{className:"w-4 h-4 animate-spin"}):e.jsx(F,{className:"w-4 h-4"}),"Sukurti"]})]}),T&&e.jsxs("p",{className:"mt-2 text-xs text-red-600 bg-red-50 border border-red-200 rounded-lg px-3 py-2 flex items-center gap-1.5",children:[e.jsx(q,{className:"w-3.5 h-3.5 shrink-0"})," ",T]}),C&&e.jsxs("p",{className:"mt-2 text-xs text-green-700 bg-green-50 border border-green-200 rounded-lg px-3 py-2 flex items-center gap-1.5",children:[e.jsx(G,{className:"w-3.5 h-3.5 shrink-0"})," ",C]})]}),e.jsxs("div",{className:"grid lg:grid-cols-[1fr_1.3fr] gap-4",children:[e.jsxs("div",{className:"bg-white rounded-xl border border-gray-200 overflow-hidden",children:[e.jsxs("div",{className:"px-5 py-3 border-b bg-gray-50 flex items-center justify-between",children:[e.jsxs("h3",{className:"font-semibold text-gray-900 text-sm",children:["Klientai (",o.length,")"]}),e.jsx("button",{onClick:E,className:"text-gray-400 hover:text-gray-600",title:"Atnaujinti","data-testid":"button-refresh-clients",children:e.jsx(oe,{className:"w-3.5 h-3.5"})})]}),o.length===0?e.jsx("div",{className:"px-5 py-12 text-center text-sm text-gray-400",children:"Klientų dar nėra. Pridėkite pirmą virÅ¡."}):e.jsx("div",{className:"divide-y divide-gray-100 max-h-[600px] overflow-y-auto",children:o.map(a=>e.jsxs("button",{onClick:()=>w(a.id),"data-testid":`button-select-client-${a.id}`,className:`w-full text-left px-5 py-3.5 hover:bg-gray-50 transition flex items-center gap-3 ${h===a.id?"bg-primary/5 border-l-4 border-primary":"border-l-4 border-transparent"}`,children:[e.jsx("div",{className:"w-9 h-9 rounded-full bg-primary/10 flex items-center justify-center shrink-0",children:e.jsx("span",{className:"text-primary font-bold text-sm",children:a.name.charAt(0).toUpperCase()})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"font-semibold text-gray-900 text-sm truncate",children:a.name}),e.jsx("p",{className:"text-xs text-gray-500 truncate",children:a.email}),e.jsxs("div",{className:"flex items-center gap-1.5 mt-1 flex-wrap",children:[a.hasPassword?e.jsx("span",{className:"text-[10px] font-bold uppercase tracking-wider bg-green-50 text-green-700 px-1.5 py-0.5 rounded",children:"Aktyvus"}):e.jsx("span",{className:"text-[10px] font-bold uppercase tracking-wider bg-amber-50 text-amber-700 px-1.5 py-0.5 rounded",children:"Laukia"}),e.jsxs("span",{className:"text-[10px] text-gray-400",children:["Sukurta ",Ee(a.createdAt)]})]}),e.jsx("div",{className:"flex items-center gap-1.5 mt-1 flex-wrap","data-testid":`text-login-info-${a.id}`,children:a.loginCount>0?e.jsxs(e.Fragment,{children:[e.jsxs("span",{className:"text-[10px] font-semibold text-blue-700 bg-blue-50 px-1.5 py-0.5 rounded",children:[a.loginCount," prisij."]}),e.jsxs("span",{className:"text-[10px] text-gray-500",children:["paskutinį kartą ",Le(a.lastLoginAt)]})]}):e.jsx("span",{className:"text-[10px] text-gray-400",children:"Dar neprisijungė"})})]}),e.jsxs("div",{className:"flex flex-col gap-1 shrink-0",onClick:s=>s.stopPropagation(),children:[!a.hasPassword&&e.jsx("button",{onClick:()=>R(a.id,a.email),title:"Pakartotinai siųsti pakvietimą","data-testid":`button-resend-${a.id}`,className:"p-1.5 text-gray-400 hover:text-primary hover:bg-primary/10 rounded",children:e.jsx(H,{className:"w-3.5 h-3.5"})}),e.jsx("button",{onClick:()=>B(a.id,a.name),title:"IÅ¡trinti","data-testid":`button-delete-client-${a.id}`,className:"p-1.5 text-gray-400 hover:text-red-600 hover:bg-red-50 rounded",children:e.jsx(K,{className:"w-3.5 h-3.5"})})]})]},a.id))})]}),e.jsxs("div",{className:"bg-white rounded-xl border border-gray-200 overflow-hidden",children:[e.jsxs("div",{className:"px-5 py-3 border-b bg-gray-50 flex items-center justify-between gap-2",children:[e.jsx("h3",{className:"font-semibold text-gray-900 text-sm",children:c?`IÅ¡tekliai · ${c.name}`:`IÅ¡tekliai (${b.length})`}),e.jsxs("button",{onClick:()=>x(!0),"data-testid":"button-add-resource",className:"flex items-center gap-1.5 px-3 py-1.5 bg-primary text-white text-xs font-semibold rounded-lg hover:bg-primary/90 transition",children:[e.jsx(F,{className:"w-3.5 h-3.5"})," Pridėti iÅ¡teklių"]})]}),!c&&e.jsx("div",{className:"px-5 py-3 bg-blue-50 border-b border-blue-100 text-xs text-blue-800",children:"Pasirinkite klientą iÅ¡ kairės, kad galėtumėte priskirti j
1am išteklius."}),b.length===0?e.jsxs("div",{className:"px-5 py-12 text-center",children:[e.jsx(de,{className:"w-8 h-8 text-gray-300 mx-auto mb-2"}),e.jsx("p",{className:"text-sm text-gray-500 mb-3",children:"Dar nėra įkeltų išteklių."}),e.jsx("button",{onClick:()=>x(!0),className:"text-xs font-semibold text-primary hover:underline",children:"+ Įkelti pirmą"})]}):e.jsxs("div",{className:"divide-y divide-gray-100 max-h-[600px] overflow-y-auto",children:[d&&c&&e.jsxs("div",{className:"px-5 py-2 bg-gray-50 text-xs text-gray-500 flex items-center gap-2",children:[e.jsx(D,{className:"w-3 h-3 animate-spin"})," Kraunami priskyrimai…"]}),b.map(a=>{const s=v.has(a.id);return e.jsxs("div",{"data-testid":`row-resource-${a.id}`,className:"px-5 py-3 flex items-start gap-3 hover:bg-gray-50 transition",children:[e.jsx("div",{className:"w-8 h-8 rounded-lg bg-primary/10 flex items-center justify-center shrink-0 mt-0.5",children:e.jsx(De,{kind:a.kind})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"font-semibold text-gray-900 text-sm",children:a.title}),a.description&&e.jsx("p",{className:"text-xs text-gray-500 mt-0.5 line-clamp-2",children:a.description}),e.jsxs("div",{className:"flex items-center gap-2 mt-1 flex-wrap",children:[e.jsx("span",{className:"text-[10px] font-bold uppercase tracking-wider text-primary bg-primary/10 px-1.5 py-0.5 rounded",children:a.kind==="link"?"nuoroda":a.kind==="html"?"html":"failas"}),a.kind!=="link"&&a.hasContent===!1&&e.jsxs("span",{title:"Failas dingo (įkeltas iki saugojimo DB pataisos). Ištrinkite ir įkelkite iš naujo.","data-testid":`badge-broken-${a.id}`,className:"text-[10px] font-bold uppercase tracking-wider text-red-700 bg-red-50 border border-red-200 px-1.5 py-0.5 rounded inline-flex items-center gap-1",children:[e.jsx(q,{className:"w-3 h-3"})," Reikia įkelti iš naujo"]}),a.fileSize?e.jsx("span",{className:"text-[10px] text-gray-400",children:he(a.fileSize)}):null,a.url?e.jsx("span",{className:"text-[10px] text-gray-400 truncate max-w-[200px]",children:a.url}):null,a.originalFilename?e.jsx("span",{className:"text-[10px] text-gray-400 truncate max-w-[200px]",children:a.originalFilename}):null]})]}),e.jsxs("div",{className:"flex items-center gap-1.5 shrink-0",children:[c&&e.jsx("button",{onClick:()=>M(a.id),"data-testid":`button-toggle-assign-${a.id}`,className:`text-[11px] font-bold uppercase tracking-wider px-2.5 py-1.5 rounded-lg border transition flex items-center gap-1 ${s?"bg-green-50 border-green-300 text-green-700 hover:bg-green-100":"bg-white border-gray-300 text-gray-600 hover:bg-gray-100"}`,children:s?e.jsxs(e.Fragment,{children:[e.jsx(G,{className:"w-3 h-3"})," Priskirta"]}):e.jsxs(e.Fragment,{children:[e.jsx(F,{className:"w-3 h-3"})," Priskirti"]})}),e.jsx("button",{onClick:()=>z(a.id,a.title),title:"Ištrinti","data-testid":`button-delete-resource-${a.id}`,className:"p-1.5 text-gray-400 hover:text-red-600 hover:bg-red-50 rounded",children:e.jsx(K,{className:"w-3.5 h-3.5"})})]})]},a.id)})]})]})]}),n&&e.jsx(Re,{adminKey:t,onClose:()=>x(!1),onCreated:async()=>{x(!1),await E()}})]})}function Re({adminKey:t,onClose:o,onCreated:f}){const[b,m]=r.useState("upload"),[h,w]=r.useState(""),[v,i]=r.useState(""),[d,u]=r.useState(""),[j,g]=r.useState(null),[N,S]=r.useState(!1),[$,k]=r.useState(""),T=async()=>{S(!0),k("");try{if(b==="link"){const y=await fetch("/api/admin/resources/link",{method:"POST",headers:{Authorization:`Bearer ${t}`,"Content-Type":"application/json"},body:JSON.stringify({title:h.trim(),description:v.trim(),url:d.trim()})});if(!y.ok){const C=await y.json().catch(()=>({}));k(C.error||"Klaida."),S(!1);return}}else{if(!j){k("Pasirinkite failą."),S(!1);return}const y=new FormData;y.append("file",j),y.append("title",h.trim()||j.name),y.append("description",v.trim());const C=await fetch("/api/admin/resources/upload",{method:"POST",headers:{Authorization:`Bearer ${t}`},body:y});if(!C.ok){const p=await C.json().catch(()=>({}));k(`${p.error||"Įkėlimo klaida"} (HTTP ${C.status})`),S(!1);return}}f()}catch(y){k(y?.message||"Tinklo klaida.")}S(!1)};return e.jsx("div",{className:"fixed inset-0 z-50 bg-black/40 backdrop-blur-sm flex items-center justify-center p-4",onClick:o,children:e.jsxs("div",{onClick:y=>y.stopPropagation(),className:"bg-white rounded-2xl border shadow-2xl w-full max-w-lg overflow-hidden",children:[e.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b",children:[e.jsx("h3",{className:"font-bold text-gray-900",children:"Pridėti naują išteklių"}),e.jsx("button",{onClick:o,className:"text-gray-400 hover:text-gray-600","data-testid":"button-close-resource-modal",children:e.jsx(X,{className:"w-5 h-5"})})]}),e.jsxs("div",{className:"p-6 space-y-4",children:[e.jsxs("div",{className:"flex gap-2 bg-gray-100 rounded-xl p-1",children:[e.jsxs("button",{onClick:()=>m("upload"),"data-testid":"tab-mode-upload",className:`flex-1 px-3 py-2 rounded-lg text-sm font-semibold transition ${b==="upload"?"bg-white shadow text-gray-900":"text-gray-500"}`,children:[e.jsx(je,{className:"w-4 h-4 inline mr-1.5 -mt-0.5"})," Įkelti failą"]}),e.jsxs("button",{onClick:()=>m("link"),"data-testid":"tab-mode-link",className:`flex-1 px-3 py-2 rounded-lg text-sm font-semibold transition ${b==="link"?"bg-white shadow text-gray-900":"text-gray-500"}`,children:[e.jsx(ce,{className:"w-4 h-4 inline mr-1.5 -mt-0.5"})," Nuoroda"]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-semibold text-gray-700 mb-1.5",children:"Pavadinimas"}),e.jsx("input",{value:h,onChange:y=>w(y.target.value),placeholder:b==="upload"?"Pvz., Kleinanzeigen skaičiuoklė":"Pvz., Demo prezentacija","data-testid":"input-resource-title",className:"w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/30 focus:border-primary"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-semibold text-gray-700 mb-1.5",children:"Aprašymas (nebūtina)"}
1),e.jsx("textarea",{value:v,onChange:y=>i(y.target.value),rows:2,"data-testid":"input-resource-description",className:"w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/30 focus:border-primary resize-none"})]}),b==="upload"?e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-semibold text-gray-700 mb-1.5",children:"Failas (PDF, video, paveikslėlis, HTML — iki 100 MB)"}),e.jsx("input",{type:"file",accept:".html,.htm,.pdf,.png,.jpg,.jpeg,.webp,.gif,.svg,.mp4,.webm,.mov,.zip,.csv,.xlsx,.pptx,.doc,.docx,text/html,application/pdf,image/*,video/*",onChange:y=>g(y.target.files?.[0]??null),"data-testid":"input-resource-file",className:"w-full text-sm file:mr-3 file:px-3 file:py-1.5 file:border-0 file:bg-primary/10 file:text-primary file:font-semibold file:rounded-lg hover:file:bg-primary/20"}),j&&e.jsxs("p",{className:"mt-1.5 text-xs text-gray-500",children:[j.name," (",he(j.size),")"]}),e.jsx("p",{className:"mt-2 text-xs text-gray-400",children:"HTML failai bus rodomi interaktyviai (sandboxed iframe), kiti — kaip atsisiunčiami / peržiūrimi failai."})]}):e.jsxs("div",{children:[e.jsx("label",{className:"block text-xs font-semibold text-gray-700 mb-1.5",children:"Nuoroda (https://…)"}),e.jsx("input",{type:"url",value:d,onChange:y=>u(y.target.value),placeholder:"https://docs.google.com/presentation/d/…","data-testid":"input-resource-url",className:"w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/30 focus:border-primary"})]}),$&&e.jsxs("p",{className:"text-xs text-red-600 bg-red-50 border border-red-200 rounded-lg px-3 py-2 flex items-center gap-1.5",children:[e.jsx(q,{className:"w-3.5 h-3.5 shrink-0"})," ",$]})]}),e.jsxs("div",{className:"px-6 py-4 border-t bg-gray-50 flex items-center justify-end gap-2",children:[e.jsx("button",{onClick:o,className:"px-4 py-2 text-sm font-semibold text-gray-700 hover:bg-gray-200 rounded-lg transition","data-testid":"button-cancel-resource",children:"Atšaukti"}),e.jsxs("button",{onClick:T,disabled:N||b==="upload"&&!j||b==="link"&&(!d.trim()||!h.trim()),"data-testid":"button-submit-resource",className:"px-4 py-2 bg-primary text-white text-sm font-semibold rounded-lg hover:bg-primary/90 transition disabled:opacity-50 flex items-center gap-1.5",children:[N?e.jsx(D,{className:"w-4 h-4 animate-spin"}):e.jsx(F,{className:"w-4 h-4"}),"Pridėti"]})]})]})})}const ee={title:"",excerpt:"",body:"",coverImage:"",linkedinUrl:"",comments:"",published:!0,publishedAt:""};function Oe(t){return t?new Date(t).toLocaleDateString("en-US",{year:"numeric",month:"short",day:"numeric"}):"—"}function Ue(t){if(!t)return"";const o=new Date(t);if(isNaN(o.getTime()))return"";const f=o.getFullYear(),b=String(o.getMonth()+1).padStart(2,"0"),m=String(o.getDate()).padStart(2,"0");return`${f}-${b}-${m}`}function Be({adminKey:t}){const[o,f]=r.useState(null),[b,m]=r.useState(!1),[h,w]=r.useState(!1),[v,i]=r.useState(null),[d,u]=r.useState(ee),[j,g]=r.useState(!1),[N,S]=r.useState(""),[$,k]=r.useState(""),[T,y]=r.useState(!1),[C,p]=r.useState(""),[n,x]=r.useState(""),P={Authorization:`Bearer ${t}`},E=async()=>{try{const s=await fetch("/api/admin/insights",{headers:P});if(s.ok){const l=await s.json();f(Array.isArray(l)?l:[]),m(!1)}else f([]),m(!0)}catch(s){console.error(s),f([]),m(!0)}};r.useEffect(()=>{E()},[]);const U=()=>{k(""),y(!1),p(""),x("")},_=()=>{i(null),u(ee),S(""),U(),w(!0)},B=s=>{i(s.id),U(),u({title:s.title,excerpt:s.excerpt??"",body:s.body,coverImage:s.coverImage??"",linkedinUrl:s.linkedinUrl??"",comments:s.comments??"",published:s.published,publishedAt:Ue(s.publishedAt)}),S(""),w(!0)},R=()=>{w(!1),i(null),u(ee),S(""),U()},M=async()=>{const s=$.trim();if(s){y(!0),p(""),x("");try{const l=await fetch("/api/admin/insights/import",{method:"POST",headers:{...P,"Content-Type":"application/json"},body:JSON.stringify({url:s})}),A=await l.json().catch(()=>({}));if(!l.ok)p(A?.error||"Could not import from that link.");else{u(L=>({...L,title:A.title||L.title,excerpt:A.excerpt||L.excerpt,body:A.body||L.body,coverImage:A.coverImage||L.coverImage,linkedinUrl:A.linkedinUrl||s}));const I=[];A.title||I.push("title"),A.body||I.push("text"),A.coverImage||I.push("image"),x(I.length?`Filled in what LinkedIn shared. Please add the ${I.join(", ")} before publishing.`:"Filled in from LinkedIn. Review it and publish.")}}catch(l){console.error(l),p("Network error.")}y(!1)}},z=async s=>{s.preventDefault(),g(!0),S("");const l={title:d.title.trim(),excerpt:d.excerpt.trim()||null,body:d.body.trim(),coverImage:d.coverImage.trim()||null,linkedinUrl:d.linkedinUrl.trim()||null,comments:d.comments.trim()||null,published:d.published};d.publishedAt&&(l.publishedAt=new Date(`${d.publishedAt}T12:00:00`).toISOString());try{const A=v?`/api/admin/insights/${v}`:"/api/admin/insights",L=await fetch(A,{method:v?"PATCH":"POST",headers:{...P,"Content-Type":"application/json"},body:JSON.stringify(l)}),J=await L.json().catch(()=>({}));L.ok?(await E(),R()):S(J?.details?.[0]?.message||J?.error||"Couldn't save.")}catch(A){console.error(A),S("Network error.")}g(!1)},c=async s=>{await fetch(`/api/admin/insights/${s.id}`,{method:"PATCH",headers:{...P,"Content-Type":"application/json"},body:JSON.stringify({published:!s.published})}),E()},a=async s=>{confirm(`Delete "${s.title}"? This action cannot be undone.`)&&(await fetch(`/api/admin/insights/${s.id}`,{method:"DELETE",headers:P}),E())};return e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-5",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-bold text-gray-900",children:"Insights"}),e.jsx("p",{className:"text-sm text-gray-500",children:"Posts shown publicly at /insights (SEO friendly)."})]}),e.jsxs("button",{onClick:_,"data-testid":"button-new-insight",className:"inline-flex items-center gap-2 px-4 py-2 rounded-lg bg-gray-900 text-white text-sm font-semibold hover:bg-gray-800",children:[e.jsx(F,{className:"w-4 h-4"})," New post"]})]}),b&&e.jsx("div",{className:"mb-4 rounded-lg bg-red-50 border border-red-200 px-4 py-3 text-sm text-red-700","data-testid":"error-insights",children:"Couldn't load posts. Check the admin key."}),o===null?e.jsxs("div",{className:"flex items-center gap-2 text-gray-500 py-10",children:[e.jsx(D,{className:"w-4 h-4 animate-spin"})," Loading…"]}):o.length===0?e.jsx("div",{className:"text-center py-12 text-gray-500","data-testid":"empty-admin-insights",children:"No posts yet. Create your first one."}):e.jsx("div",{className:"overflow-x-auto rounded-lg border border-gray-200",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"bg-gray-50 text-gray-500 text-left",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-4 py-3 font-medium",children:"Title"}),e.jsx("th",{className:"px-4 py-3 font-medium",children:"Date"}),e.jsx("th",{className:"px-4 py-3 font-medium",children:"Status"}),e.jsx("th",{className:"px-4 py-3 font-medium text-right",children:"Actions"})]})}),e.jsx("tbody",{className:"divide-y divide-gray-100",children:o.map(s=>e.jsxs("tr",{"data-testid":`row-insight-${s.slug}`,children:[e.jsxs("td",{className:"px-4 py-3",children:[e.jsx("div",{className:"font-semibold text-gray-900",children:s.title}),e.jsxs("div",{className:"text-xs text-gray-400",children:["/insights/",s.slug]})]}),e.jsx("td",{className:"px-4 py-3 text-gray-600 whitespace-nowrap",children:Oe(s.publishedAt)}),e.jsx("td",{className:"px-4 py-3",children:e.jsxs("button",{onClick:()=>c(s),"data-testid":`button-toggle-published-${s.slug}`,className:`inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-semibold ${s.published?"bg-green-100 text-green-700":"bg-gray-100 text-gray-500"}`,children:[s.published?e.jsx(V,{className:"w-3 h-3"}):e.jsx(xe,{className:"w-3 h-3"}),s.published?"Published":"Draft"]})}),e.jsx("td",{className:"px-4 py-3",children:e.jsxs("div",{className:"flex items-center justify-end gap-1",children:[e.jsx("a",{href:`/insights/${s.slug}`,target:"_blank",rel:"noopener noreferrer",className:"p-2 rounded-lg text-gray-400 hover:text-gray-700 hover:bg-gray-100",title:"View","data-testid":`link-view-${s.slug}`,children:e.jsx(me,{className:"w-4 h-4"})}),e.jsx("button",{onClick:()=>B(s),"data-testid":`button-edit-${s.slug}`,className:"p-2 rounded-lg text-gray-400 hover:text-gray-700 hover:bg-gray-100",title:"Edit",children:e.jsx(pe,{className:"w-4 h-4"})}),e.jsx("button",{onClick:()=>a(s),"data-testid":`button-delete-${s.slug}`,className:"p-2 rounded-lg text-gray-400 hover:text-red-600 hover:bg-red-50",title:"Delete",children:e.jsx(K,{className:"w-4 h-4"})})]})})]},s.id))})]})}),h&&e.jsx("div",{className:"fixed inset-0 z-50 flex items-start justify-center bg-black/40 p-4 overflow-y-auto",children:e.jsxs("div",{className:"bg-white rounded-2xl shadow-xl w-full max-w-2xl my-8",children:[e.jsxs("div",{className:"flex items-center justify-between px-6 py-4 border-b",children:[e.jsx("h3",{className:"font-bold text-gray-900",children:v?"Edit post":"New post"}),e.jsx("button",{onClick:R,className:"p-1.5 rounded-lg text-gray-400 hover:bg-gray-100","data-testid":"button-close-form",children:e.jsx(X,{className:"w-5 h-5"})})]}),e.jsxs("form",{onSubmit:z,className:"px-6 py-5 space-y-4",children:[!v&&e.jsxs("div",{className:"rounded-xl border border-blue-200 bg-blue-50 p-4",children:[e.jsxs("label",{className:"flex items-center gap-2 text-sm font-semibold text-blue-900 mb-1.5",children:[e.jsx(Ne,{className:"w-4 h-4"})," Auto-fill from a LinkedIn post"]}),e.jsx("p",{className:"text-xs text-blue-800/80 mb-2.5",children:"Paste the link to a public LinkedIn post and we'll fill in the fields for you."}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx("input",{value:$,onChange:s=>k(s.target.value),"data-testid":"input-import-url",className:"flex-1 rounded-lg border border-blue-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500/20",placeholder:"https://www.linkedin.com/posts/…"}),e.jsxs("button",{type:"button",onClick:M,disabled:T||!$.trim(),"data-testid":"button-import-linkedin",className:"inline-flex items-center gap-2 px-4 py-2 rounded-lg bg-blue-600 text-white text-sm font-semibold hover:bg-blue-700 disabled:opacity-50 whitespace-nowrap",children:[T?e.jsx(D,{className:"w-4 h-4 animate-spin"}):e.jsx(we,{className:"w-4 h-4"}),"Auto-fill"]})]}),C&&e.jsx("p",{className:"mt-2 text-xs text-red-700","data-testid":"import-error",children:C}),n&&!C&&e.jsx("p",{className:"mt-2 text-xs text-blue-800","data-testid":"import-notice",children:n})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Title *"}),e.jsx("input",{value:d.title,onChange:s=>u({...d,title:s.target.value}),required:!0,"data-testid":"input-insight-title",className:"w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-gray-900/10",placeholder:"Post title"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Short description"}),e.jsx("textarea",{value:d.excerpt,onChange:s=>u({...d,excerpt:s.target.value}),rows:2,"data-testid":"input-insight-excerpt",className:"w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-gray-900/10",placeholder:"Shown on the list card and in search results"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Text *"}),e.jsx("textarea",{value:d.body,onChange:s=>u({...d,body:s.target.value}),required:!0,rows:10,"data-testid":"input-insight-body",className:"w-full rounded-lg border border-gray-300 px-3 py-2 text-sm font-mono focus:outline-none focus:ring-2 focus:ring-gray-900/10",placeholder:"Paste the LinkedIn post text. A blank line separates paragraphs."})]}),e.jsxs("div",{className:"grid sm:grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Image URL"}),e.jsx("input",{value:d.coverImage,onChange:s=>u({...d,coverImage:s.target.value}),"data-testid":"input-insight-cover",className:"w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-gray-900/10",placeholder:"https://…"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"LinkedIn link"}),e.jsx("input",{value:d.linkedinUrl,onChange:s=>u({...d,linkedinUrl:s.target.value}),"data-testid":"input-insight-linkedin",className:"w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-gray-900/10",placeholder:"https://www.linkedin.com/posts/…"})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Comments"}),e.jsx("textarea",{value:d.comments,onChange:s=>u({...d,comments:s.target.value}),rows:8,"data-testid":"input-insight-comments",className:"w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-gray-900/10",placeholder:"Select the whole comments section on the LinkedIn post, copy it, and paste it here as-is."}),e.jsx("p",{className:"mt-1 text-xs text-gray-400",children:"Just paste the entire LinkedIn comments block — the site automatically pulls out each person's name, role, and comment, and shows them under the article. Each name links to a LinkedIn search for that person."})]}),e.jsxs("div",{className:"sm:max-w-xs",children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Post date"}),e.jsx("input",{type:"date",value:d.publishedAt,onChange:s=>u({...d,publishedAt:s.target.value}
1),"data-testid":"input-insight-date",className:"w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-gray-900/10"}),e.jsx("p",{className:"mt-1 text-xs text-gray-400",children:"Shown on the post. Leave empty when creating to use today's date."})]}),e.jsxs("label",{className:"flex items-center gap-2 text-sm text-gray-700 cursor-pointer",children:[e.jsx("input",{type:"checkbox",checked:d.published,onChange:s=>u({...d,published:s.target.checked}),"data-testid":"checkbox-insight-published",className:"rounded border-gray-300"}),"Publish immediately (visible publicly)"]}),N&&e.jsx("div",{className:"rounded-lg bg-red-50 border border-red-200 px-3 py-2 text-sm text-red-700","data-testid":"form-error",children:N}),e.jsxs("div",{className:"flex items-center justify-end gap-2 pt-2",children:[e.jsx("button",{type:"button",onClick:R,className:"px-4 py-2 rounded-lg text-sm font-semibold text-gray-600 hover:bg-gray-100",children:"Cancel"}),e.jsxs("button",{type:"submit",disabled:j,"data-testid":"button-save-insight",className:"inline-flex items-center gap-2 px-4 py-2 rounded-lg bg-gray-900 text-white text-sm font-semibold hover:bg-gray-800 disabled:opacity-60",children:[j?e.jsx(D,{className:"w-4 h-4 animate-spin"}):e.jsx(G,{className:"w-4 h-4"}),v?"Save":"Create"]})]})]})]})})]})}const Me=/bot|crawl|spider|scan|preview|fetch|proxy|monitor|validator|python|curl|wget|java\/|libwww|barracuda|mimecast|proofpoint|symantec|trendmicro|googleimageproxy|ggpht/i;function ze(t,o){if(!t.openedAt)return!1;if(t.openUserAgent&&Me.test(t.openUserAgent)||t.sentAt&&new Date(t.openedAt).getTime()-new Date(t.sentAt).getTime()<1e4)return!0;const f=new Date(t.openedAt).getTime();return o.filter(m=>m.openedAt&&m.id!==t.id&&m.email!==t.email&&Math.abs(new Date(m.openedAt).getTime()-f)<2e3).length>=2}function Q(t){if(!t)return"—";const o=new Date(t);return o.toLocaleDateString("lt-LT",{year:"numeric",month:"short",day:"numeric"})+" "+o.toLocaleTimeString("lt-LT",{hour:"2-digit",minute:"2-digit"})}function Fe({send:t,deliveries:o}){const[f,b]=r.useState(!1),m=o.filter(i=>i.status==="sent"),h=o.filter(i=>i.status==="failed"),w=o.filter(i=>i.openedAt),v=m.length>0?Math.round(w.length/m.length*100):0;return e.jsxs("div",{className:"bg-white rounded-xl border overflow-hidden",children:[e.jsxs("button",{onClick:()=>b(i=>!i),"data-testid":`button-send-${t.periodKey}`,className:"w-full flex items-center gap-4 px-5 py-4 hover:bg-gray-50 transition-colors text-left",children:[f?e.jsx(Y,{className:"w-4 h-4 text-gray-400 shrink-0"}):e.jsx(W,{className:"w-4 h-4 text-gray-400 shrink-0"}),e.jsx("div",{className:"w-9 h-9 rounded-full bg-primary/10 flex items-center justify-center shrink-0",children:e.jsx(H,{className:"w-4 h-4 text-primary"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsxs("span",{className:"font-semibold text-gray-900 text-sm",children:["Laikotarpis ",t.periodKey]}),t.completedAt?e.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-0.5 bg-green-50 border border-green-200 text-green-700 text-xs font-semibold rounded-full",children:[e.jsx(re,{className:"w-3 h-3"})," Išsiųsta"]}):e.jsx("span",{className:"inline-flex items-center gap-1 px-2 py-0.5 bg-amber-50 border border-amber-200 text-amber-700 text-xs font-semibold rounded-full",children:"Nebaigta"})]}),e.jsxs("p",{className:"text-xs text-gray-400 mt-0.5",children:[Q(t.sentAt)," · ",t.postCount," straipsn. · ",t.recipientCount," gavėjai"]})]}),e.jsx("div",{className:"flex items-center gap-5 shrink-0 text-right",children:o.length>0&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-gray-400 mb-0.5",children:"Atidarė"}),e.jsxs("p",{className:"text-sm font-bold text-gray-900",children:[w.length," / ",m.length," ",e.jsxs("span",{className:"text-xs font-semibold text-gray-400",children:["(",v,"%)"]})]})]}),h.length>0&&e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-gray-400 mb-0.5",children:"Nepavyko"}),e.jsx("p",{className:"text-sm font-bold text-red-600",children:h.length})]})]})})]}),f&&e.jsx("div",{className:"border-t bg-gray-50 px-5 py-4",children:o.length===0?e.jsx("p",{className:"text-xs text-gray-400",children:"Šio siuntimo gavėjų sąrašas neišsaugotas (siųsta prieš įdiegiant sekimą)."}):e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"text-left text-xs text-gray-400 uppercase tracking-wider",children:[e.jsx("th",{className:"pb-2 pr-4 font-semibold",children:"Gavėjas"}),e.jsx("th",{className:"pb-2 pr-4 font-semibold",children:"El. paštas"}),e.jsx("th",{className:"pb-2 pr-4 font-semibold",children:"Būsena"}),e.jsx("th",{className:"pb-2 font-semibold",children:"Atidarė"})]})}),e.jsx("tbody",{children:o.map(i=>e.jsxs("tr",{"data-testid":`row-delivery-${i.id}`,className:"border-t border-gray-200/70",children:[e.jsx("td",{className:"py-2 pr-4 font-medium text-gray-900",children:i.name}),e.jsx("td",{className:"py-2 pr-4 text-gray-600",children:i.email}),e.jsx("td",{className:"py-2 pr-4",children:i.status==="sent"?e.jsxs("span",{className:"inline-flex items-center gap-1 text-green-700 text-xs font-semibold",children:[e.jsx(re,{className:"w-3.5 h-3.5"})," Išsiųsta"]}):i.status==="failed"?e.jsxs("span",{className:"inline-flex items-center gap-1 text-red-600 text-xs font-semibold",children:[e.jsx(ke,{className:"w-3.5 h-3.5"})," Nepavyko"]}):e.jsx("span",{className:"text-xs text-gray-400",children:i.status})}),e.jsx("td",{className:"py-2",children:i.openedAt?e.jsxs("span",{className:"inline-flex items-center gap-1 text-primary text-xs font-semibold",children:[e.jsx(V,{className:"w-3.5 h-3.5"})," ",Q(i.openedAt)]}):e.jsx("span",{className:"text-xs text-gray-400",children:"—"})})]},i.id))})]})})]})}function Ke({adminKey:t,subs:o}){const[f,b]=r.useState(!1),[m,h]=r.useState("2026-07-06"),[w,v]=r.useState("2026-07-12"),[i,d]=r.useState(new Set),[u,j]=r.useState(!1),[g,N]=r.useState(!1),[S,$]=r.useState(null),k=o.filter(p=>!p.unsubscribedAt);function T(p){d(n=>{const x=new Set(n);return x.has(p)?x.delete(p):x.add(p),x})}async function y(){j(!0),$(null);try{const p=await fetch(`/api/admin/newsletter/preview?from=${m}&to=${w}`,{headers:{Authorization:`Bearer ${t}`}});if(p.ok){const n=await p.text(),x=window.open("","_blank");x&&(x.document.write(n),x.document.close())}else{const n=await p.json().catch(()=>null);$({kind:"err",text:n?.error==="No insights published in the selected date range."?"Pasirinktame laikotarpyje nėra publikuotų straipsnių.":n?.error??"Nepavyko įkelti peržiūros."})}}catch{$({kind:"err",text:"Nepavyko įkelti peržiūros."})}j(!1)}async function C(){if(i.size===0){$({kind:"err",text:"Pasirinkite bent vieną prenumeratorių."});return}if(confirm(`Ar tikrai išsiųsti naujienlaiškį (straipsniai ${m} – ${w}) ${i.size} pasirinktiems prenumeratoriams?`)){N(!0),$(null);try{const p=await fetch("/api/admin/newsletter/send-custom",{method:"POST",headers:{Authorization:`Bearer ${t}`,"Content-Type":"application/json"},body:JSON.stringify({emails:Array.from(i),from:m,to:w})}),n=await p.json().catch(()=>null);if(p.ok){const x=[`Išsiųsta ${n?.sent??0} gavėjams (${n?.postCount??0} straipsn.)`];n?.failed>0&&x.push(`nepavyko: ${n.failed}`),Array.isArray(n?.skipped)&&n.skipped.length>0&&x.push(`praleista (atsisakę/nerasta): ${n.skipped.join(", ")}`),$({kind:"ok",text:x.join(" · ")+"."}),d(new Set)}else $({kind:"err",text:n?.error==="No insights published in the selected date range."?"Pasirinktame laikotarpyje nėra publikuotų straipsnių.":n?.error??"Siuntimas nepavyko."})}catch{$({kind:"err",text:"Siuntimas nepavyko."})}N(!1)}}return e.jsxs("div",{className:"bg-white rounded-xl border overflow-hidden",children:[e.jsxs("button",{onClick:()=>b(p=>!p),"data-testid":"button-toggle-manual-send",className:"w-full flex items-center gap-3 px-5 py-4 hover:bg-gray-50 transition-colors text-left",children:[f?e.jsx(Y,{className:"w-4 h-4 text-gray-400"}):e.jsx(W,{className:"w-4 h-4 text-gray-400"}),e.jsxs("div",{children:[e.jsx("p",{className:"font-semibold text-gray-900 text-sm",children:"Rankinis siuntimas pasirinktiems gavėjams"}),e.jsx("p",{className:"text-xs text-gray-500 mt-0.5",children:"Sudarykite naujienlaiškį iš pasirinkto laikotarpio straipsnių ir išsiųskite tik pažymėtiems prenumeratoriams."})]})]}),f&&e.jsxs("div",{className:"border-t px-5 py-4 space-y-4",children:[e.jsxs("div",{className:"flex flex-wrap items-end gap-3",children:[e.jsxs("label",{className:"text-xs text-gray-500",children:["Straipsniai nuo",e.
1jsx("input",{type:"date",value:m,onChange:p=>h(p.target.value),"data-testid":"input-manual-from",className:"block mt-1 border rounded-lg px-3 py-1.5 text-sm text-gray-900"})]}),e.jsxs("label",{className:"text-xs text-gray-500",children:["iki (imtinai)",e.jsx("input",{type:"date",value:w,onChange:p=>v(p.target.value),"data-testid":"input-manual-to",className:"block mt-1 border rounded-lg px-3 py-1.5 text-sm text-gray-900"})]}),e.jsxs("button",{onClick:y,disabled:u,"data-testid":"button-manual-preview",className:"inline-flex items-center gap-2 px-4 py-2 rounded-lg border text-sm font-semibold text-gray-700 hover:bg-gray-50 disabled:opacity-50 transition-colors",children:[u?e.jsx(D,{className:"w-4 h-4 animate-spin"}):e.jsx(V,{className:"w-4 h-4"})," Peržiūrėti šabloną"]})]}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-2",children:[e.jsxs("p",{className:"text-xs font-semibold text-gray-500 uppercase tracking-wider",children:["Gavėjai (",i.size," pasirinkta)"]}),e.jsxs("div",{className:"flex gap-3 text-xs",children:[e.jsx("button",{onClick:()=>d(new Set(k.map(p=>p.email))),"data-testid":"button-select-all",className:"text-primary font-semibold hover:underline",children:"Pažymėti visus"}),e.jsx("button",{onClick:()=>d(new Set),"data-testid":"button-select-none",className:"text-gray-500 font-semibold hover:underline",children:"Išvalyti"})]})]}),e.jsxs("div",{className:"max-h-56 overflow-y-auto border rounded-lg divide-y",children:[k.length===0&&e.jsx("p",{className:"px-4 py-3 text-sm text-gray-400",children:"Nėra aktyvių prenumeratorių."}),k.map(p=>e.jsxs("label",{className:"flex items-center gap-3 px-4 py-2 hover:bg-gray-50 cursor-pointer",children:[e.jsx("input",{type:"checkbox",checked:i.has(p.email),onChange:()=>T(p.email),"data-testid":`checkbox-recipient-${p.id}`,className:"w-4 h-4 accent-primary"}),e.jsx("span",{className:"text-sm font-medium text-gray-900",children:p.name}),e.jsx("span",{className:"text-sm text-gray-500",children:p.email})]},p.id))]})]}),e.jsxs("div",{className:"flex items-center gap-3 flex-wrap",children:[e.jsxs("button",{onClick:C,disabled:g,"data-testid":"button-manual-send",className:"inline-flex items-center gap-2 px-4 py-2 rounded-lg bg-primary text-white text-sm font-semibold hover:opacity-90 disabled:opacity-50 transition-opacity",children:[g?e.jsx(D,{className:"w-4 h-4 animate-spin"}):e.jsx(H,{className:"w-4 h-4"})," Siųsti pasirinktiems (",i.size,")"]}),S&&e.jsx("p",{"data-testid":"text-manual-send-msg",className:`text-sm ${S.kind==="ok"?"text-green-700":"text-red-600"}`,children:S.text})]})]})]})}function Ve({adminKey:t,subs:o}){const[f,b]=r.useState(null),[m,h]=r.useState([]),[w,v]=r.useState(!1),[i,d]=r.useState(null),[u,j]=r.useState(!1),[g,N]=r.useState(!1);async function S(){j(!0),d(null);try{const n=await fetch("/api/admin/newsletter/preview",{headers:{Authorization:`Bearer ${t}`}});if(n.ok){const x=await n.text(),P=window.open("","_blank");P&&(P.document.write(x),P.document.close())}else{const x=await n.json().catch(()=>null);d({kind:"err",text:x?.error??"Nepavyko įkelti peržiūros."})}}catch{d({kind:"err",text:"Nepavyko įkelti peržiūros."})}j(!1)}async function $(){if(confirm("Ar tikrai išsiųsti naujienlaiškį visiems aktyviems prenumeratoriams dabar?")){N(!0),d(null);try{const n=await fetch("/api/admin/newsletter/send-weekly",{method:"POST",headers:{Authorization:`Bearer ${t}`}}),x=await n.json().catch(()=>null);n.ok?d({kind:"ok",text:`Išsiųsta: ${x?.sent??0} gavėjams (${x?.postCount??0} straipsn., laikotarpis ${x?.periodKey??""}).`}):n.status===409?d({kind:"err",text:`Šio laikotarpio naujienlaiškis jau išsiųstas (${x?.periodKey??""}).`}):d({kind:"err",text:x?.error??(x?.status==="no-posts"?"Šiuo laikotarpiu nėra publikuotų straipsnių.":x?.status==="no-subscribers"?"Nėra aktyvių prenumeratorių.":"Siuntimas nepavyko.")})}catch{d({kind:"err",text:"Siuntimas nepavyko."})}N(!1)}}r.useEffect(()=>{let n=!1;return fetch("/api/admin/newsletter/sends",{headers:{Authorization:`Bearer ${t}`}}).then(x=>x.ok?x.json():Promise.reject()).then(x=>{n||(b(Array.isArray(x.sends)?x.sends:[]),h(Array.isArray(x.deliveries)?x.deliveries:[]))}).catch(()=>{n||(v(!0),b([]))}),()=>{n=!0}},[t]);const k=o.filter(n=>n.unsubscribedAt),T=m.filter(n=>n.status==="sent").length,y=m.filter(n=>n.openedAt).sort((n,x)=>new Date(x.openedAt).getTime()-new Date(n.openedAt).getTime()),C=y.length,p=T>0?Math.round(C/T*100):0;return f===null?e.jsx("div",{className:"flex items-center justify-center py-16 text-gray-400",children:e.jsx(D,{className:"w-5 h-5 animate-spin"})}):e.jsxs("div",{className:"space-y-6",children:[w&&e.jsx("div",{className:"rounded-xl border border-red-200 bg-red-50 px-4 py-3 text-sm text-red-700",children:"Nepavyko įkelti siuntimų istorijos."}),e.jsxs("div",{className:"bg-white rounded-xl border p-5 flex flex-wrap items-center gap-3",children:[e.jsxs("div",{className:"flex-1 min-w-[200px]",children:[e.jsx("p",{className:"font-semibold text-gray-900 text-sm",children:"Kitas naujienlaiškis"}),e.jsx("p",{className:"text-xs text-gray-500 mt-0.5",children:"Siunčiamas kas 2 savaites, pirmadienį 09:00 UTC (kitas — 2026-07-27)."})]}),e.jsxs("button",{onClick:S,disabled:u,"data-testid":"button-newsletter-preview",className:"inline-flex items-center gap-2 px-4 py-2 rounded-lg border text-sm font-semibold text-gray-700 hover:bg-gray-50 disabled:opacity-50 transition-colors",children:[u?e.jsx(D,{className:"w-4 h-4 animate-spin"}):e.jsx(V,{className:"w-4 h-4"})," Peržiūrėti"]}),e.jsxs("button",{onClick:$,disabled:g,"data-testid":"button-newsletter-send-now",className:"inline-flex items-center gap-2 px-4 py-2 rounded-lg bg-primary text-white text-sm font-semibold hover:opacity-90 disabled:opacity-50 transition-opacity",children:[g?e.jsx(D,{className:"w-4 h-4 animate-spin"}):e.jsx(H,{className:"w-4 h-4"})," Siųsti dabar"]}),i&&e.jsx("p",{"data-testid":"text-newsletter-action-msg",className:`w-full text-sm ${i.kind==="ok"?"text-green-700":"text-red-600"}`,children:i.text})]}),e.jsx(Ke,{adminKey:t,subs:o}),e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsxs("div",{className:"bg-white rounded-xl border p-5",children:[e.jsx("p",{className:"text-2xl font-black text-gray-900","data-testid":"text-total-sends",children:f.length}),e.jsx("p",{className:"text-xs text-gray-500",children:"Siuntimų"})]}),e.jsxs("div",{className:"bg-white rounded-xl border p-5",children:[e.jsxs("p",{className:"text-2xl font-black text-gray-900","data-testid":"text-open-rate",children:[C," / ",T," ",e.jsxs("span",{className:"text-sm font-bold text-gray-400",children:["(",p,"%)"]})]}),e.jsx("p",{className:"text-xs text-gray-500",children:"Atidaryta laiškų (nuo sekimo įdiegimo)"})]}),e.jsxs("div",{className:"bg-white rounded-xl border p-5",children:[e.jsx("p",{className:"text-2xl font-black text-gray-900","data-testid":"text-unsub-count",children:k.length}),e.jsx("p",{className:"text-xs text-gray-500",children:"Atsisakė prenumeratos"})]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-semibold text-gray-500 uppercase tracking-wider mb-3",children:"Kas atidarė naujienlaiškius"}),e.jsx("div",{className:"bg-white rounded-xl border overflow-hidden",children:y.length===0?e.jsx("p",{className:"px-5 py-8 text-center text-gray-400 text-sm",children:"Dar nėra užfiksuotų atidarymų"}):e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b bg-gray-50",children:[e.jsx("th",{className:"text-left px-5 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Vardas"}),e.jsx("th",{className:"text-left px-5 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"El. paštas"}),e.jsx("th",{className:"text-left px-5 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Naujienlaiškis"}),e.jsx("th",{className:"text-left px-5 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Atidarė"}),e.jsx("th",{className:"text-left px-5 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Vertinimas"})]})}),e.jsx("tbody",{children:y.map(n=>{const x=ze(n,y);return e.jsxs("tr",{"data-testid":`row-opened-${n.id}`,className:`border-b last:border-0 ${x?"opacity-60":""}`,children:[e.jsx("td",{className:"px-5 py-3 font-medium text-gray-900",children:e.jsxs("span",{className:"inline-flex items-center gap-2",children:[e.jsx(V,{className:"w-3.5 h-3.5 text-primary"})," ",n.name]})}),e.jsx("td",{className:"px-5 py-3 text-gray-600",children:n.email}),e.jsx("td",{className:"px-5 py-3 text-gray-500 text-xs",children:n.periodKey}),e.jsx("td",{className:"px-5 py-3 text-gray-900 text-xs font-semibold whitespace-nowrap",children:Q(n.openedAt)}),e.jsx("td",{className:"px-5 py-3",children:x?e.jsx("span",{"data-testid":`badge-bot-${n.id}`,title:n.openUserAgent??void 0,className:"inline-flex items-center px-2 py-0.5 bg-amber-50 border border-amber-200 text-amber-700 text-xs font-semibold rounded-full",children:"Galimai robotas"}):e.jsx("span",{"data-testid":`badge-human-${n.id}`,title:n.openUserAgent??void 0,className:"inline-flex items-center px-2 py-0.5 bg-green-50 border border-green-200 text-green-700 text-xs font-semibold rounded-full",children:"Panašu į žmogų"})})]},n.id)})})]})})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-semibold text-gray-500 uppercase tracking-wider mb-3",children:"Siuntimų istorija"}),e.jsxs("div",{className:"space-y-2",children:[f.length===0&&e.jsx("div",{className:"bg-white rounded-xl border px-5 py-12 text-center text-gray-400 text-sm",children:"Dar nebuvo nė vieno savaitinio siuntimo"}),f.map(n=>e.jsx(Fe,{send:n,deliveries:m.filter(x=>x.periodKey===n.periodKey)},n.id))]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-semibold text-gray-500 uppercase tracking-wider mb-3",children:"Atsisakę prenumeratos"}),e.jsx("div",{className:"bg-white rounded-xl border overflow-hidden",children:k.length===0?e.jsx("p",{className:"px-5 py-8 text-center text-gray-400 text-sm",children:"Niekas neatsisakė prenumeratos 🎉"}):e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b bg-gray-50",children:[e.jsx("th",{className:"text-left px-5 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Vardas"}),e.jsx("th",{className:"text-left px-5 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"El. paštas"}),e.jsx("th",{className:"text-left px-5 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Atsisakė"})]})}),e.jsx("tbody",{children:k.map(n=>e.jsxs("tr",{"data-testid":`row-unsub-${n.id}`,className:"border-b last:border-0",children:[e.jsxs("td",{className:"px-5 py-3 font-medium text-gray-900 flex items-center gap-2",children:[e.jsx(ve,{className:"w-3.5 h-3.5 text-red-400"})," ",n.name]}),e.jsx("td",{className:"px-5 py-3 text-gray-600",children:n.email}),e.jsx("td",{className:"px-5 py-3 text-gray-400 text-xs",children:Q(n.unsubscribedAt??null)})]},n.id))})]})})]}),e.jsxs("p",{className:"text-xs text-gray-400 flex items-center gap-1",children:[e.jsx(me,{className:"w-3 h-3"}),"Atidarymų sekimas veikia per nematomą paveikslėlį — dalis pašto programų blokuoja paveikslėlius, tad tikrasis atidarymų skaičius gali būti didesnis."]})]})}const ge={Exploring:"#dc2626",Building:"#ea580c",Developing:"#ca8a04",Advanced:"#16a34a",Optimised:"#2563eb"},_e={Exploring:"#fef2f2",Building:"#fff7ed",Developing:"#fefce8",Advanced:"#f0fdf4",Optimised:"#eff6ff"}
1,te=["Lead Capture","Lead Qualification","Lead Routing","Agent Performance","Follow-Up Process","CRM & Data","Monetisation","Seller Experience","Reporting & Analytics","Strategic Readiness"];function be(t){try{return JSON.parse(t)}catch{return{}}}function O(t){return/^[\s]*[=+\-@]/.test(t)||/^[\t\r\n]/.test(t)?"	"+t:t}function se(t){return new Date(t).toLocaleDateString("lt-LT",{year:"numeric",month:"short",day:"numeric"})}function ae(t){const o=new Date(t);return o.toLocaleDateString("lt-LT",{year:"numeric",month:"short",day:"numeric"})+" "+o.toLocaleTimeString("lt-LT",{hour:"2-digit",minute:"2-digit"})}function Je({s:t,index:o,total:f,adminKey:b,onChanged:m}){const[h,w]=r.useState(!1),[v,i]=r.useState(t.name),[d,u]=r.useState(t.email),[j,g]=r.useState(!1),[N,S]=r.useState(""),$={Authorization:`Bearer ${b}`,"Content-Type":"application/json"},k=async()=>{if(!(!v.trim()||!d.trim())){g(!0),S("");try{const C=await fetch(`/api/admin/subscribers/${t.id}`,{method:"PATCH",headers:$,body:JSON.stringify({name:v.trim(),email:d.trim()})});if(C.ok)w(!1),m();else{const p=await C.json().catch(()=>({}));S(C.status===409?"Toks el. paštas jau naudojamas":p.error||"Nepavyko išsaugoti")}}catch{S("Nepavyko išsaugoti")}g(!1)}},T=async()=>{if(confirm(`Ištrinti prenumeratorių ${t.email}? Šio veiksmo atšaukti negalima.`)){g(!0);try{(await fetch(`/api/admin/subscribers/${t.id}`,{method:"DELETE",headers:$})).ok&&m()}catch{}g(!1)}},y=async()=>{g(!0);try{(await fetch(`/api/admin/subscribers/${t.id}`,{method:"PATCH",headers:$,body:JSON.stringify({resubscribe:!0})})).ok&&m()}catch{}g(!1)};return h?e.jsxs("tr",{className:"border-b last:border-0 bg-blue-50/40","data-testid":`row-subscriber-${t.id}`,children:[e.jsx("td",{className:"px-5 py-3 text-gray-400 text-xs",children:f-o}),e.jsx("td",{className:"px-5 py-2",children:e.jsx("input",{value:v,onChange:C=>i(C.target.value),"data-testid":`input-edit-name-${t.id}`,className:"w-full px-2 py-1.5 border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/30"})}),e.jsxs("td",{className:"px-5 py-2",children:[e.jsx("input",{value:d,onChange:C=>u(C.target.value),"data-testid":`input-edit-email-${t.id}`,className:"w-full px-2 py-1.5 border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/30"}),N&&e.jsx("p",{className:"text-xs text-red-500 mt-1",children:N})]}),e.jsx("td",{className:"px-5 py-2",children:e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx("button",{onClick:k,disabled:j,"data-testid":`button-save-sub-${t.id}`,className:"p-1.5 rounded-lg bg-primary text-white hover:bg-primary/90 disabled:opacity-50",children:e.jsx(G,{className:"w-3.5 h-3.5"})}),e.jsx("button",{onClick:()=>{w(!1),i(t.name),u(t.email),S("")},disabled:j,"data-testid":`button-cancel-sub-${t.id}`,className:"p-1.5 rounded-lg border text-gray-500 hover:bg-gray-100",children:e.jsx(X,{className:"w-3.5 h-3.5"})})]})})]}):e.jsxs("tr",{"data-testid":`row-subscriber-${t.id}`,className:`border-b last:border-0 hover:bg-gray-50 transition-colors ${t.unsubscribedAt?"opacity-60":""}`,children:[e.jsx("td",{className:"px-5 py-3 text-gray-400 text-xs",children:f-o}),e.jsxs("td",{className:"px-5 py-3 font-medium text-gray-900",children:[t.name,t.unsubscribedAt&&e.jsx("span",{className:"ml-2 inline-flex px-2 py-0.5 bg-red-50 border border-red-200 text-red-600 text-xs font-semibold rounded-full",children:"Atsisakė"})]}),e.jsx("td",{className:"px-5 py-3 text-primary",children:t.email}),e.jsx("td",{className:"px-5 py-3",children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-gray-400 text-xs",children:se(t.createdAt)}),e.jsx("button",{onClick:()=>w(!0),disabled:j,title:"Redaguoti","data-testid":`button-edit-sub-${t.id}`,className:"p-1.5 rounded-lg text-gray-400 hover:text-primary hover:bg-blue-50",children:e.jsx(pe,{className:"w-3.5 h-3.5"})}),t.unsubscribedAt&&e.jsx("button",{onClick:y,disabled:j,title:"Grąžinti prenumeratą","data-testid":`button-resub-${t.id}`,className:"px-2 py-1 rounded-lg text-xs font-semibold text-green-700 border border-green-200 hover:bg-green-50",children:"Grąžinti"}),e.jsx("button",{onClick:T,disabled:j,title:"Ištrinti","data-testid":`button-delete-sub-${t.id}`,className:"p-1.5 rounded-lg text-gray-400 hover:text-red-600 hover:bg-red-50",children:e.jsx(K,{className:"w-3.5 h-3.5"})})]})})]})}function He({adminKey:t,onAdded:o}){const[f,b]=r.useState(!1),[m,h]=r.useState(""),[w,v]=r.useState(""),[i,d]=r.useState(!1),[u,j]=r.useState(""),g=async()=>
1{if(!(!m.trim()||!w.trim())){d(!0),j("");try{const N=await fetch("/api/admin/subscribers",{method:"POST",headers:{Authorization:`Bearer ${t}`,"Content-Type":"application/json"},body:JSON.stringify({name:m.trim(),email:w.trim()})});if(N.ok)h(""),v(""),b(!1),o();else{const S=await N.json().catch(()=>({}));j(N.status===409?"Toks prenumeratorius jau yra":S.error||"Nepavyko pridėti")}}catch{j("Nepavyko pridėti")}d(!1)}};return f?e.jsxs("div",{className:"flex items-start gap-2",children:[e.jsx("div",{children:e.jsx("input",{value:m,onChange:N=>h(N.target.value),placeholder:"Vardas","data-testid":"input-new-sub-name",className:"px-3 py-1.5 border rounded-lg text-xs focus:outline-none focus:ring-2 focus:ring-primary/30 w-36"})}),e.jsxs("div",{children:[e.jsx("input",{value:w,onChange:N=>v(N.target.value),placeholder:"[email protected]","data-testid":"input-new-sub-email",onKeyDown:N=>N.key==="Enter"&&g(),className:"px-3 py-1.5 border rounded-lg text-xs focus:outline-none focus:ring-2 focus:ring-primary/30 w-52"}),u&&e.jsx("p",{className:"text-xs text-red-500 mt-1",children:u})]}),e.jsx("button",{onClick:g,disabled:i||!m.trim()||!w.trim(),"data-testid":"button-submit-new-sub",className:"px-3 py-1.5 text-xs font-semibold bg-primary text-white rounded-lg hover:bg-primary/90 disabled:opacity-50",children:i?"...":"Pridėti"}),e.jsx("button",{onClick:()=>{b(!1),j("")},"data-testid":"button-cancel-new-sub",className:"p-1.5 rounded-lg border text-gray-500 hover:bg-gray-100",children:e.jsx(X,{className:"w-3.5 h-3.5"})})]}):e.jsxs("button",{onClick:()=>b(!0),"data-testid":"button-add-subscriber",className:"flex items-center gap-2 px-3 py-1.5 text-xs font-medium bg-primary text-white rounded-lg hover:bg-primary/90 transition-colors",children:[e.jsx(F,{className:"w-3.5 h-3.5"})," Pridėti prenumeratorių"]})}function qe({lead:t}){const o=be(t.answers);return e.jsx("div",{className:"space-y-2",children:te.map((f,b)=>{const m=o[b];if(!m)return null;const h=m.flag==="discuss",w=h?"#6b7280":m.score>=8?"#16a34a":m.score>=5?"#ca8a04":"#dc2626";return e.jsxs("div",{className:"flex gap-3",children:[e.jsx("span",{className:"shrink-0 w-5 h-5 rounded-full bg-gray-200 text-gray-600 text-xs font-bold flex items-center justify-center mt-0.5",children:b+1}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-xs text-gray-400 mb-0.5",children:f}),e.jsx("p",{className:`text-sm ${h?"text-blue-700 italic":"text-gray-800"}`,children:m.text})]}),e.jsx("span",{className:"shrink-0 text-xs font-bold",style:{color:w},children:h?"discuss":`${m.score}/10`})]},b)})})}function Ge({lead:t,index:o,total:f,adminKey:b,onDeleted:m}){const[h,w]=r.useState(!1),[v,i]=r.useState(!1),d=async g=>{if(g.stopPropagation(),!!window.confirm("IÅ¡trinti šį testo pateikimą? Å io veiksmo atÅ¡aukti negalima.")){i(!0);try{(await fetch(`/api/admin/leads/${t.id}`,{method:"DELETE",headers:{Authorization:`Bearer ${b}`}})).ok&&m()}finally{i(!1)}}},u=ge[t.level]??"#2563eb",j=_e[t.level]??"#eff6ff";return e.jsxs("div",{className:"border rounded-lg overflow-hidden bg-white",children:[e.jsxs("button",{onClick:()=>w(g=>!g),"data-testid":`button-submission-${t.id}`,className:"w-full flex items-center gap-3 px-4 py-3 hover:bg-gray-50 transition-colors text-left",children:[h?e.jsx(Y,{className:"w-3.5 h-3.5 text-gray-400 shrink-0"}):e.jsx(W,{className:"w-3.5 h-3.5 text-gray-400 shrink-0"}),e.jsxs("span",{className:"text-xs text-gray-400 font-medium w-4 shrink-0",children:["#",f-o]}),e.jsxs("div",{className:"flex-1 flex items-center gap-3 flex-wrap",children:[e.jsxs("span",{className:"inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-xs font-bold",style:{background:j,color:u},children:[e.jsx(Te,{className:"w-3 h-3"}),t.score,"% — ",t.level]}),t.status==="abandoned"&&e.jsx("span",{className:"inline-flex items-center px-2 py-0.5 bg-orange-50 border border-orange-200 text-orange-600 text-xs font-semibold rounded-full","data-testid":`badge-abandoned-${t.id}`,children:"Nebaigė"}),e.jsxs("span",{className:"flex items-center gap-1 text-xs text-gray-400",children:[e.jsx(Pe,{className:"w-3 h-3"}),ae(t.createdAt)]})]}),e.jsx("span",{role:"button",onClick:d,"data-testid":`button-delete-submission-${t.id}`,title:"IÅ¡trinti pateikimą",className:`p-1.5 rounded-lg text-gray-400 hover:text-red-600 hover:bg-red-50 shrink-0 ${v?"opacity-40 pointer-events-none":""}`,children:e.jsx(K,{className:"w-3.5 h-3.5"})})]}),h&&e.jsx("div",{className:"border-t bg-gray-50 px-4 py-4",children:e.jsx(qe,{lead:t})})]})}function Qe({emailKey:t,userLeads:o,adminKey:f,onDeleted:b}){const[m,h]=r.useState(!1),[w,v]=r.useState(!1),i=o[o.length-1],d=ge[i.level]??"#2563eb",u=o.length,j=async g=>{g.stopPropagation();const N=i.email||i.name;if(window.confirm(`IÅ¡trinti dalyvį „${N}" ir visus jo testus (${u})? Å io veiksmo atÅ¡aukti negalima.`)){v(!0);try{await Promise.all(o.map(S=>fetch(`/api/admin/leads/${S.id}`,{method:"DELETE",headers:{Authorization:`Bearer ${f}`}}))),b()}finally{v(!1)}}};return e.jsxs("div",{"data-testid":`group-${t}`,className:"bg-white rounded-xl border overflow-hidden",children:[e.jsxs("button",{onClick:()=>h(g=>!g),"data-testid":`button-group-${t}`,className:"w-full flex items-center gap-4 px-5 py-4 hover:bg-gray-50 transition-colors text-left",children:[m?e.jsx(Y,{className:"w-4 h-4 text-gray-400 shrink-0"}):e.jsx(W,{className:"w-4 h-4 text-gray-400 shrink-0"}),e.jsx("div",{className:"w-9 h-9 rounded-full bg-primary/10 flex items-center justify-center shrink-0",children:e.jsx("span",{className:"text-primary font-black text-sm",children:i.name.charAt(0).toUpperCase()})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("span",{className:"font-semibold text-gray-900 text-sm",children:i.name}),u>1&&e.jsxs("span",{className:"inline-flex items-center gap-1 px-2 py-0.5 bg-amber-50 border border-amber-200 text-amber-700 text-xs font-semibold rounded-full",children:[e.jsx(ue,{className:"w-2.5 h-2.5"}),u," testai"]}),i.status==="abandoned"&&e.jsx("span",{className:"inline-flex items-center px-2 py-0.5 bg-orange-50 border border-orange-200 text-orange-600 text-xs font-semibold rounded-full","data-testid":`badge-abandoned-group-${t}`,children:"Nebaigė"})]}),e.jsx("p",{className:"text-xs text-gray-400 mt-0.5",children:i.email||"Anonimas — be el. paÅ¡to"})]}),e.jsxs("div",{className:"flex items-center gap-3 shrink-0 text-right",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-gray-400 mb-0.5",children:"Paskutinis balas"}),e.jsxs("p",{className:"text-sm font-bold",style:{color:d},children:[i.score,"% — ",i.level]})]}),e.jsx("span",{role:"button",onClick:j,"data-testid":`button-delete-group-${t}`,title:"IÅ¡trinti dalyvį ir visus jo testus",className:`p-2 rounded-lg text-gray-400 hover:text-red-600 hover:bg-red-50 ${w?"opacity-40 pointer-events-none":""}`,children:e.jsx(K,{className:"w-4 h-4"})})]})]}),m&&e.jsxs("div",{className:"border-t bg-gray-50 px-5 py-4 space-y-2",children:[e.jsx("p",{className:"text-xs font-semibold text-gray-500 uppercase tracking-wider mb-3",children:u===1?"1 pateikimas":`${u} pateikimai — pasirinkite vieną`}),o.slice().reverse().map((g,N)=>e.jsx(Ge,{lead:g,index:N,total:u,adminKey:f,onDeleted:b},g.id))]})]})}
1function We(){const[t,o]=r.useState(""),[f,b]=r.useState(!1),[m,h]=r.useState(""),[w,v]=r.useState(!1),[i,d]=r.useState(null),[u,j]=r.useState(null),[g,N]=r.useState(null),[S,$]=r.useState(!1),[k,T]=r.useState("leads"),y=async c=>{const a={Authorization:`Bearer ${c}`},[s,l,A]=await Promise.all([fetch("/api/admin/subscribers",{headers:a}),fetch("/api/admin/leads",{headers:a}),fetch("/api/admin/calculator-sessions",{headers:a})]);if(s.status===401||l.status===401||A.status===401)return!1;const I=async L=>{try{const J=await L.json();return Array.isArray(J)?J:[]}catch{return[]}};return $(!s.ok||!l.ok||!A.ok),d(await I(s)),j(await I(l)),N(await I(A)),!0},C=async()=>{if(t.trim()){v(!0),h("");try{await y(t.trim())||h("Incorrect password.")}catch{h("Sign-in failed.")}v(!1)}},[p,n]=r.useState(!1),x=async()=>{if(t.trim()){n(!0);try{await y(t.trim())}catch{}n(!1)}},P=()=>{d(null),j(null),N(null),o("")},E=()=>{if(!g)return;const a=[["Data","IP","Profiliai","Profilis→Leadai%","Leadai→Preq%","Preq→Mandatas%","Mandatas→Parduota%","Komisiniai €","Portalo dalis %","Dabar. pajamos €","HUNTERS pajamos €","Revenue gap €","Vardas","El. paštas"],...g.map(l=>[ae(l.createdAt),O(l.ipAddress??""),String(l.profiles),String(l.profileToLeadRate),String(l.leadToPreqRate),String(l.preqToMandateRate),String(l.mandateToSoldRate),String(l.commission),l.portalShare!=null?String(l.portalShare):"",String(Math.round(l.currentRevenue)),String(Math.round(l.huntersRevenue)),String(Math.round(l.revenueGap)),O(l.name??""),O(l.email??"")])].map(l=>l.map(A=>`"${A.replace(/"/g,'""')}"`).join(",")).join(`
2`),s=document.createElement("a");s.href=URL.createObjectURL(new Blob([a],{type:"text/csv;charset=utf-8;"})),s.download="calculator-sessions.csv",s.click()},U=()=>{if(!i)return;const a=[["Vardas","El. paštas","Data"],...i.map(l=>[O(l.name),O(l.email),se(l.createdAt)])].map(l=>l.map(A=>`"${A.replace(/"/g,'""')}"`).join(",")).join(`
3`),s=document.createElement("a");s.href=URL.createObjectURL(new Blob([a],{type:"text/csv;charset=utf-8;"})),s.download="prenumeratoriai.csv",s.click()},_=()=>{if(!u)return;const a=[["Vardas","El. paštas","Balas","Lygis","Data",...te],...u.map(l=>{const A=be(l.answers);return[O(l.name),O(l.email),String(l.score),O(l.level),se(l.createdAt),...te.map((I,L)=>O(A[L]?.text??""))]})].map(l=>l.map(A=>`"${A.replace(/"/g,'""')}"`).join(",")).join(`
4`),s=document.createElement("a");s.href=URL.createObjectURL(new Blob([a],{type:"text/csv;charset=utf-8;"})),s.download="testai.csv",s.click()};if(!i||!u)return e.jsx("div",{className:"min-h-screen bg-gray-50 flex items-center justify-center p-6",children:e.jsxs("div",{className:"bg-white rounded-2xl border shadow-sm p-8 w-full max-w-sm",children:[e.jsx("div",{className:"flex justify-center mb-6",children:e.jsx("div",{className:"w-12 h-12 bg-primary rounded-xl flex items-center justify-center",children:e.jsx(ie,{className:"w-6 h-6 text-white"})})}),e.jsx("h1",{className:"text-xl font-bold text-center text-gray-900 mb-1",children:"Admin"}),e.jsx("p",{className:"text-sm text-center text-gray-500 mb-6",children:"HUNTERS · Administration"}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"relative",children:[e.jsx("input",{type:f?"text":"password",value:t,onChange:c=>{o(c.target.value),h("")},onKeyDown:c=>c.key==="Enter"&&C(),placeholder:"Password","data-testid":"input-admin-key",className:"w-full px-4 py-2.5 pr-10 border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/30 focus:border-primary transition"}),e.jsx("button",{type:"button",onClick:()=>b(!f),className:"absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600",children:f?e.jsx(xe,{className:"w-4 h-4"}):e.jsx(V,{className:"w-4 h-4"})})]}),m&&e.jsx("p",{className:"text-red-500 text-xs text-center",children:m}),e.jsx("button",{onClick:C,disabled:w||!t.trim(),"data-testid":"button-admin-login",className:"w-full py-2.5 bg-primary text-white text-sm font-semibold rounded-lg hover:bg-primary/90 transition-colors disabled:opacity-50",children:w?"Loading…":"Sign in"})]})]})});const B={};u.forEach(c=>{const a=c.email?c.email.toLowerCase():`anon:${c.id}`;B[a]||(B[a]=[]),B[a].push(c)});const R=Object.entries(B).sort((c,a)=>{const s=c[1][c[1].length-1].createdAt,l=a[1][a[1].length-1].createdAt;return new Date(l).getTime()-new Date(s).getTime()}),M=R.length,z=R.filter(([,c])=>c.length>1).length;return e.jsxs("div",{className:"min-h-screen bg-gray-50",children:[e.jsxs("div",{className:"bg-white border-b px-6 py-3 flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-8 h-8 bg-primary rounded-lg flex items-center justify-center",children:e.jsx(ie,{className:"w-4 h-4 text-white"})}),e.jsx("span",{className:"font-bold text-gray-900 text-sm",children:"HUNTERS Admin"})]}),e.jsxs("button",{onClick:x,disabled:p,"data-testid":"button-refresh",className:"flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium text-gray-600 border rounded-lg hover:bg-gray-100 transition-colors disabled:opacity-50",children:[e.jsx(oe,{className:`w-3.5 h-3.5 ${p?"animate-spin":""}`})," Atnaujinti"]}),e.jsxs("button",{onClick:P,"data-testid":"button-logout",className:"flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium text-gray-600 border rounded-lg hover:bg-gray-100 transition-colors",children:[e.jsx(Se,{className:"w-3.5 h-3.5"})," Išeiti"]})]}),e.jsxs("div",{className:"max-w-5xl mx-auto p-6",children:[S&&e.jsxs("div",{"data-testid":"banner-fetch-error",className:"mb-4 flex items-center gap-2 rounded-xl border border-red-200 bg-red-50 px-4 py-3 text-sm text-red-700",children:[e.jsx(q,{className:"w-4 h-4 shrink-0"}),e.jsx("span",{children:'Nepavyko įkelti dalies duomenų — serverio klaida. Rodomi sąrašai gali būti nepilni. Pabandykite „Atnaujinti" arba bandykite vėliau.'})]}),e.jsxs("div",{className:"grid grid-cols-3 gap-4 mb-6",children:[e.jsxs("div",{className:"bg-white rounded-xl border p-5 flex items-center gap-4",children:[e.jsx("div",{className:"w-10 h-10 rounded-lg bg-blue-50 flex items-center justify-center",children:e.jsx(ne,{className:"w-5 h-5 text-primary"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-black text-gray-900",children:M}),e.jsx("p",{className:"text-xs text-gray-500",children:"Unikalūs dalyviai"})]})]}),e.jsxs("div",{className:"bg-white rounded-xl border p-5 flex items-center gap-4",children:[e.jsx("div",{className:"w-10 h-10 rounded-lg bg-amber-50 flex items-center justify-center",children:e.jsx(ue,{className:"w-5 h-5 text-amber-600"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-black text-gray-900",children:u.length}),e.jsxs("p",{className:"text-xs text-gray-500",children:["Pateikimų iš viso",z>0?` · ${z} pildė kelis kartus`:""]})]})]}),e.jsxs("div",{className:"bg-white rounded-xl border p-5 flex items-center gap-4",children:[e.jsx("div",{className:"w-10 h-10 rounded-lg bg-blue-50 flex items-center justify-center",children:e.jsx(le,{className:"w-5 h-5 text-primary"})}),e.jsxs("div",{children:[e.jsx("p",{className:"text-2xl font-black text-gray-900",children:i.length}),e.jsx("p",{className:"text-xs text-gray-500",children:"Prenumeratorių"})]})]})]}),e.jsxs("div",{className:"flex gap-1 mb-4 bg-gray-100 rounded-xl p-1 w-fit",children:[e.jsxs("button",{onClick:()=>T("leads"),"data-testid":"tab-leads",className:`flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-semibold transition-all ${k==="leads"?"bg-white shadow text-gray-900":"text-gray-500 hover:text-gray-700"}`,children:[e.jsx(ne,{className:"w-4 h-4"})," Testai (",M," dalyvių)"]}),e.jsxs("button",{onClick:()=>T("subs"),"data-testid":"tab-subscribers",className:`flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-semibold transition-all ${k==="subs"?"bg-white shadow text-gray-900":"text-gray-500 hover:text-gray-700"}`,children:[e.jsx(le,{className:"w-4 h-4"})," Prenumeratoriai (",i.length,")"]}),e.jsxs("button",{onClick:()=>T("calc"),"data-testid":"tab-calc",className:`flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-semibold transition-all ${k==="calc"?"bg-white shadow text-gray-900":"text-gray-500 hover:text-gray-700"}`,children:[e.jsx(Ce,{className:"w-4 h-4"})," Skaičiuoklė (",g?.length??0,")"]}),e.jsxs("button",{onClick:()=>T("clients"),"data-testid":"tab-clients",className:`flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-semibold transition-all ${k==="clients"?"bg-white shadow text-gray-900":"text-gray-500 hover:text-gray-700"}`,children:[e.jsx(Ae,{className:"w-4 h-4"})," Klientai (Demos)"]}),e.jsxs("button",{onClick:()=>T("insights"),"data-testid":"tab-insights",className:`flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-semibold transition-all ${k==="insights"?"bg-white shadow text-gray-900":"text-gray-500 hover:text-gray-700"}`,children:[e.jsx($e,{className:"w-4 h-4"})," Insights"]}),e.jsxs("button",{onClick:()=>T("newsletter"),"data-testid":"tab-newsletter",className:`flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-semibold transition-all ${k==="newsletter"?"bg-white shadow text-gray-900":"text-gray-500 hover:text-gray-700"}`,children:[e.jsx(H,{className:"w-4 h-4"})," Naujienlaiškiai"]})]}),k==="clients"&&e.jsx(Ie,{adminKey:t}),k==="insights"&&e.jsx(Be,{adminKey:t}),k==="newsletter"&&e.jsx(Ve,{adminKey:t,subs:i}),k==="leads"&&e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-3",children:[e.jsx("p",{className:"text-xs text-gray-500",children:"Paspauskite ant dalyvio, kad matytumėte jo testus · paspauskite ant testo, kad matytumėte atsakymus"}),e.jsxs("button",{onClick:_,"data-testid":"button-export-leads-csv",className:"flex items-center gap-2 px-3 py-1.5 text-xs font-medium border rounded-lg hover:bg-gray-100 transition-colors",children:[e.jsx(Z,{className:"w-3.5 h-3.5"})," Eksportuoti CSV"]})]}),e.jsxs("div",{className:"space-y-2",children:[R.map(([c,a])=>e.jsx(Qe,{emailKey:c,userLeads:a,adminKey:t,onDeleted:x},c)),R.length===0&&e.jsx("div",{className:"bg-white rounded-xl border px-5 py-12 text-center text-gray-400 text-sm",children:"Dar nėra testo pateikimų"})]})]}),k==="subs"&&e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-start justify-between mb-3 gap-3 flex-wrap",children:[e.jsxs("p",{className:"text-xs text-gray-500 pt-1.5",children:[i.length," prenumeratorių iš viso",i.some(c=>c.unsubscribedAt)?` · ${i.filter(c=>c.unsubscribedAt).length} atsisakė`:""]}),e.jsxs("div",{className:"flex items-start gap-2",children:[e.jsx(He,{adminKey:t,onAdded:x}),e.jsxs("button",{onClick:U,"data-testid":"button-export-csv",className:"flex items-center gap-2 px-3 py-1.5 text-xs font-medium border rounded-lg hover:bg-gray-100 transition-colors",children:[e.jsx(Z,{className:"w-3.5 h-3.5"})," Eksportuoti CSV"]})]})]}),e.jsx("div",{className:"bg-white rounded-xl border overflow-hidden",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b bg-gray-50",children:[e.jsx("th",{className:"text-left px-5 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"#"}),e.jsx("th",{className:"text-left px-5 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Vardas"}),e.jsx("th",{className:"text-left px-5 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"El. paštas"}),e.jsx("th",{className:"text-left px-5 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Data"})]})}),e.jsx("tbody",{children:i.length===0?e.jsx("tr",{children:e.jsx("td",{colSpan:4,className:"px-5 py-10 text-center text-gray-400",children:"Prenumeratorių nėra"})}):i.slice().reverse().map((c,a)=>e.jsx(Je,{s:c,index:a,total:i.length,adminKey:t,onChanged:x},c.id))})]})})]}),k==="calc"&&g!==null&&e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-3",children:[e.jsxs("p",{className:"text-xs text-gray-500",children:[g.length,' sesijų — kai vartotojas spaudžia „Show me the HUNTERS result"']}),e.jsxs("button",{onClick:E,"data-testid":"button-export-calc-csv",className:"flex items-center gap-2 px-3 py-1.5 text-xs font-medium border rounded-lg hover:bg-gray-100 transition-colors",children:[e.jsx(Z,{className:"w-3.5 h-3.5"})," Eksportuoti CSV"]})]}),e.jsx("div",{className:"bg-white rounded-xl border overflow-hidden overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm min-w-[900px]",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b bg-gray-50",children:[e.jsx("th",{className:"text-left px-4 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Data"}),e.jsx("th",{className:"text-right px-4 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Profiliai"}),e.jsx("th",{className:"text-right px-4 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"P→L%"}),e.jsx("th",{className:"text-right px-4 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"L→Q%"}),e.jsx("th",{className:"text-right px-4 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Q→M%"}),e.jsx("th",{className:"text-right px-4 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"M→S%"}),e.jsx("th",{className:"text-right px-4 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Komisiniai"}),e.jsx("th",{className:"text-right px-4 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Portalo %"}),e.jsx("th",{className:"text-right px-4 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Rev. Gap"}),e.jsx("th",{className:"text-left px-4 py-3 text-xs font-semibold text-gray-500 uppercase tracking-wider",children:"Kontaktas"})]})}),e.jsx("tbody",{children:g.length===0?e.jsx("tr",{children:e.jsx("td",{colSpan:10,className:"px-5 py-10 text-center text-gray-400",children:"Sesijų nėra"})}):g.map((c,a)=>{const s=c.email||c.name,l=Math.round(c.revenueGap),A=l>0?"#16a34a":"#dc2626";return e.jsxs("tr",{"data-testid":`row-calc-${c.id}`,className:"border-b last:border-0 hover:bg-gray-50 transition-colors",children:[e.jsx("td",{className:"px-4 py-3 text-gray-400 text-xs whitespace-nowrap",children:ae(c.createdAt)}),e.jsx("td",{className:"px-4 py-3 text-right text-gray-700 tabular-nums",children:c.profiles.toLocaleStr
4ing()}),e.jsxs("td",{className:"px-4 py-3 text-right text-gray-600 tabular-nums text-xs",children:[c.profileToLeadRate,"%"]}),e.jsxs("td",{className:"px-4 py-3 text-right text-gray-600 tabular-nums text-xs",children:[c.leadToPreqRate,"%"]}),e.jsxs("td",{className:"px-4 py-3 text-right text-gray-600 tabular-nums text-xs",children:[c.preqToMandateRate,"%"]}),e.jsxs("td",{className:"px-4 py-3 text-right text-gray-600 tabular-nums text-xs",children:[c.mandateToSoldRate,"%"]}),e.jsxs("td",{className:"px-4 py-3 text-right text-gray-600 tabular-nums text-xs",children:["€",c.commission.toLocaleString()]}),e.jsx("td",{className:"px-4 py-3 text-right text-gray-600 tabular-nums text-xs",children:c.portalShare!=null?`${c.portalShare}%`:"—"}),e.jsxs("td",{className:"px-4 py-3 text-right tabular-nums font-semibold text-xs",style:{color:A},children:[l>=0?"+":"","€",Math.abs(l).toLocaleString()]}),e.jsx("td",{className:"px-4 py-3 text-xs",children:s?e.jsxs("div",{children:[c.name&&e.jsx("p",{className:"font-medium text-gray-900",children:c.name}),c.email&&e.jsx("p",{className:"text-primary",children:c.email})]}):e.jsx("span",{className:"text-gray-300 italic",children:"—"})})]},c.id)})})]})})]})]})]})}export{We as default};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.