1import{r as o,u as q,j as e,X as O}from"./index-Cb0uFkxi.js";import{s as x}from"./client-3Rgyq3FX.js";import{B as c}from"./button-BXT4L8KI.js";import{I as h}from"./input-CyLPhXcX.js";import{S as V,a as L,b as B,c as H,d as j}from"./select-xTt2WSiW.js";import{T as X,a as Y,b as p,c as d,d as W,e as n}from"./table-DG9zpLGi.js";import{R as G}from"./refresh-cw-D_fuxE1r.js";import{P as J}from"./plus-DKLdpJ5T.js";import{T as K,a as Q}from"./toggle-right-wb-fHqlj.js";import{P as Z}from"./pencil-ZcfsXu3C.js";import{T as ee}from"./trash-2-NpQIgcu7.js";import"./tslib.es6-CmEaCU22.js";import"./index-DSefd6az.js";import"./index-BHD-fViZ.js";import"./chevron-down-CNHxCdIc.js";import"./check-C07f7c7n.js";import"./chevron-up-Co9HabI0.js";const T={code:"",type:"percentage",value:"",tier_restriction:[],max_uses:"1",expires_at:""};function be(){const[g,N]=o.useState([]),[y,_]=o.useState(!0),[E,m]=o.useState(!1),[f,b]=o.useState(null),[r,l]=o.useState(T),[w,C]=o.useState(!1),[D,k]=o.useState(null),[A,S]=o.useState(null),{toast:i}=q(),u=o.useCallback(async()=>{_(!0);try{const{data:t,error:s}=await x.from("discount_codes").select("*").order("created_at",{ascending:!1});if(s)throw s;N(t??[])}catch(t){i({title:"Error loading codes",description:t.message,variant:"destructive"})}finally{_(!1)}},[i]);o.useEffect(()=>{u()},[u]);const I=()=>{l(T),b(null),m(!0)},P=t=>{l({code:t.code,type:t.type,value:t.value!=null?String(t.value):"",tier_restriction:t.tier_restriction??[],max_uses:String(t.max_uses),expires_at:t.expires_at?t.expires_at.slice(0,10):""}),b(t.id),m(!0)},R=t=>{l(s=>({...s,tier_restriction:s.tier_restriction.includes(t)?s.tier_restriction.filter(a=>a!==t):[...s.tier_restriction,t]}))},$=async()=>{if(!r.code.trim()){i({title:"Code is required",variant:"destructive"});return}if(!r.max_uses||parseInt(r.max_uses)<1){i({title:"Max uses must be ⥠1",variant:"destructive"});return}if(r.type!=="free"&&!r.value){i({title:"Value is required for this type",variant:"destructive"});return}C(!0);try{const t={code:r.code.toUpperCase().trim(),type:r.type,value:r.type==="free"?null:parseFloat(r.value),tier_restriction:r.tier_restriction.length>0?r.tier_restriction:null,max_uses:parseInt(r.max_uses),expires_at:r.expires_at?new Date(r.expires_at).toISOString():null};if(f){const{error:s}=await x.from("discount_codes").update(t).eq("id",f);if(s)throw s;i({title:"Code updated"})}else{const{error:s}=await x.from("discount_codes").insert({...t,current_uses:0,is_active:!0});if(s)throw s;i({title:"Code created"})}m(!1),b(null),u()}catch(t){i({title:"Save failed",description:t.message,variant:"destructive"})}finally{C(!1)}},z=async(t,s)=>{if(confirm(`Delete code "${s}"? This cannot be undone.`)){S(t);try{const{error:a}=await x.from("discount_codes").delete().eq("id",t);if(a)throw a;i({title:"Code deleted"}),u()}catch(a){i({title:"Delete failed",description:a.message,variant:"destructive"})}finally{S(null)}}},F=async(t,s)=>{k(t);try{const{error:a}=await x.from("discount_codes").update({is_active:!s}).eq("id",t);if(a)throw a;N(U=>U.map(v=>v.id===t?{...v,is_active:!s}:v))}catch(a){i({title:"Update failed",description:a.message,variant:"destructive"})}finally{k(null)}},M=t=>t.type==="free"?"100% off":t.type==="percentage"?`${t.value}% off`:`$${t.value} off`;return e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-5",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:[g.length," code",g.length!==1?"s":""]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(c,{variant:"outline",size:"icon",onClick:u,className:"border-border/50",children:e.jsx(G,{className:`w-4 h-4 ${y?"animate-spin":""}`})}),e.jsxs(c,{size:"sm",onClick:I,className:"bg-primary text-primary-foreground hover:bg-primary/90",children:[e.jsx(J,{className:"w-4 h-4 mr-2"})," New Code"]})]})]}),E&&e.jsxs("div",{className:"border border-border/50 rounded-lg bg-card/50 p-5 mb-5",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsx("h3",{className:"font-heading text-sm tracking-wider text-foreground",children:f?"EDIT CODE":"NEW CODE"}),e.jsx(c,{variant:"ghost",size:"icon",className:"h-6 w-6",onClick:()=>m(!1),children:e.jsx(O,{className:"w-3.5 h-3.5"})})]}),e.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground uppercase tracking-wider mb-1.5 block",children:"Code *"}),e.jsx(h,{value:r.code,onChange:t=>l(s=>({...s,code:t.target.value.toUpperCase()})),placeholder:"e.g. SUMMIT20",className:"bg-background border-border/50 uppercase font-mono tracking-wider"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground uppercase tracking-wider mb-1.5 block",children:"Type *"}),e.jsxs(V,{value:r.type,onValueChange:t=>l(s=>({...s,type:t})),children:[e.jsx(L,{className:"bg-background border-border/50",children:e.jsx(B,{})}
1),e.jsxs(H,{className:"bg-card border-border/50",children:[e.jsx(j,{value:"percentage",children:"Percentage off"}),e.jsx(j,{value:"fixed",children:"Fixed amount off"}),e.jsx(j,{value:"free",children:"Free ticket (100%)"})]})]})]}),r.type!=="free"&&e.jsxs("div",{children:[e.jsxs("label",{className:"text-xs text-muted-foreground uppercase tracking-wider mb-1.5 block",children:[r.type==="percentage"?"Percent off (e.g. 20)":"Amount off in CAD (e.g. 100)"," *"]}),e.jsx(h,{type:"number",value:r.value,onChange:t=>l(s=>({...s,value:t.target.value})),placeholder:r.type==="percentage"?"20":"100",min:0,max:r.type==="percentage"?100:void 0,className:"bg-background border-border/50"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground uppercase tracking-wider mb-1.5 block",children:"Max Uses *"}),e.jsx(h,{type:"number",value:r.max_uses,onChange:t=>l(s=>({...s,max_uses:t.target.value})),min:1,className:"bg-background border-border/50"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-xs text-muted-foreground uppercase tracking-wider mb-1.5 block",children:"Expires (optional)"}),e.jsx(h,{type:"date",value:r.expires_at,onChange:t=>l(s=>({...s,expires_at:t.target.value})),className:"bg-background border-border/50"})]}),e.jsxs("div",{className:"sm:col-span-2",children:[e.jsx("label",{className:"text-xs text-muted-foreground uppercase tracking-wider mb-2 block",children:"Tier Restriction (leave empty for all tiers)"}),e.jsx("div",{className:"flex gap-2",children:[{key:"summit_pass",label:"Summit Pass"},{key:"all_access",label:"All Access"}].map(({key:t,label:s})=>e.jsx("button",{type:"button",onClick:()=>R(t),className:`px-3 py-1.5 rounded text-xs font-medium border transition-colors ${r.tier_restriction.includes(t)?"bg-primary/15 text-primary border-primary/30":"bg-muted text-muted-foreground border-border/30"}`,children:s},t))})]})]}),e.jsxs("div",{className:"flex justify-end gap-2 mt-5",children:[e.jsx(c,{variant:"outline",size:"sm",onClick:()=>m(!1),className:"border-border/50",children:"Cancel"}),e.jsx(c,{size:"sm",onClick:$,disabled:w,className:"bg-primary text-primary-foreground",children:w?"Savingâ¦":f?"Save Changes":"Create Code"})]})]}),e.jsx("div",{className:"border border-border/30 rounded-lg overflow-hidden",children:e.jsx("div",{className:"overflow-x-auto",children:e.jsxs(X,{children:[e.jsx(Y,{children:e.jsxs(p,{className:"border-border/30 hover:bg-transparent",children:[e.jsx(d,{className:"text-muted-foreground font-heading text-xs tracking-wider",children:"CODE"}),e.jsx(d,{className:"text-muted-foreground font-heading text-xs tracking-wider",children:"TYPE"}),e.jsx(d,{className:"text-muted-foreground font-heading text-xs tracking-wider",children:"TIERS"}),e.jsx(d,{className:"text-muted-foreground font-heading text-xs tracking-wider",children:"USES"}),e.jsx(d,{className:"text-muted-foreground font-heading text-xs tracking-wider",children:"EXPIRES"}),e.jsx(d,{className:"text-muted-foreground font-heading text-xs tracking-wider",children:"ACTIVE"}),e.jsx(d,{className:"w-10"}),e.jsx(d,{className:"w-10"})]})}),e.jsx(W,{children:y?e.jsx(p,{children:e.jsx(n,{colSpan:8,className:"text-center py-12 text-muted-foreground",children:"Loadingâ¦"})}):g.length===0?e.jsx(p,{children:e.jsx(n,{colSpan:8,className:"text-center py-12 text-muted-foreground",children:"No discount codes yet. Create one above."})}):g.map(t=>{var s;return e.jsxs(p,{className:`border-border/20 hover:bg-muted/20 ${t.is_active?"":"opacity-50"}`,children:[e.jsx(n,{className:"font-mono text-sm font-semibold text-primary tracking-wider",children:t.code}),e.jsx(n,{className:"text-sm text-foreground",children:M(t)}),e.jsx(n,{className:"text-xs text-muted-foreground",children:(s=t.tier_restriction)!=null&&s.length?t.tier_restriction.map(a=>a==="all_access"?"All Access":"Summit").join(", "):"All tiers"}),e.jsxs(n,{children:[e.jsxs("div",{className:"text-sm text-foreground",children:[t.current_uses," / ",t.max_uses]}),e.jsx("div",{className:"h-1 bg-muted rounded-full mt-1 overflow-hidden w-20",children:e.jsx("div",{className:"h-full bg-primary rounded-full",style:{width:`${Math.min(100,t.current_uses/t.max_uses*100)}%`}})})]}),e.jsx(n,{className:"text-xs text-muted-foreground",children:t.expires_at?new Date(t.expires_at).toLocaleDateString("en-CA",{month:"short",day:"numeric",year:"numeric"}):"No expiry"}),e.jsx(n,{children:e.jsx("button",{onClick:()=>F(t.id,t.is_active),disabled:D===t.id,className:"text-muted-foreground hover:text-foreground transition-colors",children:t.is_active?e.jsx(K,{className:"w-5 h-5 text-green-400"}):e.jsx(Q,{className:"w-5 h-5"})})}),e.jsx(n,{children:e.jsx(c,{size:"icon",variant:"ghost",className:"h-6 w-6 text-muted-foreground hover:text-foreground",onClick:()=>P(t),children:e.jsx(Z,{className:"w-3.5 h-3.5"})})}),e.jsx(n,{children:t.current_uses===0&&e.jsx(c,{size:"icon",variant:"ghost",className:"h-6 w-6 text-muted-foreground hover:text-destructive",onClick:()=>z(t.id,t.code),disabled:A===t.id,children:e.jsx(ee,{className:"w-3.5 h-3.5"})})})]},t.id)})})]})})})]})}export{be 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.