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-DcvYaav1.js";import{t as a}from"./list-table-CQD2C7IB.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-omdfG0qY.js";import{Gr as u,Hr as d,rs as f,za as p}from"./index-Ct5uU080.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-DuXDR687.js";import{t as x}from"./list-page-header-CePoWpko.js";import{t as S}from"./auth-status-badge-BlJK5R24.js";import{t as ee}from"./row-actions-bhl00K6X.js";import{F as C,i as w,j as T,v as E}from"./serviceApi-Bn3V4hja.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=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)(`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=[]}=d(),{data:K=[]}=u(),{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)(p,{onClick:()=>e(`/service-invoices/new`),children:[(0,O.jsx)(s,{className:`size-4 mr-2`}),` Generate Invoice`]}),filter:(0,O.jsxs)(p,{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)(p,{variant:`outline`,size:`sm`,onClick:()=>B(null),children:`Cancel`}),(0,O.jsx)(p,{variant:`destructive`,size:`sm`,onClick:X,children:`Delete`})]})]})})]})}export{A as default};