1import{r as l,j as e}from"./vendor-react-C4z7UtLC.js";import{a as S}from"./vendor-router-BJhxZ56r.js";import{a as C,d as r,B as j,J as H,L as G,s as o}from"./index-DfWsTcc5.js";import{i as A,g as v,p as K,q as L,j as q,as as D}from"./vendor-icons-BWA1YZ2v.js";import"./vendor-mapbox-C_ucLznT.js";import"./vendor-supabase-Dy2NFerq.js";import"./vendor-radix-CfgVhGxY.js";import"./vendor-query-DfuWaPf4.js";import"./vendor-motion-CbVPlxOs.js";import"./vendor-charts-BU5llLfm.js";import"./vendor-forms-DpfWj5im.js";const R=()=>{const{user:a}=C(),[d,u]=S(),[N,x]=l.useState(!0),[h,g]=l.useState(!1),[c,f]=l.useState(null),[i,m]=l.useState(null),[_,p]=l.useState(!1),b=async()=>{if(!a)return;x(!0);const[{data:s},{data:t}]=await Promise.all([o.from("user_google_calendar_tokens_public").select("user_id,google_email,expires_at,scope,created_at").eq("user_id",a.id).maybeSingle(),o.from("sales_calendar_hosts").select("*").eq("user_id",a.id).maybeSingle()]);f(s??null),m(t??null),x(!1)};l.useEffect(()=>{b()},[a==null?void 0:a.id]),l.useEffect(()=>{const s=d.get("google");s&&(s==="ok"?r.success("Google Kalender verbunden"):r.error(`Google Kalender: ${s}`),d.delete("google"),u(d,{replace:!0}))},[d,u]);const w=async()=>{g(!0);try{const s=`${window.location.origin}/admin/sales/settings/calendar`,{data:t,error:n}=await o.functions.invoke("google-calendar-oauth-start",{body:{return_url:s}});if(n||!(t!=null&&t.auth_url))throw new Error((n==null?void 0:n.message)||"no auth_url");window.location.href=t.auth_url}catch(s){r.error(`Fehler: ${s.message}`),g(!1)}},y=async()=>{if(!a||!confirm("Google Kalender wirklich trennen?"))return;const{error:s}=await o.from("user_google_calendar_tokens").delete().eq("user_id",a.id);if(s)return r.error(s.message);r.success("Verbindung getrennt"),f(null)},k=async s=>{if(a){p(!0);try{if(s){const{data:t,error:n}=await o.from("sales_calendar_hosts").upsert({user_id:a.id,display_name:a.email??"Host",active:!0},{onConflict:"user_id"}).select().single();if(n)throw n;m(t),r.success("Als Host für Demo-Termine aktiviert")}else if(i){const{error:t}=await o.from("sales_calendar_hosts").update({active:!1}).eq("id",i.id);if(t)throw t;m({...i,active:!1}),r.success("Als Host deaktiviert")}}catch(t){r.error(t.message)}finally{p(!1)}}};return e.jsxs("div",{className:"space-y-6 max-w-3xl",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-xl font-semibold",children:"Google Kalender"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:"Verbinde deinen Google Kalender, damit Termine automatisch synchronisiert werden."})]}),e.jsx("div",{className:"rounded-2xl border bg-card p-6 space-y-4",children:e.jsxs("div",{className:"flex items-start gap-4",children:[e.jsx("div",{className:"h-12 w-12 rounded-xl bg-primary/10 flex items-center justify-center",children:e.jsx(A,{className:"h-6 w-6 text-primary"})}),e.jsxs("div",{className:"flex-1",children:[e.jsx("h3",{className:"font-semibold",children:"Mein Google Kalender"}),N?e.jsxs("div",{className:"flex items-center gap-2 text-sm text-muted-foreground mt-2",children:[e.jsx(v,{className:"h-4 w-4 animate-spin"})," Ladeâ¦"]}):c?e.jsxs("div",{className:"space-y-1 mt-2",children:[e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(K,{className:"h-4 w-4 text-emerald-600"}),e.jsx("span",{className:"font-medium",children:c.google_email})]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Verbunden seit ",new Date(c.created_at).toLocaleDateString("de-CH")]})]}):e.jsxs("div",{className:"flex items-center gap-2 text-sm text-muted-foreground mt-2",children:[e.jsx(L,{className:"h-4 w-4"})," Noch nicht verbunden"]})]}),c?e.jsxs(j,{variant:"outline",size:"sm",onClick:y,children:[e.jsx(q,{className:"h-4 w-4 mr-2"})," Trennen"]}):e.jsxs(j,{onClick:w,disabled:h,children:[h?e.jsx(v,{className:"h-4 w-4 mr-2 animate-spin"}):null,"Mit Google verbinden"]})]})}),c&&e.jsx("div",{className:"rounded-2xl border bg-card p-6",children:e.jsxs("div",{className:"flex items-start gap-4",children:[e.jsx("div",{className:"h-12 w-12 rounded-xl bg-amber-500/10 flex items-center justify-center",children:e.jsx(D,{className:"h-6 w-6 text-amber-600"})}),e.jsxs("div",{className:"flex-1",children:[e.jsx("h3",{className:"font-semibold",children:"Als Demo-Host buchbar"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:"Wenn aktiv, sehen andere Sales-Mitglieder deine freien Slots, um Demo-Termine mit dir zu buchen."})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(H,{id:"host-toggle",checked:!!(i!=null&&i.active),disabled:_,onCheckedChange:k}),e.jsx(G,{htmlFor:"host-toggle",className:"sr-only",children:"Als Host aktivieren"})]})]})})]})};export{R 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.