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{Ai as i,Si as a,Vn as o,Zr as s,es as c,os as l,ss as u,za as d}from"./index-B4NicEaS.js";import{t as f}from"./page-content-DdXVZwik.js";import{t as p}from"./card-CBaLHc_f.js";import{t as m}from"./error-alert-b_a_jMDN.js";import{t as h}from"./form-field-CXKqsV1k.js";import{t as g}from"./form-select-se2tBkXI.js";import{t as _}from"./validators-CNWWIxpC.js";import{r as v}from"./infinite-scroll-select-Dj0cx973.js";var y=e(t(),1),b=n();function x(){let e=l(),{id:t}=u(),n=!!t,[x,S]=(0,y.useState)({name:``,description:``,hsn_code:``,oem_part_no:``,oem_description:``,parent:``,item_type:``,product_type:`2`,uom:``,category:``,status:`active`}),[C,w]=(0,y.useState)({}),[T,E]=(0,y.useState)({}),{data:D=[]}=a(),{data:O=[]}=s(),[k,{isLoading:A}]=o(),[j,{isLoading:M}]=i(),N=()=>{let e={};return x.parent||(e.parent=`Please select an item group first`),x.name.trim()||(e.name=`Item name is required`),x.description.trim()||(e.description=`Description is required`),x.uom||(e.uom=`UOM is required`),w(e),Object.keys(e).length===0},P=async r=>{if(r.preventDefault(),E({}),N())try{let r={...x};r.parent||=null,r.item_type||delete r.item_type,r.product_type||delete r.product_type,n?await j({id:t,data:r}).unwrap():await k(r).unwrap(),v(),e(`/masters/items`)}catch(e){E(_(e))}},F=(e,t)=>{S(n=>({...n,[e]:t})),C[e]&&w(t=>({...t,[e]:``}))};return(0,b.jsxs)(f,{children:[(0,b.jsxs)(`div`,{className:`flex items-center gap-4`,children:[(0,b.jsx)(d,{variant:`ghost`,size:`icon`,onClick:()=>e(`/masters/items`),children:(0,b.jsx)(c,{className:`h-4 w-4`})}),(0,b.jsxs)(`div`,{children:[(0,b.jsx)(`h1`,{className:`text-3xl font-bold`,children:n?`Edit Item`:`New Item`}),(0,b.jsx)(`p`,{className:`text-muted-foreground`,children:n?`Update item information`:`Add a new item`})]})]}),(0,b.jsx)(`form`,{onSubmit:P,children:(0,b.jsxs)(p,{className:`p-6 space-y-4`,children:[(0,b.jsx)(m,{errors:T}),(0,b.jsx)(g,{label:`Item Group`,required:!0,id:`parent`,value:x.parent,onChange:e=>F(`parent`,e.target.value),error:C.parent,options:O.filter(e=>e.product_type===1&&e.id!==t).map(e=>({value:e.id,label:`${e.code} — ${e.description}`})),placeholder:`Select Item Group`}),(0,b.jsx)(h,{label:`Item Name`,required:!0,id:`name`,value:x.name,onChange:e=>F(`name`,e.target.value),error:C.name,placeholder:`Enter item name`}),(0,b.jsx)(h,{label:`Description`,required:!0,id:`description`,value:x.description,onChange:e=>F(`description`,e.target.value),error:C.description}),(0,b.jsx)(h,{label:`HSN / SAC Code`,id:`hsn_code`,value:x.hsn_code,onChange:e=>F(`hsn_code`,e.target.value),placeholder:`Enter HSN / SAC code`}),(0,b.jsx)(h,{label:`OEM Part No`,id:`oem_part_no`,value:x.oem_part_no,onChange:e=>F(`oem_part_no`,e.target.value),placeholder:`Enter OEM part number`}),(0,b.jsx)(h,{label:`OEM Description`,id:`oem_description`,value:x.oem_description,onChange:e=>F(`oem_description`,e.target.value),placeholder:`Enter OEM description`}),(0,b.jsxs)(`div`,{className:`grid grid-cols-2 gap-4`,children:[(0,b.jsx)(g,{label:`Item Type`,id:`item_type`,value:x.item_type,onChange:e=>F(`item_type`,e.target.value),options:[{value:`1`,label:`Material`},{value:`2`,label:`Service`}],placeholder:`Select Type`}),(0,b.jsx)(g,{label:`Group / Product`,id:`product_type`,value:x.product_type,onChange:e=>F(`product_type`,e.target.value),options:[{value:`1`,label:`Group`},{value:`2`,label:`Product`}],placeholder:`Select`})]}),(0,b.jsx)(g,{label:`Unit of Measure`,required:!0,id:`uom`,value:x.uom,onChange:e=>F(`uom`,e.target.value),error:C.uom,options:D.map(e=>({value:e.id,label:e.name})),placeholder:`Select UOM`}),(0,b.jsx)(h,{label:`Category`,id:`category`,value:x.category,onChange:e=>F(`category`,e.target.value)}),(0,b.jsx)(g,{label:`Status`,id:`status`,value:x.status,onChange:e=>F(`status`,e.target.value),options:[{value:`active`,label:`Active`},{value:`inactive`,label:`Inactive`}]}),(0,b.jsxs)(`div`,{className:`flex justify-end gap-4`,children:[(0,b.jsx)(d,{type:`button`,variant:`outline`,onClick:()=>e(`/masters/items`),children:`Cancel`}),(0,b.jsxs)(d,{type:`submit`,disabled:A||M,children:[(0,b.jsx)(r,{className:`mr-2 h-4 w-4`}),A||M?`Saving...`:`Save`]})]})]})})]})}export{x as default};