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{es as i,os as a,ss as o,za as s}from"./index-B4NicEaS.js";import{t as c}from"./page-content-DdXVZwik.js";import{t as l}from"./card-CBaLHc_f.js";import{t as u}from"./error-alert-b_a_jMDN.js";import{t as d}from"./form-field-CXKqsV1k.js";import{t as f}from"./form-select-se2tBkXI.js";import{t as p}from"./validators-CNWWIxpC.js";import{W as m,c as h,w as g}from"./serviceApi-Bbk8BJQK.js";var _=e(t(),1),v=n();function y(){let e=a(),{id:t}=o(),n=!!t,[y,b]=(0,_.useState)({name:``,expense_type:`project`,calculation_type:`manual`,fixed_amount:``,status:`active`}),[x,S]=(0,_.useState)({}),[C,w]=(0,_.useState)({}),{data:T=[]}=g(),E=n?T.find(e=>e.id===t):void 0,[D,{isLoading:O}]=h(),[k,{isLoading:A}]=m();(0,_.useEffect)(()=>{E&&b({name:E.name,expense_type:E.expense_type,calculation_type:E.calculation_type,fixed_amount:E.fixed_amount||``,status:E.status})},[E]);let j=(e,t)=>{b(n=>({...n,[e]:t})),x[e]&&S(t=>({...t,[e]:``}))},M=()=>{let e={};return y.name.trim()||(e.name=`Name is required`),y.calculation_type===`fixed`&&(!y.fixed_amount||Number(y.fixed_amount)<=0)&&(e.fixed_amount=`Fixed amount must be greater than zero`),S(e),Object.keys(e).length===0};return(0,v.jsxs)(c,{children:[(0,v.jsxs)(`div`,{className:`flex items-center gap-4`,children:[(0,v.jsx)(s,{variant:`ghost`,size:`icon`,onClick:()=>e(`/masters/expenses`),children:(0,v.jsx)(i,{className:`h-4 w-4`})}),(0,v.jsx)(`h1`,{className:`text-3xl font-bold`,children:n?`Edit Expense`:`New Expense`})]}),(0,v.jsx)(`form`,{onSubmit:async r=>{if(r.preventDefault(),w({}),M())try{let r={name:y.name,expense_type:y.expense_type,calculation_type:y.calculation_type,status:y.status,fixed_amount:y.calculation_type===`fixed`?y.fixed_amount:null};n?await k({id:t,data:r}).unwrap():await D(r).unwrap(),e(`/masters/expenses`)}catch(e){w(p(e))}},children:(0,v.jsxs)(l,{className:`p-6 space-y-4 max-w-lg`,children:[(0,v.jsx)(u,{errors:C}),(0,v.jsx)(d,{label:`Name`,required:!0,value:y.name,onChange:e=>j(`name`,e.target.value),error:x.name}),(0,v.jsx)(f,{label:`Expense Type`,required:!0,value:y.expense_type,onChange:e=>j(`expense_type`,e.target.value),options:[{value:`project`,label:`Project`},{value:`employee`,label:`Employee`}]}),(0,v.jsx)(f,{label:`Calculation Type`,required:!0,value:y.calculation_type,onChange:e=>j(`calculation_type`,e.target.value),options:[{value:`fixed`,label:`Fixed`},{value:`manual`,label:`Manual`}]}),y.calculation_type===`fixed`&&(0,v.jsx)(d,{label:`Fixed Amount`,required:!0,type:`number`,min:0,step:`0.01`,value:y.fixed_amount,onChange:e=>j(`fixed_amount`,e.target.value),error:x.fixed_amount}),(0,v.jsx)(f,{label:`Status`,value:y.status,onChange:e=>j(`status`,e.target.value),options:[{value:`active`,label:`Active`},{value:`inactive`,label:`Inactive`}]}),(0,v.jsxs)(s,{type:`submit`,disabled:O||A,children:[(0,v.jsx)(r,{className:`mr-2 h-4 w-4`}),` `,n?`Update`:`Create`]})]})})]})}export{y as default};