1import{r,K as e,aV as A,bP as z,a7 as D,c3 as P,fx as V,ap as W,by as L,ah as B,ax as M,b9 as b}from"./vendor-DFiwhBVU.js";import{a as q,B as m,I as E,T as F,s as O}from"./index-BAPYoh1K.js";import{C as G}from"./card-DQ6Kozk9.js";import{B as H}from"./badge-BWBaE3uL.js";import{S as J,a as K,b as Q,c as X,d as T}from"./select-CC1D6Zwp.js";import{S as Y,a as Z,c as ee,d as ae}from"./sheet-BJGh7GUr.js";import"./supabase-D-Qt6SjD.js";const S=["new","in_review","approved","declined","on_hold"],x={new:"New",in_review:"In review",approved:"Approved",declined:"Declined",on_hold:"On hold"},y=o=>new Date(o).toLocaleString("en-GB",{day:"2-digit",month:"short",year:"numeric",hour:"2-digit",minute:"2-digit"});function oe(){const{isOwnerOrAdmin:o,loading:p}=q(),[u,C]=r.useState([]),[f,_]=r.useState(!0),[j,$]=r.useState(""),[d,g]=r.useState("all"),[s,v]=r.useState(null),k=async()=>{_(!0);const{data:a,error:t}=await O.from("affiliate_applications").select("*").order("created_at",{ascending:!1});if(_(!1),t){b.error("Could not load applications",{description:t.message});return}C(a||[])};r.useEffect(()=>{o&&k()},[o]);const R=r.useMemo(()=>{const a={all:u.length};for(const t of u)a[t.status]=(a[t.status]||0)+1;return a},[u]),h=r.useMemo(()=>{const a=j.trim().toLowerCase();return u.filter(t=>d!=="all"&&t.status!==d?!1:a?[t.name,t.email,t.phone,t.company,t.website,t.reference_number].filter(Boolean).some(n=>String(n).toLowerCase().includes(a)):!0)},[u,j,d]),w=async(a,t)=>{const{error:n}=await O.from("affiliate_applications").update(t).eq("id",a);return n?(b.error("Save failed",{description:n.message}),!1):(C(i=>i.map(l=>l.id===a?{...l,...t}:l)),v(i=>i&&i.id===a?{...i,...t}:i),!0)},I=async(a,t)=>{await w(a,{status:t,reviewed_at:new Date().toISOString()})&&b.success("Marked as ".concat(x[t]))},U=()=>{const t=[["Received","Name","Company","Email","Phone","Website","Status","Source","Reference"].join(",")].concat(h.map(l=>[y(l.created_at),l.name,l.company,l.email,l.phone,l.website,x[l.status]||l.status,l.source_page,l.reference_number].map(N=>'"'.concat(String(N!=null?N:"").replace(/"/g,'""'),'"')).join(","))),n=URL.createObjectURL(new Blob([t.join("\n")],{type:"text/csv"})),i=document.createElement("a");i.href=n,i.download="affiliate-applications-".concat(new Date().toISOString().slice(0,10),".csv"),i.click(),URL.revokeObjectURL(n)};return p?e.jsx("div",{className:"p-8 flex justify-center",children:e.jsx(A,{className:"w-5 h-5 animate-spin"})}):o?e.jsxs("div",{className:"p-4 md:p-6 space-y-5 max-w-7xl mx-auto",children:[e.jsxs("header",{className:"flex flex-wrap items-end justify-between gap-3",children:[e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2 text-xs uppercase tracking-wider text-muted-foreground",children:[e.jsx(D,{className:"w-3.5 h-3.5"})," Admin only"]}),e.jsx("h1",{className:"text-2xl md:text-3xl font-semibold tracking-tight",children:"Affiliate applications"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Applications submitted through the affiliate programme page."})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(m,{variant:"outline",size:"sm",onClick:U,disabled:h.length===0,children:[e.jsx(P,{className:"w-4 h-4 mr-2"})," Export"]}),e.jsxs(m,{variant:"outline",size:"sm",onClick:k,disabled:f,children:[e.jsx(V,{className:"w-4 h-4 mr-2 ".concat(f?"animate-spin":"")})," Refresh"]})]})]}),e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-6 gap-2",children:[e.jsxs("button",{onClick:()=>g("all"),className:"text-left rounded-xl p-3 ring-1 ring-border/60 hover:bg-muted/40 transition ".concat(d==="all"?"bg-muted/60":""),children:[e.jsx("div",{className:"text-[10px] uppercase tracking-wider text-muted-foreground",children:"All"}),e.jsx("div",{className:"text-2xl font-semibold",children:R.all})]}),S.map(a=>e.jsxs("button",{onClick:()=>g(a),className:"text-left rounded-xl p-3 ring-1 ring-border/60 hover:bg-muted/40 transition ".concat(d===a?"bg-muted/60":""),children:[e.jsx("div",{className:"text-[10px] uppercase tracking-wider text-muted-foreground",children:x[a]}),e.jsx("div",{className:"text-2xl font-semibold",children:R[a]||0})]},a))]}),e.jsxs("div",{className:"flex flex-wrap gap-2 items-center",children:[e.jsxs("div",{className:"relative flex-1 min-w-[220px]",children:[e.jsx(W,{className:"w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground"}),e.jsx(E,{placeholder:"Search name, company, email, phoneâ¦",value:j,onChange:a=>$(a.target.value),className:"pl-9"})]}),e.jsxs(J,{value:d,onValueChange:g,children:[e.jsx(K,{className:"w-[180px]",children:e.jsx(Q,{})}),e.jsxs(X,{children:[e.jsx(T,{value:"all",children:"All statuses"}),S.map(a=>e.jsx(T,{value:a,children:x[a]},a))]})]})]}),e.jsx(G,{className:"overflow-hidden",children:f?e.jsx("div",{className:"p-10 flex justify-center",children:e.jsx(A,{className:"w-5 h-5 animate-spin"})}):h.length===0?e.jsx("div",{className:"p-10 text-center text-sm text-muted-foreground",children:"No affiliate applications to show."}):e.jsx("div",{className:"divide-y divide-border/60",children:h.map(a=>e.jsxs("button",{onClick:()=>v(a),className:"w-full text-left p-4 hover:bg-muted/40 transition flex flex-wrap items-center gap-3",children:[e.jsxs("div",{className:"min-w-[180px] flex-1",children:[e.jsx("div",{className:"font-medium",children:a.name||"Unnamed applicant"}),e.jsx("div",{className:"text-xs text-muted-foreground",children:a.company||"No company given"})]}),e.jsxs("div",{className:"text-xs text-muted-foreground min-w-[200px] space-y-0.5",children:[a.email&&e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(L,{className:"w-3 h-3"}),a.email]}),a.phone&&e.jsxs("div",{className:"flex items-center gap-1.5",children:[e.jsx(B,{className:"w-3 h-3"}),a.phone]})]}),e.jsx(H,{variant:"secondary",children:x[a.status]||a.status}),e.jsx("div",{className:"text-xs text-muted-foreground w-[150px] text-right",children:y(a.created_at)})]},a.id))})}),e.jsx(Y,{open:!!s,onOpenChange:a=>!a&&v(null),children:e.jsx(Z,{className:"w-full sm:max-w-lg overflow-y-auto",children:s&&e.jsxs(e.Fragment,{children:[e.jsx(ee,{children:e.jsx(ae,{children:s.name||"Affiliate application"})}),e.jsxs("div",{className:"mt-4 space-y-4 text-sm",children:[e.jsx("div",{className:"flex flex-wrap gap-2",children:S.map(a=>e.jsx(m,{size:"sm",variant:s.status===a?"default":"outline",onClick:()=>I(s.id,a),children:x[a]},a))}),e.jsxs("dl",{className:"space-y-2",children:[e.jsx(c,{label:"Received",value:y(s.created_at)}),e.jsx(c,{label:"Company",value:s.company}),e.jsx(c,{label:"Email",value:s.email}),e.jsx(c,{label:"Phone",value:s.phone}),e.jsx(c,{label:"Website",value:s.website}),e.jsx(c,{label:"Referral code",value:s.referral_code}),e.jsx(c,{label:"Source page",value:s.source_page}),e.jsx(c,{label:"Reference",value:s.reference_number})]}),s.experience&&e.jsxs("div",{children:[e.jsx("div",{className:"text-xs uppercase tracking-wider text-muted-foreground mb-1",children:"Experience"}),e.jsx("p",{className:"whitespace-pre-wrap",children:s.experience})]}),e.jsxs("div",{children:[e.jsx("div",{className:"text-xs uppercase tracking-wider text-muted-foreground mb-1",children:"Reason / decision note"}),e.jsx(E,{defaultValue:s.review_reason||"",placeholder:"Why approved, declined or on hold",onBlur:a=>{a.target.value!==(s.review_reason||"")&&w(s.id,{review_reason:a.target.value})}})]}),e.jsxs("div",{children:[e.jsx("div",{className:"text-xs uppercase tracking-wider text-muted-foreground mb-1",children:"Internal notes"}),e.jsx(F,{rows:5,defaultValue:s.notes||"",placeholder:"Notes for the team",onBlur:a=>{a.target.value!==(s.notes||"")&&w(s.id,{notes:a.target.value})}})]}),e.jsxs("div",{className:"flex flex-wrap gap-2 pt-2",children:[s.email&&e.jsx(m,{size:"sm",variant:"outline",asChild:!0,children:e.jsxs("a",{href:"mailto:".concat(s.email),children:[e.jsx(L,{className:"w-4 h-4 mr-2"})," Email"]})}),s.phone&&e.jsx(m,{size:"sm",variant:"outline",asChild:!0,children:e.jsxs("a",{href:"tel:".concat(s.phone),children:[e.jsx(B,{className:"w-4 h-4 mr-2"})," Call"]})}),s.website&&e.jsx(m,{size:"sm",variant:"outline",asChild:!0,children:e.jsxs("a",{href:s.website.startsWith("http")?s.website:"https://".concat(s.website),target:"_blank",rel:"noreferrer noopener",children:[e.jsx(M,{className:"w-4 h-4 mr-2"})," Website"]})})]})]})]})})})]}):e.jsx(z,{to:"/staff",replace:!0})}function c({label:o,value:p}){return p?e.jsxs("div",{className:"flex gap-3",children:[e.jsx("dt",{className:"w-32 shrink-0 text-muted-foreground",children:o}),e.jsx("dd",{className:"flex-1 break-words",children:p})]}):null}export{oe 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.