import{bR as N,r as n,p as e,I as l,ae as y,q as S,H as D,W as A,Q as T,s as w,x as O,ai as W,aj as I,ak as g,al as a,aq as P,ar as t,aw as E,b8 as z,bb as F,ct as H,cu as B,cv as L,cw as _,cx as $,cy as q,cz as M,cA as R,cs as p,aI as x,B as V}from"./index-ChEG8aX7.js";const K={1:{label:"Draft",variant:"secondary"},2:{label:"Submitted",variant:"default"},3:{label:"Approved",variant:"default"},4:{label:"In Progress",variant:"default"},5:{label:"Completed",variant:"default"},6:{label:"Cancelled",variant:"destructive"}},U=()=>{const c=N(),[j,v]=n.useState([]),[k,m]=n.useState(!0),[i,f]=n.useState(""),[o,d]=n.useState(null),h=async()=>{try{m(!0);const s=i?`search=${i}`:"",r=await p.getWorkOrders(s);v(r?.results||r||[])}catch(s){console.error("Error fetching work orders:",s),x.error("Failed to load work orders")}finally{m(!1)}};n.useEffect(()=>{h()},[]);const u=()=>{h()},b=async()=>{if(o)try{await p.deleteWorkOrder(o),x.success("Work order deleted"),d(null),h()}catch(s){const r=s?.response?.data?.detail||s?.response?.data?.[0]||"Failed to delete";x.error(r)}},C=s=>{const r=K[s]||{label:String(s),variant:"outline"};return e.jsx(V,{variant:r.variant,children:r.label})};return e.jsxs("div",{className:"space-y-4 p-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-semibold",children:"Work Orders"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Manage sub-contract work orders"})]}),e.jsxs(l,{onClick:()=>c("/projectmanagement/pms/work-orders/create"),children:[e.jsx(y,{className:"h-4 w-4 mr-2"})," New Work Order"]})]}),e.jsxs(S,{children:[e.jsx(D,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(A,{placeholder:"Search by code...",value:i,onChange:s=>f(s.target.value),onKeyDown:s=>s.key==="Enter"&&u(),className:"max-w-sm"}),e.jsx(l,{variant:"outline",size:"icon",onClick:u,children:e.jsx(T,{className:"h-4 w-4"})})]})}),e.jsx(w,{children:k?e.jsx("div",{className:"flex items-center justify-center py-10",children:e.jsx(O,{className:"h-8 w-8 animate-spin text-muted-foreground"})}):j.length===0?e.jsx("div",{className:"text-center py-10 text-muted-foreground",children:"No work orders found. Create your first one."}):e.jsxs(W,{children:[e.jsx(I,{children:e.jsxs(g,{children:[e.jsx(a,{children:"Code"}),e.jsx(a,{children:"Project"}),e.jsx(a,{children:"Vendor"}),e.jsx(a,{children:"Contract Type"}),e.jsx(a,{children:"Total Value"}),e.jsx(a,{children:"Status"}),e.jsx(a,{className:"text-right",children:"Actions"})]})}),e.jsx(P,{children:j.map(s=>e.jsxs(g,{className:"cursor-pointer",onClick:()=>c(`/projectmanagement/pms/work-orders/${s.id}`),children:[e.jsx(t,{className:"font-mono",children:s.code||"-"}),e.jsx(t,{children:s.project?.name||s.project?.code||"-"}),e.jsx(t,{children:s.vendor?.name||"-"}),e.jsx(t,{children:s.contract_type_name||"-"}),e.jsxs(t,{children:["₹",Number(s.total_value||0).toLocaleString("en-IN",{minimumFractionDigits:2})]}),e.jsx(t,{children:C(s.status)}),e.jsx(t,{className:"text-right",onClick:r=>r.stopPropagation(),children:e.jsxs("div",{className:"flex items-center justify-end gap-1",children:[e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>c(`/projectmanagement/pms/work-orders/${s.id}`),children:e.jsx(E,{className:"h-4 w-4"})}),s.status===1&&e.jsxs(e.Fragment,{children:[e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>c(`/projectmanagement/pms/work-orders/${s.id}/edit`),children:e.jsx(z,{className:"h-4 w-4"})}),e.jsx(l,{variant:"ghost",size:"icon",onClick:()=>d(s.id),children:e.jsx(F,{className:"h-4 w-4 text-destructive"})})]})]})})]},s.id))})]})})]}),e.jsx(H,{open:!!o,onOpenChange:()=>d(null),children:e.jsxs(B,{children:[e.jsxs(L,{children:[e.jsx(_,{children:"Delete Work Order?"}),e.jsx($,{children:"This action cannot be undone."})]}),e.jsxs(q,{children:[e.jsx(M,{children:"Cancel"}),e.jsx(R,{onClick:b,children:"Delete"})]})]})})]})};export{U as default};
