import{a as e,t}from"./react-DK-MTtD8.js";import{r as n,s as r}from"./utils-CXcsdpX-.js";import{r as i}from"./api-CC_sFP6f.js";import{t as a}from"./list-table-CF_0QrYo.js";import{t as o}from"./funnel-BT4pjjO7.js";import{t as s}from"./plus-CuMPbIyn.js";import{n as c,t as l}from"./filter-sheet-_WHotb9o.js";import{Gr as u,Hr as d,rs as f,za as p}from"./index-CvTTLloU.js";import{t as m}from"./page-content-Bh5Zxb_Y.js";import{t as h}from"./badge-CL1e8lO_.js";import{a as g,i as _,n as v,o as y,t as b}from"./dialog-DrW_2c38.js";import{t as x}from"./list-page-header-D6ls_V1f.js";import{t as S}from"./auth-status-badge-BlJK5R24.js";import{t as C}from"./row-actions-C-24DtZS.js";import{F as w,a as T,y as E}from"./serviceApi-8BCtX3rt.js";var D=e(t(),1),O=r(),k=[{value:`draft`,label:`Draft`},{value:`approved`,label:`Approved`},{value:`in_progress`,label:`In Progress`},{value:`completed`,label:`Completed`},{value:`cancelled`,label:`Cancelled`}];function A(){let e=f(),[t,r]=(0,D.useState)(``),[A,j]=(0,D.useState)(`all`),[M,N]=(0,D.useState)(`all`),[P,F]=(0,D.useState)(`all`),[I,L]=(0,D.useState)(!1),[R,z]=(0,D.useState)(null),B={};A!==`all`&&(B.status=A),M!==`all`&&(B.branch=M),P!==`all`&&(B.client=P),t&&(B.search=t);let{data:V=[],isLoading:H,refetch:U}=w(B),{data:W=[]}=d(),{data:G=[]}=u(),[K]=E(),[q]=T(),J=async()=>{R&&(await K(R).unwrap().catch(()=>{}),z(null),U())},Y=async(e,t)=>{try{await q({id:e,new_status:t}).unwrap(),U()}catch{}},X=()=>{j(`all`),N(`all`),F(`all`)},Z=[A,M,P].filter(e=>e!==`all`).length;return(0,O.jsxs)(m,{children:[(0,O.jsx)(x,{title:`Service Requests`,description:`Manage customer service requests`,searchPlaceholder:`Search by code, client, service type...`,searchValue:t,onSearchChange:r,action:(0,O.jsxs)(p,{onClick:()=>e(`/service-requests/new`),children:[(0,O.jsx)(s,{className:`size-4 mr-2`}),` New Request`]}),filter:(0,O.jsxs)(p,{variant:`outline`,size:`sm`,onClick:()=>L(!0),className:`relative`,children:[(0,O.jsx)(o,{className:`mr-2 h-4 w-4`}),` Filters`,Z>0&&(0,O.jsx)(h,{variant:`destructive`,className:`ml-2 size-5 p-0 flex items-center justify-center text-[10px]`,children:Z})]})}),(0,O.jsx)(a,{data:V,onRowClick:t=>e(`/service-requests/${t.id}`),columns:[{key:`code`,label:`Code`,render:e=>(0,O.jsx)(`span`,{className:`font-mono`,children:e.code})},{key:`client_name`,label:`Client`},{key:`service_type`,label:`Service Type`},{key:`location_name`,label:`Location`},{key:`start_date`,label:`Start Date`,render:e=>n(e.start_date)},{key:`end_date`,label:`End Date`,render:e=>n(e.end_date)},{key:`branch_name`,label:`Branch`,render:e=>e.branch_name||`—`},{key:`authorized_status`,label:`Approval Status`,render:e=>(0,O.jsx)(S,{status:e.authorized_status})},{key:`status`,label:`Status`,render:e=>(0,O.jsx)(`select`,{value:e.status,onClick:e=>e.stopPropagation(),onChange:t=>{t.stopPropagation(),Y(e.id,t.target.value)},className:`border rounded px-2 py-1 text-xs bg-background cursor-pointer focus:outline-none focus:ring-1 focus:ring-ring`,children:k.map(e=>(0,O.jsx)(`option`,{value:e.value,children:e.label},e.value))})},{key:`actions`,label:`Actions`,sortable:!1,render:t=>(0,O.jsx)(C,{onView:()=>e(`/service-requests/${t.id}`),onEdit:()=>e(`/service-requests/${t.id}/edit`),onDelete:()=>z(t.id),approval:{entityType:`ServiceRequest`,objectId:t.id,contentTypeId:i.SERVICE_REQUEST,authorizedStatus:t.authorized_status??1,onApprovalChange:()=>U()}})}],isLoading:H,rowKey:e=>e.id,emptyMessage:`No service requests found`}),(0,O.jsxs)(c,{open:I,onOpenChange:L,onClear:X,children:[(0,O.jsx)(l,{label:`Status`,value:A,onChange:j,placeholder:`All Status`,options:k}),(0,O.jsx)(l,{label:`Branch`,value:M,onChange:N,placeholder:`All Branches`,options:W.map(e=>({value:String(e.id),label:e.name}))}),(0,O.jsx)(l,{label:`Client`,value:P,onChange:F,placeholder:`All Clients`,options:G.map(e=>({value:String(e.id),label:e.name}))})]}),(0,O.jsx)(b,{open:!!R,onOpenChange:e=>{e||z(null)},children:(0,O.jsxs)(v,{className:`sm:max-w-md`,children:[(0,O.jsx)(g,{children:(0,O.jsx)(y,{children:`Delete Service Request`})}),(0,O.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Are you sure you want to delete this service request?`}),(0,O.jsxs)(_,{children:[(0,O.jsx)(p,{variant:`outline`,size:`sm`,onClick:()=>z(null),children:`Cancel`}),(0,O.jsx)(p,{variant:`destructive`,size:`sm`,onClick:J,children:`Delete`})]})]})})]})}export{A as default};