import{a as e,t}from"./react-D0JuimcS.js";import{n,o as r}from"./utils-Dac180Gn.js";import{r as i}from"./api-NMlTtVre.js";import{t as a}from"./list-table-dcNbCPvq.js";import{t as o}from"./funnel-BDsPorMA.js";import{t as s}from"./plus-VCCEyniC.js";import{n as c,t as l}from"./filter-sheet-Baado-aR.js";import{Fo as u,Mr as d,ga as f,kr as p}from"./index-BP3TYB4i.js";import{t as m}from"./page-content-DDtdeMxM.js";import{t as h}from"./badge-Crg76WQE.js";import{a as g,i as _,n as v,o as y,t as b}from"./dialog-BeOPOVZK.js";import{t as x}from"./list-page-header-48VsPegf.js";import{t as S}from"./auth-status-badge-pVqOsTb5.js";import{t as ee}from"./row-actions-_6H5dRTY.js";import{F as C,i as w,j as T,v as E}from"./serviceApi-BlxU9Cj1.js";var D=e(t(),1),O=r(),k=[{value:`draft`,label:`Draft`},{value:`submitted`,label:`Submitted`},{value:`approved`,label:`Approved`},{value:`sent`,label:`Sent`},{value:`paid`,label:`Paid`},{value:`partially_paid`,label:`Partially Paid`},{value:`cancelled`,label:`Cancelled`}];function A(){let e=u(),[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)(`all`),[te,R]=(0,D.useState)(!1),[z,B]=(0,D.useState)(null),V={};A!==`all`&&(V.status=A),M!==`all`&&(V.branch=M),P!==`all`&&(V.client=P),I!==`all`&&(V.service_request=I),t&&(V.search=t);let{data:H=[],isLoading:U,refetch:W}=T(V),{data:G=[]}=p(),{data:K=[]}=d(),{data:q=[]}=C(),[J]=E(),[Y]=w(),X=async()=>{z&&(await J(z).unwrap().catch(()=>{}),B(null),W())},Z=async(e,t)=>{try{await Y({id:e,new_status:t}).unwrap(),W()}catch{}},Q=()=>{j(`all`),N(`all`),F(`all`),L(`all`)},$=[A,M,P,I].filter(e=>e!==`all`).length;return(0,O.jsxs)(m,{children:[(0,O.jsx)(x,{title:`Service Invoices`,description:`Invoices generated from approved daily logs`,searchPlaceholder:`Search by code, client...`,searchValue:t,onSearchChange:r,action:(0,O.jsxs)(f,{onClick:()=>e(`/service-invoices/new`),children:[(0,O.jsx)(s,{className:`size-4 mr-2`}),` Generate Invoice`]}),filter:(0,O.jsxs)(f,{variant:`outline`,size:`sm`,onClick:()=>R(!0),className:`relative`,children:[(0,O.jsx)(o,{className:`mr-2 h-4 w-4`}),` Filters`,$>0&&(0,O.jsx)(h,{variant:`destructive`,className:`ml-2 size-5 p-0 flex items-center justify-center text-[10px]`,children:$})]})}),(0,O.jsx)(a,{data:H,onRowClick:t=>e(`/service-invoices/${t.id}`),columns:[{key:`code`,label:`Invoice No`,render:e=>(0,O.jsx)(`span`,{className:`font-mono`,children:e.code})},{key:`service_request_code`,label:`Service Request`,render:e=>(0,O.jsx)(`span`,{className:`font-mono text-xs`,children:e.service_request_code})},{key:`client_name`,label:`Client`},{key:`invoice_date`,label:`Invoice Date`,render:e=>n(e.invoice_date)},{key:`total_amount`,label:`Total`,className:`text-center`,render:e=>(0,O.jsx)(`span`,{className:`font-mono`,children:Number(e.total_amount).toLocaleString()})},{key:`status`,label:`Status`,render:e=>(0,O.jsx)(`select`,{value:e.status,onClick:e=>e.stopPropagation(),onChange:t=>{t.stopPropagation(),Z(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:`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:`actions`,label:`Actions`,sortable:!1,render:t=>(0,O.jsx)(ee,{onView:()=>e(`/service-invoices/${t.id}`),onEdit:()=>e(`/service-invoices/${t.id}/edit`),onDelete:()=>B(t.id),approval:{entityType:`ServiceInvoice`,objectId:t.id,contentTypeId:i.SERVICE_INVOICE,authorizedStatus:t.authorized_status??1,onApprovalChange:()=>W()}})}],isLoading:U,rowKey:e=>e.id,emptyMessage:`No service invoices found`}),(0,O.jsxs)(c,{open:te,onOpenChange:R,onClear:Q,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:G.map(e=>({value:String(e.id),label:e.name}))}),(0,O.jsx)(l,{label:`Client`,value:P,onChange:F,placeholder:`All Clients`,options:K.map(e=>({value:String(e.id),label:e.name}))}),(0,O.jsx)(l,{label:`Service Request`,value:I,onChange:L,placeholder:`All Requests`,options:q.map(e=>({value:e.id,label:`${e.code} — ${e.client_name}`}))})]}),(0,O.jsx)(b,{open:!!z,onOpenChange:e=>{e||B(null)},children:(0,O.jsxs)(v,{className:`sm:max-w-md`,children:[(0,O.jsx)(g,{children:(0,O.jsx)(y,{children:`Delete Service Invoice`})}),(0,O.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Are you sure you want to delete this service invoice?`}),(0,O.jsxs)(_,{children:[(0,O.jsx)(f,{variant:`outline`,size:`sm`,onClick:()=>B(null),children:`Cancel`}),(0,O.jsx)(f,{variant:`destructive`,size:`sm`,onClick:X,children:`Delete`})]})]})})]})}export{A as default};