import{a as e,t}from"./react-DK-MTtD8.js";import{r as n,s as r}from"./utils-CXcsdpX-.js";import{t as i}from"./list-table-CQD2C7IB.js";import{t as a}from"./funnel-BT4pjjO7.js";import{t as o}from"./plus-CuMPbIyn.js";import{n as s,t as c}from"./filter-sheet-omdfG0qY.js";import{D as l,F as u,Gr as d,Hr as f,rs as p,za as m}from"./index-Ct5uU080.js";import{t as h}from"./page-content-Bh5Zxb_Y.js";import{t as g}from"./badge-CL1e8lO_.js";import{a as _,i as v,n as y,o as b,t as x}from"./dialog-DuXDR687.js";import{t as S}from"./list-page-header-CePoWpko.js";import{t as C}from"./row-actions-bhl00K6X.js";import{t as w}from"./workflow-status-badge-B4zdc-CO.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};