1import{j as e}from"./ui-749bda0b.js";import{r as a}from"./vendor-4a6eca19.js";import{br as t,N as s,ai as r,V as i,az as l,aF as n,R as c}from"./index-b7973960.js";import{M as d}from"./MetadataEditor-23881c16.js";import{d as m,e as o,f as u}from"./storeApi-09630e4b.js";import{F as x}from"./fileUpload-06bfa7b7.js";import{b as p,u as g}from"./router-2a1143ef.js";import{X as h,J as b}from"./icons-05e2bda1.js";import"./redux-b8f2f78f.js";import"./utils-2f253737.js";const y=()=>{const{id:y,categoryId:j}=p(),f=g(),v=Boolean(y),[N,I]=a.useState({categoryId:j?parseInt(j):0,name:"",metadata:{},image:"",price:0,offerPrice:void 0,isAvailable:!0,itemType:t.Product,priority:1}),[M,C]=a.useState({}),[k,S]=a.useState(!1),[w,T]=a.useState(!1),[P,E]=a.useState(!1),[O,A]=a.useState(null),[U,L]=a.useState(""),[R,F]=a.useState(!1),[z,_]=a.useState(!1),[D,J]=a.useState("en");a.useState(["en","hi","ta"]);const[$,B]=a.useState(!1),[G,{isLoading:H}]=m(),[q,{isLoading:V}]=o(),{data:W,isLoading:Y}=u({itemId:y},{skip:!v});a.useEffect(()=>{if(v&&W?.data){const e=W.data?.item;I({categoryId:e.categoryId,name:e.name,metadata:e.metadata||{},image:e.image,price:e.price,offerPrice:e.offerPrice,isAvailable:!!e.isAvailable,itemType:e.itemType,priority:e.priority||1,nameMultilingual:e.nameMultilingual||{},multilingualMetadata:e.multilingualMetadata||{}}),L(e.image),(e.nameMultilingual||e.multilingualMetadata)&&B(!0)}},[v,W]),a.useEffect(()=>()=>{U&&U.startsWith("blob:")&&URL.revokeObjectURL(U)},[U]);const K=(e,a)=>{I(t=>({...t,[e]:a})),M[e]&&C(a=>{const t={...a};return delete t[e],t})},Q=H||V||w||P,X=a.useMemo(()=>{try{return Object.keys(t).filter(e=>isNaN(Number(e))).map(e=>({label:e,value:t[e]}))}catch(e){return[]}},[]);return e.jsxs("div",{className:"min-h-screen bg-gray-50",children:[e.jsx("div",{className:"bg-white border-b border-gray-200 px-6 py-4",children:e.jsx("div",{className:"max-w-7xl mx-auto",children:e.jsx("div",{className:"flex items-center justify-between",children:e.jsx("div",{className:"flex items-center space-x-4",children:e.jsx("div",{children:e.jsx("h1",{className:"text-2xl font-bold text-gray-900",children:v?"Edit Store Item":"Add Store Item"})})})})})}),e.jsxs("div",{className:"w-full mx-auto px-6 py-6",children:[Y&&v&&e.jsx("div",{className:"flex justify-center items-center py-12",children:e.jsxs("div",{className:"flex flex-col items-center gap-3",children:[e.jsx(s,{size:"lg"}),e.jsx("p",{className:"text-gray-600",children:"Loading item data..."})]})}),(!Y||!v)&&e.jsx("div",{className:"max-w-2xl mx-auto bg-white rounded-lg shadow-sm border border-gray-200 p-6",children:e.jsxs("form",{onSubmit(e){e.preventDefault(),(()=>{const e={};return N.categoryId&&0!==N.categoryId||(e.categoryId="Category ID is required"),N.name.trim()||(e.name="Item name is required"),N.image.trim()||O||(e.image="Please upload an image or provide an image URL"),O&&!N.image.trim()&&(e.image="Please upload the selected file before submitting"),(!N.price||N.price<=0)&&(e.price="Price must be greater than 0"),(!N.itemType||N.itemType<=0)&&(e.itemType="Item type must be greater than 0"),(!N.priority||N.priority<=0)&&(e.priority="Priority must be greater than 0"),C(e),0===Object.keys(e).length})()&&S(!0)},className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"Category ID"}),e.jsx(r,{type:"number",value:N.categoryId,readOnly:!0,className:"bg-gray-50"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"Item Name *"}),e.jsx(r,{type:"text",value:N.name,onChange:e=>K("name",e.target.value),placeholder:"Enter item name",className:M.name?"border-red-500":""}),M.name&&e.jsx("p",{className:"text-red-500 text-sm mt-1",children:M.name})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"Item Image *"}),U&&e.jsx("div",{className:"mb-4",children:e.jsxs("div",{className:"relative inline-block",children:[e.jsx("img",{src:U,alt:"Item preview",className:"w-32 h-32 object-cover rounded-lg border border-gray-200"}),e.jsx("button",{type:"button",onClick(){L(""),I(e=>({...e,image:""})),A(null)},className:"absolute -top-2 -right-2 bg-red-500 text-white rounded-full p-1 hover:bg-red-600 transition-colors",children:e.jsx(h,{size:14})})]})}),e.jsxs("div",{className:"flex items-center gap-3 ",children:[e.jsx("input",{type:"file",accept:"image/*",onChange(e){const a=e.target.files?.[0];if(a){if(!a.type.startsWith("image/"))return void l.error("Please select a valid image file");if(a.size>5242880)return void l.error("File size must be less than 5MB");A(a);const e=URL.createObjectURL(a);L(e),M.image&&C(e=>{const a={...e};return delete a.image,a})}},className:"hidden",id:"item-image"}),e.jsxs("label",{htmlFor:"item-image",className:"flex items-center space-x-2 px-4 py-2 border border-gray-300 rounded-lg cursor-pointer hover:bg-gray-50 transition-colors",children:[e.jsx(b,{size:16}),e.jsx("span",{children:"Choose Image"})]}),O&&e.jsxs("button",{type:"button",async onClick(){if(O)try{E(!0);const e=await x.upload(-1,n.STORE_ITEM,O);I(a=>({...a,image:e})),L(e),A(null),l.success("Image uploaded successfully!")}catch(e){l.error("Failed to upload image. Please try again.")}finally{E(!1)}else l.error("Please select a file first")},disabled:P,className:"flex items-center space-x-2 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:bg-blue-400 transition-colors",children:[P&&e.jsx(s,{size:"sm"}),e.jsx("span",{children:P?"Uploading...":"Upload"})]})]}),e.jsxs("div",{className:"mt-4",children:[e.jsx("label",{className:"block text-sm text-gray-600 mb-1",children:"Or enter image URL:"}),e.jsx(r,{type:"url",value:N.image,onChange:e=>K("image",e.target.value),placeholder:"https://example.com/image.jpg",className:M.image?"border-red-500":""})]}),M.image&&e.jsx("p",{className:"text-red-500 text-sm mt-1",children:M.image})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"Price *"}),e.jsx(r,{type:"number",value:N.price,onChange:e=>K("price",parseFloat(e.target.value)),placeholder:"Enter price",className:M.price?"border-red-500":""}),M.price&&e.jsx("p",{className:"text-red-500 text-sm mt-1",children:M.price})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"Offer Price"}),e.jsx(r,{type:"number",value:N.offerPrice||"",onChange:e=>K("offerPrice",e.target.value?parseFloat(e.target.value):void 0),placeholder:"Enter offer price (optional)"}),e.jsx("p",{className:"text-gray-500 text-sm mt-1",children:"Leave empty if no offer price"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"Item Type *"}
1),e.jsx("select",{value:N.itemType,onChange:e=>K("itemType",parseInt(e.target.value)||1),className:"w-full bg-transparent px-3 py-2 border rounded "+(M.itemType?"border-red-500":"border-gray-300"),children:X.map(a=>e.jsxs("option",{value:a.value,children:[a.label," (",a.value,")"]},a.value))}),M.itemType&&e.jsx("p",{className:"text-red-500 text-sm mt-1",children:M.itemType}),e.jsx("p",{className:"text-gray-500 text-sm mt-1",children:"Numeric identifier for the item type"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-2",children:"Priority *"}),e.jsx(r,{type:"number",min:"1",value:N.priority,onChange:e=>K("priority",parseInt(e.target.value)||1),placeholder:"Enter priority",className:M.priority?"border-red-500":""}),M.priority&&e.jsx("p",{className:"text-red-500 text-sm mt-1",children:M.priority}),e.jsx("p",{className:"text-gray-500 text-sm mt-1",children:"Higher priority items appear first in the list"})]}),e.jsxs("div",{children:[e.jsxs("label",{className:"flex items-center space-x-3",children:[e.jsx("input",{type:"checkbox",checked:N.isAvailable,onChange:e=>K("isAvailable",e.target.checked),className:"h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded"}),e.jsx("span",{className:"text-sm font-medium text-gray-700",children:"Item is available"})]}),e.jsx("p",{className:"text-gray-500 text-sm mt-1 ml-7",children:"Available items can be purchased by customers"})]}),e.jsx("div",{children:e.jsxs("label",{className:"flex items-center space-x-3",children:[e.jsx("input",{type:"checkbox",checked:z,onChange:e=>_(e.target.checked),className:"h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded"}),e.jsx("span",{className:"text-sm font-medium text-gray-700",children:"Show Metadata Editor"})]})}),e.jsx("div",{children:e.jsxs("label",{className:"flex items-center space-x-3",children:[e.jsx("input",{type:"checkbox",checked:$,onChange:e=>B(e.target.checked),className:"h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded"}),e.jsx("span",{className:"text-sm font-medium text-gray-700",children:"Enable Multilingual Support"})]})}),$&&e.jsxs("div",{className:"space-y-6 border-t pt-6",children:[e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("h3",{className:"text-lg font-semibold",children:"Multilingual Content"}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("label",{className:"text-sm font-medium text-gray-700",children:"Select Language:"}),e.jsxs("select",{value:D,onChange:e=>J(e.target.value),className:"px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500",children:[e.jsx("option",{value:"en",children:"English (en)"}),e.jsx("option",{value:"hi",children:"Hindi (hi)"}),e.jsx("option",{value:"ta",children:"Tamil (ta)"}),e.jsx("option",{value:"te",children:"Telugu (te)"})]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsxs("label",{className:"text-sm font-medium text-gray-700",children:["Item Name (",D.toUpperCase(),")"]}),e.jsx(r,{type:"text",placeholder:`Enter item name in ${D}`,value:N.nameMultilingual?.[D]||"",onChange(e){return a=D,t=e.target.value,void I(e=>({...e,nameMultilingual:{...e.nameMultilingual,[a]:t}}));var a,t}}),N.nameMultilingual&&Object.keys(N.nameMultilingual).length>0&&e.jsxs("div",{className:"mt-2 p-3 bg-gray-50 rounded-md",children:[e.jsx("p",{className:"text-xs font-medium text-gray-600 mb-2",children:"Current Translations:"}),e.jsx("div",{className:"space-y-1",children:Object.entries(N.nameMultilingual).map(([a,t])=>e.jsxs("div",{className:"text-sm",children:[e.jsxs("span",{className:"font-medium",children:[a.toUpperCase(),":"]})," ",t]},a))})]})]}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between border-b pb-2",children:[e.jsxs("h4",{className:"text-md font-semibold",children:["Metadata for ",D.toUpperCase()]}),N.multilingualMetadata?.[D]?e.jsx("span",{className:"text-xs px-2 py-1 bg-green-100 text-green-700 rounded-full",children:"â Has Data"}):e.jsx("span",{className:"text-xs px-2 py-1 bg-yellow-100 text-yellow-700 rounded-full",children:"â Empty - Add Content"})]}),e.jsx(d,{value:(()=>
1{if($){if(N.multilingualMetadata&&N.multilingualMetadata[D])try{return JSON.parse(N.multilingualMetadata[D])}catch(e){return{}}return{}}return N.metadata||{}})(),onChange:e=>((e,a)=>{I(t=>({...t,multilingualMetadata:{...t.multilingualMetadata,[e]:JSON.stringify(a)}}))})(D,e),disabled:!1,includeInApi:R,onIncludeInApiChange:F,locale:D,multilingualMetadata:N.multilingualMetadata,isForStoreItem:!0}),N.multilingualMetadata&&Object.keys(N.multilingualMetadata).length>0&&e.jsx("div",{className:"mt-4 p-3 bg-blue-50 rounded-md",children:e.jsxs("p",{className:"text-xs font-medium text-blue-800 mb-2",children:["Metadata available for: ",Object.keys(N.multilingualMetadata).join(", ").toUpperCase()]})})]})]}),z&&e.jsx(d,{value:N.metadata,onChange:e=>K("metadata",e),disabled:!1,includeInApi:R,onIncludeInApiChange:e=>F(e),isForStoreItem:!0}),e.jsxs("div",{className:"flex items-center justify-end space-x-4 pt-6 border-t border-gray-200",children:[e.jsx("button",{type:"button",onClick(){f(`${c.SEGMENTS.INTERNAL_CRM.ROOT}/${c.SEGMENTS.INTERNAL_CRM.MANAGE_STORE_ITEMS.replace(":categoryId",j)}`)},className:"px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition-colors",children:"Cancel"}),e.jsxs("button",{type:"submit",disabled:Q,className:"flex items-center space-x-2 px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:bg-blue-400 transition-colors",children:[Q&&e.jsx(s,{size:"sm"}),e.jsx("span",{children:v?"Update Item":"Create Item"})]})]})]})}),e.jsx(i,{modalVisible:k,onClose:()=>S(!1),children:e.jsxs("div",{className:"text-center",children:[e.jsx("h3",{className:"text-lg font-semibold text-gray-900 mb-4",children:v?"Update Store Item":"Create Store Item"}),e.jsxs("p",{className:"text-gray-600 mb-6",children:["Are you sure you want to ",v?"update":"create"," this store item?"]}),e.jsx("div",{className:"bg-gray-50 p-4 rounded-lg mb-6 text-left",children:e.jsxs("div",{className:"space-y-2",children:[e.jsxs("p",{children:[e.jsx("strong",{children:"Name:"})," ",N.name]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Price:"})," â¹",N.price]}),N.offerPrice&&e.jsxs("p",{children:[e.jsx("strong",{children:"Offer Price:"})," â¹",N.offerPrice]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Item Type:"})," ",N.itemType]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Priority:"})," ",N.priority]}),e.jsxs("p",{children:[e.jsx("strong",{children:"Available:"})," ",N.isAvailable?"Yes":"No"]}),N.image&&e.jsxs("div",{children:[e.jsx("strong",{children:"Image:"}),e.jsx("img",{src:N.image,alt:"Item",className:"w-16 h-16 object-cover rounded mt-1"})]})]})}),e.jsxs("div",{className:"flex items-center justify-center space-x-4",children:[e.jsx("button",{onClick:()=>S(!1),disabled:w,className:"px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 transition-colors disabled:opacity-50",children:"Cancel"}),e.jsxs("button",{async onClick(){try{T(!0);const e={categoryId:N.categoryId,name:N.name,image:N.image,price:N.price,...N.offerPrice&&{offerPrice:N.offerPrice},isAvailable:N.isAvailable,itemType:N.itemType,priority:N.priority,...R&&N.metadata&&Object.keys(N.metadata).length>0&&{metadata:N.metadata}};if($&&(N.multilingualMetadata&&Object.keys(N.multilingualMetadata).length>0&&(e.localeMetadata=Object.entries(N.multilingualMetadata).map(([e,a])=>{try{return{locale:e,metadata:"string"==typeof a?JSON.parse(a):a}}catch(t){return null}}).filter(Boolean)),N.nameMultilingual&&Object.keys(N.nameMultilingual).length>0&&(e.localeName=Object.entries(N.nameMultilingual).map(([e,a])=>({locale:e,name:a})))),v){const a=e;await q({itemId:y,data:a}).unwrap(),l.success("Store item updated successfully!")}else{const a=e;await G(a).unwrap(),l.success("Store item created successfully!")}I({categoryId:j?parseInt(j):0,name:"",metadata:{},image:"",price:0,offerPrice:void 0,isAvailable:!0,itemType:1,priority:1}),F(!1),S(!1),f(-1)}catch(e){const a=e?.data?.message||e?.message||`Failed to ${v?"update":"create"} store item. Please try again.`;l.error(a)}finally{T(!1)}},disabled:w,className:"flex items-center space-x-2 px-6 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:bg-blue-400 transition-colors",children:[w&&e.jsx(s,{size:"sm"}),e.jsx("span",{children:v?"Update":"Create"})]})]})]})})]})]})};export{y as StoreItemPage,y as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.