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,Io as a,ga as o,ko as s}from"./index-BP3TYB4i.js";import{t as c}from"./page-content-DDtdeMxM.js";import{t as l}from"./card-BVFw-jgA.js";import{t as u}from"./error-alert-qe7c2K0-.js";import{t as d}from"./form-field-Co56F5gt.js";import{t as f}from"./form-select-Rqf9_CvJ.js";import{t as p}from"./validators-CNWWIxpC.js";import{W as m,c as h,w as g}from"./serviceApi-BlxU9Cj1.js";var _=e(t(),1),v=n();function y(){let e=i(),{id:t}=a(),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)(o,{variant:`ghost`,size:`icon`,onClick:()=>e(`/masters/expenses`),children:(0,v.jsx)(s,{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)(o,{type:`submit`,disabled:O||A,children:[(0,v.jsx)(r,{className:`mr-2 h-4 w-4`}),` `,n?`Update`:`Create`]})]})})]})}export{y as default};