import{a as e,t}from"./react-DK-MTtD8.js";import{s as n}from"./utils-CXcsdpX-.js";import{r}from"./api-DcvYaav1.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{Gr as l,Hr as u,K as d,rs as f,rt 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"./auth-status-badge-BlJK5R24.js";import{t as w}from"./row-actions-bhl00K6X.js";import{t as T}from"./workflow-status-badge-B4zdc-CO.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=f(),[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}=p(V),{data:G=[]}=u(),{data:K=[]}=l(),[q]=d(),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)(m,{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)(m,{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:`tender_number`,label:`Tender No`,render:e=>e.tender_number?(0,D.jsx)(`span`,{className:`font-mono text-xs`,children:e.tender_number}):`—`},{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)(m,{variant:`outline`,size:`sm`,onClick:()=>B(null),children:`Cancel`}),(0,D.jsx)(m,{variant:`destructive`,size:`sm`,onClick:J,children:`Delete`})]})]})})]})}export{k as default};