1import{s as se,a3 as C,a as ae,n as te,r as h,j as e,C as F,f as M,g as k,B as m,N as re,i as O,O as ne,y as ie,z as oe,E as q,F as g,H as ce,I as x,t as L,Z as le}from"./index-CCt2ShGp.js";import{S as de}from"./SuperAdminLayout-BacwHyB6.js";import{I as U}from"./input-fPmjrrnt.js";import{L as N}from"./label-CMksTN8T.js";import{S as P,a as $,b as I,c as H,d as o}from"./select-Dk0TIuWV.js";import{D as ue,f as he,a as me,b as ge,c as xe}from"./dialog-Dkm-XMJX.js";import{S as pe}from"./switch-BPDhsQwk.js";import{u as S}from"./useMutation-xcc-40wd.js";import{a as T}from"./errorUtils-CI9_dhnI.js";import{R as je}from"./ruler-DOgMrzwn.js";import{S as fe}from"./square-pen-BtWpGLzI.js";import"./TopNavbar-CQRsNqmG.js";import"./format-BpaU9Ea-.js";import"./image-DD_uRKCU.js";import"./useImageService-C-9_45Pk.js";import"./database-DUZU19H-.js";const p="https://api.tawrida.xyz",E=()=>({"Content-Type":"application/json",...T()}),ve=t=>se({queryKey:["superadmin-units-of-measure",t],queryFn:async()=>{const n=new URLSearchParams;t!=null&&t.search&&n.append("search",t.search),t!=null&&t.category&&t.category!=="all"&&n.append("category",t.category);const a=await fetch(`${p}/api/superadmin/units-of-measure?${n.toString()}`,{headers:T()});if(!a.ok){const r=await a.text();throw console.error("useUnitsOfMeasure: Error response:",r),new Error("Failed to fetch units of measure")}return a.json()},staleTime:5*60*1e3}),ye=()=>{const t=C();return S({mutationFn:async n=>{const a=await fetch(`${p}/api/superadmin/units-of-measure`,{method:"POST",headers:E(),body:JSON.stringify(n)});if(!a.ok){const r=await a.json();throw new Error(r.error||"Failed to create unit of measure")}return a.json()},onSuccess:()=>{t.invalidateQueries({queryKey:["superadmin-units-of-measure"]})}})},be=()=>{const t=C();return S({mutationFn:async({unitId:n,unitData:a})=>{const r=await fetch(`${p}/api/superadmin/units-of-measure/${n}`,{method:"PUT",headers:E(),body:JSON.stringify(a)});if(!r.ok){const c=await r.json();throw new Error(c.error||"Failed to update unit of measure")}return r.json()},onSuccess:()=>{t.invalidateQueries({queryKey:["superadmin-units-of-measure"]})}})},Ce=()=>{const t=C();return S({mutationFn:async n=>{const a=await fetch(`${p}/api/superadmin/units-of-measure/${n}`,{method:"DELETE",headers:T()});if(!a.ok){const r=await a.json();throw new Error(r.message||r.error||"Failed to delete unit of measure")}return a.json()},onSuccess:()=>{t.invalidateQueries({queryKey:["superadmin-units-of-measure"]})}})},Se=()=>{const t=C();return S({mutationFn:async({unitId:n,isActive:a})=>{const r=await fetch(`${p}/api/superadmin/units-of-measure/${n}`,{method:"PUT",headers:E(),body:JSON.stringify({is_active:a})});if(!r.ok){const c=await r.json();throw new Error(c.error||"Failed to toggle unit status")}return r.json()},onSuccess:()=>{t.invalidateQueries({queryKey:["superadmin-units-of-measure"]})}})};function He(){const{t,isRTL:n}=ae(),{toast:a}=te(),[r,c]=h.useState(""),[w,R]=h.useState("all"),[B,j]=h.useState(!1),[l,f]=h.useState(null),[d,u]=h.useState({name:"",abbreviation:"",category:"weight"}),{data:v=[],isLoading:K}=ve({search:r||void 0,category:w!=="all"?w:void 0}),A=ye(),D=be(),Q=Ce(),_=Se(),z=()=>{a({title:"Unit created",description:"Unit of measure has been created successfully."}),j(!1),b()},J=()=>{a({title:"Unit updated",description:"Unit of measure has been updated successfully."}),f(null),b()},V=()=>{a({title:"Unit deleted",description:"Unit of measure has been deleted successfully."})},y=(s,i)=>{a({title:`Error ${i} unit`,description:s.message,variant:"destructive"})},b=()=>{u({name:"",abbreviation:"",category:"weight"})},W=s=>{f(s),u({name:s.name,abbreviation:s.abbreviation,category:s.category})},Z=()=>{l?D.mutate({unitId:l.id,unitData:d},{onSuccess:J,onError:s=>y(s,"updating")}):A.mutate(d,{onSuccess:z,onError:s=>y(s,"creating")})},G=s=>{_.mutate({unitId:s.id,isActive:!s.is_active},{onError:i=>y(i,"toggling")})},X=s=>{Q.mutate(s,{onSuccess:V,onError:i=>y(i,"deleting")})},Y=Array.from(new Set(v.map(s=>s.category))),ee=s=>({weight:"bg-blue-100 text-blue-800",volume:"bg-green-100 text-green-800",piece:"bg-gray-100 text-gray-800",length:"bg-purple-100 text-purple-800",packaging:"bg-orange-100 text-orange-800"})[s]||"bg-gray-100 text-gray-800";return e.jsx(de,{children:e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold",children:"Units of Measure Management"}),e.jsx("p",{className:"text-muted-foreground",children:"Manage units of measure used across the platform"})]}),e.jsxs(F,{children:[e.jsx(M,{children:e.jsxs(k,{className:"text-lg flex items-center justify-between",children:[e.jsx("span",{children:"Units Overview"}),e.jsxs(ue,{open:B||!!l,onOpenChange:s=>{s||(j(!1),f(null),b())},children:[e.jsx(he,{asChild:!0,children:e.jsxs(m,{onClick:()=>j(!0),children:[e.jsx(re,{className:"h-4 w-4 mr-2"}),"Add Unit"]})}),e.jsxs(me,{children:[e.jsx(ge,{children:e.jsx(xe,{children:l?"Edit Unit of Measure":"Add New Unit of Measure"})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx(N,{htmlFor:"name",children:"Name"}),e.jsx(U,{id:"name",value:d.name,onChange:s=>u(i=>({...i,name:s.target.value})),placeholder:"e.g., Kilogram"})]}),e.jsxs("div",{children:[e.jsx(N,{htmlFor:"abbreviation",children:"Abbreviation"}),e.jsx(U,{id:"abbreviation",value:d.abbreviation,onChange:s=>u(i=>({...i,abbreviation:s.target.value})),placeholder:"e.g., kg"})]}),e.jsxs("div",{children:[e.jsx(N,{htmlFor:"category",children:"Category"}),e.jsxs(P,{value:d.category,onValueChange:s=>u(i=>({...i,category:s})),children:[e.jsx($,{children:e.jsx(I,{})}
1),e.jsxs(H,{children:[e.jsx(o,{value:"weight",children:"Weight"}),e.jsx(o,{value:"volume",children:"Volume"}),e.jsx(o,{value:"piece",children:"Piece"}),e.jsx(o,{value:"length",children:"Length"}),e.jsx(o,{value:"packaging",children:"Packaging"})]})]})]}),e.jsxs("div",{className:"flex gap-2 justify-end",children:[e.jsx(m,{variant:"outline",onClick:()=>{j(!1),f(null),b()},children:"Cancel"}),e.jsxs(m,{onClick:Z,disabled:A.isPending||D.isPending,children:[l?"Update":"Create"," Unit"]})]})]})]})]})]})}),e.jsx(O,{children:e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-3 gap-4",children:[e.jsxs("div",{className:"relative",children:[e.jsx(ne,{className:"absolute left-3 top-3 h-4 w-4 text-muted-foreground"}),e.jsx(U,{placeholder:"Search units...",value:r,onChange:s=>c(s.target.value),className:"pl-10"})]}),e.jsxs(P,{value:w,onValueChange:R,children:[e.jsx($,{children:e.jsx(I,{placeholder:"Filter by category"})}),e.jsxs(H,{children:[e.jsx(o,{value:"all",children:"All Categories"}),Y.map(s=>e.jsx(o,{value:s,children:s.charAt(0).toUpperCase()+s.slice(1)},s))]})]}),e.jsxs("div",{className:"text-sm text-muted-foreground flex items-center",children:[e.jsx(je,{className:"h-4 w-4 mr-2"}),v.length," units found"]})]})})]}),e.jsxs(F,{children:[e.jsx(M,{children:e.jsxs(k,{className:"text-lg",children:["Units of Measure (",v.length,")"]})}),e.jsx(O,{children:K?e.jsx("div",{className:"text-center py-8",children:"Loading units..."}):e.jsxs(ie,{children:[e.jsx(oe,{children:e.jsxs(q,{children:[e.jsx(g,{children:"Name"}),e.jsx(g,{children:"Abbreviation"}),e.jsx(g,{children:"Category"}),e.jsx(g,{children:"Status"}),e.jsx(g,{children:"Actions"})]})}),e.jsx(ce,{children:v.map(s=>e.jsxs(q,{children:[e.jsx(x,{className:"font-medium",children:s.name}),e.jsx(x,{children:e.jsx(L,{variant:"outline",children:s.abbreviation})}),e.jsx(x,{children:e.jsx(L,{className:ee(s.category),children:s.category.charAt(0).toUpperCase()+s.category.slice(1)})}),e.jsx(x,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(pe,{checked:s.is_active,onCheckedChange:()=>G(s)}),e.jsx("span",{className:"text-sm",children:s.is_active?"Active":"Inactive"})]})}),e.jsx(x,{children:e.jsxs("div",{className:"flex gap-2",children:[e.jsx(m,{size:"sm",variant:"outline",onClick:()=>W(s),children:e.jsx(fe,{className:"h-4 w-4"})}),e.jsx(m,{size:"sm",variant:"outline",onClick:()=>X(s.id),children:e.jsx(le,{className:"h-4 w-4"})})]})})]},s.id))})]})})]})]})})}export{He 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.