import{a as e,t}from"./react-D0JuimcS.js";import{o as n}from"./utils-Dac180Gn.js";import{r}from"./api-NMlTtVre.js";import{t as i}from"./list-table-dcNbCPvq.js";import{t as a}from"./funnel-BDsPorMA.js";import{t as o}from"./plus-VCCEyniC.js";import{n as s,t as c}from"./filter-sheet-Baado-aR.js";import{Fo as l,G as u,Mr as d,ga as f,kr as p,nt as m}from"./index-BP3TYB4i.js";import{t as h}from"./page-content-DDtdeMxM.js";import{t as g}from"./badge-Crg76WQE.js";import{a as _,i as v,n as y,o as b,t as x}from"./dialog-BeOPOVZK.js";import{t as S}from"./list-page-header-48VsPegf.js";import{t as C}from"./auth-status-badge-pVqOsTb5.js";import{t as w}from"./row-actions-_6H5dRTY.js";import{t as T}from"./workflow-status-badge-B5qOWo7t.js";var E=e(t(),1),D=n(),O=[{value:`confirmed`,label:`Confirmed`},{value:`draft`,label:`Draft`},{value:`partially_invoiced`,label:`Partially Invoiced`},{value:`cancelled`,label:`Cancelled`},{value:`closed`,label:`Closed`}];function k(){let e=l(),[t,n]=(0,E.useState)(``),[k,A]=(0,E.useState)(`all`),[j,M]=(0,E.useState)(`all`),[N,P]=(0,E.useState)(`all`),[F,I]=(0,E.useState)(`all`),[L,R]=(0,E.useState)(!1),[z,B]=(0,E.useState)(null),V={};k!==`all`&&(V.so_status=k),t&&(V.search=t),j!==`all`&&(V.branch=j),N!==`all`&&(V.authorized_status=N),F!==`all`&&(V.client=F);let{data:H=[],isLoading:U,refetch:W}=m(V),{data:G=[]}=p(),{data:K=[]}=d(),[q]=u(),J=async()=>{z&&(await q(z).unwrap().catch(()=>{}),B(null),W())},Y=()=>{A(`all`),M(`all`),P(`all`),I(`all`)},X=[k,j,N,F].filter(e=>e!==`all`).length;return(0,D.jsxs)(h,{children:[(0,D.jsx)(S,{title:`Sales Orders`,description:`Orders generated from approved offers`,searchPlaceholder:`Search by SO number, client, BE code...`,searchValue:t,onSearchChange:n,action:(0,D.jsxs)(f,{onClick:()=>e(`/sales-orders/new`),size:`sm`,children:[(0,D.jsx)(o,{className:`mr-2 h-4 w-4`}),` Create SO`]}),filter:(0,D.jsxs)(f,{variant:`outline`,size:`sm`,onClick:()=>R(!0),className:`relative`,children:[(0,D.jsx)(a,{className:`mr-2 h-4 w-4`}),` Filters`,X>0&&(0,D.jsx)(g,{variant:`destructive`,className:`ml-2 size-5 p-0 flex items-center justify-center text-[10px]`,children:X})]})}),(0,D.jsx)(i,{data:H,onRowClick:t=>e(`/sales-orders/${t.id}`),columns:[{key:`code`,label:`SO No`,render:e=>(0,D.jsx)(`span`,{className:`font-mono`,children:e.code})},{key:`client_name`,label:`Client`},{key:`source_be_code`,label:`Source BE`,render:e=>(0,D.jsx)(`span`,{className:`font-mono text-xs`,children:e.source_be_code})},{key:`source_tender_code`,label:`Source Enquiry`,render:e=>(0,D.jsx)(`span`,{className:`font-mono text-xs`,children:e.source_tender_code})},{key:`so_status`,label:`Status`,render:e=>(0,D.jsx)(T,{status:e.so_status})},{key:`grand_total`,label:`Grand Total`,className:`text-center`,render:e=>(0,D.jsx)(`span`,{className:`font-mono`,children:Number(e.grand_total).toLocaleString()})},{key:`total_invoiced`,label:`Total Invoiced`,className:`text-center`,render:e=>(0,D.jsx)(`span`,{className:`font-mono`,children:Number(e.total_invoiced).toLocaleString()})},{key:`branch_name`,label:`Branch`,render:e=>e.branch_name||`—`},{key:`authorized_status`,label:`Approval Status`,render:e=>(0,D.jsx)(C,{status:e.authorized_status})},{key:`actions`,label:`Actions`,sortable:!1,render:t=>(0,D.jsx)(w,{onView:()=>e(`/sales-orders/${t.id}`),onEdit:()=>e(`/sales-orders/${t.id}/edit`),onDelete:()=>B(t.id),approval:{entityType:`SalesOrder`,objectId:t.id,contentTypeId:r.SALES_ORDER,authorizedStatus:t.authorized_status??1,onApprovalChange:()=>W()}})}],isLoading:U,rowKey:e=>e.id,emptyMessage:`No sales orders found`}),(0,D.jsxs)(s,{open:L,onOpenChange:R,onClear:Y,children:[(0,D.jsx)(c,{label:`Status`,value:k,onChange:A,placeholder:`All Status`,options:O}),(0,D.jsx)(c,{label:`Client`,value:F,onChange:I,placeholder:`All Clients`,options:K.map(e=>({value:String(e.id),label:e.name}))}),(0,D.jsx)(c,{label:`Branch`,value:j,onChange:M,placeholder:`All Branches`,options:G.map(e=>({value:String(e.id),label:e.name}))}),(0,D.jsx)(c,{label:`Authorization`,value:N,onChange:P,placeholder:`All`,options:[{value:`1`,label:`Pending`},{value:`2`,label:`Approved`},{value:`3`,label:`Rejected`}]})]}),(0,D.jsx)(x,{open:!!z,onOpenChange:e=>{e||B(null)},children:(0,D.jsxs)(y,{className:`sm:max-w-md`,children:[(0,D.jsx)(_,{children:(0,D.jsx)(b,{children:`Delete Sales Order`})}),(0,D.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Are you sure you want to delete this sales order?`}),(0,D.jsxs)(v,{children:[(0,D.jsx)(f,{variant:`outline`,size:`sm`,onClick:()=>B(null),children:`Cancel`}),(0,D.jsx)(f,{variant:`destructive`,size:`sm`,onClick:J,children:`Delete`})]})]})})]})}export{k as default};