1import{j as e}from"./vendor-query-D1Mshrk-.js";import{r as h,L as u}from"./vendor-react-BfQMi0FG.js";import{u as Y,B as c,C as G,d as z,J as X,P as H,I as J,e as p,f as r,j as b,t as f}from"./index-D3kn74s7.js";import{C as N,d as P}from"./card-C6DfUXn8.js";import{S,a as D,b as _,c as F,d as m}from"./select-Da-jnnzS.js";import{S as w}from"./switch-vuioQ0-X.js";import{u as q,a as K,b as Q}from"./use-promotions-U4HcfCyU.js";import{P as W}from"./page-header-C7x1KRLA.js";import{D as Z}from"./data-table-BfHa5KKj.js";import{C as T}from"./confirm-dialog-CV5i7jpD.js";import{P as L}from"./pencil-BdlRQFJ9.js";import"./vendor-ui-nQy3bJlE.js";import"./check-CXo9hMCb.js";import"./table-B-sxBKmb.js";import"./alert-dialog-D07W3RYy.js";const E={PERCENTAGE_OFF:"Percentage Off",FIXED_AMOUNT_OFF:"Fixed Amount Off",FREE_DELIVERY:"Free Delivery",BUY_X_GET_Y:"Buy X Get Y",MEAL_DEAL:"Meal Deal",FREE_ITEM:"Free Item",POINTS_MULTIPLIER:"Points Multiplier",SPEND_AND_SAVE:"Spend & Save"};function $(a){return a.isActive?a.validUntil&&new Date(a.validUntil)<new Date?e.jsx(r,{variant:"destructive",children:"Expired"}):e.jsx(r,{variant:"default",children:"Active"}):e.jsx(r,{variant:"secondary",children:"Inactive"})}function I(a){switch(a.type){case"PERCENTAGE_OFF":return`${a.discountValue}%${a.maxDiscountAmount?` (max ${a.maxDiscountAmount})`:""}`;case"FIXED_AMOUNT_OFF":return`£${Number(a.discountValue).toFixed(2)} off`;case"FREE_DELIVERY":return"Free delivery";case"BUY_X_GET_Y":return"BOGO";case"MEAL_DEAL":return a.bundlePrice?`£${Number(a.bundlePrice).toFixed(2)}`:"Meal deal";default:return`${a.discountValue}`}}function U(a,l){if(!a&&!l)return"Always";const t=d=>new Date(d).toLocaleDateString("en-GB",{day:"2-digit",month:"short"});return a&&l?`${t(a)} - ${t(l)}`:a?`From ${t(a)}`:`Until ${t(l)}`}function O(a){const l=a._count?.redemptions??a.usageCount,t=a.totalUsageLimit;return t?`${l}/${t}`:`${l}`}function ge(){const a=Y(s=>s.isSuperAdmin),[l,t]=h.useState(1),[d,x]=h.useState(""),[j,k]=h.useState(""),[g,R]=h.useState(""),{data:o,isLoading:y}=q({page:l,limit:30,...d?{search:d}:{},...j?{type:j}:{},...g?{isActive:g}:{}}),V=K(),v=Q(),A=s=>{V.mutate(s,{onSuccess:()=>f.success("Promotion deleted"),onError:()=>f.error("Failed to delete promotion")})},C=(s,i)=>{v.mutate({id:s,isActive:i},{onError:()=>f.error("Failed to toggle promotion")})},B=[{header:"Name",cell:s=>e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:s.name}),s.code&&e.jsx(r,{variant:"outline",className:"mt-1",children:s.code})]})},{header:"Type",cell:s=>e.jsx(r,{variant:"secondary",children:E[s.type]??s.type})},{header:"Discount",cell:s=>I(s)},{header:"Status",cell:s=>$(s)},{header:"Usage",cell:s=>O(s)},{header:"Valid Period",cell:s=>U(s.validFrom,s.validUntil)},{header:"Channels",cell:s=>e.jsx("div",{className:"flex flex-wrap gap-1",children:(s.channels??[]).map(i=>e.jsx(r,{variant:"outline",className:"text-xs",children:i},i))})},{header:"Actions",cell:s=>e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(w,{checked:s.isActive,onCheckedChange:i=>C(s.id,i),disabled:v.isPending,"aria-label":"Toggle active"}),e.jsx(c,{variant:"ghost",size:"icon",asChild:!0,title:"Edit",children:e.jsx(u,{to:`/dashboard/promotions/${s.id}`,children:e.jsx(L,{className:"h-4 w-4"})})}),e.jsx(T,{trigger:e.jsx(c,{variant:"ghost",size:"icon",children:e.jsx(b,{className:"h-4 w-4 text-destructive"})}),title:"Delete Promotion",description:`Are you sure you want to delete "${s.name}"?`,onConfirm:()=>A(s.id)})]})}],n=o?.meta,M=n&&n.totalPages>1&&e.jsxs("div",{className:"flex items-center justify-between px-2",children:[e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Page ",n.page," of ",n.totalPages," (",n.total," total)"]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(c,{variant:"outline",size:"sm",disabled:n.page<=1,onClick:()=>t(n.page-1),children:e.jsx(G,{className:"h-4 w-4"})}),e.jsx(c,{variant:"outline",size:"sm",disabled:n.page>=n.totalPages,onClick:()=>t(n.page+1),children:e.jsx(z,{className:"h-4 w-4"})})]})]});return e.jsxs("div",{className:"space-y-6",children:[e.jsx(W,{title:"Promotions",description:"Manage promotional offers and discount codes",action:e.jsxs("div",{className:"flex gap-2",children:[a&&e.jsx(c,{variant:"outline",asChild:!0,children:e.jsxs(u,{to:"/dashboard/promotions/ai-create",children:[e.jsx(X,{className:"mr-2 h-4 w-4"}),"Create with AI"]})}),e.jsx(c,{asChild:!0,children:e.jsxs(u,{to:"/dashboard/promotions/new",children:[e.jsx(H,{className:"mr-2 h-4 w-4"}),"New Promotion"]})})]})}),e.jsxs("div",{className:"flex flex-col gap-3 sm:flex-row",children:[e.jsx(J,{placeholder:"Search by name or code...",value:d,onChange:s=>{x(s.target.value),t(1)},className:"sm:max-w-xs"}),e.jsxs(S,{value:j||"_all",onValueChange:s=>{k(s==="_all"?"":s),t(1)},children:[e.jsx(D,{className:"sm:w-48",children:e.jsx(_,{placeholder:"All types"})}),e.jsxs(F,{children:[e.jsx(m,{value:"_all",children:"All types"}
1),Object.entries(E).map(([s,i])=>e.jsx(m,{value:s,children:i},s))]})]}),e.jsxs(S,{value:g||"_all",onValueChange:s=>{R(s==="_all"?"":s),t(1)},children:[e.jsx(D,{className:"sm:w-36",children:e.jsx(_,{placeholder:"All statuses"})}),e.jsxs(F,{children:[e.jsx(m,{value:"_all",children:"All statuses"}),e.jsx(m,{value:"true",children:"Active"}),e.jsx(m,{value:"false",children:"Inactive"})]})]})]}),e.jsx("div",{className:"hidden md:block",children:e.jsx(Z,{columns:B,data:o?.promotions??[],meta:o?.meta,onPageChange:t,isLoading:y})}),e.jsxs("div",{className:"md:hidden space-y-3",children:[y?Array.from({length:5}).map((s,i)=>e.jsx(N,{children:e.jsxs(P,{className:"space-y-3 pt-4",children:[e.jsx(p,{className:"h-5 w-3/4"}),e.jsx(p,{className:"h-4 w-1/2"}),e.jsx(p,{className:"h-4 w-full"})]})},i)):o?.promotions.length===0?e.jsx(N,{children:e.jsx(P,{className:"py-8 text-center text-muted-foreground",children:"No promotions found."})}):o?.promotions.map(s=>e.jsx(ee,{promotion:s,onToggle:i=>C(s.id,i),toggleDisabled:v.isPending,onDelete:()=>A(s.id)},s.id)),M]})]})}function ee({promotion:a,onToggle:l,toggleDisabled:t,onDelete:d}){return e.jsx(N,{children:e.jsxs(P,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsx("p",{className:"font-semibold truncate",children:a.name}),a.code&&e.jsx(r,{variant:"outline",className:"mt-1",children:a.code})]}),e.jsx("div",{className:"ml-2 flex shrink-0 items-center gap-2",children:$(a)})]}),e.jsxs("div",{className:"mt-3 grid grid-cols-2 gap-x-4 gap-y-2 text-sm",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Type"}),e.jsx(r,{variant:"secondary",className:"mt-0.5",children:E[a.type]??a.type})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Discount"}),e.jsx("p",{className:"font-medium",children:I(a)})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Usage"}),e.jsx("p",{children:O(a)})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Valid"}),e.jsx("p",{children:U(a.validFrom,a.validUntil)})]}),e.jsxs("div",{className:"col-span-2",children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:"Channels"}),e.jsx("div",{className:"flex gap-1 mt-0.5",children:(a.channels??[]).map(x=>e.jsx(r,{variant:"outline",className:"text-xs",children:x},x))})]})]}),e.jsxs("div",{className:"mt-3 flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(w,{checked:a.isActive,onCheckedChange:l,disabled:t,"aria-label":"Toggle active"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:a.isActive?"Active":"Inactive"})]}),e.jsxs("div",{className:"flex gap-1",children:[e.jsx(c,{variant:"outline",size:"icon",className:"h-8 w-8",asChild:!0,title:"Edit",children:e.jsx(u,{to:`/dashboard/promotions/${a.id}`,children:e.jsx(L,{className:"h-3.5 w-3.5"})})}),e.jsx(T,{trigger:e.jsx(c,{variant:"outline",size:"icon",className:"h-8 w-8",children:e.jsx(b,{className:"h-3.5 w-3.5 text-destructive"})}),title:"Delete Promotion",description:`Are you sure you want to delete "${a.name}"?`,onConfirm:d})]})]})]})})}export{ge as PromotionListPage};
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.