import{r as p,j as e,s as c,t as o}from"./index-CEqTxpJr.js";function j(){const[n,r]=p.useState([]),[t,a]=p.useState(null);async function d(){const{data:s}=await c.from("blog_posts").select("id,title,slug,category,published").order("created_at",{ascending:!1});r(s??[])}p.useEffect(()=>{d()},[]);async function f(s){if(s.preventDefault(),!t)return;const i=new FormData(s.currentTarget),x=Object.fromEntries(i.entries());x.published=i.get("published")==="on";const m=t.id,{error:u}=m?await c.from("blog_posts").update(x).eq("id",m):await c.from("blog_posts").insert(x);u?o.error(u.message):(o.success("Saved"),a(null),d())}async function h(s){if(!confirm("Delete this post?"))return;const{error:i}=await c.from("blog_posts").delete().eq("id",s);i?o.error(i.message):(o.success("Deleted"),d())}return e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("h1",{className:"font-display text-3xl font-bold",children:"Blog Posts"}),e.jsx("button",{onClick:()=>a({}),className:"bg-navy px-4 py-2 text-[10px] font-bold uppercase tracking-widest text-ivory hover:bg-gold hover:text-navy",children:"+ New Post"})]}),e.jsx("div",{className:"mt-8 overflow-x-auto bg-white ring-1 ring-navy/10",children:e.jsxs("table",{className:"min-w-full text-sm",children:[e.jsx("thead",{className:"bg-navy/5 text-left text-[10px] uppercase tracking-widest",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-4 py-3",children:"Title"}),e.jsx("th",{className:"px-4 py-3",children:"Category"}),e.jsx("th",{className:"px-4 py-3",children:"Status"}),e.jsx("th",{className:"px-4 py-3"})]})}),e.jsx("tbody",{children:n.map(s=>e.jsxs("tr",{className:"border-t border-navy/5",children:[e.jsx("td",{className:"px-4 py-3 font-medium",children:s.title}),e.jsx("td",{className:"px-4 py-3 text-xs",children:s.category}),e.jsx("td",{className:"px-4 py-3 text-xs",children:s.published?"Live":"Draft"}),e.jsxs("td",{className:"px-4 py-3 text-right",children:[e.jsx("button",{onClick:()=>a(s),className:"text-xs underline",children:"Edit"}),e.jsx("button",{onClick:()=>h(s.id),className:"ml-3 text-xs text-destructive underline",children:"Delete"})]})]},s.id))})]})}),t&&e.jsx("div",{className:"fixed inset-0 z-50 grid place-items-center bg-navy/40 p-4",onClick:()=>a(null),children:e.jsxs("form",{onSubmit:f,onClick:s=>s.stopPropagation(),className:"max-h-[90vh] w-full max-w-2xl overflow-y-auto bg-white p-8",children:[e.jsx("h2",{className:"font-display text-xl font-bold",children:t.id?"Edit Post":"New Post"}),e.jsxs("div",{className:"mt-6 grid gap-4",children:[e.jsx(l,{name:"title",label:"Title",def:t.title}),e.jsx(l,{name:"slug",label:"Slug",def:t.slug}),e.jsx(l,{name:"category",label:"Category",def:t.category}),e.jsx(l,{name:"cover_image",label:"Cover Image URL",def:t.cover_image}),e.jsx(b,{name:"excerpt",label:"Excerpt",def:t.excerpt,rows:2}),e.jsx(b,{name:"content",label:"Content",def:t.content,rows:10}),e.jsx(l,{name:"meta_title",label:"SEO Title",def:t.meta_title}),e.jsx(l,{name:"meta_description",label:"SEO Description",def:t.meta_description}),e.jsxs("label",{className:"flex items-center gap-2 text-xs",children:[e.jsx("input",{type:"checkbox",name:"published",defaultChecked:t.published!==!1})," Published"]})]}),e.jsxs("div",{className:"mt-6 flex justify-end gap-3",children:[e.jsx("button",{type:"button",onClick:()=>a(null),className:"px-4 py-2 text-xs",children:"Cancel"}),e.jsx("button",{type:"submit",className:"bg-navy px-6 py-2 text-[10px] font-bold uppercase tracking-widest text-ivory hover:bg-gold hover:text-navy",children:"Save"})]})]})})]})}function l({name:n,label:r,def:t}){return e.jsxs("label",{className:"block",children:[e.jsx("span",{className:"block text-[10px] uppercase tracking-widest text-navy/50",children:r}),e.jsx("input",{name:n,defaultValue:t||"",className:"mt-1 w-full border-b border-navy/15 bg-transparent py-2 text-sm outline-none focus:border-gold"})]})}function b({name:n,label:r,def:t,rows:a=4}){return e.jsxs("label",{className:"block",children:[e.jsx("span",{className:"block text-[10px] uppercase tracking-widest text-navy/50",children:r}),e.jsx("textarea",{name:n,defaultValue:t||"",rows:a,className:"mt-1 w-full border-b border-navy/15 bg-transparent py-2 text-sm outline-none focus:border-gold"})]})}export{j as component};
