1import{u as R,a as Y,b as $,j as r}from"./vendor-query-ENmfDhBq.js";import{h as J,e as X,r as h}from"./vendor-react-Cmo9WzyS.js";import{B as b,C as g,f,g as y,i as x,L as i,I as n,d as p,e as w}from"./index-CznMJwf7.js";import{T as A}from"./textarea-CgE0Hb_n.js";import{S as B,a as E,b as O,c as T,d as W}from"./select-CRKHhgwN.js";import{C as Z}from"./CategorySelector-9NH0R5JO.js";import{S as ee}from"./SupplierSearchableSelect-D1xdMXay.js";import{b as U}from"./currency-BEM_IRAX.js";import{b as re,a as te}from"./useOrgCurrencies-yfxk4N7n.js";import{c as se}from"./logistics-5BZPcp--.js";import{L as k,A as ae,at as ce}from"./vendor-ui-DI8clteo.js";import"./vendor-pdf-CnsmIwn4.js";import"./vendor-radix-OnsNJ2sB.js";import"./vendor-supabase-INnzmioh.js";import"./vendor-charts-CNk8afJ_.js";import"./vendor-auth-Ftfj713I.js";import"./badge-D-GfAQVU.js";import"./popover-BTpQwQxN.js";import"./command-IkwW2mhX.js";import"./dialog-BdhnzXUn.js";import"./searchable-select-DXEFirRu.js";import"./entityCodes-Bn9KpwRH.js";function Fe(){const G=re(),H=te(),{id:u}=J(),j=X(),_=R(),m=!!u,[s,c]=h.useState({model_number:"",product_name:"",specifications:"",category_id:null,default_supplier_id:null,base_factory_price:"",factory_currency:"CNY",base_customer_price:"",customer_currency:"USD",moq:"",lead_time_days:"",notes:"",product_length_cm:"",product_width_cm:"",product_height_cm:"",default_qty_per_carton:"",default_length_cm:"",default_width_cm:"",default_height_cm:"",cbm_per_carton_override:"",default_gw_per_carton:"",net_weight_per_carton:"",qty_per_20gp:"",qty_per_40hq:""}),{data:a,isLoading:V}=Y({queryKey:["product-edit",u],queryFn:async()=>{if(!u)return null;const{data:e,error:t}=await w.from("products").select("*").eq("id",u).single();if(t)throw t;return e},enabled:m}),C=h.useRef(null);h.useEffect(()=>{var e,t,l,d,N,q,S,F,P,Q,D,I,L,K,M;a&&C.current!==a.id&&(C.current=a.id,c({model_number:a.model_number||"",product_name:a.product_name||"",specifications:a.specifications||"",category_id:a.category_id,default_supplier_id:a.default_supplier_id,base_factory_price:((e=a.base_factory_price)==null?void 0:e.toString())||"",factory_currency:a.factory_currency||"CNY",base_customer_price:((t=a.base_customer_price)==null?void 0:t.toString())||"",customer_currency:a.customer_currency||"USD",moq:((l=a.moq)==null?void 0:l.toString())||"",lead_time_days:((d=a.lead_time_days)==null?void 0:d.toString())||"",notes:a.notes||"",product_length_cm:((N=a.product_length_cm)==null?void 0:N.toString())||"",product_width_cm:((q=a.product_width_cm)==null?void 0:q.toString())||"",product_height_cm:((S=a.product_height_cm)==null?void 0:S.toString())||"",default_qty_per_carton:((F=a.default_qty_per_carton)==null?void 0:F.toString())||"",default_length_cm:((P=a.default_length_cm)==null?void 0:P.toString())||"",default_width_cm:((Q=a.default_width_cm)==null?void 0:Q.toString())||"",default_height_cm:((D=a.default_height_cm)==null?void 0:D.toString())||"",cbm_per_carton_override:"",default_gw_per_carton:((I=a.default_gw_per_carton)==null?void 0:I.toString())||"",net_weight_per_carton:((L=a.net_weight_per_carton)==null?void 0:L.toString())||"",qty_per_20gp:((K=a.qty_per_20gp)==null?void 0:K.toString())||"",qty_per_40hq:((M=a.qty_per_40hq)==null?void 0:M.toString())||""}))},[a]);const o=h.useMemo(()=>{const e=parseFloat(s.default_length_cm),t=parseFloat(s.default_width_cm),l=parseFloat(s.default_height_cm);return e&&t&&l?se(e,t,l):0},[s.default_length_cm,s.default_width_cm,s.default_height_cm]);s.cbm_per_carton_override&&parseFloat(s.cbm_per_carton_override);const v=$({mutationFn:async e=>{const t={model_number:e.model_number,product_name:e.product_name,specifications:e.specifications||null,category_id:e.category_id,default_supplier_id:e.default_supplier_id,base_factory_price:e.base_factory_price?parseFloat(e.base_factory_price):null,factory_currency:e.factory_currency,base_customer_price:e.base_customer_price?parseFloat(e.base_customer_price):null,customer_currency:e.customer_currency,moq:e.moq?parseInt(e.moq):null,lead_time_days:e.lead_time_days?parseInt(e.lead_time_days):null,notes:e.notes||null,source:"manual",product_length_cm:e.product_length_cm?parseFloat(e.product_length_cm):null,product_width_cm:e.product_width_cm?parseFloat(e.product_width_cm):null,product_height_cm:e.product_height_cm?parseFloat(e.product_height_cm):null,default_qty_per_carton:e.default_qty_per_carton?parseInt(e.default_qty_per_carton):null,default_length_cm:e.default_length_cm?parseFloat(e.default_length_cm):null,default_width_cm:e.default_width_cm?parseFloat(e.default_width_cm):null,default_height_cm:e.default_height_cm?parseFloat(e.default_height_cm):null,default_gw_per_carton:e.default_gw_per_carton?parseFloat(e.default_gw_per_carton):null,net_weight_per_carton:e.net_weight_per_carton?parseFloat(e.net_weight_per_carton):null,qty_per_20gp:e.qty_per_20gp?parseInt(e.qty_per_20gp):null,qty_per_40hq:e.qty_per_40hq?parseInt(e.qty_per_40hq):null};if(m){const{data:l,error:d}=await w.from("products").update(t).eq("id",u).select("id");if(d)throw d;if(!l||l.length===0)throw new Error("Update didn't persist â you may
1not have permission to edit this product.");return{id:u}}else{const{data:l,error:d}=await w.from("products").insert(t).select("id").single();if(d)throw d;return l}},onSuccess:e=>{const t=(e==null?void 0:e.id)||u;_.invalidateQueries({queryKey:["products"]}),_.invalidateQueries({queryKey:["products-library"]}),t&&(_.invalidateQueries({queryKey:["product-detail",t]}),_.invalidateQueries({queryKey:["product-edit",t]}),_.invalidateQueries({queryKey:["product-readiness",t]}),_.invalidateQueries({queryKey:["product-customers",t]})),p.success(m?"Product updated":"Product created"),j(`/dashboard/products/${t}`)},onError:e=>{e.code==="23505"?p.error("A product with this model number already exists"):p.error("Failed to save: "+e.message)}}),z=e=>{if(e.preventDefault(),!s.model_number.trim()){p.error("Model number is required");return}if(!s.product_name.trim()){p.error("Product name is required");return}v.mutate(s)};return m&&V?r.jsx("div",{className:"flex justify-center py-16",children:r.jsx(k,{className:"h-8 w-8 animate-spin"})}):r.jsxs("div",{className:"max-w-3xl mx-auto space-y-6",children:[r.jsxs("div",{className:"flex items-center gap-3",children:[r.jsx(b,{variant:"ghost",size:"icon","aria-label":"Back",onClick:()=>j(-1),children:r.jsx(ae,{className:"h-5 w-5"})}),r.jsx("h1",{className:"text-2xl font-bold",children:m?"Edit Product":"Add New Product"})]}),r.jsxs("form",{onSubmit:z,className:"space-y-6",children:[r.jsxs(g,{children:[r.jsx(f,{children:r.jsx(y,{children:"Basic Information"})}),r.jsxs(x,{className:"space-y-4",children:[r.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-4",children:[r.jsxs("div",{children:[r.jsx(i,{children:"Model Number *"}),r.jsx(n,{value:s.model_number,onChange:e=>c(t=>({...t,model_number:e.target.value})),placeholder:"e.g., WDC-001",className:"font-mono"})]}),r.jsxs("div",{children:[r.jsx(i,{children:"Product Name *"}),r.jsx(n,{value:s.product_name,onChange:e=>c(t=>({...t,product_name:e.target.value})),placeholder:"e.g., Wooden Dining Chair"})]})]}),r.jsxs("div",{children:[r.jsx(i,{children:"Specifications"}),r.jsx(A,{value:s.specifications,onChange:e=>c(t=>({...t,specifications:e.target.value})),placeholder:"Technical specifications, dimensions, materials...",rows:3})]}),r.jsx(Z,{value:s.category_id,onChange:e=>c(t=>({...t,category_id:e})),productName:s.product_name,specifications:s.specifications,imageUrl:(a==null?void 0:a.primary_image_url)??null,canCreate:!0}),r.jsxs("div",{children:[r.jsx(i,{className:"text-sm",children:"Product Size (cm)"}),r.jsxs("div",{className:"grid grid-cols-3 gap-3 mt-1",children:[r.jsx(n,{type:"number",step:"any",value:s.product_length_cm,onChange:e=>c(t=>({...t,product_length_cm:e.target.value})),placeholder:"Length",className:"font-mono"}),r.jsx(n,{type:"number",step:"any",value:s.product_width_cm,onChange:e=>c(t=>({...t,product_width_cm:e.target.value})),placeholder:"Width",className:"font-mono"}),r.jsx(n,{type:"number",step:"any",value:s.product_height_cm,onChange:e=>c(t=>({...t,product_height_cm:e.target.value})),placeholder:"Height",className:"font-mono"})]})]})]})]}),r.jsxs(g,{children:[r.jsx(f,{children:r.jsx(y,{children:"Supplier & Pricing"})}),r.jsxs(x,{className:"space-y-4",children:[r.jsxs("div",{children:[r.jsx(i,{children:"Default Supplier"}),r.jsx(ee,{value:s.default_supplier_id,onValueChange:e=>c(t=>({...t,default_supplier_id:e||null})),allowNone:!0,placeholder:"Select supplier..."})]}),r.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-4",children:[r.jsxs("div",{children:[r.jsxs(i,{className:"flex items-center gap-2",children:["Factory Price",r.jsxs(B,{value:s.factory_currency,onValueChange:e=>c(t=>({...t,factory_currency:e})),children:[r.jsx(E,{className:"w-24 h-7 text-xs",children:r.jsx(O,{})}),r.jsx(T,{children:G.map(e=>r.jsxs(W,{value:e,children:[U(e)," ",e]},e))})]})]}),r.jsx(n,{type:"number",step:"0.01",value:s.base_factory_price,onChange:e=>c(t=>({...t,base_factory_price:e.target.value})),placeholder:"0.00",className:"font-mono"})]}),r.jsxs("div",{children:[r.jsxs(i,{className:"flex items-center gap-2",children:["Customer Price",r.jsxs(B,{value:s.customer_currency,onValueChange:e=>c(t=>({...t,customer_currency:e})),children:[r.jsx(E,{className:"w-24 h-7 text-xs",children:r.jsx(O,{})}),r.jsx(T,{children:H.map(e=>r.jsxs(W,{value:e,children:[U(e)," ",e]},e))})]})]}),r.jsx(n,{type:"number",step:"0.01",value:s.base_customer_price,onChange:e=>c(t=>({...t,base_customer_price:e.target.value})),placeholder:"0.00",className:"font-mono"})]})]}),r.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-4",children:[r.jsxs("div",{children:[r.jsx(i,{children:"MOQ (Minimum Order Quantity)"}),r.jsx(n,{type:"number",value:s.moq,onChange:e=>c(t=>({...t,moq:e.target.value})),placeholder:"e.g., 100"})]}
1),r.jsxs("div",{children:[r.jsx(i,{children:"Lead Time (Days)"}),r.jsx(n,{type:"number",value:s.lead_time_days,onChange:e=>c(t=>({...t,lead_time_days:e.target.value})),placeholder:"e.g., 30"})]})]})]})]}),r.jsxs(g,{children:[r.jsx(f,{children:r.jsx(y,{children:"Packing Details"})}),r.jsxs(x,{className:"space-y-4",children:[r.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-4",children:[r.jsxs("div",{children:[r.jsx(i,{children:"Pieces per Carton"}),r.jsx(n,{type:"number",step:"1",value:s.default_qty_per_carton,onChange:e=>c(t=>({...t,default_qty_per_carton:e.target.value})),placeholder:"e.g., 400"}),r.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Set to 1 for large items (furniture, machines)"})]}),r.jsxs("div",{children:[r.jsx(i,{children:"CBM per Carton"}),r.jsx(n,{type:"number",step:"any",value:s.cbm_per_carton_override||(o?o.toFixed(6):""),onChange:e=>c(t=>({...t,cbm_per_carton_override:e.target.value})),placeholder:o?o.toFixed(6):"0.000000",className:"font-mono"}),o>0&&!s.cbm_per_carton_override&&r.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:"Auto-calculated from dimensions below"})]})]}),r.jsxs("div",{children:[r.jsx(i,{className:"text-sm",children:"Carton Dimensions (cm)"}),r.jsxs("div",{className:"grid grid-cols-3 gap-3 mt-1",children:[r.jsx("div",{children:r.jsx(n,{type:"number",step:"any",value:s.default_length_cm,onChange:e=>c(t=>({...t,default_length_cm:e.target.value})),placeholder:"Length",className:"font-mono"})}),r.jsx("div",{children:r.jsx(n,{type:"number",step:"any",value:s.default_width_cm,onChange:e=>c(t=>({...t,default_width_cm:e.target.value})),placeholder:"Width",className:"font-mono"})}),r.jsx("div",{children:r.jsx(n,{type:"number",step:"any",value:s.default_height_cm,onChange:e=>c(t=>({...t,default_height_cm:e.target.value})),placeholder:"Height",className:"font-mono"})})]}),o>0&&r.jsxs("p",{className:"text-xs text-muted-foreground mt-1",children:[s.default_length_cm," Ã ",s.default_width_cm," Ã ",s.default_height_cm," cm = ",o.toFixed(6)," CBM"]})]}),r.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-4",children:[r.jsxs("div",{children:[r.jsx(i,{children:"Gross Weight per Carton (KG)"}),r.jsx(n,{type:"number",step:"any",value:s.default_gw_per_carton,onChange:e=>c(t=>({...t,default_gw_per_carton:e.target.value})),placeholder:"e.g., 12.5",className:"font-mono"})]}),r.jsxs("div",{children:[r.jsx(i,{children:"Net Weight per Carton (KG)"}),r.jsx(n,{type:"number",step:"any",value:s.net_weight_per_carton,onChange:e=>c(t=>({...t,net_weight_per_carton:e.target.value})),placeholder:"e.g., 11.2 (optional)",className:"font-mono"})]})]}),r.jsxs("div",{className:"grid grid-cols-1 sm:grid-cols-2 gap-4",children:[r.jsxs("div",{children:[r.jsx(i,{children:"Qty per 20GP container"}),r.jsx(n,{type:"number",step:"1",value:s.qty_per_20gp,onChange:e=>c(t=>({...t,qty_per_20gp:e.target.value})),placeholder:"e.g., 1800",className:"font-mono"})]}),r.jsxs("div",{children:[r.jsx(i,{children:"Qty per 40HQ container"}),r.jsx(n,{type:"number",step:"1",value:s.qty_per_40hq,onChange:e=>c(t=>({...t,qty_per_40hq:e.target.value})),placeholder:"e.g., 4400",className:"font-mono"})]})]})]})]}),r.jsxs(g,{children:[r.jsx(f,{children:r.jsx(y,{children:"Additional Notes"})}),r.jsx(x,{children:r.jsx(A,{value:s.notes,onChange:e=>c(t=>({...t,notes:e.target.value})),placeholder:"Any additional notes...",rows:3})})]}),r.jsxs("div",{className:"flex justify-end gap-4",children:[r.jsx(b,{type:"button",variant:"outline",onClick:()=>j(-1),children:"Cancel"}),r.jsxs(b,{type:"submit",disabled:v.isPending,children:[v.isPending?r.jsx(k,{className:"h-4 w-4 mr-2 animate-spin"}):r.jsx(ce,{className:"h-4 w-4 mr-2"}),m?"Update Product":"Create Product"]})]})]})]})}export{Fe 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.