1import{X as te,r as u,j as e,a as v}from"./app-Mnt2IEGe.js";import{A as se}from"./AuthenticatedLayout-Dh41BUxo.js";import{R as ae}from"./RichTextEditor-CvhqU08b.js";import{n as O,C as re}from"./CustomizationFieldsEditor-D835RYja.js";import"./ApplicationLogo-JvKRx6b1.js";import"./ResponsiveNavLink-xYyYAnIm.js";import"./transition-caWIvkel.js";import"./chevron-down-BlAiga6f.js";function ue({product:l,categories:P}){var J;const{props:w}=te(),$=(J=w==null?void 0:w.auth)==null?void 0:J.user,[n,d]=u.useState(()=>{var t;return{name:l.name||"",slug:l.slug||"",description:l.description||"",short_description:l.short_description||"",price:l.price||0,compare_at_price:l.compare_at_price||"",inventory:l.inventory||0,is_active:l.is_active,is_customizable:l.is_customizable,is_featured:l.is_featured,primary_image:l.primary_image||"",categories:((t=l.categories)==null?void 0:t.map(s=>s.id))||[],meta_title:l.meta_title||"",meta_description:l.meta_description||"",meta_keywords:l.meta_keywords||"",customization_fields:O(l.customization_fields)}}),[C,h]=u.useState(!1),[x,S]=u.useState({}),[k,y]=u.useState(null),c=t=>{const s=x==null?void 0:x[t];return s?Array.isArray(s)?s[0]:s:""},j=(t,s="w-full border rounded px-3 py-2")=>`${s} border-gray-300 focus:outline-none focus:ring-2 focus:ring-primary-light ${c(t)?"border-rose-300 bg-rose-50 focus:ring-rose-100":""}`,B=u.useMemo(()=>{const t=c("categories");if(t)return t;const a=Object.keys(x||{}).find(i=>i.startsWith("categories."));if(!a)return"";const r=x[a];return Array.isArray(r)?r[0]:typeof r=="string"?r:""},[x]),F=u.useMemo(()=>{const t=c("customization_fields");if(t)return t;const a=Object.keys(x||{}).find(i=>i.startsWith("customization_fields."));if(!a)return"";const r=x[a];return Array.isArray(r)?r[0]:typeof r=="string"?r:""},[x]),z=(t,s)=>{var r,i,o,p;const a=(i=(r=t==null?void 0:t.response)==null?void 0:r.data)==null?void 0:i.errors;if(a&&typeof a=="object"){const g=Object.keys(a)[0];if(g){const _=a[g];if(Array.isArray(_))return _[0];if(typeof _=="string")return _}}return((p=(o=t==null?void 0:t.response)==null?void 0:o.data)==null?void 0:p.message)||(t==null?void 0:t.message)||s},L=async t=>{var s,a;t.preventDefault(),h(!0),S({}),y(null);try{const r={...n};r.is_customizable?r.customization_fields=O(r.customization_fields):r.customization_fields=[],await v.patch(`/admin/products/${l.id}`,r),y({type:"success",message:"Product saved successfully."})}catch(r){const i=(a=(s=r.response)==null?void 0:s.data)==null?void 0:a.errors;i?(S(i),y({type:"error",message:"Please fix the highlighted fields and try again."})):y({type:"error",message:"Failed to save the product. Please try again."})}finally{h(!1)}},U=t=>{d(s=>({...s,categories:s.categories.includes(t)?s.categories.filter(a=>a!==t):[...s.categories,t]}))},W=t=>{d(s=>({...s,customization_fields:O(t)}))},M=t=>!t||typeof t!="object"?[]:Object.entries(t).map(([s,a])=>({key:s??"",value:a??""})),X=t=>{const s={};return(t||[]).forEach(({key:a,value:r})=>{const i=(a??"").trim();i&&(s[i]=r??"")}),s},A=t=>{const s=t!=null&&t.options&&typeof t.options=="object"?t.options:{};return{...t,options:s,option_pairs:M(s)}},[q,E]=u.useState(l.images||[]),[b,N]=u.useState(()=>(l.variants||[]).map(A)),m=u.useRef(b);u.useEffect(()=>{m.current=b},[b]);const R=u.useMemo(()=>{const t=new Set,s=new Map;return(b||[]).forEach(a=>{(a.option_pairs||[]).forEach(({key:r,value:i})=>{const o=r==null?void 0:r.toString().trim();o&&(t.add(o),s.has(o)||s.set(o,new Set),i!=null&&i!==""&&s.get(o).add(i.toString()))})}),{keys:Array.from(t),entries:Array.from(s.entries()).map(([a,r])=>({key:a,values:Array.from(r)}))}},[b]),V=t=>{!t||!("primary_image"in t)||d(s=>({...s,primary_image:t.primary_image||""}))},G=async t=>{const s=Array.from(t.target.files||[]);for(const a of s){const r=new FormData;r.append("image",a);try{const{data:i}=await v.post(`/admin/products/${l.id}/images`,r,{headers:{"Content-Type":"multipart/form-data"}});i!=null&&i.image&&E(o=>[i.image,...o]),V(i)}catch{}}t.target.value=""},H=async t=>{const{data:s}=await v.patch(`/admin/products/${l.id}/images/${t.id}/primary`);E(a=>a.map(r=>({...r,is_primary:r.id===t.id}))),V(s)},I=async t=>{if(!confirm("Delete image?"))return;const{data:s}=await v.delete(`/admin/products/${l.id}/images/${t.id}`);E(a=>a.filter(r=>r.id!==t.id)),V(s)},Q=async()=>{const t={sku:"",options:{},price_override:null,compare_at_price:null,inventory:0,is_active:!0};try{const{data:s}=await v.post(`/admin/products/${l.id}/variants`,t);if(s!=null&&s.variant){const a=A(s.variant);N(r=>[...r,a]),m.current=[...m.current||[],a],y({type:"success",message:"Variant added."})}}catch(s){y({type:"error",message:z(s,"Failed to add variant.")})}},f=async(t,s)=>{try{await v.patch(`/admin/products/${l.id}/variants/${t}`,s);const a=r=>(r||[]).map(i=>{if(i.id!==t)return i;const o={...i,...s};return s.options!==void 0&&(o.options=s.options,o.option_pairs=M(s.options)),o});return N(r=>a(r)),m.current=a(m.current),y({type:"success",message:"Variant updated."}),!0}catch(a){return y({type:"error",message:z(a,"Failed to update variant.")}),!1}},Y=async t=>{if(confirm("Delete variant?"))try{await v.delete(`/admin/products/${l.id}/variants/${t}`),N(s=>s.filter(a=>a.id!==t)),m.current=(m.current||[]).filter(s=>s.id!==t),y({type:"success",message:"Variant deleted."})}catch(s){y({type:"error",message:z(s,"Failed to delete variant.")})}},D=async(t,s)=>{const a=X(s),r=m.current.find(p=>p.id===t),i=r?r.options||{}:{};if(JSON.stringify(i)===JSON.stringify(a))return;if(!await f(t,{options:a})&&r){const p=A({...r,options:i});N(g=>g.map(_=>_.id===t?p:_)),m.current=m.current.map(g=>g.id===t?p:g)}},K=(t,s,a,r)=>{const i=o=>(o||[]).map(p=>{if(p.id!==t)return p;const g=[...p.option_pairs||[]];for(;g.length<=s;)g.push({key:"",value:""});return g[s]={...g[s],[a]:r},{...p,option_pairs:g}});N(o=>i(o)),m.current=i(m.current)},T=t=>{const s=m.current.find(a=>a.id===t);s&&D(t,s.option_pairs||[]).catch(()=>{})},Z=t=>{const s=a=>(a||[]).map(r=>{if(r.id!==t)return r;const i=[...r.option_pairs||[]];return i.push({key:"",value:""}),{...r,option_pairs:i}});N(a=>s(a)),m.current=s(m.current)},ee=(t,s)=>{let a=null;const r=i=>(i||[]).map(o=>{if(o.id!==t)return o;const p=[...o.option_pairs||[]];return p.splice(s,1),a=p,{...o,option_pairs:p}});N(i=>
1r(i)),m.current=r(m.current),a!==null&&D(t,a).catch(()=>{})};return e.jsx(se,{user:$,header:e.jsx("h1",{className:"text-xl font-bold",children:"Edit Product"}),children:e.jsx("div",{className:"max-w-4xl mx-auto py-2",children:e.jsxs("form",{onSubmit:L,className:"bg-white border rounded-xl p-6 space-y-4",children:[e.jsxs("div",{className:"grid md:grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-sm text-gray-700",children:"Name"}),e.jsx("input",{className:j("name"),value:n.name,onChange:t=>d({...n,name:t.target.value})}),c("name")&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:c("name")})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm text-gray-700",children:"Slug"}),e.jsx("input",{className:j("slug"),value:n.slug,onChange:t=>d({...n,slug:t.target.value})}),c("slug")&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:c("slug")})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm text-gray-700",children:"Description"}),e.jsx("div",{className:"mt-2",children:e.jsx(ae,{value:n.description,onChange:t=>d(s=>({...s,description:t})),placeholder:"Tell customers everything about this productâ¦"})}),c("description")&&e.jsx("p",{className:"mt-2 text-xs text-rose-600",children:c("description")})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm text-gray-700",children:"Short Description"}),e.jsx("input",{className:j("short_description"),value:n.short_description,onChange:t=>d({...n,short_description:t.target.value})}),c("short_description")&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:c("short_description")})]}),e.jsxs("div",{className:"grid md:grid-cols-3 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-sm text-gray-700",children:"Price"}),e.jsx("input",{type:"number",className:j("price"),value:n.price,onChange:t=>d({...n,price:Number(t.target.value)})}),c("price")&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:c("price")})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm text-gray-700",children:"Compare at"}),e.jsx("input",{type:"number",className:j("compare_at_price"),value:n.compare_at_price||"",onChange:t=>d({...n,compare_at_price:t.target.value})}),c("compare_at_price")&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:c("compare_at_price")})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm text-gray-700",children:"Inventory"}),e.jsx("input",{type:"number",className:j("inventory"),value:n.inventory,onChange:t=>d({...n,inventory:Number(t.target.value)})}),c("inventory")&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:c("inventory")})]})]}),e.jsxs("div",{className:"grid md:grid-cols-3 gap-4",children:[e.jsxs("label",{className:"flex items-center gap-2",children:[e.jsx("input",{type:"checkbox",checked:n.is_active,onChange:t=>d({...n,is_active:t.target.checked})})," ","Active"]}),e.jsxs("label",{className:"flex items-center gap-2",children:[e.jsx("input",{type:"checkbox",checked:n.is_featured,onChange:t=>d({...n,is_featured:t.target.checked})})," ","Featured"]})]}),n.is_customizable&&e.jsxs("div",{className:"space-y-3 rounded-xl border border-indigo-100 bg-indigo-50/40 p-4",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx("p",{className:"text-sm font-semibold text-indigo-700",children:"Customization form"}),e.jsx("p",{className:"text-xs text-primary",children:"Add the fields customers must complete before adding this product to the cart."})]}),F&&e.jsx("p",{className:"text-xs text-rose-600",children:F}),e.jsx(re,{fields:n.customization_fields,onChange:W})]}),e.jsxs("div",{className:"grid md:grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-sm text-gray-700",children:"Meta Title"}),e.jsx("input",{className:j("meta_title"),value:n.meta_title,onChange:t=>d({...n,meta_title:t.target.value})}),c("meta_title")&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:c("meta_title")})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm text-gray-700",children:"Meta Keywords (comma separated)"}),e.jsx("input",{className:j("meta_keywords"),value:n.meta_keywords,onChange:t=>d({...n,meta_keywords:t.target.value})}),c("meta_keywords")&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:c("meta_keywords")})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm text-gray-700",children:"Meta Description"}),e.jsx("textarea",{className:j("meta_description","w-full border rounded px-3 py-2"),rows:3,value:n.meta_description,onChange:t=>d({...n,meta_description:t.target.value})}),c("meta_description")&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:c("meta_description")})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm text-gray-700",children:"Primary Image URL"}),e.jsx("input",{className:j("primary_image"),value:n.primary_image,onChange:t=>d({...n,primary_image:t.target.value})}),e.jsx("p",{className:"mt-1 text-xs text-gray-500",children:"Shown on product listing cards. Fills in automatically when you upload an image or set one as primary below; leave it blank to keep using the primary image from the gallery."}),c("primary_image")&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:c("primary_image")})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm font-semibold text-gray-900",children:"Product Images"}),e.jsx("div",{className:"mt-2",children:e.jsx("input",{type:"file",multiple:!0,accept:"image/*",onChange:G})}),e.jsx("div",{className:"mt-3 grid grid-cols-2 md:grid-cols-4 gap-3",children:q.map(t=>e.jsxs("div",{className:"border rounded-lg p-2",children:[e.jsx("img",{src:t.path,alt:t.alt||"",className:"w-full h-32 object-cover rounded"}),e.jsxs("div",{className:"flex items-center justify-between mt-2 text-xs",children:[e.jsx("button",{type:"button",onClick:()=>H(t),className:`px-2 py-1 rounded ${t.is_primary?"bg-green-600 text-white":"bg-gray-100"}`,children:t.is_primary?"Primary":"Set Primary"}),e.jsx("button",{type:"button",onClick:()=>I(t),className:"px-2 py-1 rounded bg-red-100 text-red-700",children:"Delete"})]})]},t.id))})]}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-2",children:[e.jsx("label",{className:"text-sm font-semibold text-gray-900",children:"Variants"}),e.jsx("button",{type:"button",onClick:Q,className:"px-3 py-1 bg-primary text-white rounded text-sm",children:"Add Variant"})]}),e.jsxs("div",{className:"mb-3 space-y-1 rounded-lg border border-blue-100 bg-primary-light px-3 py-2 text-xs text-blue-900",children:[e.jsx("p",{className:"font-semibold",children:"Variant options guide"}),e.jsxs("p",{children:["Add option name/value pairs (for example,"," ",e.jsx("code",{children:"Color â Black"}),") using the fields below. Keep the same option names (like"," ",e.jsx("code",{children:"Color"}),", ",e.jsx("code",{children:"Size"}),") across every variant so the storefront can match selections accurately."]}),R.entries.length>0?e.jsxs("div",{children:[e.jsx("p",{className:"font-semibold",children:"Current option names"}),e.jsx("ul",{className:"list-disc pl-4",children:R.entries.map(({key:t,values:s})=>e.jsxs("li",{children:[e.jsx("span",{className:"font-semibold",children:t}),s.length>0&&e.jsxs("span",{className:"ml-1 text-blue-800",children:["â values seen:"," ",s.join(", ")
1]})]},t))})]}):e.jsxs("p",{children:["Need ideas? Try names such as"," ",e.jsx("code",{children:"Color"}),", ",e.jsx("code",{children:"Size"}),","," ",e.jsx("code",{children:"Pack Size"}),", or"," ",e.jsx("code",{children:"Material"}),"."]})]}),b.length===0?e.jsx("div",{className:"text-sm text-gray-500",children:"No variants"}):e.jsx("div",{className:"overflow-auto",children:e.jsxs("table",{className:"min-w-full text-sm border",children:[e.jsx("thead",{className:"bg-gray-50",children:e.jsxs("tr",{children:[e.jsx("th",{className:"p-2 text-left",children:"SKU"}),e.jsx("th",{className:"p-2 text-left",children:"Options (JSON)"}),e.jsx("th",{className:"p-2 text-left",children:"Sale Price"}),e.jsx("th",{className:"p-2 text-left",children:"MRP"}),e.jsx("th",{className:"p-2 text-left",children:"Inventory"}),e.jsx("th",{className:"p-2 text-left",children:"Active"}),e.jsx("th",{className:"p-2"})]})}),e.jsx("tbody",{children:b.map(t=>e.jsxs("tr",{className:"border-t",children:[e.jsx("td",{className:"p-2",children:e.jsx("input",{defaultValue:t.sku||"",className:"border rounded px-2 py-1",onBlur:s=>f(t.id,{sku:s.target.value})})}),e.jsx("td",{className:"p-2",children:e.jsxs("div",{className:"space-y-2",children:[(t.option_pairs&&t.option_pairs.length>0?t.option_pairs:[{key:"",value:""}]).map((s,a,r)=>e.jsxs("div",{className:"flex flex-wrap items-center gap-2",children:[e.jsx("input",{value:s.key,placeholder:"Option name (e.g. Color)",className:"flex-1 min-w-[140px] border rounded px-2 py-1",onChange:i=>K(t.id,a,"key",i.target.value),onBlur:()=>T(t.id)}),e.jsx("span",{className:"text-gray-400 text-sm",children:"â"}),e.jsx("input",{value:s.value,placeholder:"Value (e.g. Red)",className:"flex-1 min-w-[140px] border rounded px-2 py-1",onChange:i=>K(t.id,a,"value",i.target.value),onBlur:()=>T(t.id)}),r.length>1&&e.jsx("button",{type:"button",onClick:()=>ee(t.id,a),className:"text-xs text-red-600",children:"Remove"})]},`${t.id}-pair-${a}`)),e.jsx("button",{type:"button",onClick:()=>Z(t.id),className:"text-xs font-semibold text-primary",children:"+ Add option"})]})}),e.jsx("td",{className:"p-2",children:e.jsx("input",{type:"number",defaultValue:t.price_override??"",className:"border rounded px-2 py-1 w-28",onBlur:s=>{const a=s.target.value.trim();if(a===""){f(t.id,{price_override:null});return}const r=Number(a);f(t.id,{price_override:Number.isNaN(r)?null:r})}})}),e.jsx("td",{className:"p-2",children:e.jsx("input",{type:"number",defaultValue:t.compare_at_price??"",className:"border rounded px-2 py-1 w-28",onBlur:s=>{const a=s.target.value.trim();if(a===""){f(t.id,{compare_at_price:null});return}const r=Number(a);f(t.id,{compare_at_price:Number.isNaN(r)?null:r})}})}),e.jsx("td",{className:"p-2",children:e.jsx("input",{type:"number",defaultValue:t.inventory,className:"border rounded px-2 py-1 w-20",onBlur:s=>f(t.id,{inventory:Number(s.target.value)})})}),e.jsx("td",{className:"p-2 text-center",children:e.jsx("input",{type:"checkbox",defaultChecked:t.is_active,onChange:s=>f(t.id,{is_active:s.target.checked})})}),e.jsx("td",{className:"p-2 text-right",children:e.jsx("button",{type:"button",onClick:()=>Y(t.id),className:"text-red-600",children:"Delete"})})]},t.id))})]})})]}),e.jsxs("div",{children:[e.jsx("label",{className:"text-sm text-gray-700",children:"Categories"}),B&&e.jsx("p",{className:"mt-1 text-xs text-rose-600",children:B}),e.jsx("div",{className:"flex flex-wrap gap-3 mt-1",children:P.map(t=>e.jsxs("label",{className:`px-3 py-1 border rounded-full text-sm cursor-pointer ${n.categories.includes(t.id)?"bg-primary-light border-blue-400":"bg-white"}`,children:[e.jsx("input",{type:"checkbox",className:"mr-2",checked:n.categories.includes(t.id),onChange:()=>U(t.id)}),t.name]},t.id))})]}),e.jsx(ie,{productId:l.id,productName:l.name}),e.jsx("button",{type:"submit",disabled:C,className:`px-5 py-2 rounded text-white ${C?"bg-blue-400 cursor-not-allowed":"bg-primary hover:bg-blue-700"}`,children:C?"Savingâ¦":"Save"}),k&&e.jsx("div",{className:`rounded-md border px-3 py-2 text-sm ${k.type==="success"?"border-emerald-200 bg-emerald-50 text-emerald-700":"border-rose-200 bg-rose-50 text-rose-700"}`,children:k.message})]})})})}function ie({productId:l,productName:P}){const[w,$]=u.useState([]),[n,d]=u.useState(!0);u.useEffect(()=>{C()},[l]);const C=async()=>{try{const x=await(await fetch(`${route("admin.bundles.list")}?product_id=${l}`)).json();$(x.data||[])}catch(h){console.error("Failed to load bundles:",h)}finally{d(!1)}};return e.jsxs("div",{className:"border-t pt-6",children:[e.jsxs("div",{className:"flex justify-between items-center mb-4",children:[e.jsx("h3",{className:"text-lg font-semibold text-gray-800",children:"Product Bundles"}),e.jsx("a",{href:route("admin.bundles.index"),className:"bg-blue-600 text-white px-4 py-2 rounded hover:bg-blue-700 text-sm",children:"Manage Bundles"})]}),e.jsxs("p",{className:"text-sm text-gray-600 mb-4",children:["Create bundles to suggest complementary products when this item is added to cart. ",`"${P}"`," will be the primary product in the bundle."]}),n?e.jsx("p",{className:"text-gray-600",children:"Loading bundles..."}):w.length===0?e.jsxs("div",{className:"bg-slate-50 p-4 rounded text-center text-gray-600",children:[e.jsx("p",{children:"No bundles created for this product yet."}),e.jsx("a",{href:route("admin.bundles.index"),className:"text-blue-600 hover:underline text-sm mt-2 inline-block",children:"Create one now â"})]}):e.jsx("div",{className:"space-y-3",children:w.map(h=>{var x;return e.jsxs("div",{className:"bg-slate-50 border border-slate-200 rounded p-4 flex justify-between items-start",children:[e.jsxs("div",{className:"flex-1",children:[e.jsx("h4",{className:"font-semibold text-gray-800",children:h.name}),h.description&&e.jsx("p",{className:"text-sm text-gray-600 mt-1",children:h.description}),e.jsx("div",{className:"mt-2 flex flex-wrap gap-2",children:(x=h.items)==null?void 0:x.map(S=>{var k;return e.jsx("span",{className:"text-xs bg-white border border-slate-300 px-2 py-1 rounded",children:(k=S.product)==null?void 0:k.name},S.id)})})]}),e.jsxs("div",{className:"ml-4 flex gap-2",children:[e.jsx("span",{className:`px-2 py-1 rounded text-xs font-semibold ${h.is_active?"bg-green-100 text-green-700":"bg-gray-100 text-gray-700"}`,children:h.is_active?"Active":"Inactive"}),e.jsx("a",{href:route("admin.bundles.index"),className:"text-blue-600 hover:text-blue-800 text-sm",children:"Edit"})]})]}
1,h.id)})})]})}export{ue 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.