1import{u as U,a as C,b as I,j as e}from"./query-DC4SozEQ.js";import{r as K}from"./react-QZzH67i4.js";import{aS as A,aT as E,aV as P,aU as k,l as M,B as w,aa as L,b9 as R,aY as B,Z as D,n as T,m as H,af as p,aN as b,aZ as Q,aJ as F,U as O,R as V,bn as G,bh as $,aR as Y,b3 as Z}from"./index-CQCFO_Ja.js";import{A as J}from"./AdminLayout-B5Y8KN9N.js";import{S as N}from"./stat-tile-DTNuC_QE.js";import{U as W}from"./user-plus-CSu60XNH.js";import{S as X,a as q,c as ee,d as ae}from"./select-Jz1LkxvT.js";import{T as se,a as te,b as v,c as _,d as ne,e as f}from"./table-C-L5b1vS.js";import{H as y}from"./handshake-DaMxc-Ni.js";import"./i18n-DYzyxLbr.js";import"./ui-B2lVShOO.js";import"./supabase-xEDCcE6x.js";import"./dollar-sign-DaeE4-3T.js";import"./trending-up-B7BV2tdv.js";import"./database-BxeuT9q7.js";import"./inbox-kEIueDKR.js";import"./receipt-Ds1GXA37.js";import"./key-round-B4idDfKg.js";import"./history-CU9bBcHt.js";import"./shield-alert-dwPCPDD6.js";import"./order-status-D50BudvO.js";import"./chevron-left-BXjQhHqh.js";import"./pgrst-filter-DGRsim3R.js";import"./sheet-9hIUcW7C.js";import"./trending-down-4L2c6BsW.js";import"./minus-D1TXrtmY.js";(function(){try{var o=typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:typeof self<"u"?self:{};o.SENTRY_RELEASE={id:"06d80ffa4c67e89fc83d29f7ce6c589a45ea4a1b"};var d=new o.Error().stack;d&&(o._sentryDebugIds=o._sentryDebugIds||{},o._sentryDebugIds[d]="39532b95-530d-446f-a89d-6cdd31a9befa",o._sentryDebugIdIdentifier="sentry-dbid-39532b95-530d-446f-a89d-6cdd31a9befa")}catch{}})();const re="Kooperationsvertrag (Partnerprogramm)";function oe(){const o="ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789";let d="";const u=new Uint32Array(14);crypto.getRandomValues(u);for(let g=0;g<14;g++)d+=o[u[g]%o.length];return d+"!"}function ce(){const o=U(),{data:d,isLoading:u}=C({queryKey:["admin-partner-onboard"],queryFn:async()=>{const{data:s}=await p.from("partner_organizations").select("id, source_inquiry_id"),r=new Set((s||[]).map(x=>x.source_inquiry_id).filter(Boolean)),{data:l}=await p.from("partner_inquiries").select("id, company_name, contact_name, contact_email, contact_phone, website, partner_type, status").order("created_at",{ascending:!1});return(l||[]).filter(x=>x.contact_email&&!r.has(x.id))}}),g=I({mutationFn:async s=>{const r=oe(),{data:l,error:x}=await p.from("partner_organizations").insert({partner_type:s.partner_type||"other",company_name:s.company_name,contact_name:s.contact_name||null,contact_email:s.contact_email,contact_phone:s.contact_phone||null,website:s.website||null,source_inquiry_id:s.id,status:"active"}).select("id").single();if(x)throw new Error(x.message);const{data:h,error:a}=await p.functions.invoke("admin-create-partner-user",{body:{email:s.contact_email,password:r,firstName:s.contact_name||s.company_name,partnerId:l.id}});if(a||!(h!=null&&h.userId))throw new Error((a==null?void 0:a.message)||"User-Anlage fehlgeschlagen");return await p.from("partner_inquiries").update({status:"converted"}).eq("id",s.id),{password:r,email:s.contact_email}},onSuccess:s=>{b.success(`Partner angelegt. Login: ${s.email} / ${s.password} (auch per Mail gesendet)`,{duration:12e3}),o.invalidateQueries({queryKey:["admin-partner-onboard"]}),o.invalidateQueries({queryKey:["admin-partner-koop-signatures"]})},onError:s=>b.error(s.message)}),{data:m}=C({queryKey:["admin-partner-koop-signatures"],queryFn:async()=>{const{data:s}=await p.from("contracts").select("id").eq("name",re).eq("is_active",!0).maybeSingle();if(!s)return{versionId:null,rows:[]};const{data:r}=await p.from("contract_versions").select("id").eq("contract_id",s.id).order("version_number",{ascending:!1}).limit(1).maybeSingle();if(!r)return{versionId:null,rows:[]};const l=r.id,{data:x}=await p.from("contract_signatures").select("signer_role, signer_org_id, signed_at, document_hash").eq("contract_version_id",l).gt("valid_until",new Date().toISOString()),h=x||[],a=h.filter(t=>t.signer_role==="partner"&&t.signer_org_id),c=new Set(h.filter(t=>t.signer_role==="company"&&t.signer_org_id).map(t=>t.signer_org_id)),i=[...new Set(a.map(t=>t.signer_org_id))];let j={};if(i.length){const{data:t}=await p.from("partner_organizations").select("id, company_name").in("id",i);j=Object.fromEntries((t||[]).map(z=>[z.id,z.company_name]))}return{versionId:l,rows:a.map(t=>({partnerId:t.signer_org_id,name:j[t.signer_org_id]||t.signer_org_id,signedAt:t.signed_at,hash:t.document_hash,counterSigned:c.has(t.signer_org_id)}))}}}),n=I({mutationFn:async s=>{if(!(m!=null&&m.versionId))throw new Error("Keine Vertragsversion");const{data:r,error:l}=await p.functions.invoke("sign-partner-contract",{body:{contractVersionId:m.versionId,signerName:"docto24",role:"company",partnerId:s}});if(l||!(r!=null&&r.success))throw new Error((l==null?void 0:l.message)||(r==null?void 0:r.error)||"Gegenzeichnung fehlgeschlagen");return r},onSuccess:()=>{b.success("docto24 hat gegengezeichnet"),o.invalidateQueries({queryKey:["admin-partner-koop-signatures"]})},onError:s=>b.error(s.message)});return e.jsxs("div",{className:"space-y-4 mb-6",children:[e.jsxs(A,{children:[e.jsx(E,{className:"py-3 px-4",children:e.jsxs(P,{className:"text-sm font-medium flex items-center gap-2",children:[e.jsx(W,{className:"h-4 w-4 text-primary"})," Partner onboarden (Login anlegen)"]})}),e.jsx(k,{className:"pt-0 pb-4 px-4",children:u?e.jsx(M,{className:"h-16"}):d!=null&&d.length?e.jsx("div",{className:"space-y-2",children:d.map(s=>e.jsxs("div",{className:"flex items-center gap-3 flex-wrap border rounded-md p-2.5",children:[e.jsxs("span",{className:"font-medium text-sm flex-1 min-w-[160px]",children:[s.company_name,e.jsxs("span",{className:"text-muted-foreground font-normal",children:[" · ",s.contact_email]})]}),e.jsx(w,{size:"sm",disabled:g.isPending,onClick:()=>g.mutate(s),children:g.isPending?e.jsx(L,{className:"h-3 w-3 animate-spin"}):"Onboarden + Login"})]},s.id))}):e.jsx("p",{className:"text-sm text-muted-foreground",children:"Keine offenen Anfragen mit E-Mail zum Onboarden."})})]}),(m==null?void 0:m.versionId)&&e.jsxs(A,{children:[e.jsx(E,{className:"py-3 px-4",children:e.jsxs(P,{className:"text-sm font-medium flex items-center gap-2",children:[e.jsx(R,{className:"h-4 w-4 text-primary"})," Kooperationsvertrag-Unterzeichnungen"]})}),e.jsx(k,{className:"pt-0 pb-4 px-4",children:m.rows.length?e.jsx("div",{className:"space-y-2",children:m.rows.map(s=>e.jsxs("div",{className:"flex items-center gap-3 flex-wrap border rounded-md p-2.5",children:[e.jsx("span",{className:"font-medium text-sm flex-1 min-w-[160px]",children:s.name}),e.jsxs("span",{className:"text-xs text-muted-foreground",children:[B(new Date(s.signedAt),"dd.MM.yyyy HH:mm",{locale:Q})," Uhr"]}),s.hash&&e.jsxs("span",{className:"text-[11px] text-muted-foreground flex items-center gap-1",children:[e.jsx(D,{className:"h-3 w-3"})," ",s.hash.slice(0,12),"â¦"]}),s.counterSigned?e.jsxs(T,{variant:"outline",className:"text-green-600 border-green-200 gap-1",children:[e.jsx(H,{className:"h-3 w-3"})," docto24 gegengezeichnet"]}):e.jsx(w,{size:"sm",disabled:n.isPending,onClick:()=>n.mutate(s.partnerId),children:n.isPending?e.jsx(L,{className:"h-3 w-3 animate-spin"}):"Gegenzeichnen"})]},s.partnerId))}):e.jsx("p",{className:"text-sm text-muted-foreground",children:"Noch kein Partner hat den Kooperationsvertrag unterzeichnet."})})]})]})}const S={new:{label:"Neu",color:"bg-blue-500"}
1,contacted:{label:"Kontaktiert",color:"bg-amber-500"},negotiating:{label:"Verhandlung",color:"bg-purple-500"},converted:{label:"Konvertiert",color:"bg-emerald-500"},rejected:{label:"Abgelehnt",color:"bg-red-500"}},ie={fitness:"Fitness/Studio",coach:"Coach/Ernährung",app:"App/Plattform",other:"Sonstiges"};function Le(){const o=U(),[d,u]=K.useState(null),[g,m]=K.useState(""),{data:n,isLoading:s}=C({queryKey:["program-inquiries"],queryFn:async()=>{const{data:a,error:c}=await p.from("partner_inquiries").select("*").order("created_at",{ascending:!1});if(c)throw c;return a}}),r=I({mutationFn:async({id:a,status:c,notes:i})=>{const j={status:c,updated_at:new Date().toISOString()};i!==void 0&&(j.notes=i);const{error:t}=await p.from("partner_inquiries").update(j).eq("id",a);if(t)throw t},onSuccess:()=>{b.success("Anfrage aktualisiert"),o.invalidateQueries({queryKey:["program-inquiries"]})},onError:()=>b.error("Fehler beim Aktualisieren")}),l=(a,c)=>{r.mutate({id:a,status:c})},x=a=>{var c;r.mutate({id:a,status:((c=n==null?void 0:n.find(i=>i.id===a))==null?void 0:c.status)||"new",notes:g}),u(null)},h={total:(n==null?void 0:n.length)||0,new:(n==null?void 0:n.filter(a=>a.status==="new").length)||0,converted:(n==null?void 0:n.filter(a=>a.status==="converted").length)||0};return e.jsxs(J,{title:"Partner-Programm",description:"Anfragen aus dem B2B2C-Partner-Programm (Fitness, Coaches, Health-Apps)",children:[e.jsx(ce,{}),e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-3 gap-4",children:[e.jsx(N,{icon:y,label:"Gesamt",value:h.total,accent:"primary",index:0}),e.jsx(N,{icon:F,label:"Neu / Offen",value:h.new,accent:"info",index:1}),e.jsx(N,{icon:O,label:"Konvertiert",value:h.converted,accent:"success",index:2})]}),e.jsxs(A,{children:[e.jsx(E,{children:e.jsxs(P,{className:"flex items-center gap-2",children:[e.jsx(y,{className:"h-5 w-5"}),"Eingehende Anfragen"]})}),e.jsx(k,{children:s?e.jsxs("div",{className:"text-center py-8",children:[e.jsx(V,{className:"h-6 w-6 animate-spin mx-auto mb-2 text-muted-foreground"}),e.jsx("p",{className:"text-muted-foreground",children:"Lade Anfragen..."})]}):n!=null&&n.length?e.jsx("div",{className:"rounded-md border",children:e.jsxs(se,{children:[e.jsx(te,{children:e.jsxs(v,{children:[e.jsx(_,{children:"Unternehmen"}),e.jsx(_,{children:"Typ"}),e.jsx(_,{children:"Kontakt"}),e.jsx(_,{children:"Reichweite"}),e.jsx(_,{children:"Datum"}),e.jsx(_,{children:"Status"}),e.jsx(_,{children:"Aktionen"})]})}),e.jsx(ne,{children:n.map(a=>{const c=S[a.status]||S.new;return e.jsxs(e.Fragment,{children:[e.jsxs(v,{className:a.status==="new"?"bg-blue-50/50 dark:bg-blue-950/10":"",children:[e.jsxs(f,{children:[e.jsx("div",{className:"font-medium",children:a.company_name}),a.website&&e.jsxs("div",{className:"flex items-center gap-1 text-xs text-muted-foreground mt-0.5",children:[e.jsx(G,{className:"h-3 w-3"}),a.website]})]}),e.jsx(f,{children:e.jsx(T,{variant:"outline",className:"text-[10px]",children:ie[a.partner_type]||a.partner_type})}),e.jsxs(f,{children:[e.jsx("div",{className:"text-sm",children:a.contact_name}),e.jsxs("div",{className:"flex items-center gap-1 text-xs text-muted-foreground",children:[e.jsx(F,{className:"h-3 w-3"}),e.jsx("a",{href:`mailto:${a.contact_email}`,className:"hover:underline",children:a.contact_email})]}),a.contact_phone&&e.jsxs("div",{className:"flex items-center gap-1 text-xs text-muted-foreground",children:[e.jsx($,{className:"h-3 w-3"}),a.contact_phone]})]}),e.jsx(f,{children:a.audience_size&&e.jsxs("div",{className:"flex items-center gap-1 text-sm",children:[e.jsx(O,{className:"h-3 w-3"}),a.audience_size]})}),e.jsx(f,{children:e.jsx("span",{className:"text-sm",children:new Date(a.created_at).toLocaleDateString("de-DE")})}),e.jsx(f,{children:e.jsxs(X,{value:a.status,onValueChange:i=>l(a.id,i),children:[e.jsx(q,{className:"w-[140px] h-8",children:e.jsx(T,{className:`${c.color} text-white text-[10px]`,children:c.label})}),e.jsx(ee,{children:Object.entries(S).map(([i,j])=>e.jsx(ae,{value:i,children:j.label},i))})]})}),e.jsx(f,{children:e.jsx(w,{variant:"ghost",size:"sm",onClick:()=>{d===a.id?u(null):(u(a.id),m(a.notes||""))},children:e.jsx(Y,{className:"h-4 w-4"})})})]},a.id),d===a.id&&e.jsx(v,{children:e.jsx(f,{colSpan:7,children:e.jsxs("div",{className:"p-4 bg-muted/30 rounded-lg space-y-3",children:[a.message&&e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-semibold mb-1",children:"Nachricht:"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:a.message})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-semibold mb-1",children:"Interne Notizen:"}),e.jsx(Z,{value:g,onChange:i=>m(i.target.value),rows:3,placeholder:"Notizen zu dieser Anfrage..."}),e.jsx(w,{size:"sm",className:"mt-2",onClick:()=>x(a.id),children:"Notizen speichern"})]})]})})},`${a.id}-expanded`)]})})})]})}):e.jsxs("div",{className:"text-center py-12",children:[e.jsx(y,{className:"h-12 w-12 mx-auto mb-4 text-muted-foreground
1/50"}),e.jsx("p",{className:"text-muted-foreground",children:"Noch keine Partner-Anfragen"})]})})]})]})]})}export{Le 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.