import{i as e}from"./rolldown-runtime-Dd_uD5pT.js";import{t}from"./react-BatatxyT.js";import{s as n}from"./utils-BO1RPuol.js";import{t as r}from"./save-D-WHGflg.js";import{aa as i,es as a,os as o,vo as s,za as c}from"./index-CsuVxiRQ.js";import{t as l}from"./page-content-DdXVZwik.js";import{t as u}from"./card-CBaLHc_f.js";import{t as d}from"./error-alert-b_a_jMDN.js";import{t as f}from"./form-field-4j0NqnRe.js";import{t as p}from"./server-search-select-Dg-Yw5Va.js";import{t as m}from"./validators-CNWWIxpC.js";import{t as h}from"./form-textarea-vnTsM02Z.js";import{I as g,l as _}from"./serviceApi-RIObRUm3.js";var v=e(t(),1),y=n();function b(){let e=o(),[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)(c,{variant:`ghost`,size:`icon`,onClick:()=>e(`/service-settlements`),children:(0,y.jsx)(a,{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)(i,{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)(i,{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)(i,{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)(c,{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)(s,{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)(c,{type:`button`,onClick:async()=>{if(j({}),!t){j({employee:`Employee is required`});return}if(!S){j({branch:`Branch is required`});return}try{let n=await P({employee:t,expense_ids:I.map(e=>e.id),branch:S,service_request:b||null,remarks:O}).unwrap();e(`/service-settlements/${n.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};