1import{i as W,c as G,u as J,r as o,a as y,j as e,L as v,d as k}from"./index-Bratghjx.js";import{u as U}from"./sales-mode-VDfmZj0A.js";import{B as u}from"./button-W8DTlMrk.js";import{I as C}from"./input-CyjFVtTp.js";import{L as j}from"./label-CaTTPTiq.js";import{T as X}from"./textarea-BdED8NPx.js";import{c as Y}from"./celebrate-Chn3pgjW.js";import{P as Z}from"./ProductImage-Dbe2g-Xk.js";import{P as ee}from"./PaymentInfoCard-DviVMh4C.js";import{P as se}from"./PaymentMethodDialog-LF-lpEhy.js";import{s as te}from"./errors-DucJlKMR.js";import{C as ae}from"./check-Cf8MB9EI.js";import{P as E}from"./plus-CmgX0k4P.js";import{A as T}from"./arrow-left-1kdsW6vx.js";import{E as re}from"./eye-B3533gdR.js";import{L as V}from"./loader-circle-BlACLt8x.js";import{M as ne}from"./minus-CZLYqwO6.js";import{T as le}from"./trash-2-qHD3jdjC.js";import"./utils-BQHNewu7.js";import"./index-BmCOvtY6.js";import"./swish-C7YBi3o-.js";import"./copy-D7GMHK56.js";import"./smartphone-em43ttb3.js";import"./truck-qRMauruh.js";import"./dialog-DD2kvupv.js";import"./x-DHGO-boL.js";const oe=[["path",{d:"M15.2 3a2 2 0 0 1 1.4.6l3.8 3.8a2 2 0 0 1 .6 1.4V19a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z",key:"1c8476"}],["path",{d:"M17 21v-7a1 1 0 0 0-1-1H8a1 1 0 0 0-1 1v7",key:"1ydtos"}],["path",{d:"M7 3v4a1 1 0 0 0 1 1h7",key:"t51u73"}]],de=W("save",oe),x=n=>Math.round(Number(n??0)).toLocaleString("sv-SE");function Fe(){const{user:n}=G(),F=J(),{shared:O}=U(),[h,A]=o.useState(null),[d,g]=o.useState({}),[D,S]=o.useState({}),[r,p]=o.useState({name:"",phone:"",area:"",note:""}),[w,L]=o.useState(!1),[i,M]=o.useState(null),[_,q]=o.useState(0),[b,z]=o.useState({swish:"",label:"laget"}),[B,N]=o.useState(!1);o.useEffect(()=>{if(!n)return;let s=!0;return(async()=>{const t=await y.catalog().catch(()=>null);if(!s)return;const a=t?.products??[],f={};for(const l of a){const m=l.rows?.find(R=>R.in_stock);m&&(f[l.id]=m.id)}S(f),A(a)})(),()=>{s=!1}},[n]),o.useEffect(()=>{if(!n)return;let s=!0;return(async()=>{const t=await y.profile().catch(()=>null);s&&z({swish:(t?.swish_number??"").trim(),label:t?.team_name||"laget"})})(),()=>{s=!1}},[n]);const P=(s,t)=>g(a=>({...a,[s]:Math.min(99,Math.max(0,(a[s]??0)+t))})),H=s=>g(t=>{const a={...t};return delete a[s],a}),c=o.useMemo(()=>(h??[]).flatMap(s=>s.rows.filter(t=>(d[t.id]??0)>0).map(t=>({rowId:t.id,name:s.name,label:t.label,n:d[t.id]??0,price:t.price,line:(d[t.id]??0)*t.price,commission:(d[t.id]??0)*(t.commission??0)}))),[h,d]),K=c.reduce((s,t)=>s+t.n,0),I=c.reduce((s,t)=>s+t.line,0),$=c.reduce((s,t)=>s+t.commission,0),Q=async()=>{if(c.length===0){k.error("Välj minst en produkt.");return}if(!r.name.trim()||!r.phone.trim()||!r.area.trim()){k.error("Fyll i namn, telefonnummer och leveransadress.");const t=r.name.trim()?r.phone.trim()?"loc":"ph":"c";document.getElementById(t)?.focus();return}const s=typeof n?.username=="string"&&n.username||(typeof n?.email=="string"?n.email:"");L(!0);try{const t=await y.placeOrder(s,{...r,items:c.map(a=>({id:a.rowId,qty:a.n})),...O&&n?.id?{shared_seller_id:n.id}:{}});Y(),q(I),M(t??{}),N(!0),g({}),p({name:"",phone:"",area:"",note:""}),window.scrollTo({top:0,behavior:"smooth"})}catch(t){k.error(te(t,"Kunde inte spara ordern"))}finally{L(!1)}};return i?e.jsxs("div",{className:"animate-[fade-in_0.4s_ease-out] rounded-3xl bg-card p-8 text-center shadow-[var(--shadow-card)]",children:[e.jsx("div",{className:"mx-auto flex h-16 w-16 items-center justify-center rounded-full bg-primary/15 text-primary",children:e.jsx(ae,{className:"h-8 w-8"})}),e.jsx("h1",{className:"mt-4 font-display text-xl font-bold text-foreground",children:"Order registrerad!"}),e.jsxs("p",{className:"mt-1 text-sm text-muted-foreground",children:[i.order_number?`#${i.order_number} · `:"",i.total?.formatted??""," ",i.name?`· ${i.name}`:""]}),e.jsx("p",{className:"mt-1 text-xs font-bold uppercase tracking-wider text-amber-700",children:"Obetald â ta betalt nu"}),e.jsx(u,{size:"lg",className:"mt-5 h-14 w-full rounded-2xl font-bold",onClick:()=>N(!0),children:"Välj betalsätt"}),e.jsx(ee,{swishNumber:b.swish,amount:Number(i.total?.value??_),sellerLabel:b.label}),e.jsx(se,{open:B,onOpenChange:N,swishNumber:b.swish,amount:Number(i.total?.value??_),sellerLabel:b.label}),e.jsxs(u,{className:"mt-5 h-14 w-full rounded-2xl font-bold",onClick:()=>M(null),children:[e.jsx(E,{className:"mr-2 h-4 w-4"})," Ny order"]}),e.jsx(u,{variant:"outline",className:"mt-2 h-12 w-full rounded-2xl font-bold",onClick:()=>F({to:"/orders"}),children:"Se mina ordrar"}),e.jsx(u,{asChild:!0,variant:"ghost",className:"mt-2 h-12 w-full rounded-2xl font-bold text-muted-foreground",children:e.jsxs(v,{to:"/dashboard",children:[e.jsx(T,{className:"mr-2 h-4 w-4"})," Tillbaka till dashboard"]})})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-6 animate-[fade-in_0.4s_ease-out] pb-40",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsxs(v,{to:"/dashboard","aria-label":"Tillbaka till dashboard",className:"flex h-11 items-center gap-1.5 rounded-full bg-muted px-4 font-bold text-foreground shadow-[var(--shadow-soft)]",children:[e.jsx(T,{className:"h-5 w-5"}),e.jsx("span",{className:"text-sm",children:"Tillbaka"})]}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("h1",{className:"font-display text-2xl font-bold leading-tight text-foreground sm:text-3xl",children:"Nytt sälj"}),e.jsx("p",{className:"truncate text-sm text-muted-foreground",children:"Välj sockorna kunden vill ha"})]}),e.jsx(u,{asChild:!0,size:"sm",className:"shrink-0 rounded-full font-bold",children:e.jsxs(v,{to:"/show",children:[e.jsx(re,{className:"mr-1.5 h-4 w-4"}),e.jsx("span",{className:"hidden sm:inline",children:"Visa kunden"}),e.jsx("span",{className:"sm:hidden",children:"Visa"})]})})]}),h?e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"space-y-4",children:[e.jsx("h2",{className:"text-xs font-bold uppercase tracking-wider text-muted-foreground",children:"Sockor att sälja"}),h.map(s=>{const t=D[s.id],a=s.rows.find(l=>l.id===t)??null,f=s.rows.reduce((l,m)=>l+(d[m.id]??0),0);return e.jsxs("div",{className:"rounded-3xl bg-card p-4 shadow-[var(--shadow-soft)] sm:p-5",children:[e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx(Z,{src:s.image,alt:s.name,className:"h-16 w-16 shrink-0"}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("p",{className:"font-bold text-foreground",children:s.name}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Pris ",x(s.rows[0]?.price)," kr · Du tjänar ",x(s.rows[0]?.commission)," kr"]})]}),f>0&&e.jsxs("span",{className:"shrink-0 rounded-full bg-primary/10 px-2.5 py-1 text-xs font-bold text-primary",children:["Ã",f]})]}),(s.rows.length>1||s.rows[0]?.label)&&e.jsxs("div",{className:"mt-4",children:[e.jsx("p",{className:"mb-2 text-[10px] font-bold uppercase tracking-wider text-muted-foreground",children:"Storlek"}),e.jsx("div",{className:"flex flex-wrap gap-2",children:s.rows.map(l=>e.jsx("button",{type:"button",disabled:!l.in_stock,onClick:()=>S(m=>({...m,[s.id]:l.id})),className:`rounded-full px-3.5 py-2 text-sm font-semibold transition disabled:opacity-40 ${t===l.id?"bg-primary text-primary-foreground shadow-[var(--shadow-glow)]":"bg-muted text-foreground"}`,children:l.label||"Standard"},l.id))})]}),e.jsx("div",{className:"my-4 border-t border-border/60"}),a?e.jsxs("div",{className:"flex items-center justify-between gap-3",children:[e.jsx("button",{type:"button","aria-label":"Minska",disabled:(d[a.id]??0)===0,onClick:()=>P(a.id,-1),className:"flex h-12 w-12 items-center justify-center rounded-full border-2 border-border bg-background disabled:opacity-40",children:e.jsx(ne,{className:"h-4 w-4"})}),e.jsx("p",{className:"flex-1 text-center font-display text-2xl font-bold tabular-nums",children:d[a.id]??0}),e.jsx("button",{type:"button","aria-label":"Ãka",disabled:!a.in_stock,onClick:()=>P(a.id,1),className:"flex h-12 w-12 items-center justify-center rounded-full bg-primary text-primary-foreground shadow-[var(--shadow-glow)] disabled:opacity-40",children:e.jsx(E,{className:"h-4 w-4"})})]}):e.jsx("p",{className:"rounded-full bg-muted py-3 text-center text-sm text-muted-foreground",children:"Välj variant först"})]},s.id)})]}),c.length>0&&e.jsxs("div",{className:"space-y-3",children:[e.jsxs("h2",{className:"text-xs font-bold uppercase tracking-wider text-muted-foreground",children:["Det här köper kunden (",K," st)"]}),e.jsx("div",{className:"space-y-1 rounded-2xl bg-card p-3 shadow-[var(--shadow-soft)]",children:c.map(s=>e.jsxs("div",{className:"flex items-center gap-3 rounded-xl p-2",children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("p",{className:"text-sm font-semibold text-foreground",children:[s.name,s.label&&e.jsx("span",{className:"ml-2 text-xs font-normal text-muted-foreground",children:s.label})]}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[s.n," à ",x(s.price)," kr = ",x(s.line)," kr"]})]}),e.jsx("button",{type:"button","aria-label":"Ta bort",onClick:()=>H(s.rowId),className:"flex h-8 w-8 items-center justify-center rounded-full text-muted-foreground",children:e.jsx(le,{className:"h-3.5 w-3.5"})})]},s.rowId))})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsx("h2",{className:"text-xs font-bold uppercase tracking-wider text-muted-foreground",children:"Vem är kunden?"}),e.jsxs("div",{className:"space-y-3 rounded-2xl bg-card p-4 shadow-[var(--shadow-soft)]",children:[e.jsxs("div",{children:[e.jsx(j,{htmlFor:"c",children:"Namn *"}),e.jsx(C,{id:"c",className:"rounded-xl",value:r.name,onChange:s=>p({...r,name:s.target.value}),placeholder:"Mormor Eva",maxLength:100,required:!0})]}),e.jsxs("div",{children:[e.jsx(j,{htmlFor:"ph",children:"Telefonnummer *"}),e.jsx(C,{id:"ph",type:"tel",inputMode:"tel",className:"rounded-xl",value:r.phone,onChange:s=>p({...r,phone:s.target.value}),placeholder:"070-123 45 67",maxLength:30,required:!0})]}),e.jsxs("div",{children:[e.jsx(j,{htmlFor:"loc",children:"Leveransadress / omrÃ¥de *"}),e.jsx(C,{id:"loc",className:"rounded-xl",value:r.area,onChange:s=>p({...r,area:s.target.value}),placeholder:"t.ex. Storgatan 5, gula huset",maxLength:250,required:!0}),e.jsx("p",{className:"mt-1 text-[11px] text-muted-foreground",children:"Hjälper dig hitta tillbaka â bra vid dörrknackning."})]}),e.jsxs("div",{children:[e.jsx(j,{htmlFor:"note",children:"Anteckning (frivilligt)"}),e.jsx(X,{id:"note",rows:2,className:"rounded-xl",value:r.note,onChange:s=>p({...r,note:s.target.value}),maxLength:1e3})]})]})]})]}):e.jsx("div",{className:"flex justify-center py-16",children:e.jsx(V,{className:"h-6 w-6 animate-spin text-primary"})})]}),h&&e.jsx("div",{className:"fixed inset-x-4 bottom-20 z-40 rounded-3xl bg-secondary p-4 text-secondary-foreground shadow-[var(--shadow-card)] sm:bottom-4 sm:left-auto sm:right-8 sm:max-w-sm",children:e.jsxs("div",{className:"flex items-center justify-between gap-3",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"text-xs font-semibold uppercase tracking-wider opacity-70",children:"Kunden betalar"}),e.jsxs("p",{className:"font-display text-2xl font-bold tabular-nums",children:[x(I)," kr"]}),e.jsxs("p",{className:"truncate text-xs text-primary",children:["Du tjänar +",x($)," kr"]})]}
1),e.jsxs(u,{className:"h-14 rounded-2xl px-6 font-bold shadow-[var(--shadow-glow)]",disabled:c.length===0||w,onClick:Q,children:[w?e.jsx(V,{className:"mr-2 h-5 w-5 animate-spin"}):e.jsx(de,{className:"mr-2 h-5 w-5"}),w?"Spararâ¦":"Klar"]})]})})]})}export{Fe as component};
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.