1import{c as Ae,ay as W,r as A,s as K,az as E,j as e,C as p,a as D,b as q,U as le,d as v,b5 as xe,aO as Me,aP as Le,aQ as he,q as De,x as U,p as d,aS as je,B as Y,dd as ge,aF as Z,aG as ee,aH as se,aI as te,b4 as me,a0 as M,aA as R,i as Fe,an as Ee,a9 as ce,y as qe,bD as ye,bN as fe,cg as be,bj as ue,bE as ve,bF as g,bG as y,bH as f,bI as b,bJ as C,a$ as pe,S as O,l as V,m as B,n as Q,o as S,bK as H,c2 as Re,cA as Ke,cB as $e,k as Oe,bT as N,cK as Ve,ch as Be}from"./index-dn_A-Uuf.js";import{S as Ne}from"./switch-DW6lvyQY.js";import{T as Ce,a as we,b as J,c as k,d as Pe,e as I}from"./table-BqaNjL1u.js";import{D as Qe}from"./dollar-sign-JvdcY73g.js";import{P as G}from"./plus-EyV51iby.js";import{C as de}from"./circle-check-big-ud8n1KTu.js";import{C as oe}from"./circle-x-oHWZPErl.js";import{C as ze}from"./calendar-CfMCfocE.js";import{f as z}from"./format-KyDl7DBR.js";import"./en-US-DlkruSZM.js";const He=Ae("Pen",[["path",{d:"M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z",key:"1a8usu"}]]),Ue=be({name:N().min(1,"Name is required"),description:N().optional(),monthlyPrice:N().min(1,"Monthly price is required"),annualPrice:N().min(1,"Annual price is required"),features:N().optional(),isActive:Ve().default(!0),sortOrder:N().default("0"),linkedLoyaltyProgramId:N().optional(),linkedLoyaltyTierId:N().optional(),linkedMembershipClassId:N().optional(),revenueAccountId:N().optional()}),_e=be({customerId:N().min(1,"Customer is required"),planId:N().min(1,"Plan is required"),billingCycle:Be(["monthly","annual"])});function w(s){return new Intl.NumberFormat("en-US",{style:"currency",currency:"USD"}).format(s/100)}function X({status:s}){const o={active:"default",trialing:"secondary",paused:"outline",cancelled:"destructive",past_due:"destructive",pending:"outline",paid:"default",failed:"destructive",refunded:"secondary",void:"outline"};return e.jsx(Y,{variant:o[s]||"outline",className:"capitalize",children:s.replace("_"," ")})}function Ge({plan:s,onEdit:o,onDelete:h}){const j=Array.isArray(s.features)?s.features:[],l=(s.monthlyPrice*12-s.annualPrice)/100;return e.jsxs(p,{className:`relative ${s.isActive?"":"opacity-60"}`,children:[e.jsx(D,{className:"pb-3",children:e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsxs("div",{children:[e.jsx(q,{className:"text-lg",children:s.name}),s.description&&e.jsx(Fe,{className:"mt-1",children:s.description})]}),e.jsxs("div",{className:"flex gap-1",children:[e.jsx(d,{variant:"ghost",size:"icon",onClick:o,"data-testid":`button-edit-plan-${s.id}`,children:e.jsx(He,{className:"h-4 w-4"})}),e.jsx(d,{variant:"ghost",size:"icon",onClick:h,"data-testid":`button-delete-plan-${s.id}`,children:e.jsx(Ee,{className:"h-4 w-4 text-destructive"})})]})]})}),e.jsxs(v,{className:"space-y-4",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"bg-muted/50 rounded-lg p-3",children:[e.jsx("div",{className:"text-xs text-muted-foreground mb-1",children:"Monthly"}),e.jsx("div",{className:"text-xl font-bold",children:w(s.monthlyPrice)})]}),e.jsxs("div",{className:"bg-muted/50 rounded-lg p-3",children:[e.jsx("div",{className:"text-xs text-muted-foreground mb-1",children:"Annual"}),e.jsx("div",{className:"text-xl font-bold",children:w(s.annualPrice)}),l>0&&e.jsxs("div",{className:"text-xs text-primary",children:["Save $",l.toFixed(0),"/yr"]})]})]}),s.linkedProgramName&&s.linkedTierName&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(ce,{className:"h-4 w-4 text-amber-500"}),e.jsxs("span",{children:[s.linkedTierName," tier â ",s.linkedProgramName]})]}),!s.linkedProgramName&&s.linkedLoyaltyTier&&e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(ce,{className:"h-4 w-4 text-amber-500"}),e.jsxs("span",{className:"capitalize",children:[s.linkedLoyaltyTier," tier benefits included"]})]}),j.length>0&&e.jsxs("div",{className:"space-y-1",children:[e.jsx("div",{className:"text-xs font-medium text-muted-foreground uppercase",children:"Features"}),e.jsx("ul",{className:"text-sm space-y-1",children:j.map((m,x)=>e.jsxs("li",{className:"flex items-center gap-2",children:[e.jsx(de,{className:"h-3 w-3 text-primary"}),m]},x))})]})]}),e.jsxs(ge,{className:"border-t pt-4 flex-wrap gap-3",children:[e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(le,{className:"h-4 w-4 text-muted-foreground"}),e.jsxs("span",{children:[s.activeSubscriptions," active"]})]}),e.jsxs("div",{className:"flex items-center gap-2 text-sm",children:[e.jsx(qe,{className:"h-4 w-4 text-muted-foreground"}),e.jsxs("span",{children:[w(s.totalRevenue)," revenue"]})]}),!s.isActive&&e.jsx(Y,{variant:"outline",children:"Inactive"})]})]})}function Je({plan:s,open:o,onOpenChange:h}){const{toast:j}=W(),{data:l}=K({queryKey:["/api/loyalty-programs"]}),{data:m=[]}
1=K({queryKey:["/api/membership-classes"]}),{data:x=[]}=K({queryKey:["/api/gl/accounts"]}),F=x.filter(t=>t.isActive&&t.accountType?.code==="REVENUE").sort((t,n)=>t.code.localeCompare(n.code)),u=(l||[]).filter(t=>t.status==="active"),L=m.filter(t=>t.isActive),i=ye({resolver:fe(Ue),defaultValues:{name:s?.name||"",description:s?.description||"",monthlyPrice:s?(s.monthlyPrice/100).toFixed(2):"",annualPrice:s?(s.annualPrice/100).toFixed(2):"",features:s?.features?.join(` 2`)||"",isActive:s?.isActive??!0,sortOrder:s?.sortOrder?.toString()||"0",linkedLoyaltyProgramId:s?.linkedLoyaltyProgramId?.toString()||"",linkedLoyaltyTierId:s?.linkedLoyaltyTierId?.toString()||"",linkedMembershipClassId:s?.linkedMembershipClassId?.toString()||"",revenueAccountId:s?.revenueAccountId?.toString()||""}});A.useEffect(()=>{o&&i.reset({name:s?.name||"",description:s?.description||"",monthlyPrice:s?(s.monthlyPrice/100).toFixed(2):"",annualPrice:s?(s.annualPrice/100).toFixed(2):"",features:s?.features?.join(` 3`)||"",isActive:s?.isActive??!0,sortOrder:s?.sortOrder?.toString()||"0",linkedLoyaltyProgramId:s?.linkedLoyaltyProgramId?.toString()||"",linkedLoyaltyTierId:s?.linkedLoyaltyTierId?.toString()||"",linkedMembershipClassId:s?.linkedMembershipClassId?.toString()||"",revenueAccountId:s?.revenueAccountId?.toString()||""})},[o,s]);const c=i.watch("linkedLoyaltyProgramId"),P=u.find(t=>t.id.toString()===c),T=P?.mode==="graduated"?(P?.tiers||[]).sort((t,n)=>t.sortOrder-n.sortOrder):[],r=E({mutationFn:async t=>await R("POST","/api/subscription-plans",t),onSuccess:()=>{M.invalidateQueries({queryKey:["/api/subscription-plans"]}),j({title:"Success",description:"Plan created successfully"}),h(!1),i.reset()},onError:t=>{j({title:"Error",description:t.message,variant:"destructive"})}}),$=E({mutationFn:async t=>await R("PATCH",`/api/subscription-plans/${s?.id}`,t),onSuccess:()=>{M.invalidateQueries({queryKey:["/api/subscription-plans"]}),j({title:"Success",description:"Plan updated successfully"}),h(!1)},onError:t=>{j({title:"Error",description:t.message,variant:"destructive"})}}),ae=t=>{const n={name:t.name,description:t.description||null,monthlyPrice:Math.round(parseFloat(t.monthlyPrice)*100),annualPrice:Math.round(parseFloat(t.annualPrice)*100),trialDays:0,features:t.features?t.features.split(` 4`).filter(Boolean):[],isActive:t.isActive,sortOrder:parseInt(t.sortOrder)||0,linkedLoyaltyProgramId:t.linkedLoyaltyProgramId?parseInt(t.linkedLoyaltyProgramId):null,linkedLoyaltyTierId:t.linkedLoyaltyTierId?parseInt(t.linkedLoyaltyTierId):null,linkedLoyaltyTier:null,linkedMembershipClassId:t.linkedMembershipClassId?parseInt(t.linkedMembershipClassId):null,revenueAccountId:t.revenueAccountId?parseInt(t.revenueAccountId):null};s?$.mutate(n):r.mutate(n)};return e.jsx(Z,{open:o,onOpenChange:h,children:e.jsxs(ee,{className:"max-w-lg max-h-[90vh] overflow-y-auto",children:[e.jsxs(se,{children:[e.jsx(te,{children:s?"Edit Plan":"Create New Plan"}),e.jsx(ue,{children:s?"Update the subscription plan details":"Define a new subscription plan for your customers"})]}),e.jsx(ve,{...i,children:e.jsxs("form",{onSubmit:i.handleSubmit(ae),className:"space-y-4",children:[e.jsx(g,{control:i.control,name:"name",render:({field:t})=>e.jsxs(y,{children:[e.jsx(f,{children:"Plan Name"}),e.jsx(b,{children:e.jsx(U,{placeholder:"e.g., Premium Membership",...t,"data-testid":"input-plan-name"})}),e.jsx(C,{})]})}),e.jsx(g,{control:i.control,name:"description",render:({field:t})=>e.jsxs(y,{children:[e.jsx(f,{children:"Description"}),e.jsx(b,{children:e.jsx(pe,{placeholder:"Brief description of the plan...",...t,"data-testid":"input-plan-description"})}),e.jsx(C,{})]})}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsx(g,{control:i.control,name:"monthlyPrice",render:({field:t})=>e.jsxs(y,{children:[e.jsx(f,{children:"Monthly Price ($)"}),e.jsx(b,{children:e.jsx(U,{type:"number",step:"0.01",placeholder:"29.99",...t,"data-testid":"input-monthly-price"})}),e.jsx(C,{})]})}),e.jsx(g,{control:i.control,name:"annualPrice",render:({field:t})=>e.jsxs(y,{children:[e.jsx(f,{children:"Annual Price ($)"}),e.jsx(b,{children:e.jsx(U,{type:"number",step:"0.01",placeholder:"299.00",...t,"data-testid":"input-annual-price"})}),e.jsx(C,{})]})})]}),e.jsx(g,{control:i.control,name:"features",render:({field:t})=>e.jsxs(y,{children:[e.jsx(f,{children:"Features (one per line)"}),e.jsx(b,{children:e.jsx(pe,{placeholder:`Unlimited access 5Priority support 6Exclusive discounts`,rows:4,...t,"data-testid":"input-features"})}),e.jsx(C,{})]})}),e.jsx(g,{control:i.control,name:"linkedLoyaltyProgramId",render:({field:t})=>e.jsxs(y,{children:[e.jsxs(f,{className:"flex items-center gap-2",children:[e.jsx(ce,{className:"h-4 w-4"}),"Linked Loyalty Program"]}),e.jsxs(O,{onValueChange:n=>
6{t.onChange(n==="none"?"":n),(n==="none"||n===""||u.find(ie=>ie.id.toString()===n)?.mode==="single_tier")&&i.setValue("linkedLoyaltyTierId","")},value:t.value||"none",children:[e.jsx(b,{children:e.jsx(V,{"data-testid":"select-linked-loyalty-program",children:e.jsx(B,{placeholder:"No program linked"})})}),e.jsxs(Q,{children:[e.jsx(S,{value:"none",children:"No program linked"}),u.map(n=>e.jsx(S,{value:n.id.toString(),children:n.name},n.id))]})]}),e.jsx(H,{children:"Subscribers will be automatically enrolled in this loyalty program"}),e.jsx(C,{})]})}),P&&T.length>0&&e.jsx(g,{control:i.control,name:"linkedLoyaltyTierId",render:({field:t})=>e.jsxs(y,{children:[e.jsx(f,{children:"Linked Tier"}),e.jsxs(O,{onValueChange:t.onChange,value:t.value||"",children:[e.jsx(b,{children:e.jsx(V,{"data-testid":"select-linked-loyalty-tier",children:e.jsx(B,{placeholder:"Select a tier"})})}),e.jsx(Q,{children:T.map(n=>e.jsxs(S,{value:n.id.toString(),children:[n.name," (",n.discountRate/100,"% discount)"]},n.id))})]}),e.jsx(H,{children:"Subscribers will be locked to this tier while their subscription is active"}),e.jsx(C,{})]})}),P&&P.mode==="single_tier"&&e.jsxs("div",{className:"text-sm text-muted-foreground bg-muted/50 rounded-lg p-3",children:[e.jsx("span",{className:"font-medium",children:P.singleTierName})," â subscribers will be enrolled as members with ",P.singleTierDiscountRate/100,"% discount benefits"]}),e.jsx(g,{control:i.control,name:"linkedMembershipClassId",render:({field:t})=>e.jsxs(y,{children:[e.jsxs(f,{className:"flex items-center gap-2",children:[e.jsx(Re,{className:"h-4 w-4"}),"Linked Membership Class"]}),e.jsxs(O,{onValueChange:n=>t.onChange(n==="none"?"":n),value:t.value||"none",children:[e.jsx(b,{children:e.jsx(V,{"data-testid":"select-linked-membership-class",children:e.jsx(B,{placeholder:"No membership class linked"})})}),e.jsxs(Q,{children:[e.jsx(S,{value:"none",children:"No membership class linked"}),L.map(n=>e.jsx(S,{value:n.id.toString(),children:n.name},n.id))]})]}),e.jsx(H,{children:"Subscribers will be automatically assigned to this membership class"}),e.jsx(C,{})]})}),e.jsx(g,{control:i.control,name:"revenueAccountId",render:({field:t})=>e.jsxs(y,{children:[e.jsx(f,{children:"Revenue Account (GL)"}),e.jsxs(O,{onValueChange:n=>t.onChange(n==="none"?"":n),value:t.value||"none",children:[e.jsx(b,{children:e.jsx(V,{"data-testid":"select-plan-revenue-account",children:e.jsx(B,{placeholder:"Default (4300 Membership Dues Revenue)"})})}),e.jsxs(Q,{children:[e.jsx(S,{value:"none",children:"Default (4300 Membership Dues Revenue)"}),F.map(n=>e.jsxs(S,{value:n.id.toString(),children:[n.code," â ",n.name]},n.id))]})]}),e.jsx(H,{children:"Optional override of which revenue account dues for this plan post to. Leave on Default unless your accountant has set up dedicated dues accounts."}),e.jsx(C,{})]})}),e.jsx(g,{control:i.control,name:"isActive",render:({field:t})=>e.jsxs(y,{className:"flex items-center justify-between rounded-lg border p-3",children:[e.jsxs("div",{className:"space-y-0.5",children:[e.jsx(f,{children:"Active"}),e.jsx(H,{children:"Make this plan available for new subscriptions"})]}),e.jsx(b,{children:e.jsx(Ne,{checked:t.value,onCheckedChange:t.onChange,"data-testid":"switch-plan-active"})})]})}),e.jsxs(me,{children:[e.jsx(d,{type:"button",variant:"outline",onClick:()=>h(!1),children:"Cancel"}),e.jsx(d,{type:"submit",disabled:r.isPending||$.isPending,"data-testid":"button-save-plan",children:r.isPending||$.isPending?"Saving...":s?"Update Plan":"Create Plan"})]})]})})]})})}function Xe({open:s,onOpenChange:o}){const{toast:h}=W(),{data:j}=K({queryKey:["/api/customers"]}),{data:l}=K({queryKey:["/api/subscription-plans"]}),m=ye({resolver:fe(_e),defaultValues:{customerId:"",planId:"",billingCycle:"monthly"}}),x=E({mutationFn:async i=>await R("POST","/api/subscriptions",i),onSuccess:()=>{M.invalidateQueries({queryKey:["/api/subscriptions"]}),M.invalidateQueries({queryKey:["/api/subscription-plans"]}),h({title:"Success",description:"Subscription created successfully"}),o(!1),m.reset()},onError:i=>{h({title:"Error",description:i.message,variant:"destructive"})}}),F=i=>
6{x.mutate({customerId:parseInt(i.customerId),planId:parseInt(i.planId),billingCycle:i.billingCycle})},u=l?.find(i=>i.id.toString()===m.watch("planId")),L=m.watch("billingCycle");return e.jsx(Z,{open:s,onOpenChange:o,children:e.jsxs(ee,{children:[e.jsxs(se,{children:[e.jsx(te,{children:"Create Subscription"}),e.jsx(ue,{children:"Subscribe a customer to a recurring billing plan"})]}),e.jsx(ve,{...m,children:e.jsxs("form",{onSubmit:m.handleSubmit(F),className:"space-y-4",children:[e.jsx(g,{control:m.control,name:"customerId",render:({field:i})=>e.jsxs(y,{children:[e.jsx(f,{children:"Customer"}),e.jsxs(O,{onValueChange:i.onChange,value:i.value,children:[e.jsx(b,{children:e.jsx(V,{"data-testid":"select-customer",children:e.jsx(B,{placeholder:"Select a customer"})})}),e.jsx(Q,{children:j?.map(c=>e.jsxs(S,{value:c.id.toString(),children:[c.name," ",c.email&&`(${c.email})`]},c.id))})]}),e.jsx(C,{})]})}),e.jsx(g,{control:m.control,name:"planId",render:({field:i})=>e.jsxs(y,{children:[e.jsx(f,{children:"Plan"}),e.jsxs(O,{onValueChange:i.onChange,value:i.value,children:[e.jsx(b,{children:e.jsx(V,{"data-testid":"select-plan",children:e.jsx(B,{placeholder:"Select a plan"})})}),e.jsx(Q,{children:l?.filter(c=>c.isActive).map(c=>e.jsxs(S,{value:c.id.toString(),children:[c.name," - ",w(c.monthlyPrice),"/mo"]},c.id))})]}),e.jsx(C,{})]})}),e.jsx(g,{control:m.control,name:"billingCycle",render:({field:i})=>e.jsxs(y,{children:[e.jsx(f,{children:"Billing Cycle"}),e.jsxs(O,{onValueChange:i.onChange,value:i.value,children:[e.jsx(b,{children:e.jsx(V,{"data-testid":"select-billing-cycle",children:e.jsx(B,{})})}),e.jsxs(Q,{children:[e.jsxs(S,{value:"monthly",children:["Monthly ",u&&`- ${w(u.monthlyPrice)}/month`]}),e.jsxs(S,{value:"annual",children:["Annual ",u&&`- ${w(u.annualPrice)}/year`]})]})]}),e.jsx(C,{})]})}),u&&e.jsx(p,{className:"bg-muted/50",children:e.jsx(v,{className:"pt-4",children:e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{className:"text-sm text-muted-foreground",children:"First charge:"}),e.jsx("span",{className:"font-semibold",children:w(L==="annual"?u.annualPrice:u.monthlyPrice)})]})})}),e.jsxs(me,{children:[e.jsx(d,{type:"button",variant:"outline",onClick:()=>o(!1),children:"Cancel"}),e.jsx(d,{type:"submit",disabled:x.isPending,"data-testid":"button-create-subscription",children:x.isPending?"Creating...":"Create Subscription"})]})]})})]})})}function We({subscription:s,open:o,onOpenChange:h,onSubscriptionUpdate:j}){const{toast:l}=W(),{data:m}=K({queryKey:["/api/subscriptions",s.id,"history"],enabled:o}),x=E({mutationFn:async r=>await R("POST",`/api/subscriptions/${s.id}/cancel`,{reason:r}),onSuccess:()=>{M.invalidateQueries({queryKey:["/api/subscriptions"]}),M.invalidateQueries({queryKey:["/api/subscription-plans"]}),l({title:"Subscription cancelled"}),h(!1)},onError:r=>{l({title:"Error",description:r.message,variant:"destructive"})}}),F=E({mutationFn:async()=>await R("POST",`/api/subscriptions/${s.id}/pause`,{}),onSuccess:()=>{M.invalidateQueries({queryKey:["/api/subscriptions"]}),l({title:"Subscription paused"}),j?.({...s,status:"paused"})},onError:r=>{l({title:"Error",description:r.message,variant:"destructive"})}}),u=E({mutationFn:async()=>await R("POST",`/api/subscriptions/${s.id}/resume`,{}),onSuccess:()=>{M.invalidateQueries({queryKey:["/api/subscriptions"]}),l({title:"Subscription resumed"}),j?.({...s,status:"active"})},onError:r=>{l({title:"Error",description:r.message,variant:"destructive"})}}),L=E({mutationFn:async r=>await R("PATCH",`/api/subscriptions/${s.id}`,{autoRenew:r}),onSuccess:()=>{M.invalidateQueries({queryKey:["/api/subscriptions"]}),l({title:"Auto-renewal updated"})},onError:r=>{l({title:"Error",description:r.message,variant:"destructive"})}}),[i,c]=A.useState(""),[P,T]=A.useState(!1);return e.jsx(Z,{open:o,onOpenChange:h,children:e.jsxs(ee,{className:"max-w-2xl",children:[e.jsxs(se,{children:[e.jsx(te,{children:"Subscription Details"}),e.jsxs(ue,{children:[s.customer.name," - ",s.plan.name]})]}),e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs(p,{children:[e.jsx(D,{className:"pb-2",children:e.jsx(q,{className:"text-sm font-medium text-muted-foreground",children:"Status"})}),e.jsx(v,{children:e.jsx(X,{status:s.status})})]}),e.jsxs(p,{children:[e.jsx(D,{className:"pb-2",children:e.jsx(q,{className:"text-sm font-medium text-muted-foreground",children:"Billing"})}),e.jsxs(v,{children:[e.jsx("span",{className:"capitalize",children:s.billingCycle}),e.jsxs("span",{className:"text-muted-foreground ml-2",children:["(",w(s.billingCycle==="annual"?s.plan.annualPrice:s.plan.monthlyPrice),")"]})]})]}),e.jsxs(p,{children:[e.jsx(D,{className:"pb-2",children:e.jsx(q,{className:"text-sm font-medium text-muted-foreground",children:"Current Period"})}),e.jsxs(v,{className:"text-sm",children:[z(new Date(s.currentPeriodStart),"MMM d, yyyy")," - ",z(new Date(s.currentPeriodEnd),"MMM d, yyyy")]})]}),e.jsxs(p,{children:[e.jsx(D,{className:"pb-2",children:e.jsx(q,{className:"text-sm font-medium text-muted-foreground",children:"Auto-Renew"})}),e.jsx(v,{children:e.jsx(Ne,{checked:s.autoRenew,onCheckedChange:r=>L.mutate(r),disabled:s.status==="cancelled","data-testid":"switch-auto-renew"})})]})]}),e.jsxs("div",{className:"flex flex-wrap gap-2",children:[s.status==="active"&&e.jsxs(d,{variant:"outline",onClick:()=>F.mutate(),disabled:F.isPending,"data-testid":"button-pause-subscription",children:[e.jsx(Ke,{className:"h-4 w-4 mr-2"}),"Pause"]}),s.status==="paused"&&e.jsxs(d,{variant:"outline",onClick:()=>u.mutate(),disabled:u.isPending,"data-testid":"button-resume-subscription",children:[e.jsx($e,{className:"h-4 w-4 mr-2"}),"Resume"]}
6),s.status!=="cancelled"&&e.jsxs(d,{variant:"destructive",onClick:()=>T(!0),"data-testid":"button-cancel-subscription",children:[e.jsx(oe,{className:"h-4 w-4 mr-2"}),"Cancel"]})]}),P&&e.jsx(p,{className:"border-destructive",children:e.jsxs(v,{className:"pt-4 space-y-4",children:[e.jsxs("div",{className:"flex items-center gap-2 text-destructive",children:[e.jsx(Oe,{className:"h-5 w-5"}),e.jsx("span",{className:"font-medium",children:"Confirm Cancellation"})]}),e.jsx(U,{placeholder:"Reason for cancellation...",value:i,onChange:r=>c(r.target.value),"data-testid":"input-cancel-reason"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(d,{variant:"outline",onClick:()=>T(!1),children:"Keep Subscription"}),e.jsx(d,{variant:"destructive",onClick:()=>x.mutate(i),disabled:x.isPending,"data-testid":"button-confirm-cancel",children:x.isPending?"Cancelling...":"Confirm Cancel"})]})]})}),s.invoices&&s.invoices.length>0&&e.jsxs("div",{children:[e.jsx("h4",{className:"font-medium mb-2",children:"Recent Invoices"}),e.jsxs(Ce,{children:[e.jsx(we,{children:e.jsxs(J,{children:[e.jsx(k,{children:"Invoice"}),e.jsx(k,{children:"Date"}),e.jsx(k,{children:"Amount"}),e.jsx(k,{children:"Status"})]})}),e.jsx(Pe,{children:s.invoices.slice(0,5).map(r=>e.jsxs(J,{children:[e.jsx(I,{className:"font-mono text-xs",children:r.invoiceNumber}),e.jsx(I,{children:z(new Date(r.createdAt),"MMM d, yyyy")}),e.jsx(I,{children:w(r.totalAmount)}),e.jsx(I,{children:e.jsx(X,{status:r.status})})]},r.id))})]})]}),m&&m.length>0&&e.jsxs("div",{children:[e.jsx("h4",{className:"font-medium mb-2",children:"Activity History"}),e.jsx("div",{className:"space-y-2 max-h-40 overflow-y-auto",children:m.map(r=>e.jsxs("div",{className:"flex items-center gap-2 text-sm text-muted-foreground",children:[e.jsx("span",{className:"text-xs",children:z(new Date(r.createdAt),"MMM d, yyyy HH:mm")}),e.jsx(Y,{variant:"outline",className:"capitalize",children:r.action.replace("_"," ")}),r.notes&&e.jsxs("span",{children:["- ",r.notes]})]},r.id))})]})]})]})})}function cs(){const{toast:s}=W(),[o,h]=A.useState("subscriptions"),[j,l]=A.useState(!1),[m,x]=A.useState(),[F,u]=A.useState(!1),[L,i]=A.useState(null),[c,P]=A.useState(""),[T,r]=A.useState(null),{data:$,isLoading:ae}=K({queryKey:["/api/subscription-plans"]}),{data:t,isLoading:n}=K({queryKey:["/api/subscriptions"]}),_=E({mutationFn:async a=>{await R("DELETE",`/api/subscription-plans/${a}`,{})},onSuccess:()=>{M.invalidateQueries({queryKey:["/api/subscription-plans"]}),s({title:"Plan deactivated"})},onError:a=>{s({title:"Error",description:a.message,variant:"destructive"})}}),ie=a=>{x(a),l(!0)},Se=()=>{l(!1),x(void 0)},ne=t?.filter(a=>a.customer.name.toLowerCase().includes(c.toLowerCase())||a.plan.name.toLowerCase().includes(c.toLowerCase())||a.customer.email?.toLowerCase().includes(c.toLowerCase())),ke=t?.filter(a=>a.status==="active").length||0,Ie=t?.filter(a=>a.status==="active").reduce((a,re)=>{const Te=re.billingCycle==="annual"?re.plan.annualPrice/12:re.plan.monthlyPrice;return a+Te},0)||0;return e.jsxs("div",{className:"space-y-6 px-2 md:px-0",children:[e.jsx("div",{className:"flex items-center justify-between flex-wrap gap-4",children:e.jsxs("div",{children:[e.jsx("h1",{className:"text-xl md:text-3xl font-heading font-bold",children:"Subscription Plans"}),e.jsx("p",{className:"text-muted-foreground",children:"Manage subscription plans and member billing"})]})}),e.jsxs("div",{className:"grid grid-cols-3 gap-3 md:gap-4",children:[e.jsxs(p,{children:[e.jsxs(D,{className:"flex flex-row items-center justify-between space-y-0 pb-2 gap-2",children:[e.jsx(q,{className:"text-sm font-medium",children:"Active Subscriptions"}),e.jsx(le,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(v,{children:e.jsx("div",{className:"text-2xl font-bold","data-testid":"text-active-subscriptions",children:ke})})]}),e.jsxs(p,{children:[e.jsxs(D,{className:"flex flex-row items-center justify-between space-y-0 pb-2 gap-2",children:[e.jsx(q,{className:"text-xs sm:text-sm font-medium",children:"Monthly Recurring Revenue"}),e.jsx(Qe,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(v,{children:e.jsx("div",{className:"text-2xl font-bold","data-testid":"text-mrr",children:w(Ie)})})]}),e.jsxs(p,{children:[e.jsxs(D,{className:"flex flex-row items-center justify-between space-y-0 pb-2 gap-2",children:[e.jsx(q,{className:"text-sm font-medium",children:"Available Plans"}),e.jsx(xe,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(v,{children:e.jsx("div",{className:"text-2xl font-bold","data-testid":"text-plans-count",children:$?.filter(a=>a.isActive).length||0})})]})]}),e.jsxs(Me,{value:o,onValueChange:h,children:[e.jsxs("div",{className:"flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between",children:[e.jsxs(Le,{className:"w-full sm:w-auto",children:[e.jsx(he,{value:"subscriptions","data-testid":"tab-subscriptions",children:"Subscriptions"}),e.jsx(he,{value:"plans","data-testid":"tab-plans",children:"Plans"})]}),e.jsxs("div",{className:"flex gap-2 w-full sm:w-auto",children:[o==="subscriptions"&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"relative flex-1 sm:flex-none",children:[e.jsx(De,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(U,{placeholder:"Search subscriptions...",value:c,onChange:a=>P(a.target.value),className:"pl-9 w-full sm:w-64","data-testid":"input-search-subscriptions"})]}),e.jsxs(d,{onClick:()=>u(!0),"data-testid":"button-new-subscription",children:[e.jsx(G,{className:"h-4 w-4 sm:mr-2"}),e.jsx("span",{className:"hidden sm:inline",children:"New Subscription"})]})]}),o==="plans"&&e.jsxs(d,{onClick:()=>{x(void 0),l(!0)},"data-testid":"button-new-plan",children:[e.jsx(G,{className:"h-4 w-4 sm:mr-2"}),e.jsx("span",{className:"hidden sm:inline",children:"New Plan"})]})]})]}),e.jsx(je,{value:"subscriptions",className:"mt-6",children:n?e.jsx("div",{className:"text-center py-12 text-muted-foreground",children:"Loading subscriptions..."}):ne?.length===0?e.jsx(p,{children:e.jsxs(v,{className:"text-center py-12",children:[e.jsx(le,{className:"h-12 w-12 text-muted-foreground mx-auto mb-4"}),e.jsx("h3",{className:"text-lg font-medium mb-2",children:"No subscriptions yet"}),e.jsx("p",{className:"text-muted-foreground mb-4",children:"Create your first subscription to start billing customers"}),e.jsxs(d,{onClick:()=>u(!0),"data-testid":"button-create-first-subscription",children:[e.jsx(G,{className:"h-4 w-4 mr-2"}),"Create Subscription"]})]})}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"hidden md:block",children:e.jsx(p,{children:e.jsxs(Ce,{children:[e.jsx(we,{children:e.jsxs(J,{children:[e.jsx(k,{children:"Customer"}),e.jsx(k,{children:"Plan"}),e.jsx(k,{children:"Billing"}),e.jsx(k,{children:"Status"}),e.jsx(k,{children:"Next Billing"}),e.jsx(k,{children:"Auto-Renew"}),e.jsx(k,{className:"w-24"})]})}),e.jsx(Pe,{children:ne?.map(a=>e.jsxs(J,{"data-testid":`row-subscription-${a.id}`,children:[e.jsx(I,{children:e.jsxs("div",{children:[e.jsx("div",{className:"font-medium",children:a.customer.name}),e.jsx("div",{className:"text-xs text-muted-foreground",children:a.customer.email})]})}),e.jsx(I,{children:a.plan.name}),e.jsxs(I,{children:[e.jsx("div",{className:"capitalize",children:a.billingCycle}),e.jsx("div",{className:"text-xs text-muted-foreground",children:w(a.billingCycle==="annual"?a.plan.annualPrice:a.plan.monthlyPrice)})]}),e.jsx(I,{children:e.jsx(X,{status:a.status})}
6),e.jsx(I,{children:a.status!=="cancelled"&&z(new Date(a.currentPeriodEnd),"MMM d, yyyy")}),e.jsx(I,{children:a.autoRenew?e.jsx(de,{className:"h-4 w-4 text-primary"}):e.jsx(oe,{className:"h-4 w-4 text-muted-foreground"})}),e.jsx(I,{children:e.jsx(d,{variant:"ghost",size:"sm",onClick:()=>i(a),"data-testid":`button-view-subscription-${a.id}`,children:"View"})})]},a.id))})]})})}),e.jsx("div",{className:"md:hidden space-y-3",children:ne?.map(a=>e.jsxs(p,{"data-testid":`card-subscription-${a.id}`,children:[e.jsxs(D,{className:"flex flex-row items-center justify-between gap-2 space-y-0 pb-2",children:[e.jsxs("div",{children:[e.jsx("div",{className:"font-medium",children:a.customer.name}),e.jsx("div",{className:"text-xs text-muted-foreground",children:a.customer.email})]}),e.jsx(X,{status:a.status})]}),e.jsxs(v,{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-sm",children:a.plan.name}),e.jsx(Y,{variant:"outline",className:"capitalize",children:a.billingCycle})]}),e.jsx("div",{className:"text-sm text-muted-foreground",children:w(a.billingCycle==="annual"?a.plan.annualPrice:a.plan.monthlyPrice)}),a.status!=="cancelled"&&e.jsxs("div",{className:"flex items-center gap-2 text-sm text-muted-foreground",children:[e.jsx(ze,{className:"h-4 w-4"}),e.jsxs("span",{children:["Next billing: ",z(new Date(a.currentPeriodEnd),"MMM d, yyyy")]})]}),e.jsx("div",{className:"flex items-center gap-2 text-sm",children:a.autoRenew?e.jsxs(e.Fragment,{children:[e.jsx(de,{className:"h-4 w-4 text-primary"}),e.jsx("span",{children:"Auto-renew on"})]}):e.jsxs(e.Fragment,{children:[e.jsx(oe,{className:"h-4 w-4 text-muted-foreground"}),e.jsx("span",{className:"text-muted-foreground",children:"Auto-renew off"})]})})]}),e.jsx(ge,{children:e.jsx(d,{variant:"outline",className:"w-full",onClick:()=>i(a),"data-testid":`button-view-subscription-${a.id}`,children:"View"})})]},a.id))})]})}),e.jsx(je,{value:"plans",className:"mt-6",children:ae?e.jsx("div",{className:"text-center py-12 text-muted-foreground",children:"Loading plans..."}):$?.length===0?e.jsx(p,{children:e.jsxs(v,{className:"text-center py-12",children:[e.jsx(xe,{className:"h-12 w-12 text-muted-foreground mx-auto mb-4"}),e.jsx("h3",{className:"text-lg font-medium mb-2",children:"No plans created yet"}),e.jsx("p",{className:"text-muted-foreground mb-4",children:"Create subscription plans to offer recurring billing to customers"}),e.jsxs(d,{onClick:()=>{x(void 0),l(!0)},"data-testid":"button-create-first-plan",children:[e.jsx(G,{className:"h-4 w-4 mr-2"}),"Create Plan"]})]})}):e.jsx("div",{className:"grid gap-4 md:grid-cols-2 lg:grid-cols-3",children:$?.map(a=>e.jsx(Ge,{plan:a,onEdit:()=>ie(a),onDelete:()=>r({id:a.id,name:a.name})},a.id))})})]}),e.jsx(Je,{plan:m,open:j,onOpenChange:Se}),e.jsx(Xe,{open:F,onOpenChange:u}),L&&e.jsx(We,{subscription:L,open:!!L,onOpenChange:a=>!a&&i(null),onSubscriptionUpdate:i}),e.jsx(Z,{open:T!==null,onOpenChange:a=>{a||r(null)},children:e.jsxs(ee,{className:"max-w-sm",children:[e.jsx(se,{children:e.jsx(te,{children:"Delete Subscription Plan?"})}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Are you sure you want to delete ",e.jsx("strong",{children:T?.name}),"? This action cannot be undone."]}),e.jsxs(me,{children:[e.jsx(d,{variant:"outline",onClick:()=>r(null),"data-testid":"button-cancel-delete-plan",children:"Cancel"}),e.jsx(d,{variant:"destructive",disabled:_.isPending,onClick:()=>{T&&(_.mutate(T.id),r(null))},"data-testid":"button-confirm-delete-plan",children:_.isPending?"Deleting...":"Delete"})]})]})})]})}export{cs 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.