import{a as e,t}from"./react-D0JuimcS.js";import{o as n}from"./utils-Dac180Gn.js";import{t as r}from"./save-Cm-Rqwuf.js";import{Fo as i,Ki as a,Ya as o,ga as s,ko as c}from"./index-BP3TYB4i.js";import{t as l}from"./page-content-DDtdeMxM.js";import{t as u}from"./card-BVFw-jgA.js";import{t as d}from"./error-alert-qe7c2K0-.js";import{t as f}from"./form-field-Co56F5gt.js";import{t as p}from"./server-search-select-DBsG1oIH.js";import{t as m}from"./validators-CNWWIxpC.js";import{t as h}from"./form-textarea-BNo2HuOu.js";import{I as g,l as _}from"./serviceApi-BlxU9Cj1.js";var v=e(t(),1),y=n();function b(){let e=i(),[t,n]=(0,v.useState)(``),[b,x]=(0,v.useState)(``),[S,C]=(0,v.useState)(``),[w,T]=(0,v.useState)(``),[E,D]=(0,v.useState)(``),[O,k]=(0,v.useState)(``),[A,j]=(0,v.useState)({}),[M,{isFetching:N}]=g(),[P,{isLoading:F}]=_(),[I,L]=(0,v.useState)([]);return(0,y.jsxs)(l,{children:[(0,y.jsxs)(`div`,{className:`flex items-center gap-4`,children:[(0,y.jsx)(s,{variant:`ghost`,size:`icon`,onClick:()=>e(`/service-settlements`),children:(0,y.jsx)(c,{className:`h-4 w-4`})}),(0,y.jsx)(`h1`,{className:`text-3xl font-bold`,children:`New Expense Settlement`})]}),(0,y.jsxs)(u,{className:`p-6 space-y-4`,children:[(0,y.jsx)(d,{errors:A}),(0,y.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,y.jsxs)(`div`,{className:`space-y-2`,children:[(0,y.jsxs)(a,{children:[`Employee `,(0,y.jsx)(`span`,{className:`text-destructive`,children:`*`})]}),(0,y.jsx)(p,{url:`/users/`,value:t,onValueChange:e=>n(e),placeholder:`Select Employee`,mapOption:e=>({value:String(e.id),label:`${e.first_name} ${e.last_name||e.username}`})}),A.employee&&(0,y.jsx)(`p`,{className:`text-xs text-destructive mt-1`,children:A.employee})]}),(0,y.jsxs)(`div`,{className:`space-y-2`,children:[(0,y.jsxs)(a,{children:[`Branch `,(0,y.jsx)(`span`,{className:`text-destructive`,children:`*`})]}),(0,y.jsx)(p,{url:`/users/me/branches/`,value:S,onValueChange:e=>C(e),placeholder:`Select Branch`,mapOption:e=>({value:String(e.id),label:e.name})}),A.branch&&(0,y.jsx)(`p`,{className:`text-xs text-destructive mt-1`,children:A.branch})]})]}),(0,y.jsxs)(`div`,{className:`grid grid-cols-3 gap-4`,children:[(0,y.jsxs)(`div`,{className:`space-y-2`,children:[(0,y.jsx)(a,{children:`Service Request`}),(0,y.jsx)(p,{url:`/service/requests/`,value:b,onValueChange:e=>x(e),placeholder:`All Requests`,mapOption:e=>({value:e.id,label:`${e.code} — ${e.client_name}`})})]}),(0,y.jsx)(f,{label:`Date From`,type:`date`,value:w,onChange:e=>T(e.target.value)}),(0,y.jsx)(f,{label:`Date To`,type:`date`,value:E,onChange:e=>D(e.target.value)})]}),(0,y.jsx)(h,{label:`Remarks`,value:O,onChange:e=>k(e.target.value),rows:2}),(0,y.jsxs)(s,{type:`button`,onClick:async()=>{if(j({}),L([]),!t){j({employee:`Employee is required`});return}if(!S){j({branch:`Branch is required`});return}try{let e={};t&&(e.employee=t),b&&(e.service_request=b),w&&(e.date_from=w),E&&(e.date_to=E);let n=await M(e).unwrap();if(!n||n.length===0){j({expenses:`No pending expenses found for the selected filters`});return}L(n)}catch(e){j(m(e))}},disabled:N,className:`w-full`,children:[(0,y.jsx)(o,{className:`mr-2 h-4 w-4`}),` Find Expenses`]})]}),I.length>0&&(0,y.jsxs)(u,{className:`p-6 space-y-4`,children:[(0,y.jsxs)(`h2`,{className:`text-lg font-semibold`,children:[`Pending Expenses (`,I.length,`)`]}),(0,y.jsx)(`div`,{className:`border rounded-md overflow-auto`,children:(0,y.jsxs)(`table`,{className:`w-full text-sm`,children:[(0,y.jsx)(`thead`,{className:`bg-muted/50`,children:(0,y.jsxs)(`tr`,{children:[(0,y.jsx)(`th`,{className:`text-left p-2`,children:`Date`}),(0,y.jsx)(`th`,{className:`text-left p-2`,children:`Service Request`}),(0,y.jsx)(`th`,{className:`text-left p-2`,children:`Expense Type`}),(0,y.jsx)(`th`,{className:`text-center p-2`,children:`Amount`})]})}),(0,y.jsx)(`tbody`,{children:I.map(e=>(0,y.jsxs)(`tr`,{className:`border-t`,children:[(0,y.jsx)(`td`,{className:`p-2`,children:e.daily_log_date||`—`}),(0,y.jsx)(`td`,{className:`p-2`,children:e.service_request_code||`—`}),(0,y.jsx)(`td`,{className:`p-2`,children:e.expense_master_name||`—`}),(0,y.jsx)(`td`,{className:`p-2 text-center`,children:e.amount??`—`})]},e.id))})]})}),(0,y.jsxs)(s,{type:`button`,onClick:async()=>{if(j({}),!t){j({employee:`Employee is required`});return}if(!S){j({branch:`Branch is required`});return}try{e(`/service-settlements/${(await P({employee:t,expense_ids:I.map(e=>e.id),branch:S,service_request:b||null,remarks:O}).unwrap()).id}`)}catch(e){j(m(e))}},disabled:F,className:`w-full`,children:[(0,y.jsx)(r,{className:`mr-2 h-4 w-4`}),` Create Settlement`]})]})]})}export{b as default};