import{af as F,ad as O,$ as c,K as e,g as $,ag as P,L as q,b as B,a9 as S}from"./index-CqfREQhZ.js";import{f as d,p as I}from"./products-Q1MHi1iw.js";import{s as j}from"./client-Bz6r3n8Z.js";import{C as L}from"./circle-check-BTdJ3rF1.js";import"./tslib.es6-Du-meQkk.js";function U(){const{cart:t,cartTotal:o,clearCart:x}=F(),u=O(),[r,p]=c.useState({name:"",phone:"",email:"",address:"",city:"Kigali",notes:""}),[i,M]=c.useState("momo"),[_,T]=c.useState(!1),[X,A]=c.useState(""),[h,v]=c.useState(!1),f=o>2e5?0:5e3,b=o+f,l=(s,n)=>p(y=>({...y,[s]:n})),D=async s=>{if(s.preventDefault(),!r.name||!r.phone||!r.address){S.error("Please fill all required fields");return}v(!0);try{const n=t.map(a=>a.product.name),{data:y}=await j.from("products").select("id, name").in("name",n),W=new Map((y||[]).map(a=>[a.name,a.id])),{data:g,error:C}=await j.from("orders").insert({customer_name:r.name,email:r.email||null,phone:r.phone,address:`${r.address}, ${r.city}`,payment:i==="momo"?"MoMo":i==="cod"?"Cash on Delivery":"Bank Transfer",amount:b,channel:"Web",notes:r.notes||null}).select().single();if(C||!g)throw C||new Error("Failed to create order");const E=t.map(a=>({order_id:g.id,product_id:W.get(a.product.name)||null,product_name:a.product.name,quantity:a.qty,unit_price:a.selectedPrice??a.product.price})),{error:k}=await j.from("order_items").insert(E);if(k)throw k;A(g.order_number),T(!0),setTimeout(()=>x(),500)}catch(n){S.error(n?.message||"Could not place order")}finally{v(!1)}},w=`Hi Amazing Soko, here's my order:

*Customer:* ${r.name}
*Phone:* ${r.phone}
*Address:* ${r.address}, ${r.city}
*Payment:* ${i}

${t.map(s=>`• ${s.product.name} x${s.qty} — ${d((s.selectedPrice??s.product.price)*s.qty)}`).join(`
`)}

*Total:* ${d(b)}`;return _?e.jsxs("div",{className:"container mx-auto px-4 py-20 text-center max-w-lg",children:[e.jsx(L,{className:"w-16 h-16 mx-auto text-success"}),e.jsx("h1",{className:"text-3xl font-bold mt-6 text-primary",children:"Order placed!"}),e.jsxs("p",{className:"text-muted-foreground mt-2",children:["Thank you ",r.name,". Your order ",e.jsx("b",{children:X})," is now in our system. We'll contact you shortly to confirm delivery."]}),e.jsxs("a",{href:P(w),target:"_blank",rel:"noreferrer",className:"mt-6 inline-flex items-center gap-2 bg-brand text-brand-foreground px-6 py-3 rounded-full font-semibold",children:[e.jsx($,{className:"w-4 h-4"})," Confirm on WhatsApp"]}),e.jsx("div",{className:"mt-3",children:e.jsx(q,{to:"/",className:"text-sm text-muted-foreground hover:text-primary",children:"Continue shopping"})})]}):t.length===0?e.jsxs("div",{className:"container mx-auto px-4 py-20 text-center",children:[e.jsx("p",{children:"Your cart is empty."}),e.jsx("button",{onClick:()=>u({to:"/shop"}),className:"mt-4 bg-brand text-brand-foreground px-5 py-2 rounded-full",children:"Shop now"})]}):e.jsxs("div",{className:"container mx-auto px-4 py-10",children:[e.jsx("h1",{className:"text-3xl font-bold text-primary mb-8",children:"Checkout"}),e.jsxs("form",{onSubmit:D,className:"grid lg:grid-cols-[1fr_380px] gap-8",children:[e.jsxs("div",{className:"space-y-6",children:[e.jsxs(N,{title:"Customer Information",children:[e.jsx(m,{label:"Full name *",value:r.name,onChange:s=>l("name",s)}),e.jsx(m,{label:"Phone *",value:r.phone,onChange:s=>l("phone",s),placeholder:"+250 7XX XXX XXX"}),e.jsx(m,{label:"Email",value:r.email,onChange:s=>l("email",s),type:"email"})]}),e.jsxs(N,{title:"Delivery Address",children:[e.jsx(m,{label:"Address *",value:r.address,onChange:s=>l("address",s)}),e.jsx(m,{label:"City",value:r.city,onChange:s=>l("city",s)}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-semibold uppercase tracking-wider text-muted-foreground",children:"Notes"}),e.jsx("textarea",{value:r.notes,onChange:s=>l("notes",s.target.value),rows:3,className:"mt-1 w-full bg-secondary rounded-lg px-4 py-2.5 text-sm outline-none"})]})]}),e.jsx(N,{title:"Payment Method",children:e.jsx("div",{className:"space-y-2",children:[{id:"momo",t:"Mobile Money (MoMo)",d:"Pay via MTN/Airtel Money"},{id:"cod",t:"Cash on Delivery",d:"Pay when you receive"},{id:"bank",t:"Bank Transfer",d:"We'll send account details"}].map(s=>e.jsxs("label",{className:`flex items-center gap-3 border rounded-xl p-4 cursor-pointer transition ${i===s.id?"border-brand bg-brand/5":"border-border hover:border-primary/30"}`,children:[e.jsx("input",{type:"radio",name:"pay",checked:i===s.id,onChange:()=>M(s.id),className:"accent-[var(--brand)]"}),e.jsxs("div",{children:[e.jsx("div",{className:"font-semibold text-sm",children:s.t}),e.jsx("div",{className:"text-xs text-muted-foreground",children:s.d})]})]},s.id))})})]}),e.jsxs("aside",{className:"bg-card border border-border rounded-2xl p-6 h-fit lg:sticky lg:top-40 space-y-4",children:[e.jsx("h3",{className:"font-bold text-lg",children:"Order Summary"}),e.jsx("div",{className:"space-y-3 max-h-64 overflow-auto pr-1",children:t.map(({product:s,qty:n})=>e.jsxs("div",{className:"flex gap-3 text-sm",children:[e.jsx("img",{src:I(s),alt:"",className:"w-12 h-12 rounded-lg object-cover bg-secondary"}),e.jsxs("div",{className:"flex-1",children:[e.jsx("div",{className:"font-medium line-clamp-1",children:s.name}),e.jsxs("div",{className:"text-xs text-muted-foreground",children:["x",n]})]}),e.jsx("div",{className:"font-semibold",children:d(s.price*n)})]},s.id))}),e.jsxs("div",{className:"border-t border-border pt-4 space-y-2 text-sm",children:[e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-muted-foreground",children:"Subtotal"}),e.jsx("span",{children:d(o)})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-muted-foreground",children:"Delivery"}),e.jsx("span",{children:f===0?"Free":d(f)})]}),e.jsxs("div",{className:"flex justify-between text-lg pt-2",children:[e.jsx("span",{className:"font-semibold",children:"Total"}),e.jsx("span",{className:"font-extrabold text-primary",children:d(b)})]})]}),e.jsxs("button",{disabled:h,type:"submit",className:"w-full bg-primary text-primary-foreground py-3 rounded-full font-semibold hover:opacity-90 transition disabled:opacity-60 inline-flex items-center justify-center gap-2",children:[h&&e.jsx(B,{className:"w-4 h-4 animate-spin"}),h?"Placing order…":"Place Order"]}),e.jsxs("a",{href:P(w),target:"_blank",rel:"noreferrer",className:"flex items-center justify-center gap-2 bg-brand text-brand-foreground py-3 rounded-full font-semibold hover:opacity-90 transition",children:[e.jsx($,{className:"w-4 h-4"})," Confirm via WhatsApp"]})]})]})]})}function N({title:t,children:o}){return e.jsxs("div",{className:"bg-card border border-border rounded-2xl p-6",children:[e.jsx("h2",{className:"font-bold mb-4",children:t}),e.jsx("div",{className:"space-y-3",children:o})]})}function m({label:t,value:o,onChange:x,type:u="text",placeholder:r}){return e.jsxs("div",{children:[e.jsx("label",{className:"text-xs font-semibold uppercase tracking-wider text-muted-foreground",children:t}),e.jsx("input",{type:u,value:o,onChange:p=>x(p.target.value),placeholder:r,className:"mt-1 w-full bg-secondary rounded-lg px-4 py-2.5 text-sm outline-none"})]})}export{U as component};
