import{i as e}from"./rolldown-runtime-Dd_uD5pT.js";import{t}from"./react-BatatxyT.js";import{r as n,s as r}from"./utils-BO1RPuol.js";import{t as i}from"./list-table-DNq_Jbx2.js";import{t as a}from"./funnel-CzLimyxu.js";import{t as o}from"./plus-BEDg_66Y.js";import{n as s,t as c}from"./filter-sheet-guEWYDqt.js";import{D as l,F as u,Gr as d,Hr as f,os as p,za as m}from"./index-CsuVxiRQ.js";import{t as h}from"./page-content-DdXVZwik.js";import{t as g}from"./badge-DLCseWie.js";import{a as _,i as v,n as y,o as b,t as x}from"./dialog-BT7QjyH-.js";import{t as S}from"./list-page-header-BnFKDjpb.js";import{t as C}from"./row-actions-DALRrgnK.js";import{t as w}from"./workflow-status-badge-BdRZ0is7.js";var T=e(t(),1),E=r(),D=[{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 O(){let e=p(),[t,r]=(0,T.useState)(``),[O,k]=(0,T.useState)(`all`),[A,j]=(0,T.useState)(`all`),[M,N]=(0,T.useState)(`all`),[P,F]=(0,T.useState)(`all`),[I,L]=(0,T.useState)(!1),[R,z]=(0,T.useState)(null),B={};O!==`all`&&(B.status=O),A!==`all`&&(B.invoice_type=A),t&&(B.search=t),M!==`all`&&(B.branch=M),P!==`all`&&(B.client=P);let{data:V=[],isLoading:H,refetch:U}=u(B),{data:W=[]}=f(),{data:G=[]}=d(),[K]=l(),q=async()=>{R&&(await K(R).unwrap().catch(()=>{}),z(null),U())},J=()=>{k(`all`),j(`all`),N(`all`),F(`all`)},Y=[O,A,M,P].filter(e=>e!==`all`).length;return(0,E.jsxs)(h,{children:[(0,E.jsx)(S,{title:`Sales Invoices`,description:`Invoices generated against confirmed sales orders`,searchPlaceholder:`Search by invoice number, client, SO code...`,searchValue:t,onSearchChange:r,action:(0,E.jsxs)(m,{onClick:()=>e(`/sales-invoices/new`),children:[(0,E.jsx)(o,{className:`size-4 mr-2`}),` Create Invoice`]}),filter:(0,E.jsxs)(m,{variant:`outline`,size:`sm`,onClick:()=>L(!0),className:`relative`,children:[(0,E.jsx)(a,{className:`mr-2 h-4 w-4`}),` Filters`,Y>0&&(0,E.jsx)(g,{variant:`destructive`,className:`ml-2 size-5 p-0 flex items-center justify-center text-[10px]`,children:Y})]})}),(0,E.jsx)(i,{data:V,onRowClick:t=>e(`/sales-invoices/${t.id}`),columns:[{key:`code`,label:`Invoice No`,render:e=>(0,E.jsx)(`span`,{className:`font-mono`,children:e.code})},{key:`tender_number`,label:`Tender No`,render:e=>e.tender_number?(0,E.jsx)(`span`,{className:`font-mono text-xs`,children:e.tender_number}):`—`},{key:`invoice_date`,label:`Date`,render:e=>n(e.invoice_date)},{key:`client_name`,label:`Client`},{key:`sales_order_code`,label:`Linked SO`,render:e=>(0,E.jsx)(`span`,{className:`font-mono text-xs`,children:e.sales_order_code})},{key:`net_payable`,label:`Net Payable`,className:`text-center`,render:e=>(0,E.jsx)(`span`,{className:`font-mono`,children:Number(e.net_payable).toLocaleString()})},{key:`outstanding_amount`,label:`Outstanding`,className:`text-center`,render:e=>(0,E.jsx)(`span`,{className:`font-mono`,children:Number(e.outstanding_amount).toLocaleString()})},{key:`status`,label:`Status`,render:e=>(0,E.jsx)(w,{status:e.status})},{key:`branch_name`,label:`Branch`,render:e=>e.branch_name||`—`},{key:`actions`,label:`Actions`,sortable:!1,render:t=>(0,E.jsx)(C,{onView:()=>e(`/sales-invoices/${t.id}`),onEdit:()=>e(`/sales-invoices/${t.id}/edit`),onDelete:()=>z(t.id)})}],isLoading:H,rowKey:e=>e.id,emptyMessage:`No sales invoices found`}),(0,E.jsxs)(s,{open:I,onOpenChange:L,onClear:J,children:[(0,E.jsx)(c,{label:`Status`,value:O,onChange:k,placeholder:`All Status`,options:D}),(0,E.jsx)(c,{label:`Branch`,value:M,onChange:N,placeholder:`All Branches`,options:W.map(e=>({value:String(e.id),label:e.name}))}),(0,E.jsx)(c,{label:`Client`,value:P,onChange:F,placeholder:`All Clients`,options:G.map(e=>({value:String(e.id),label:e.name}))})]}),(0,E.jsx)(x,{open:!!R,onOpenChange:e=>{e||z(null)},children:(0,E.jsxs)(y,{className:`sm:max-w-md`,children:[(0,E.jsx)(_,{children:(0,E.jsx)(b,{children:`Delete Sales Invoice`})}),(0,E.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Are you sure you want to delete this sales invoice? This action cannot be undone.`}),(0,E.jsxs)(v,{children:[(0,E.jsx)(m,{variant:`outline`,size:`sm`,onClick:()=>z(null),children:`Cancel`}),(0,E.jsx)(m,{variant:`destructive`,size:`sm`,onClick:q,children:`Delete`})]})]})})]})}export{O as default};