1import{j as e,U as z,ad as f,$ as J,r as u,s as g,K as E,a6 as h,aa as w,J as O}from"./index-CEqNdWMB.js";import{u as H,A as I}from"./usePermissions-BfkERgFT.js";import{L as x}from"./label-MzaZtEZW.js";import{C as K,c as Q}from"./card-CYkSJyY5.js";import{w as L,R as V,D as G}from"./withComplianceLocks-Dl6ZPFAM.js";import{u as X,E as Z,R as ee,C as te}from"./useRenewalReadiness-UONiz2g7.js";import{T as ae}from"./trash-2-D0k5VdET.js";import{L as D}from"./loader-circle-CSZF46_Z.js";import{S as A}from"./save-C88H4vFI.js";import{C as T}from"./check-CYl4c-Z5.js";import"./textarea-BCupudha.js";import"./checkbox-CcAbeY7r.js";import"./index-BSd2hjwl.js";import"./select-B9ajeO-3.js";import"./index-BdQq_4o_.js";import"./index-NdaxtzIS.js";import"./form-layout-CimphA65.js";import"./chevron-up-C686eY-E.js";import"./index-CEO5QSAz.js";import"./index-pPSH0L7x.js";import"./popover-CNmXZ7yJ.js";import"./list-BfBxiN3O.js";import"./dialog-C4_r9fMQ.js";import"./lock-HR5d3pcE.js";import"./arrow-up-Cd7LoR5K.js";import"./arrow-down-DTLOy3YU.js";import"./plus-RZqlGGkR.js";import"./user-plus-CE5CcnFh.js";import"./upload-2F43P0Uy.js";const se=i=>`Action required: Your ${i} 501(c)(3) renewal`,re=i=>({type:"doc",content:[{type:"paragraph",content:[{type:"text",text:"Hi {{first_name}},"}]},{type:"paragraph",content:[{type:"text",text:`It's time to complete your ${i} 501(c)(3) renewal for {{ministry_name}}. The form takes about 10 minutes. Use the secure button below to open it.`}]},{type:"paragraph",content:[{type:"text",text:"If you have any questions or need a paper copy, just reply to this email and we'll help."}]},{type:"paragraph",content:[{type:"text",text:"Thank you for partnering with us."}]},{type:"paragraph",content:[{type:"text",text:"â Christian International"}]}]}),ie=i=>[i.first_name,i.last_name].filter(Boolean).join(" ").trim()||"(no name)",B=({recipients:i,onRemove:c,removable:a=!0})=>e.jsxs("div",{className:"rounded-md border border-border",children:[e.jsxs("div",{className:"flex items-center gap-2 px-3 py-2 border-b border-border bg-muted/40",children:[e.jsx(z,{className:"h-4 w-4 text-muted-foreground"}),e.jsxs("h3",{className:"text-sm font-semibold",children:["Added recipients (",i.length,")"]})]}),i.length===0?e.jsx("p",{className:"px-3 py-6 text-sm text-muted-foreground text-center",children:"No recipients yet. Add one above or import a file."}):e.jsx("ul",{className:"max-h-72 overflow-auto divide-y divide-border",children:i.map(n=>e.jsxs("li",{className:"grid grid-cols-[1fr_auto] items-center gap-3 px-3 py-2 text-sm",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsxs("div",{className:"flex flex-wrap items-baseline gap-x-2",children:[e.jsx("span",{className:"font-medium truncate",children:ie(n)}),n.ministry_name&&e.jsxs("span",{className:"text-xs text-muted-foreground truncate",children:["· ",n.ministry_name]})]}),e.jsx("div",{className:"text-xs text-muted-foreground truncate",children:n.email})]}),a&&c&&e.jsx(f,{size:"sm",variant:"ghost",onClick:()=>c(n.id),className:"h-8 px-2 text-muted-foreground hover:text-destructive",children:e.jsx(ae,{className:"h-4 w-4"})})]},n.id))})]}),p=[{id:"details",title:"Details",sub:"Title, year, due date"},{id:"email",title:"Email",sub:"Subject and body"},{id:"form",title:"Form",sub:"What recipients will fill out"},{id:"recipients",title:"Recipients",sub:"Who receives the notice"},{id:"review",title:"Review",sub:"Save as draft"}],ne=()=>{const i=new Date().getFullYear()+1;return{year:i,title:`${i} 501(c)(3) Renewal`,due_date:null,from_name:null,reply_to:"[email protected]",subject:se(i),body_doc:re(i),form_schema:G,status:"draft"}},Me=()=>{const i=H(),c=J(),[a,n]=u.useState(ne),[r,M]=u.useState(null),[v,_]=u.useState([]),[l,N]=u.useState(0),[y,S]=u.useState(!1),[F,R]=u.useState(!1),o=p[l],b=async()=>{if(r)return r;S(!0);const{data:t,error:s}=await g.from("annual_renewals").insert({year:a.year,title:a.title,due_date:a.due_date,from_name:a.from_name,reply_to:a.reply_to,subject:a.subject,body_doc:a.body_doc,form_schema:a.form_schema,status:"draft"}).select("*").single();return S(!1),s||!t?(w({title:"Couldn't save draft",description:s==null?void 0:s.message,variant:"destructive"}),null):(M(t.id),n({...t}),t.id)},m=async t=>{if(n(d=>({...d,...t})),!r)return;R(!0);const{error:s}=await g.from("annual_renewals").update(t).eq("id",r);R(!1),s&&w({title:"Save failed",description:s.message,variant:"destructive"})};u.useEffect(()=>{r&&(o.id!=="recipients"&&o.id!=="review"||(async()=>{const{data:t}=await g.from("renewal_recipients").select("*").eq("renewal_id",r).is("deleted_at",null).order("created_at",{ascending:!0});_(t??[])})())},[r,o.id]);const k=X(r?{...a,id:r,created_at:"",updated_at:""}:{...a,id:"pending",created_at:"",updated_at:""},v),P=async()=>{var t;
1l===0&&!r&&!await b()||((t=p[l+1])==null?void 0:t.id)==="recipients"&&!r&&!await b()||N(s=>Math.min(s+1,p.length-1))},q=()=>N(t=>Math.max(t-1,0)),Y=async()=>{await b()&&(w({title:"Saved as draft"}),c("/admin/501c3-renewal"))},U=async()=>{const t=await b();t&&c(`/admin/501c3-renewal/${t}`)},W=()=>{if(!r){c("/admin/501c3-renewal");return}c("/admin/501c3-renewal")},$=u.useMemo(()=>{const t={};if(!a.form_schema)return t;const s=a.form_schema.sections.flatMap(d=>d.fields);return s.find(d=>d.id==="ministry_name")&&(t.ministry_name="Sample Ministry"),s.find(d=>d.id==="leader_first_name")&&(t.leader_first_name="Sample"),s.find(d=>d.id==="leader_last_name")&&(t.leader_last_name="Leader"),t},[a.form_schema]);return i.isLoading?e.jsx(I,{header:e.jsx("h1",{className:"text-2xl font-semibold",children:"New Renewal"}),children:e.jsxs("div",{className:"flex items-center gap-2 text-sm text-muted-foreground py-8",children:[e.jsx(D,{className:"h-4 w-4 animate-spin"})," Loadingâ¦"]})}):e.jsxs(I,{header:e.jsxs("div",{className:"flex flex-wrap items-start justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-semibold",children:"New Renewal â Setup"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["Step ",l+1," of ",p.length,": ",o.title," â ",o.sub]}),!r&&e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Nothing is saved yet. The draft is created when you click Next or Save as draft."})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[(F||y)&&e.jsxs("span",{className:"text-xs text-muted-foreground inline-flex items-center gap-1",children:[e.jsx(D,{className:"h-3 w-3 animate-spin"})," Saving"]}),e.jsx(f,{variant:"ghost",onClick:W,children:"Cancel"}),e.jsxs(f,{variant:"outline",onClick:Y,disabled:y,children:[e.jsx(A,{className:"h-4 w-4 mr-1.5"})," Save as draft and exit"]})]})]}),children:[e.jsx("ol",{className:"flex flex-wrap items-center gap-2 mb-6 text-xs",children:p.map((t,s)=>{const d=s<l,C=s===l;return e.jsxs("li",{className:"flex items-center gap-2",children:[e.jsxs("button",{type:"button",disabled:s>l,onClick:()=>s<=l&&N(s),className:["inline-flex items-center gap-1.5 rounded-full px-3 py-1.5 border transition",C?"bg-primary text-primary-foreground border-primary":d?"bg-muted text-foreground border-border hover:bg-accent":"bg-card text-muted-foreground border-border"].join(" "),children:[e.jsx("span",{className:"inline-flex items-center justify-center w-4 h-4 rounded-full bg-background/40 text-[10px]",children:d?e.jsx(T,{className:"h-3 w-3"}):s+1}),e.jsx("span",{children:t.title})]}),s<p.length-1&&e.jsx(E,{className:"h-3 w-3 text-muted-foreground"})]},t.id)})}),e.jsx(K,{children:e.jsxs(Q,{className:"p-6 space-y-5",children:[o.id==="details"&&e.jsxs("div",{className:"space-y-4 max-w-xl",children:[e.jsxs("div",{children:[e.jsx(x,{children:"Title"}),e.jsx(h,{value:a.title,onChange:t=>n({...a,title:t.target.value}),onBlur:()=>r&&m({title:a.title})})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(x,{children:"Year"}),e.jsx(h,{type:"number",value:a.year,onChange:t=>n({...a,year:parseInt(t.target.value||"0",10)}),onBlur:()=>r&&m({year:a.year})})]}),e.jsxs("div",{children:[e.jsx(x,{children:"Due date (optional)"}),e.jsx(h,{type:"date",value:a.due_date??"",onChange:t=>n({...a,due_date:t.target.value}),onBlur:()=>r&&m({due_date:a.due_date})})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx(x,{children:"From name"}),e.jsx(h,{value:a.from_name??"",placeholder:"Christian International",onChange:t=>n({...a,from_name:t.target.value}),onBlur:()=>r&&m({from_name:a.from_name})})]}),e.jsxs("div",{children:[e.jsx(x,{children:"Reply-to email"}),e.jsx(h,{type:"email",value:a.reply_to??"",placeholder:"[email protected]",onChange:t=>n({...a,reply_to:t.target.value}),onBlur:()=>r&&m({reply_to:a.reply_to})})]})]})]}),o.id==="email"&&e.jsxs("div",{className:"space-y-3 max-w-3xl",children:[e.jsxs("div",{children:[e.jsx(x,{children:"Subject line"}),e.jsx(h,{value:a.subject??"",onChange:t=>n({...a,subject:t.target.value}),onBlur:()=>m({subject:a.subject})})]}),e.jsxs("div",{children:[e.jsx(x,{className:"mb-2 block",children:"Email body"}),e.jsx(Z,{doc:a.body_doc,subject:a.subject??"",onChange:t=>{n({...a,body_doc:t}),m({body_doc:t})}})]})]}),o.id==="form"&&e.jsxs("div",{className:"space-y-3",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"A default form is pre-loaded with compliance-required sections (marked with a lock icon). You can add custom fields anywhere. Locked items can't be removed or retyped â that protects the renewal's legal posture. You can change real legal copy once Christian International leadership provides the final wording."}),e.jsx(ee,{schema:L(a.form_schema??{version:1,sections:[]}),onChange:t=>{n({...a,form_schema:t}),m({form_schema:t})}})]}),o.id==="recipients"&&e.jsxs("div",{className:"space-y-4 max-w-2xl",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Add recipients now or skip and add them later from the renewal's Manage page. The same email can receive more than one form if the ministry names are different â useful for leaders who oversee multiple 501(c)(3) organizations."}),r?e.jsxs(e.Fragment,{children:[e.jsx(te,{renewalId:r,onImported:async()=>{const{data:t}=await g.from("renewal_recipients").select("*").eq("renewal_id",r).is("deleted_at",null).order("created_at",{ascending:!0});_(t??[])}}),e.jsx(B,{recipients:v,onRemove:async t=>{const{error:s}=await g.from("renewal_recipients").update({deleted_at:new Date().toISOString()}).eq("id",t);if(s){w({title:"Couldn't remove",description:s.message,variant:"destructive"});return}_(d=>d.filter(C=>C.id!==t))}})]}):e.jsx("div",{className:"text-sm text-muted-foreground",children:"Preparing draftâ¦"})]}),o.id==="review"&&e.jsxs("div",{className:"space-y-4 max-w-2xl",children:[e.jsx("h3",{className:"text-base font-semibold",children:"Almost ready"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:["This is being saved as a ",e.jsx("span",{className:"font-medium text-foreground",children:"Draft"}),". It will appear in your Renewals list with a Draft badge. You can keep editing from the Manage page and only send notices when you're ready."]}),e.jsxs("div",{className:"rounded-md border border-border bg-card divide-y",children:[e.jsx(j,{label:"Title",value:a.title}),e.jsx(j,{label:"Year",value:String(a.year)}),e.jsx(j,{label:"Due date",value:a.due_date?new Date(a.due_date).toLocaleDateString():"â"}),e.jsx(j,{label:"Subject",value:a.subject||"(none)"}),e.jsx(j,{label:"Recipients",value:`${v.length}`})]}),e.jsx(B,{recipients:v,removable:!1}),e.jsxs("div",{className:"rounded-md border border-border bg-muted/30 p-3",children:[e.jsx("h4",{className:"text-sm font-medium mb-2",children:"Setup checklist"}
1),k.ready?e.jsxs("p",{className:"text-sm text-green-700 inline-flex items-center gap-1",children:[e.jsx(T,{className:"h-4 w-4"})," Ready to send when you are."]}):e.jsx("ul",{className:"space-y-1 text-sm",children:k.issues.map(t=>e.jsxs("li",{className:"text-muted-foreground",children:["⢠",t.label]},t.id))}),e.jsx("p",{className:"text-xs text-muted-foreground mt-2",children:"Incomplete items will be shown on the Manage page so you can fix them anytime."})]}),e.jsxs("details",{className:"rounded-md border border-border bg-card p-3 text-sm",children:[e.jsx("summary",{className:"cursor-pointer font-medium",children:"Quick form preview"}),e.jsx("div",{className:"mt-3 max-h-[400px] overflow-auto",children:e.jsx(V,{schema:L(a.form_schema??{version:1,sections:[]}),value:$,onChange:()=>{},errors:{},disabled:!0})})]})]})]})}),e.jsxs("div",{className:"mt-5 flex items-center justify-between",children:[e.jsxs(f,{variant:"ghost",onClick:q,disabled:l===0,children:[e.jsx(O,{className:"h-4 w-4 mr-1"})," Back"]}),l<p.length-1?e.jsxs(f,{onClick:P,disabled:y,children:["Next ",e.jsx(E,{className:"h-4 w-4 ml-1"})]}):e.jsxs(f,{onClick:U,disabled:y,children:[e.jsx(A,{className:"h-4 w-4 mr-1.5"})," Finish â go to Manage"]})]})]})},j=({label:i,value:c})=>e.jsxs("div",{className:"grid grid-cols-[8rem_1fr] gap-3 px-3 py-2 text-sm",children:[e.jsx("div",{className:"text-muted-foreground",children:i}),e.jsx("div",{className:"font-medium break-words",children:c})]});export{Me 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.