1import{b as Q,r as j,u as H,d as v,j as e}from"./vendor-query-B0-uRJqS.js";import{D as L}from"./DualLayout-Cj3Hqufw.js";import{u as I,aU as U,s as d,B as n,_ as B,ay as R,bF as $,bG as G,az as V,aK as J,t as W,v as X,w as Y,x as Z,L as N,I as ee,T as se,S as ae,a9 as te,ae as ie,af as re,ag as ne,ah as oe,aj as ce,ak as le,al as de,am as ue}from"./index-DNYOfx3x.js";import{T as me,a as he,b as A,c as u,d as pe,e as m}from"./table-ALWKH3Dv.js";import{a as xe}from"./vendor-react-CW1TH9gT.js";import{A as ge,a as je}from"./arrow-up-eIH6DXRH.js";import{P as ve}from"./pencil-BBrucd4V.js";import{T as ye}from"./trash-2-DmYLl9RK.js";import"./vendor-radix-Mcgp0sn6.js";import"./vendor-charts-BGFylbhW.js";function ke({layoutType:D="admin"}){const{user:y}=I(),{toast:o}=U(),T=xe(),h=Q(),[E,p]=j.useState(!1),[x,f]=j.useState(null),[c,C]=j.useState(null),[i,l]=j.useState({name:"",description:"",is_active:!0}),{data:r=[],isLoading:k}=H({queryKey:["aptitude-categories"],queryFn:async()=>{const{data:s,error:a}=await d.from("aptitude_categories").select("*").order("display_order",{ascending:!0});if(a)throw a;return s}}),_=v({mutationFn:async s=>{const a=Math.max(...r.map(P=>P.display_order),-1),{error:t}=await d.from("aptitude_categories").insert({name:s.name,description:s.description||null,is_active:s.is_active,display_order:a+1,created_by:y==null?void 0:y.id});if(t)throw t},onSuccess:()=>{h.invalidateQueries({queryKey:["aptitude-categories"]}),o({title:"Category created successfully"}),w()},onError:s=>{o({title:"Error creating category",description:s.message,variant:"destructive"})}}),b=v({mutationFn:async({id:s,data:a})=>{const{error:t}=await d.from("aptitude_categories").update({name:a.name,description:a.description||null,is_active:a.is_active}).eq("id",s);if(t)throw t},onSuccess:()=>{h.invalidateQueries({queryKey:["aptitude-categories"]}),o({title:"Category updated successfully"}),w()},onError:s=>{o({title:"Error updating category",description:s.message,variant:"destructive"})}}),q=v({mutationFn:async s=>{const{error:a}=await d.from("aptitude_categories").delete().eq("id",s);if(a)throw a},onSuccess:()=>{h.invalidateQueries({queryKey:["aptitude-categories"]}),o({title:"Category deleted successfully"}),C(null)},onError:s=>{o({title:"Error deleting category",description:s.message,variant:"destructive"})}}),g=v({mutationFn:async({id:s,newOrder:a})=>{const{error:t}=await d.from("aptitude_categories").update({display_order:a}).eq("id",s);if(t)throw t},onSuccess:()=>{h.invalidateQueries({queryKey:["aptitude-categories"]})}}),F=()=>{f(null),l({name:"",description:"",is_active:!0}),p(!0)},S=s=>{f(s),l({name:s.name,description:s.description||"",is_active:s.is_active}),p(!0)},w=()=>{p(!1),f(null),l({name:"",description:"",is_active:!0})},O=()=>{if(!i.name.trim()){o({title:"Name is required",variant:"destructive"});return}x?b.mutate({id:x.id,data:i}):_.mutate(i)},M=s=>{if(s===0)return;const a=r[s],t=r[s-1];g.mutate({id:a.id,newOrder:t.display_order}),g.mutate({id:t.id,newOrder:a.display_order})},z=s=>{if(s===r.length-1)return;const a=r[s],t=r[s+1];g.mutate({id:a.id,newOrder:t.display_order}),g.mutate({id:t.id,newOrder:a.display_order})},K=D==="expert"?"/expert":"/admin";return e.jsxs(L,{title:"Aptitude Exams",breadcrumb:"Categories",layoutType:D,children:[e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-2xl font-bold",children:"Categories"}),e.jsx("p",{className:"text-muted-foreground",children:"Manage aptitude exam categories"})]}),e.jsxs(n,{onClick:F,children:[e.jsx(B,{className:"mr-2 h-4 w-4"}),"Add Category"]})]}),e.jsxs(R,{children:[e.jsx($,{children:e.jsx(G,{children:"All Categories"})}),e.jsx(V,{children:k?e.jsx("p",{children:"Loading..."}):r.length===0?e.jsx("p",{className:"text-muted-foreground",children:"No categories yet. Create your first category!"}):e.jsxs(me,{children:[e.jsx(he,{children:e.jsxs(A,{children:[e.jsx(u,{className:"w-12",children:"Order"}),e.jsx(u,{children:"Name"}),e.jsx(u,{children:"Description"}),e.jsx(u,{className:"w-24",children:"Status"}),e.jsx(u,{className:"w-40",children:"Actions"})]})}),e.jsx(pe,{children:r.map((s,a)=>e.jsxs(A,{children:[e.jsx(m,{children:e.jsxs("div",{className:"flex flex-col gap-1",children:[e.jsx(n,{variant:"ghost",size:"icon",className:"h-6 w-6",onClick:()=>M(a),disabled:a===0,children:e.jsx(ge,{className:"h-3 w-3"})}),e.jsx(n,{variant:"ghost",size:"icon",className:"h-6 w-6",onClick:()=>z(a),disabled:a===r.length-1,children:e.jsx(je,{className:"h-3 w-3"})})]})}),e.jsx(m,{className:"font-medium",children:s.name}),e.jsx(m,{className:"max-w-xs truncate",children:s.description||"-"}),e.jsx(m,{children:e.jsx("span",{className:`inline-flex items-center rounded-full px-2 py-1 text-xs font-medium ${s.is_active?"bg-green-100 text-green-700":"bg-gray-100 text-gray-700"}`,children:s.is_active?"Active":"Inactive"})}),e.jsx(m,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(n,{variant:"ghost",size:"icon",onClick:()=>T(`${K}/aptitude/${s.id}`),children:e.jsx(J,{className:"h-4 w-4"})}),e.jsx(n,{variant:"ghost",size:"icon",onClick:()=>S(s),children:e.jsx(ve,{className:"h-4 w-4"})}),e.jsx(n,{variant:"ghost",size:"icon",onClick:()=>C(s),children:e.jsx(ye,{className:"h-4 w-4 text-destructive"})})]})})]},s.id))})]})})]})]}),e.jsx(W,{open:E,onOpenChange:p,children:e.jsxs(X,{children:[e.jsx(Y,{children:e.jsx(Z,{children:x?"Edit Category":"Create Category"})}),e.jsxs("div",{className:"space-y-4 py-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(N,{htmlFor:"name",children:"Name *"}),e.jsx(ee,{id:"name",value:i.name,onChange:s=>l({...i,name:s.target.value}),placeholder:"e.g., Verbal Reasoning"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(N,{htmlFor:"description",children:"Description"}),e.jsx(se,{id:"description",value:i.description,onChange:s=>l({...i,description:s.target.value}),placeholder:"Optional description for this category",rows:3})]}),e.jsxs("div",{className:"flex items-center space-x-2",children:[e.jsx(ae,{id:"is_active",checked:i.is_active,onCheckedChange:s=>l({...i,is_active:s})}),e.jsx(N,{htmlFor:"is_active",children:"Active"})]})]}),e.jsxs(te,{children:[e.jsx(n,{variant:"outline",onClick:w,children:"Cancel"}),e.jsx(n,{onClick:O,disabled:_.isPending||b.isPending,children:x?"Update":"Create"})]})]})}),e.jsx(ie,{open:!!c,onOpenChange:()=>C(null),children:e.jsxs(re,{children:[e.jsxs(ne,{children:[e.jsx(oe,{children:"Delete Category"}),e.jsxs(ce,{children:['Are you sure you want to delete "',c==null?void 0:c.name,'"? This will also delete all questions under this category. This action cannot be undone.']})]}),e.jsxs(le,{children:[e.jsx(de,{children:"Cancel"}),e.jsx(ue,{onClick:()=>c&&q.mutate(c.id),className:"bg-destructive text-destructive-foreground hover:bg-destructive/90",children:"Delete"})]})]})})]})}export{ke 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.