import{a as e,t}from"./react-DK-MTtD8.js";import{s as n}from"./utils-CXcsdpX-.js";import{t as r}from"./save-DstqMto4.js";import{Ai as i,Si as a,Vn as o,Zo as s,Zr as c,is as l,rs as u,za as d}from"./index-Ct5uU080.js";import{t as f}from"./page-content-Bh5Zxb_Y.js";import{t as p}from"./card-CCLQVS9k.js";import{t as m}from"./error-alert-eQ6ClPXs.js";import{t as h}from"./form-field-BuHvZ1sK.js";import{t as g}from"./form-select-DIeUz5Nk.js";import{t as _}from"./validators-CNWWIxpC.js";var v=e(t(),1),y=n();function b(){let e=u(),{id:t}=l(),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=[]}=a(),{data:D=[]}=c(),[O,{isLoading:k}]=o(),[A,{isLoading:j}]=i(),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)(d,{variant:`ghost`,size:`icon`,onClick:()=>e(`/masters/items`),children:(0,y.jsx)(s,{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)(d,{type:`button`,variant:`outline`,onClick:()=>e(`/masters/items`),children:`Cancel`}),(0,y.jsxs)(d,{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};