1import{a5 as Q,k as Z,l as ee,r as l,s as S,q as c,j as e,L as te,B as g,A as se,C as k,c as L,d as P,n as T,e as E,v as i,I as w,bd as F,an as ae}from"./index-tvAeWDDu.js";import{T as H}from"./textarea-DCmtwFFs.js";import{R as re}from"./RichTextEditor-DOstvrjg.js";import"./index-fpNFZvnt.js";import"./index-D2JO0wo6.js";import"./with-selector-D7ZXHaTi.js";import"./select-D_9989fq.js";import"./index-BdQq_4o_.js";import"./chevron-down-DdqJBhHb.js";import"./chevron-up-eQONr_xY.js";import"./popover-dkSIJrHY.js";import"./list-Bh-YSaYX.js";import"./quote-BwikCpR4.js";import"./minus-D1ewOfKj.js";import"./plus-D42lu7Dc.js";import"./image-CaX07GNk.js";function _e(){const{id:x}=Q(),{user:h,isAdmin:R,isMarketing:O,isTrustee:B,loading:y}=Z(),p=R||O||B,f=ee(),d=!!x,[a,r]=l.useState({title:"",slug:"",content:"",meta_title:"",meta_description:"",is_published:!1,show_in_navigation:!1,navigation_order:0}),[M,u]=l.useState(!1),[I,U]=l.useState(d),[b,$]=l.useState("visual"),[A,W]=l.useState(!0),q=l.useRef(!1),V=t=>{try{const n=new DOMParser().parseFromString(t,"text/html"),K=new Set(["area","base","br","col","embed","hr","img","input","link","meta","param","source","track","wbr"]),j=[],N=o=>" ".repeat(o),D=(o,v)=>{if(o.nodeType===Node.TEXT_NODE){const m=o.textContent?.trim();m&&j.push(`${N(v)}${m}`);return}if(o.nodeType!==Node.ELEMENT_NODE)return;const C=o,_=C.tagName.toLowerCase(),z=Array.from(C.attributes).map(m=>` ${m.name}="${m.value}"`).join("");if(K.has(_)){j.push(`${N(v)}<${_}${z}>`);return}j.push(`${N(v)}<${_}${z}>`),Array.from(C.childNodes).forEach(m=>D(m,v+1)),j.push(`${N(v)}</${_}>`)};return Array.from(n.body.childNodes).forEach(o=>D(o,0)),j.join(` 2`).trim()||t}catch{return t}};l.useEffect(()=>{!y&&(!h||!p)&&f("/admin/login")},[h,p,y,f]),l.useEffect(()=>{d&&h&&p&&!q.current&&(q.current=!0,X())},[d,x,h,p]);const X=async()=>{if(!x)return;const{data:t,error:s}=await S.from("pages").select("*").eq("id",x).single();s?(c.error("Failed to load page"),console.error(s),f("/admin/pages")):t&&r({title:t.title,slug:t.slug,content:t.content,meta_title:t.meta_title||"",meta_description:t.meta_description||"",is_published:t.is_published,show_in_navigation:t.show_in_navigation,navigation_order:t.navigation_order}),U(!1)},Y=t=>t.toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,""),G=t=>{r(s=>({...s,title:t,slug:d?s.slug:Y(t)}))},J=async t=>{t.preventDefault(),u(!0);try{if(!a.title.trim()){c.error("Title is required"),u(!1);return}if(!a.slug.trim()){c.error("Slug is required"),u(!1);return}if(!a.content.trim()){c.error("Content is required"),u(!1);return}const s={...a,created_by:h?.id};if(d){const{error:n}=await S.from("pages").update(s).eq("id",x);if(n)throw n;c.success("Page updated successfully")}else{const{error:n}=await S.from("pages").insert([s]);if(n){if(n.code==="23505")c.error("A page with this slug already exists");else throw n;u(!1);return}c.success("Page created successfully")}f("/admin/pages")}catch(s){console.error("Error saving page:",s),c.error("Failed to save page"),u(!1)}};return y||!h||!p||I?null:e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx("header",{className:"border-b border-border",children:e.jsx("div",{className:"container mx-auto px-4 py-4",children:e.jsx(te,{to:"/admin/pages",children:e.jsxs(g,{variant:"ghost",size:"sm",children:[e.jsx(se,{className:"mr-2 h-4 w-4"}),"Back to Pages"]})})})}),e.jsxs("main",{className:"container mx-auto px-4 py-8 max-w-4xl",children:[e.jsx("h1",{className:"text-3xl font-bold text-foreground mb-8",children:d?"Edit Page":"Create New Page"}),e.jsxs("form",{onSubmit:J,className:"space-y-6",children:[e.jsxs(k,{children:[e.jsxs(L,{children:[e.jsx(P,{children:"Page Content"}),e.jsx(T,{children:"Basic information about your page"})]}),e.jsxs(E,{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(i,{htmlFor:"title",children:"Title *"}),e.jsx(w,{id:"title",value:a.title,onChange:t=>G(t.target.value),placeholder:"Page title",required:!0})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(i,{htmlFor:"slug",children:"URL Slug *"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-muted-foreground text-sm",children:"/page/"}),e.jsx(w,{id:"slug",value:a.slug,onChange:t=>r(s=>({...s,slug:t.target.value})),placeholder:"url-slug",required:!0})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"The URL path for this page. Use lowercase letters, numbers, and hyphens only."})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(i,{htmlFor:"content",children:"Content *"}),e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(g,{type:"button",variant:b==="visual"?"default":"outline",size:"sm",onClick:()=>$("visual"),children:"Visual"}),e.jsx(g,{type:"button",variant:b==="html"?"default":"outline",size:"sm",onClick:()=>$("html"),children:"HTML"})]}),b==="html"&&e.jsx(g,{type:"button",variant:"outline",size:"sm",onClick:()=>r(t=>({...t,content:V(t.content)})),children:"Format HTML"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(i,{htmlFor:"preview-toggle",className:"text-xs text-muted-foreground",children:"Live Preview"}),e.jsx(F,{id:"preview-toggle",checked:A,onCheckedChange:W})]})]})]}),b==="visual"?e.jsxs("div",{className:"mt-1",children:[e.jsx(re,{value:a.content,onChange:t=>r(s=>({...s,content:t})),placeholder:"Write your page content here..."}),e.jsx("p",{className:"text-xs text-muted-foreground mt-2",children:"Switch to HTML for custom markup or embeds."})]}):e.jsxs("div",{className:"mt-1",children:[e.jsx(H,{id:"content",value:a.content,onChange:t=>r(s=>({...s,content:t.target.value})),placeholder:"Write your page content here...",rows:12,required:!0}),e.jsx("p",{className:"text-xs text-muted-foreground mt-2",children:"You can paste full HTML here. Switch back to Visual for basic formatting."})]}),A&&e.jsxs("div",{className:"mt-4 rounded-md border border-input bg-muted/20 p-4",children:[e.jsx("p",{className:"text-xs font-semibold uppercase tracking-wide text-muted-foreground mb-3",children:"Live Preview"}),e.jsx("div",{className:"prose prose-sm max-w-none",dangerouslySetInnerHTML:{__html:ae(a.content||"")}})]})]})]})]}),e.jsxs(k,{children:[e.jsxs(L,{children:[e.jsx(P,{children:"SEO Settings"}),e.jsx(T,{children:"Optimize for search engines"})]}),e.jsxs(E,{className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(i,{htmlFor:"meta_title",children:"Meta Title"}),e.jsx(w,{id:"meta_title",value:a.meta_title,onChange:t=>r(s=>({...s,meta_title:t.target.value})),placeholder:"Leave blank to use page title",maxLength:60}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[a.meta_title.length,"/60 characters"]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(i,{htmlFor:"meta_description",children:"Meta Description"}),e.jsx(H,{id:"meta_description",value:a.meta_description,onChange:t=>r(s=>({...s,meta_description:t.target.value})),placeholder:"A brief description of this page",rows:3,maxLength:160}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[a.meta_description.length,"/160 characters"]})]})]})]}),e.jsxs(k,{children:[e.jsxs(L,{children:[e.jsx(P,{children:"Page Settings"}),e.jsx(T,{children:"Control visibility and navigation"})]}),e.jsxs(E,{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"space-y-0.5",children:[e.jsx(i,{htmlFor:"is_published",children:"Published"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Make this page visible to the public"})]}),e.jsx(F,{id:"is_published",checked:a.is_published,onCheckedChange:t=>r(s=>({...s,is_published:t}))})]}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{className:"space-y-0.5",children:[e.jsx(i,{htmlFor:"show_in_navigation",children:"Show in Navigation"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Add this page to the site navigation menu"})]}),e.jsx(F,{id:"show_in_navigation",checked:a.show_in_navigation,onCheckedChange:t=>r(s=>({...s,show_in_navigation:t}))})]}),a.show_in_navigation&&e.jsxs("div",{className:"space-y-2",children:[e.jsx(i,{htmlFor:"navigation_order",children:"Navigation Order"}),e.jsx(w,{id:"navigation_order",type:"number",value:a.navigation_order,onChange:t=>r(s=>({...s,navigation_order:parseInt(t.target.value)||0})),min:0}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Lower numbers appear first in the navigation menu"})]})]})]}),e.jsxs("div",{className:"flex gap-4",children:[e.jsx(g,{type:"submit",disabled:M,children:M?"Saving...":d?"Update Page":"Create Page"}),e.jsx(g,{type:"button",variant:"outline",onClick:()=>f("/admin/pages"),children:"Cancel"})]})]})]})]})}export{_e 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.