1import{r as a,b as R,u as V,e as d,j as e,H as G,B as l,A as J,P as K,d as h,ae as U,z as W,D as X,c as w,I as D,Q as Y}from"./index-B46WE3Zi.js";import{s as Z}from"./slugify-DEJq8_e7.js";import{D as S,a as k,b as E,c as F,d as M,e as $}from"./dialog-CDXMAqnv.js";const ae=()=>{const[p,A]=a.useState([]),[P,f]=a.useState(!0),[B,n]=a.useState(!1),[g,j]=a.useState(null),[r,o]=a.useState({name:"",slug:""}),[y,b]=a.useState(!1),[O,c]=a.useState(!1),[x,N]=a.useState(null),[m,v]=a.useState(!1),{toast:i}=R(),q=V();a.useEffect(()=>{u()},[]);const u=async()=>{f(!0);try{const{data:s,error:t}=await d.from("blog_categories").select("*, blog_posts(count)").order("name",{ascending:!0});if(t)throw t;const Q=s.map(C=>({...C,postCount:C.blog_posts[0]?.count||0}));A(Q)}catch(s){console.error("Error fetching categories:",s),i({title:"Error",description:"Failed to load categories.",variant:"destructive"})}finally{f(!1)}},I=s=>{const t=s.target.value;o({name:t,slug:Z(t)})},T=async s=>{if(s.preventDefault(),!r.name||!r.slug){i({title:"Validation Error",description:"Name and slug are required.",variant:"destructive"});return}b(!0);try{if(g){const{error:t}=await d.from("blog_categories").update({name:r.name,slug:r.slug}).eq("id",g.id);if(t)throw t;i({title:"Success",description:"Category updated successfully."})}else{const{error:t}=await d.from("blog_categories").insert([{name:r.name,slug:r.slug}]);if(t)throw t;i({title:"Success",description:"Category created successfully."})}n(!1),u()}catch(t){i({title:"Error",description:t.message||"Failed to save category.",variant:"destructive"})}finally{b(!1)}},_=async()=>{if(x){v(!0);try{const{error:s}=await d.from("blog_categories").delete().eq("id",x.id);if(s)throw s;i({title:"Success",description:"Category deleted. Posts were unassigned.",className:"bg-green-500 text-white"}),c(!1),N(null),u()}catch(s){i({title:"Error",description:s.message||"Failed to delete category.",variant:"destructive"})}finally{v(!1)}}},L=()=>{j(null),o({name:"",slug:""}),n(!0)},z=s=>{j(s),o({name:s.name,slug:s.slug}),n(!0)},H=s=>{N(s),c(!0)};return e.jsx("div",{className:"min-h-screen bg-gray-50 pb-32",children:e.jsxs("div",{className:"container mx-auto px-4 py-8",children:[e.jsx(G,{children:e.jsx("title",{children:"Admin - Manage Blog Categories"})}),e.jsxs("div",{className:"flex flex-col md:flex-row justify-between items-start md:items-center mb-8 gap-4",children:[e.jsxs("div",{children:[e.jsxs(l,{variant:"ghost",className:"mb-2 pl-0 hover:pl-2 transition-all",onClick:()=>q("/admin/blog"),children:[e.jsx(J,{className:"w-4 h-4 mr-2"})," Back to Blog Posts"]}),e.jsx("h1",{className:"text-3xl font-bold text-gray-900",children:"Blog Categories"}),e.jsx("p",{className:"text-gray-500 mt-1",children:"Manage categories for your blog posts."})]}),e.jsxs(l,{className:"bg-[var(--primary-blue)] hover:bg-blue-700 text-white flex items-center gap-2",onClick:L,children:[e.jsx(K,{className:"w-4 h-4"})," Add Category"]})]}),P?e.jsx("div",{className:"bg-white rounded-xl shadow-sm border border-gray-200 p-8 flex justify-center",children:e.jsx(h,{className:"w-8 h-8 text-[var(--primary-blue)] animate-spin"})}):e.jsx("div",{className:"bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden",children:e.jsxs("table",{className:"w-full text-left",children:[e.jsx("thead",{className:"bg-gray-50 border-b border-gray-200",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-6 py-4 text-xs font-bold text-gray-500 uppercase tracking-wider",children:"Name"}),e.jsx("th",{className:"px-6 py-4 text-xs font-bold text-gray-500 uppercase tracking-wider",children:"Slug"}),e.jsx("th",{className:"px-6 py-4 text-xs font-bold text-gray-500 uppercase tracking-wider text-center",children:"Post Count"}),e.jsx("th",{className:"px-6 py-4 text-xs font-bold text-gray-500 uppercase tracking-wider text-right",children:"Actions"})]})}),e.jsx("tbody",{className:"divide-y divide-gray-200",children:p.length===0?e.jsx("tr",{children:e.jsx("td",{colSpan:"4",className:"px-6 py-12 text-center text-gray-500 italic",children:e.jsxs("div",{className:"flex flex-col items-center",children:[e.jsx(U,{className:"w-12 h-12 text-gray-300 mb-2"}),e.jsx("p",{children:"No categories found."})]})})}):p.map(s=>e.jsxs("tr",{className:"hover:bg-gray-50 transition-colors",children:[e.jsx("td",{className:"px-6 py-4 font-medium text-gray-900",children:s.name}),e.jsx("td",{className:"px-6 py-4 text-gray-500",children:s.slug}),e.jsx("td",{className:"px-6 py-4 text-center",children:e.jsx("span",{className:"inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800",children:s.postCount})}),e.jsx("td",{className:"px-6 py-4 text-right",children:e.jsxs("div",{className:"flex items-center justify-end gap-2",children:[e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>z(s),className:"text-blue-600 hover:bg-blue-50",children:e.jsx(W,{className:"w-4 h-4"})}),e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>H(s),className:"text-red-600 hover:bg-red-50",children:e.jsx(X,{className:"w-4 h-4"})})]})})]},s.id))})]})}),e.jsx(S,{open:B,onOpenChange:n,children:e.jsxs(k,{children:[e.jsx(E,{children:e.jsx(F,{children:g?"Edit Category":"Add New Category"})}),e.jsxs("form",{onSubmit:T,className:"space-y-4 pt-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(w,{htmlFor:"name",children:"Name *"}),e.jsx(D,{id:"name",value:r.name,onChange:I,required:!0})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(w,{htmlFor:"slug",children:"Slug *"}),e.jsx(D,{id:"slug",value:r.slug,onChange:s=>o({...r,slug:s.target.value}),required:!0})]}),e.jsxs(M,{className:"pt-4",children:[e.jsx(l,{type:"button",variant:"outline",onClick:()=>n(!1),children:"Cancel"}),e.jsxs(l,{type:"submit",disabled:y,children:[y&&e.jsx(h,{className:"w-4 h-4 mr-2 animate-spin"})," Save"]})]})]})]})}),e.jsx(S,{open:O,onOpenChange:c,children:e.jsxs(k,{children:[e.jsxs(E,{children:[e.jsxs(F,{className:"flex items-center gap-2 text-red-600",children:[e.jsx(Y,{className:"w-5 h-5"})," Confirm Deletion"]}),e.jsxs($,{children:["Delete category '",x?.name,"'? Posts will be unassigned but not deleted."]})]}),e.jsxs(M,{className:"mt-4",children:[e.jsx(l,{variant:"outline",onClick:()=>c(!1),disabled:m,children:"Cancel"}),e.jsxs(l,{variant:"destructive",onClick:_,disabled:m,children:[m?e.jsx(h,{className:"w-4 h-4 animate-spin mr-2"}):null," Confirm Delete"]})]})]})})]})})};export{ae 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.