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