import{i as e}from"./rolldown-runtime-Dd_uD5pT.js";import{t}from"./react-BatatxyT.js";import{u as n}from"./api-672io9y2.js";import{s as r}from"./utils-BO1RPuol.js";import{r as i}from"./api-CycUopBp.js";import{t as a}from"./list-table-DNq_Jbx2.js";import{t as o}from"./funnel-CzLimyxu.js";import{t as s}from"./plus-BEDg_66Y.js";import{n as c,t as l}from"./filter-sheet-guEWYDqt.js";import{Hr as u,os as d,za as f}from"./index-CsuVxiRQ.js";import{t as p}from"./page-content-DdXVZwik.js";import{t as m}from"./badge-DLCseWie.js";import{a as h,i as g,n as _,o as v,t as y}from"./dialog-BT7QjyH-.js";import{t as b}from"./list-page-header-BnFKDjpb.js";import{t as x}from"./row-actions-DALRrgnK.js";import{E as S,F as ee,g as C,r as w}from"./serviceApi-RIObRUm3.js";var T=e(t(),1),E=r(),D=[{value:`pending`,label:`Pending`},{value:`settled`,label:`Settled`}];function O(){let e=d(),[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={};O!==`all`&&(R.status=O),A!==`all`&&(R.branch=A),M!==`all`&&(R.employee=M),P!==`all`&&(R.service_request=P),t&&(R.search=t);let{data:z=[],isLoading:B,refetch:V}=S(R),{data:H=[]}=u(),{data:U}=n(),W=U?.results||[],{data:G=[]}=ee(),[K]=C(),[q]=w(),[J,Y]=(0,T.useState)(null),X=async()=>{J&&(await K(J).unwrap().catch(()=>{}),Y(null),V())},Z=async(e,t)=>{try{await q({id:e,new_status:t}).unwrap(),V()}catch{}},Q=()=>{k(`all`),j(`all`),N(`all`),F(`all`)},$=[O,A,M,P].filter(e=>e!==`all`).length;return(0,E.jsxs)(p,{children:[(0,E.jsx)(b,{title:`Expense Settlements`,description:`Employee expense settlement records`,searchPlaceholder:`Search by code, employee...`,searchValue:t,onSearchChange:r,action:(0,E.jsxs)(f,{onClick:()=>e(`/service-settlements/new`),children:[(0,E.jsx)(s,{className:`size-4 mr-2`}),` New Settlement`]}),filter:(0,E.jsxs)(f,{variant:`outline`,size:`sm`,onClick:()=>L(!0),className:`relative`,children:[(0,E.jsx)(o,{className:`mr-2 h-4 w-4`}),` Filters`,$>0&&(0,E.jsx)(m,{variant:`destructive`,className:`ml-2 size-5 p-0 flex items-center justify-center text-[10px]`,children:$})]})}),(0,E.jsx)(a,{data:z,onRowClick:t=>e(`/service-settlements/${t.id}`),columns:[{key:`code`,label:`Code`,render:e=>(0,E.jsx)(`span`,{className:`font-mono`,children:e.code})},{key:`employee_name`,label:`Employee`},{key:`service_request_code`,label:`Service Request`,render:e=>e.service_request_code?(0,E.jsx)(`span`,{className:`font-mono text-xs`,children:e.service_request_code}):`—`},{key:`total_amount`,label:`Total`,className:`text-center`,render:e=>(0,E.jsx)(`span`,{className:`font-mono`,children:Number(e.total_amount).toLocaleString()})},{key:`period_from`,label:`Period From`},{key:`period_to`,label:`Period To`},{key:`status`,label:`Status`,render:e=>(0,E.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:D.map(e=>(0,E.jsx)(`option`,{value:e.value,children:e.label},e.value))})},{key:`actions`,label:`Actions`,sortable:!1,render:t=>(0,E.jsx)(x,{onView:()=>e(`/service-settlements/${t.id}`),onDelete:()=>Y(t.id),approval:{entityType:`ExpenseSettlement`,objectId:t.id,contentTypeId:i.EXPENSE_SETTLEMENT,authorizedStatus:t.authorized_status??1,onApprovalChange:()=>V()}})}],isLoading:B,rowKey:e=>e.id,emptyMessage:`No expense settlements found`}),(0,E.jsxs)(c,{open:I,onOpenChange:L,onClear:Q,children:[(0,E.jsx)(l,{label:`Status`,value:O,onChange:k,placeholder:`All Status`,options:D}),(0,E.jsx)(l,{label:`Branch`,value:A,onChange:j,placeholder:`All Branches`,options:H.map(e=>({value:String(e.id),label:e.name}))}),(0,E.jsx)(l,{label:`Employee`,value:M,onChange:N,placeholder:`All Employees`,options:W.map(e=>({value:String(e.id),label:`${e.first_name} ${e.last_name}`.trim()||e.username}))}),(0,E.jsx)(l,{label:`Service Request`,value:P,onChange:F,placeholder:`All Requests`,options:G.map(e=>({value:e.id,label:`${e.code} — ${e.client_name}`}))})]}),(0,E.jsx)(y,{open:!!J,onOpenChange:e=>{e||Y(null)},children:(0,E.jsxs)(_,{className:`sm:max-w-md`,children:[(0,E.jsx)(h,{children:(0,E.jsx)(v,{children:`Delete Expense Settlement`})}),(0,E.jsx)(`p`,{className:`text-sm text-muted-foreground`,children:`Are you sure you want to delete this expense settlement?`}),(0,E.jsxs)(g,{children:[(0,E.jsx)(f,{variant:`outline`,size:`sm`,onClick:()=>Y(null),children:`Cancel`}),(0,E.jsx)(f,{variant:`destructive`,size:`sm`,onClick:X,children:`Delete`})]})]})})]})}export{O as default};