1import{k as ve,K as ke,h as Se,r as a,s as L,j as e,m as p,n as g,I as _,B as f,d as R,Q as Ce,a as V,W as he,v as Ee}from"./index-Ct8eLu1I.js";import{B as Le}from"./barcode-scanner-button-BDgHuj0e.js";import{C as Q}from"./circle-x-DAvesdV7.js";import{L as _e}from"./log-in-BUn-YFrb.js";import{R as pe}from"./rotate-ccw-LStRxjRV.js";import{S as Re}from"./search-BsvfnwMH.js";import"./camera-C-_PLVKn.js";function Ue(){const ge=ve(),{orgSlug:n}=ke(),[I,T]=Se(),[o,q]=a.useState(()=>{const s=sessionStorage.getItem(`pos-session-${n}`);return s?JSON.parse(s).pin:""}),[N,X]=a.useState(()=>!!sessionStorage.getItem(`pos-session-${n}`)),[G,H]=a.useState(()=>{const s=sessionStorage.getItem(`pos-session-${n}`);return s?JSON.parse(s).staffName:""}),[fe,Y]=a.useState(()=>{const s=sessionStorage.getItem(`pos-session-${n}`);return s?JSON.parse(s).staffLocation:""}),[Z,ee]=a.useState(!1),[se,b]=a.useState(""),[i,O]=a.useState(I.get("code")||""),[$,F]=a.useState(!1),[r,v]=a.useState(null),[te,d]=a.useState(""),[x,k]=a.useState(null),[P,S]=a.useState(""),[l,ae]=a.useState(()=>{const s=sessionStorage.getItem(`pos-session-${n}`);return s?JSON.parse(s).orgInfo:null}),C=a.useRef(null),[U,re]=a.useState(!1),[D,le]=a.useState(null),[j,B]=a.useState(0),[ne,ce]=a.useState(""),[M,W]=a.useState(!1),[y,z]=a.useState(""),[oe,A]=a.useState(!1),[ie,w]=a.useState("");a.useEffect(()=>{I.get("auto")==="1"&&i&&N&&(K(),T(s=>{const t=new URLSearchParams(s);return t.delete("auto"),t},{replace:!0}))},[N]),a.useEffect(()=>{N&&!U&&C.current&&C.current.focus()},[N,U]),a.useEffect(()=>{if(!N)return;const s=I.get("wallet");if(s){const t=s.split(":");if(t.length===3){const[u,c,h]=t;Ne(u,c==="none"?null:c,h),T(ue=>{const E=new URLSearchParams(ue);return E.delete("wallet"),E},{replace:!0})}}},[N]);const Ne=async(s,t,u)=>{re(!0),le({userId:s,familyMemberId:t,orgId:u}),W(!0),w(""),A(!1),z("");try{const{data:c,error:h}=await L.functions.invoke("pos-wallet-redeem",{body:{action:"balance",org_slug:n,pin:o,user_id:s,family_member_id:t,organization_id:u}});if(h)throw h;if(c!=null&&c.error)throw new Error(c.error);B(Number(c==null?void 0:c.balance)||0),ce((c==null?void 0:c.rider_name)||"Rider")}catch(c){w(c.message||"Failed to load wallet")}finally{W(!1)}},de=async()=>{if(!D||!y)return;const s=parseFloat(y);if(isNaN(s)||s<=0||s>j){w(s>j?"Amount exceeds balance":"Enter a valid amount");return}W(!0),w("");try{const{data:t,error:u}=await L.functions.invoke("pos-wallet-redeem",{body:{org_slug:n,pin:o,user_id:D.userId,family_member_id:D.familyMemberId,organization_id:D.orgId,amount:s,note:P}});if(u)throw u;if(t!=null&&t.error)throw new Error(t.error);B(typeof(t==null?void 0:t.balance)=="number"?t.balance:j-s),A(!0)}catch(t){w(t.message||"Redemption failed")}finally{W(!1)}},J=()=>{var s;
1re(!1),le(null),B(0),ce(""),z(""),A(!1),w(""),S(""),(s=C.current)==null||s.focus()},xe=async()=>{if(!(!o||!n)){ee(!0),b("");try{const{data:s,error:t}=await L.functions.invoke("pos-coupon-check",{body:{org_slug:n,pin:o,mode:"login"}});if(t)throw t;if(s!=null&&s.error){b(s.error);return}s!=null&&s.success?(ae(s.organization),H(s.staff.name),Y(s.staff.location),sessionStorage.setItem(`pos-session-${n}`,JSON.stringify({staffName:s.staff.name,staffLocation:s.staff.location,orgInfo:s.organization,pin:o})),X(!0)):b("Invalid PIN")}catch(s){b(s.message||"Login failed")}finally{ee(!1)}}},je=()=>{sessionStorage.removeItem(`pos-session-${n}`),X(!1),q(""),H(""),Y(""),O(""),v(null),k(null),d(""),S(""),J()},K=async()=>{if(!(!i||!n)){F(!0),d(""),v(null),k(null);try{const{data:s,error:t}=await L.functions.invoke("pos-coupon-check",{body:{code:i.toUpperCase(),org_slug:n}});if(t)throw t;s!=null&&s.organization&&ae(s.organization),s!=null&&s.error?d(s.error):v(s)}catch(s){d(s.message||"Check failed")}finally{F(!1)}}},ye=async()=>{if(!(!i||!n)){F(!0),d(""),k(null);try{const{data:s,error:t}=await L.functions.invoke("pos-coupon-redeem",{body:{code:i.toUpperCase(),pin:o,org_slug:n,location:fe,note:P}});if(t)throw t;s!=null&&s.error?d(s.error):(k(s),v(null))}catch(s){d(s.message||"Redeem failed")}finally{F(!1)}}},we=()=>{var s;O(""),v(null),k(null),d(""),S(""),(s=C.current)==null||s.focus()},be=s=>{const t=s.match(/[?&]wallet=([^&]+)/);if(t&&t[1].split(":").length===3){const E=new URL(window.location.href);E.searchParams.set("wallet",t[1]),window.location.href=E.toString();return}const c=s.replace(/^https?:\/\/.*[?&]code=/,"").split("&")[0].toUpperCase()||s.toUpperCase(),h=new URL(window.location.href);h.searchParams.set("code",c),h.searchParams.set("auto","1"),window.location.href=h.toString()},m=(l==null?void 0:l.primary_color)||"#dc2626",me=s=>s.discount_type==="percent"?`${s.discount_value}% OFF`:s.discount_type==="free_item"?s.title||"FREE ITEM":`$${Number(s.discount_value).toFixed(2)} OFF`;return N?e.jsxs("div",{className:"min-h-screen",style:{background:"#111"},children:[e.jsxs("div",{className:"flex items-center justify-between px-4 py-3",style:{background:"#1a1a1a",borderBottom:`2px solid ${m}`},children:[e.jsxs("div",{className:"flex items-center gap-3",children:[(l==null?void 0:l.logo_url)&&e.jsx("img",{src:l.logo_url,alt:"",className:"h-8"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-white text-sm font-semibold",children:(l==null?void 0:l.name)||"POS"}),e.jsxs("p",{className:"text-gray-400 text-xs",children:["Logged in as ",e.jsx("span",{className:"text-white",children:G})]})]})]}),e.jsxs(f,{variant:"ghost",size:"sm",onClick:je,className:"text-gray-400 hover:text-white gap-1",children:[e.jsx(Ce,{className:"h-4 w-4"})," Out"]})]}),e.jsx("div",{className:"max-w-md mx-auto p-4 space-y-4",children:U?e.jsx(e.Fragment,{children:M&&!oe?e.jsx(p,{className:"border-0 shadow-xl",style:{background:"#1a1a1a"},children:e.jsx(g,{className:"p-8 flex items-center justify-center",children:e.jsx(R,{className:"h-8 w-8 animate-spin text-gray-400"})})}):oe?e.jsx(p,{className:"border-green-500/50 border",style:{background:"#1a1a1a"},children:e.jsxs(g,{className:"p-6 text-center space-y-4",children:[e.jsx(V,{className:"h-16 w-16 text-green-400 mx-auto"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xl font-bold text-green-400",children:"WALLET REDEEMED!"}),e.jsxs("p",{className:"text-2xl font-black text-white mt-2",children:["-$",parseFloat(y).toFixed(2)]}),e.jsx("p",{className:"text-gray-300 mt-1",children:ne}),e.jsxs("p",{className:"text-sm text-gray-500 mt-1",children:["Remaining balance: $",j.toFixed(2)]}),e.jsxs("p",{className:"text-xs text-gray-500",children:["by ",G]})]}),e.jsxs(f,{onClick:J,className:"gap-2 h-11 text-white",style:{backgroundColor:m},children:[e.jsx(pe,{className:"h-4 w-4"})," Done"]})]})}):e.jsx(p,{className:"border-0 shadow-xl",style:{background:"#1a1a1a"},children:e.jsxs(g,{className:"p-4 space-y-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(he,{className:"h-5 w-5 text-green-400"}),e.jsx("p",{className:"text-xs text-gray-400 uppercase tracking-wider font-medium",children:"Wallet Redemption"})]}),e.jsxs("div",{className:"text-center py-4 rounded-lg",style:{background:`${m}20`,border:`1px solid ${m}40`},children:[e.jsx("p",{className:"text-sm text-gray-400 uppercase",children:l!=null&&l.name?`${l.name} credit`:"Available Balance"}),e.jsxs("p",{className:"text-4xl font-black text-white",children:["$",j.toFixed(2)]}),e.jsx("p",{className:"text-gray-300 mt-1",children:ne}),e.jsxs("p",{className:"text-[11px] text-gray-400 mt-2 px-4",children:["This credit is held by ",(l==null?void 0:l.name)||"this organization"," and can only be redeemed here."]})]}),j>0?e.jsxs("div",{className:"space-y-3",children:[e.jsx(_,{type:"number",step:"0.01",min:"0.01",max:j,placeholder:"Amount to deduct",value:y,onChange:s=>{z(s.target.value),w("")},className:"text-center text-xl h-12 bg-black border-gray-700 text-white placeholder:text-gray-600",autoFocus:!0,onKeyDown:s=>s.key==="Enter"&&de()}),e.jsx(_,{placeholder:"Note (optional)",value:P,onChange:s=>S(s.target.value),className:"text-sm bg-black border-gray-700 text-white placeholder:text-gray-600"}),e.jsxs(f,{onClick:de,disabled:!y||M,className:"w-full h-11 gap-2 text-white font-semibold",style:{backgroundColor:"#16a34a"},children:[M?e.jsx(R,{className:"h-4 w-4 animate-spin"}):e.jsx(he,{className:"h-4 w-4"}),"Redeem $",y?parseFloat(y).toFixed(2):"0.00"]})]}):e.jsx("p",{className:"text-center text-gray-400 text-sm",children:"No balance available"}),ie&&e.jsxs("div",{className:"flex items-center gap-2 justify-center",children:[e.jsx(Q,{className:"h-4 w-4 text-red-400"}),e.jsx("p",{className:"text-sm text-red-400",children:ie})]}),e.jsx(f,{variant:"ghost",onClick:J,className:"w-full text-gray-400 hover:text-white",children:"â Back to Coupons"})]})})}):e.jsxs(e.Fragment,{children:[e.jsx(p,{className:"border-0 shadow-xl",style:{background:"#1a1a1a"},children:e.jsxs(g,{className:"p-4 space-y-3",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("p",{className:"text-xs text-gray-400 uppercase tracking-wider font-medium",children:"Coupon Code"}),e.jsx(Le,{onScan:be,mode:"qr",className:"text-gray-400 hover:text-white"})]}),e.jsx(_,{ref:C,placeholder:"Enter or scan code",value:i,onChange:s=>O(s.target.value.toUpperCase()),className:"text-center text-xl font-mono tracking-widest h-12 bg-black border-gray-700 text-white placeholder:text-gray-600",onKeyDown:s=>s.key==="Enter"&&K()}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs(f,{onClick:K,disabled:!i||$,variant:"outline",className:"h-11 gap-2 border-gray-600 text-gray-200 hover:bg-gray-800 hover:text-white",children:[$&&!r?e.jsx(R,{className:"h-4 w-4 animate-spin"}):e.jsx(Re,{className:"h-4 w-4"}),"Look Up"]}),e.jsxs(f,{onClick:ye,disabled:!i||$||!(r!=null&&r.valid),className:"h-11 gap-2 text-white font-semibold",style:{backgroundColor:r!=null&&r.valid?"#16a34a":"#555"},children:[$&&r?e.jsx(R,{className:"h-4 w-4 animate-spin"}):e.jsx(V,{className:"h-4 w-4"}),"Redeem"]})]}),e.jsx(_,{placeholder:"Note (optional)",value:P,onChange:s=>S(s.target.value),className:"text-sm bg-black border-gray-700 text-white placeholder:text-gray-600"})]})}),te&&e.jsx(p,{className:"border-red-500/50 border",style:{background:"#1a1a1a"},children:e.jsxs(g,{className:"p-4 flex items-center gap-3",children:[e.jsx(Q,{className:"h-6 w-6 text-red-400 flex-shrink-0"}),e.jsx("p",{className:"text-red-400 font-medium",children:te})]})}),r&&e.jsx(p,{className:"border-0 shadow-xl",style:{background:"#1a1a1a"},children:e.jsxs(g,{className:"p-4 space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(Ee,{className:"text-sm px-3 py-1",style:{backgroundColor:r.valid?"#16a34a":"#dc2626",color:"white"},children:r.valid?"â VALID":"â INVALID"}),r.reason&&e.jsx("span",{className:"text-sm text-gray-400",children:r.reason})]}),r.coupon&&e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"text-center py-4 rounded-lg",style:{background:`${m}20`,border:`1px solid ${m}40`},children:[e.jsx("p",{className:"text-3xl font-black text-white",children:me(r.coupon)}),r.coupon.subtitle&&e.jsx("p",{className:"text-sm text-gray-300 mt-1",children:r.coupon.subtitle})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3 text-sm",children:[e.jsxs("div",{className:"space-y-0.5",children:[e.jsx("span",{className:"text-gray-500 text-xs uppercase",children:ge.racer}),e.jsx("p",{className:"text-white font-medium",children:r.coupon.rider_name||"â"})]}),e.jsxs("div",{className:"space-y-0.5",children:[e.jsx("span",{className:"text-gray-500 text-xs uppercase",children:"Member #"}),e.jsx("p",{className:"text-white font-medium",children:r.coupon.member_number||"â"})]}),e.jsxs("div",{className:"space-y-0.5",children:[e.jsx("span",{className:"text-gray-500 text-xs uppercase",children:"Uses"}),e.jsxs("p",{className:"text-white font-medium",children:[r.coupon.uses," / ",r.coupon.max_uses]})]}),e.jsxs("div",{className:"space-y-0.5",children:[e.jsx("span",{className:"text-gray-500 text-xs uppercase",children:"Expires"}),e.jsx("p",{className:"text-white font-medium",children:r.coupon.expires_at?new Date(r.coupon.expires_at).toLocaleDateString():"Never"})]})]})]})]})}),(x==null?void 0:x.success)&&e.jsx(p,{className:"border-green-500/50 border",style:{background:"#1a1a1a"},children:e.jsxs(g,{className:"p-6 text-center space-y-4",children:[e.jsx(V,{className:"h-16 w-16 text-green-400 mx-auto"}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xl font-bold text-green-400",children:"REDEEMED!"}),x.coupon&&e.jsxs("div",{className:"mt-3 space-y-1",children:[e.jsx("p",{className:"text-2xl font-black text-white",children:me(x.coupon)}),e.jsxs("p",{className:"text-gray-300",children:[x.coupon.rider_name," ⢠#",x.coupon.member_number]}),e.jsxs("p",{className:"text-xs text-gray-500",children:["by ",x.redeemed_by]})]})]}),e.jsxs(f,{onClick:we,className:"gap-2 h-11 text-white",style:{backgroundColor:m},children:[e.jsx(pe,{className:"h-4 w-4"})," Next Coupon"]})]})})]})})]}):e.jsx("div",{className:"min-h-screen flex items-center justify-center p-4",style:{background:"#111"},children:e.jsxs("div",{className:"w-full max-w-sm space-y-6",children:[e.jsxs("div",{className:"text-center space-y-3",children:[l!=null&&l.logo_url?e.jsx("img",{src:l.logo_url,alt:"Logo",className:"h-16 mx-auto"}):e.jsx("div",{className:"text-3xl font-bold text-white tracking-tight",children:(n==null?void 0:n.replace(/-/g," ").toUpperCase())||"POS"}),e.jsx("p",{className:"text-sm text-gray-400 uppercase tracking-widest",children:"Coupon Verifier"})]}),e.jsx(p,{className:"border-0 shadow-2xl",style:{background:"#1a1a1a"},children:e.jsxs(g,{className:"p-6 space-y-5",children:[e.jsx("div",{className:"text-center",children:e.jsx("p",{className:"text-gray-300 text-sm mb-3",children:"Enter your staff PIN to continue"})}),e.jsx(_,{type:"password",inputMode:"numeric",placeholder:"â â â â â â",value:o,onChange:s=>{q(s.target.value),b("")},className:"text-center text-3xl tracking-[0.5em] h-14 bg-black border-gray-700 text-white placeholder:text-gray-600",maxLength:6,autoFocus:!0,onKeyDown:s=>s.key==="Enter"&&xe()}),se&&e.jsxs("div",{className:"flex items-center gap-2 justify-center",children:[e.jsx(Q,{className:"h-4 w-4 text-red-400"}),e.jsx("p",{className:"text-sm text-red-400",children:se})]}),e.jsxs(f,{onClick:xe,disabled:!o||Z,className:"w-full h-12 text-base font-semibold gap-2",style:{backgroundColor:m},children:[Z?e.jsx(R,{className:"h-5 w-5 animate-spin"}):e.jsx(_e,{className:"h-5 w-5"}),"Sign In"]})]})})]})})}export{Ue 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.