Files
turftracker/frontend/dist/assets/Products-BYbbRN4N.js
2026-04-09 13:19:47 -05:00

2 lines
38 KiB
JavaScript

import{r as d,y as k,z as b,j as e,f as ee,L as Y,c as G,q as te,s as W,A as se,t as _,R as H}from"./index-9FS9bw8D.js";import{F as ae}from"./MagnifyingGlassIcon-DIIDUrHz.js";const de=()=>{const[o,T]=d.useState([]),[N,A]=d.useState([]),[i,l]=d.useState([]),[u,q]=d.useState(!0),[y,x]=d.useState(!1),[g,S]=d.useState(""),[f,E]=d.useState(""),[I,C]=d.useState(""),[F,R]=d.useState(""),[j,P]=d.useState("shared"),[D,U]=d.useState(null),[M,a]=d.useState(!1);d.useEffect(()=>{const s=setTimeout(()=>E(g),300);return()=>clearTimeout(s)},[g]);const m=d.useCallback(async()=>{try{q(!0);const[s,v]=await Promise.all([k.getAll({category:I,type:F,search:f}),k.getCategories()]);T(s.data.data.sharedProducts||[]),A(s.data.data.userProducts||[]),l(v.data.data.categories||[])}catch(s){console.error("Failed to fetch products:",s),b.error("Failed to load products"),T([]),A([])}finally{q(!1)}},[I,F,f]);d.useEffect(()=>{m()},[m]);const w=s=>{S(s.target.value)},V=async s=>{var v;try{const r=(await k.createUserProduct(s)).data.data.userProduct;if(s.spreaderSettings&&s.spreaderSettings.length>0){const n=s.spreaderSettings.map(c=>{const p={userProductId:r.id,settingValue:c.settingValue,rateDescription:c.rateDescription||null,notes:c.notes&&c.notes.trim()?c.notes.trim():null};return c.equipmentId?p.equipmentId=parseInt(c.equipmentId):(p.spreaderBrand=c.spreaderBrand,p.spreaderModel=c.spreaderModel||null),se.create(p)});await Promise.all(n)}b.success(`Custom product created successfully${(v=s.spreaderSettings)!=null&&v.length?` with ${s.spreaderSettings.length} spreader setting(s)`:""}!`),x(!1),m()}catch(t){console.error("Failed to create product:",t),b.error("Failed to create product")}},z=async s=>{if(window.confirm("Are you sure you want to delete this custom product?"))try{await k.deleteUserProduct(s),b.success("Product deleted successfully"),m()}catch(v){console.error("Failed to delete product:",v),b.error("Failed to delete product")}},L=s=>{U(s),a(!0)},O=async s=>{try{await k.updateUserProduct(D.id,s),b.success("Product updated successfully!"),a(!1),U(null),m()}catch(v){console.error("Failed to update product:",v),b.error("Failed to update product")}},J=async s=>{var v,t,r,n;try{const c={productId:s.id,customName:s.name,brand:s.brand,categoryId:null,productType:s.productType,activeIngredients:s.activeIngredients,description:s.description,customRateAmount:((t=(v=s.rates)==null?void 0:v[0])==null?void 0:t.rateAmount)||null,customRateUnit:((n=(r=s.rates)==null?void 0:r[0])==null?void 0:n.rateUnit)||(s.productType==="granular"?"lbs/1000 sq ft":"oz/1000 sq ft"),notes:`Added from shared product: ${s.name}`},p=await k.createUserProduct(c);b.success(`"${s.name}" added to your products!`),m()}catch(c){console.error("Failed to add product to my products:",c),b.error("Failed to add product to your collection")}},B=({product:s,isUserProduct:v=!1,onAddToMyProducts:t})=>e.jsxs("div",{className:"card",children:[e.jsxs("div",{className:"flex justify-between items-start mb-3",children:[e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx("div",{className:"p-2 bg-green-100 rounded-lg",children:e.jsx(G,{className:"h-5 w-5 text-green-600"})}),e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-gray-900",children:v?s.customName||s.baseProductName:s.name}),s.brand&&e.jsx("p",{className:"text-sm text-gray-600",children:s.brand}),e.jsx("span",{className:`inline-flex items-center px-2 py-1 rounded-full text-xs font-medium mt-1 ${s.productType==="liquid"?"bg-blue-100 text-blue-800":s.productType==="granular"?"bg-green-100 text-green-800":s.productType==="seed"?"bg-yellow-100 text-yellow-800":"bg-gray-100 text-gray-800"}`,children:s.productType?s.productType.charAt(0).toUpperCase()+s.productType.slice(1):"Type not set"})]})]}),v&&e.jsxs("div",{className:"flex gap-1",children:[e.jsx("button",{onClick:()=>L(s),className:"p-1 text-gray-400 hover:text-blue-600",children:e.jsx(te,{className:"h-4 w-4"})}),e.jsx("button",{onClick:()=>z(s.id),className:"p-1 text-gray-400 hover:text-red-600",children:e.jsx(W,{className:"h-4 w-4"})})]})]}),s.categoryName&&e.jsxs("p",{className:"text-sm text-gray-500 mb-2",children:["Category: ",s.categoryName]}),s.productType==="seed"?(()=>{let r=[];if(Array.isArray(s.seedBlend)&&s.seedBlend.length)r=s.seedBlend;else if(s.activeIngredients)try{const n=typeof s.activeIngredients=="string"?JSON.parse(s.activeIngredients):s.activeIngredients;r=(n==null?void 0:n.seedBlend)||[]}catch{}return e.jsxs("div",{className:"mb-3",children:[e.jsx("p",{className:"text-sm font-medium text-gray-900",children:"Seed Blend:"}),r.length===0?e.jsx("p",{className:"text-sm text-gray-600",children:"No blend details"}):e.jsx("div",{className:"mt-1 flex flex-wrap gap-2",children:r.map((n,c)=>e.jsxs("span",{className:"px-2 py-1 rounded bg-gray-100 text-gray-800 text-xs",children:[n.cultivar," — ",parseFloat(n.percent||0).toFixed(1),"%"]},c))})]})})():s.activeIngredients&&e.jsxs("p",{className:"text-sm text-gray-700 mb-3",children:[e.jsx("strong",{children:"Active Ingredients:"})," ",s.activeIngredients]}),v&&(()=>{if(s.productType==="seed")try{const r=typeof s.activeIngredients=="string"?JSON.parse(s.activeIngredients):s.activeIngredients,n=r==null?void 0:r.seedRates;if(n!=null&&n.new||n!=null&&n.overseed)return e.jsxs("div",{className:"bg-gray-50 p-3 rounded-lg mb-3",children:[e.jsx("p",{className:"text-sm font-medium text-gray-900 mb-1",children:"Your Rates:"}),n.new&&e.jsxs("p",{className:"text-sm text-gray-700",children:["New Lawn: ",n.new," ",n.unit||s.customRateUnit]}),n.overseed&&e.jsxs("p",{className:"text-sm text-gray-700",children:["Overseeding: ",n.overseed," ",n.unit||s.customRateUnit]})]})}catch{}return s.customRateAmount?e.jsxs("div",{className:"bg-gray-50 p-3 rounded-lg mb-3",children:[e.jsx("p",{className:"text-sm font-medium text-gray-900",children:"Your Rate:"}),e.jsxs("p",{className:"text-sm text-gray-700",children:[s.customRateAmount," ",s.customRateUnit]})]}):null})(),!v&&s.rates&&s.rates.length>0&&e.jsxs("div",{className:"bg-gray-50 p-3 rounded-lg mb-3",children:[e.jsx("p",{className:"text-sm font-medium text-gray-900 mb-2",children:"Application Rates:"}),s.productType==="seed"?(()=>{const r=s.rates.find(c=>(c.applicationType||"").toLowerCase().includes("new")),n=s.rates.find(c=>(c.applicationType||"").toLowerCase().includes("over"));return e.jsxs(e.Fragment,{children:[r&&e.jsxs("div",{className:"text-sm text-gray-700",children:[e.jsx("strong",{children:"New Lawn:"})," ",r.rateAmount," ",r.rateUnit]}),n&&e.jsxs("div",{className:"text-sm text-gray-700",children:[e.jsx("strong",{children:"Overseeding:"})," ",n.rateAmount," ",n.rateUnit]}),!r&&!n&&s.rates.slice(0,2).map((c,p)=>e.jsxs("div",{className:"text-sm text-gray-700",children:[e.jsxs("strong",{children:[c.applicationType,":"]})," ",c.rateAmount," ",c.rateUnit]},p))]})})():s.rates.slice(0,2).map((r,n)=>e.jsxs("div",{className:"text-sm text-gray-700",children:[e.jsxs("strong",{children:[r.applicationType,":"]})," ",r.rateAmount," ",r.rateUnit]},n)),s.rates.length>2&&e.jsxs("p",{className:"text-xs text-gray-500 mt-1",children:["+",s.rates.length-2," more rates"]})]}),s.notes&&e.jsxs("p",{className:"text-sm text-gray-600 mt-2",children:[e.jsx("strong",{children:"Notes:"})," ",s.notes]}),!v&&e.jsx("div",{className:"mt-3 pt-3 border-t border-gray-200",children:e.jsx("button",{onClick:()=>t(s),className:"text-blue-600 hover:text-blue-700 text-sm font-medium",children:"Add to My Products →"})})]});return e.jsxs("div",{className:"p-6",children:[e.jsxs("div",{className:"flex justify-between items-center mb-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-gray-900",children:"Products"}),e.jsx("p",{className:"text-gray-600",children:"Manage your lawn care products and application rates"})]}),e.jsxs("button",{onClick:()=>x(!0),className:"btn-primary flex items-center gap-2",children:[e.jsx(ee,{className:"h-5 w-5"}),"Add Custom Product"]})]}),e.jsx("div",{className:"card mb-6",children:e.jsxs("div",{className:"flex flex-wrap gap-4",children:[e.jsx("div",{className:"flex-1 min-w-64",children:e.jsxs("div",{className:"relative",children:[e.jsx(ae,{className:"h-5 w-5 absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400"}),e.jsx("input",{type:"text",className:"input pl-10",placeholder:"Search products...",value:g,onChange:w})]})}),e.jsx("div",{className:"min-w-48",children:e.jsxs("select",{className:"input",value:I,onChange:s=>C(s.target.value),children:[e.jsx("option",{value:"",children:"All Categories"}),i.map(s=>e.jsx("option",{value:s.id,children:s.name},s.id))]})}),e.jsx("div",{className:"min-w-32",children:e.jsxs("select",{className:"input",value:F,onChange:s=>R(s.target.value),children:[e.jsx("option",{value:"",children:"All Types"}),e.jsx("option",{value:"liquid",children:"Liquid"}),e.jsx("option",{value:"granular",children:"Granular"}),e.jsx("option",{value:"seed",children:"Seed"}),e.jsx("option",{value:"powder",children:"Powder"})]})})]})}),e.jsx("div",{className:"mb-6",children:e.jsx("div",{className:"border-b border-gray-200",children:e.jsxs("nav",{className:"-mb-px flex",children:[e.jsxs("button",{onClick:()=>P("shared"),className:`py-2 px-4 border-b-2 font-medium text-sm ${j==="shared"?"border-blue-500 text-blue-600":"border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300"}`,children:["Shared Products (",o.length,")"]}),e.jsxs("button",{onClick:()=>P("custom"),className:`py-2 px-4 border-b-2 font-medium text-sm ${j==="custom"?"border-blue-500 text-blue-600":"border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300"}`,children:["My Custom Products (",N.length,")"]})]})})}),u&&e.jsxs("div",{className:"flex items-center gap-3 text-gray-600 mb-4",role:"status","aria-live":"polite",children:[e.jsx(Y,{size:"sm"}),e.jsx("span",{children:"Refreshing product catalog…"})]}),j==="shared"?o.length===0?u?e.jsxs("div",{className:"card flex flex-col items-center justify-center py-12",children:[e.jsx(Y,{size:"lg"}),e.jsx("p",{className:"text-gray-600 mt-4",children:"Loading shared products…"})]}):e.jsxs("div",{className:"card text-center py-12",children:[e.jsx(G,{className:"h-16 w-16 text-gray-300 mx-auto mb-4"}),e.jsx("h3",{className:"text-lg font-medium text-gray-900 mb-2",children:"No Products Found"}),e.jsx("p",{className:"text-gray-600",children:"Try adjusting your search or filters"})]}):e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6",children:o.map(s=>e.jsx(B,{product:s,onAddToMyProducts:J},s.id))}):N.length===0?u?e.jsxs("div",{className:"card flex flex-col items-center justify-center py-12",children:[e.jsx(Y,{size:"lg"}),e.jsx("p",{className:"text-gray-600 mt-4",children:"Loading your custom products…"})]}):e.jsxs("div",{className:"card text-center py-12",children:[e.jsx(G,{className:"h-16 w-16 text-gray-300 mx-auto mb-4"}),e.jsx("h3",{className:"text-lg font-medium text-gray-900 mb-2",children:"No Custom Products Yet"}),e.jsx("p",{className:"text-gray-600 mb-6",children:"Create your own products with custom rates and notes"}),e.jsx("button",{onClick:()=>x(!0),className:"btn-primary",children:"Add Your First Product"})]}):e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6",children:N.map(s=>e.jsx(B,{product:s,isUserProduct:!0},s.id))}),y&&e.jsx(le,{onSubmit:V,onCancel:()=>x(!1),sharedProducts:o,categories:i}),M&&D&&e.jsx(ne,{product:D,onSubmit:O,onCancel:()=>{a(!1),U(null)},sharedProducts:o,categories:i})]})},le=({onSubmit:o,onCancel:T,sharedProducts:N,categories:A})=>{const[i,l]=d.useState({productId:"",customName:"",categoryId:"",productType:"granular",brand:"",activeIngredients:"",customRateAmount:"",customRateUnit:"lbs/1000 sq ft",notes:""}),[u,q]=d.useState([]),[y,x]=d.useState([]),[g,S]=d.useState(""),[f,E]=d.useState("");d.useEffect(()=>{if(i.productType==="seed"){const a=parseFloat(g),m=parseFloat(f);let w="";!isNaN(a)&&!isNaN(m)?w=((a+m)/2).toFixed(2):isNaN(a)?isNaN(m)||(w=m):w=a,l(V=>({...V,customRateAmount:w}))}},[i.productType,g,f]);const[I,C]=d.useState([]),[F,R]=d.useState(!1),[j,P]=d.useState({equipmentId:"",settingValue:"",rateDescription:"",notes:""});d.useEffect(()=>{(async()=>{R(!0);try{const m=await _.getSpreaders();C(m.data.data.spreaders||[])}catch(m){console.error("Failed to load spreaders:",m),b.error("Failed to load spreader equipment")}finally{R(!1)}})()},[]);const D=()=>{if(!j.equipmentId||!j.settingValue){b.error("Please select spreader equipment and enter setting value");return}const a=I.find(w=>w.id===parseInt(j.equipmentId)),m={...j,equipmentId:parseInt(j.equipmentId),id:Date.now(),equipmentName:a==null?void 0:a.name,equipmentManufacturer:a==null?void 0:a.manufacturer,equipmentModel:a==null?void 0:a.model};q([...u,m]),P({equipmentId:"",settingValue:"",rateDescription:"",notes:""})},U=a=>{q(u.filter(m=>m.id!==a))},M=a=>{if(a.preventDefault(),!i.productId&&!i.customName){b.error("Please select a base product or enter a custom name");return}if(!i.productId&&(!i.categoryId||!i.productType)){b.error("Please select a category and product type for custom products");return}const m={productId:i.productId?parseInt(i.productId):null,customName:i.customName||null,customRateAmount:i.customRateAmount?parseFloat(i.customRateAmount):null,customRateUnit:i.customRateUnit||null,notes:i.notes||null,brand:i.brand||null,categoryId:i.categoryId?parseInt(i.categoryId):null,productType:i.productType||null,activeIngredients:i.activeIngredients||null,description:i.description||null,spreaderSettings:i.productType==="granular"?u:[]};if(i.productType==="seed"){const w={seedBlend:Array.isArray(y)?y:[]};(g||f)&&(w.seedRates={new:g?parseFloat(g):null,overseed:f?parseFloat(f):null,unit:i.customRateUnit}),m.activeIngredients=JSON.stringify(w)}o(m)};return e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-white rounded-lg p-6 w-full max-w-lg max-h-[90vh] overflow-y-auto",children:[e.jsx("h3",{className:"text-lg font-semibold mb-4",children:"Add Custom Product"}),e.jsxs("form",{onSubmit:M,className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Base Product (Optional)"}),e.jsxs("select",{className:"input",value:i.productId,onChange:a=>l({...i,productId:a.target.value}),children:[e.jsx("option",{value:"",children:"Select a base product..."}),N.map(a=>e.jsxs("option",{value:a.id,children:[a.name," ",a.brand&&`- ${a.brand}`]},a.id))]}),e.jsx("p",{className:"text-xs text-gray-500 mt-1",children:"Choose from our database or create a completely custom product below"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Custom Name"}),e.jsx("input",{type:"text",className:"input",value:i.customName,onChange:a=>l({...i,customName:a.target.value}),placeholder:"e.g., My 24-0-11 Blend, Custom Herbicide Mix"}),e.jsx("p",{className:"text-xs text-gray-500 mt-1",children:"Required if no base product selected. Used to identify this product."})]}),!i.productId&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Category *"}),e.jsxs("select",{className:"input",value:i.categoryId,onChange:a=>l({...i,categoryId:a.target.value}),required:!i.productId,children:[e.jsx("option",{value:"",children:"Select category..."}),A.map(a=>e.jsx("option",{value:a.id,children:a.name},a.id))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Product Type *"}),e.jsxs("select",{className:"input",value:i.productType,onChange:a=>l({...i,productType:a.target.value}),children:[e.jsx("option",{value:"granular",children:"Granular"}),e.jsx("option",{value:"liquid",children:"Liquid"}),e.jsx("option",{value:"seed",children:"Seed"}),e.jsx("option",{value:"powder",children:"Powder"})]})]})]}),i.productType==="seed"&&e.jsx(K,{value:y,onChange:x}),e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Brand"}),e.jsx("input",{type:"text",className:"input",value:i.brand,onChange:a=>l({...i,brand:a.target.value}),placeholder:"e.g., Scotts, Syngenta, Custom Mix"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Active Ingredients"}),e.jsx("input",{type:"text",className:"input",value:i.activeIngredients,onChange:a=>l({...i,activeIngredients:a.target.value}),placeholder:"e.g., 2,4-D 25%, Nitrogen 24%, Iron 2%"})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-6 items-start",children:[e.jsx("div",{children:i.productType==="seed"?e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Seeding Rates"}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"label text-xs",children:"New Lawn (lbs/1000 sq ft)"}),e.jsx("input",{type:"number",step:"0.01",className:"input",value:g,onChange:a=>S(a.target.value),placeholder:"e.g., 7"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label text-xs",children:"Overseeding (lbs/1000 sq ft)"}),e.jsx("input",{type:"number",step:"0.01",className:"input",value:f,onChange:a=>E(a.target.value),placeholder:"e.g., 3"})]})]})]}):e.jsxs(e.Fragment,{children:[e.jsx("label",{className:"label",children:"Application Rate"}),e.jsx("input",{type:"number",step:"0.01",className:"input",value:i.customRateAmount,onChange:a=>l({...i,customRateAmount:a.target.value}),placeholder:"2.5"})]})}),e.jsxs("div",{className:"pt-6",children:[e.jsx("label",{className:"label",children:"Rate Unit"}),e.jsxs("select",{className:"input",value:i.customRateUnit,onChange:a=>l({...i,customRateUnit:a.target.value}),children:[e.jsx("option",{value:"lbs/1000 sq ft",children:"lbs/1000 sq ft"}),e.jsx("option",{value:"oz/1000 sq ft",children:"oz/1000 sq ft"}),e.jsx("option",{value:"gal/acre",children:"gal/acre"}),e.jsx("option",{value:"fl oz/1000 sq ft",children:"fl oz/1000 sq ft"}),e.jsx("option",{value:"ml/1000 sq ft",children:"ml/1000 sq ft"}),e.jsx("option",{value:"tbsp/1000 sq ft",children:"tbsp/1000 sq ft"})]})]})]}),i.productType==="granular"&&e.jsxs("div",{className:"border-t pt-4",children:[e.jsx("h4",{className:"text-md font-semibold text-gray-900 mb-3",children:"Spreader Settings"}),e.jsx("p",{className:"text-sm text-gray-600 mb-4",children:"Add spreader settings for different brands/models. This helps determine the correct spreader dial setting when applying this product."}),u.length>0&&e.jsxs("div",{className:"space-y-2 mb-4",children:[e.jsx("label",{className:"label",children:"Added Settings:"}),u.map(a=>e.jsxs("div",{className:"flex items-center justify-between bg-gray-50 p-3 rounded-lg",children:[e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"font-medium",children:[a.equipmentName||`${a.equipmentManufacturer} ${a.equipmentModel}`.trim()," - Setting: ",a.settingValue]}),a.rateDescription&&e.jsx("div",{className:"text-sm text-gray-600",children:a.rateDescription})]}),e.jsx("button",{type:"button",onClick:()=>U(a.id),className:"text-red-600 hover:text-red-800",children:e.jsx(W,{className:"h-4 w-4"})})]},a.id))]}),e.jsxs("div",{className:"bg-blue-50 p-4 rounded-lg space-y-3",children:[e.jsx("h5",{className:"text-sm font-semibold text-blue-900",children:"Add Spreader Setting"}),I.length===0?e.jsx("div",{className:"text-sm text-blue-800 bg-blue-100 p-3 rounded",children:e.jsx("p",{children:"No spreader equipment found. Please add spreader equipment to your inventory first."})}):e.jsx(e.Fragment,{children:e.jsxs("div",{children:[e.jsx("label",{className:"label text-xs",children:"Select Spreader Equipment *"}),e.jsxs("select",{className:"input text-sm",value:j.equipmentId,onChange:a=>P({...j,equipmentId:a.target.value}),children:[e.jsx("option",{value:"",children:"Choose spreader..."}),I.map(a=>e.jsxs("option",{value:a.id,children:[a.name," ",a.spreaderType&&`(${a.spreaderType})`]},a.id))]})]})}),I.length>0&&e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"label text-xs",children:"Setting Value *"}),e.jsx("input",{type:"text",className:"input text-sm",value:j.settingValue,onChange:a=>P({...j,settingValue:a.target.value}),placeholder:"#14, 4, 20, etc."})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label text-xs",children:"Rate Description"}),e.jsx("input",{type:"text",className:"input text-sm",value:j.rateDescription,onChange:a=>P({...j,rateDescription:a.target.value}),placeholder:"e.g., 1 lb N per 1000 sq ft"})]})]}),I.length>0&&e.jsxs("div",{children:[e.jsx("label",{className:"label text-xs",children:"Notes (Optional)"}),e.jsx("textarea",{className:"input text-sm",rows:"2",value:j.notes,onChange:a=>P({...j,notes:a.target.value}),placeholder:"Additional notes about this setting..."})]}),e.jsx("button",{type:"button",onClick:D,disabled:!j.equipmentId||!j.settingValue||I.length===0,className:"btn-primary text-sm px-3 py-1 disabled:opacity-50 disabled:cursor-not-allowed",children:"Add Setting"})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Notes"}),e.jsx("textarea",{className:"input",rows:"3",value:i.notes,onChange:a=>l({...i,notes:a.target.value}),placeholder:"Special mixing instructions, storage notes, etc."})]}),e.jsxs("div",{className:"flex gap-3 pt-4",children:[e.jsx("button",{type:"submit",className:"btn-primary flex-1",children:"Create Product"}),e.jsx("button",{type:"button",onClick:T,className:"btn-secondary flex-1",children:"Cancel"})]})]})]})})},ne=({product:o,onSubmit:T,onCancel:N,sharedProducts:A,categories:i})=>{const[l,u]=d.useState({productId:o.baseProductId||"",customName:o.customName||"",customRateAmount:o.customRateAmount||"",customRateUnit:o.customRateUnit||"lbs/1000 sq ft",notes:o.notes||"",brand:o.brand||"",categoryId:o.categoryId||"",productType:o.productType||"",activeIngredients:o.activeIngredients||"",description:o.description||""}),[q,y]=d.useState([]),[x,g]=d.useState(""),[S,f]=d.useState("");d.useEffect(()=>{if(l.productType==="seed"){const t=parseFloat(x),r=parseFloat(S);let n="";!isNaN(t)&&!isNaN(r)?n=((t+r)/2).toFixed(2):isNaN(t)?isNaN(r)||(n=r):n=t,u(c=>({...c,customRateAmount:n}))}},[l.productType,x,S]);const[E,I]=d.useState("basic"),[C,F]=d.useState([]),[R,j]=d.useState([]),[P,D]=d.useState(!1),[U,M]=d.useState(null),[a,m]=d.useState({equipmentId:"",settingValue:"",rateDescription:"",notes:""}),[w,V]=d.useState(!1);d.useEffect(()=>{(async()=>{D(!0);try{const r=await _.getSpreaders();j(r.data.data.spreaders||[])}catch(r){console.error("Failed to load spreaders:",r),b.error("Failed to load spreader equipment")}finally{D(!1)}})()},[]),d.useEffect(()=>{(async()=>{var r;if(o.productType==="granular"&&o.id){V(!0);try{const n=await fetch(`/api/product-spreader-settings/user-product/${o.id}`,{headers:{Authorization:`Bearer ${localStorage.getItem("authToken")}`}});if(n.ok){const c=await n.json();console.log("Received spreader settings data:",c),F(((r=c.data)==null?void 0:r.settings)||[])}else{console.error("Failed to fetch spreader settings:",n.status,n.statusText);const c=await n.text();console.error("Error response body:",c)}}catch(n){console.error("Failed to load spreader settings:",n)}finally{V(!1)}}})()},[o.id,o.productType]),d.useEffect(()=>{if(o.productType==="seed")try{const t=typeof o.activeIngredients=="string"?JSON.parse(o.activeIngredients):o.activeIngredients;t!=null&&t.seedBlend&&y(t.seedBlend),t!=null&&t.seedRates&&(g(t.seedRates.new||""),f(t.seedRates.overseed||""),t.seedRates.unit&&!l.customRateUnit&&u(r=>({...r,customRateUnit:t.seedRates.unit})))}catch{}},[]);const z=t=>{if(t.preventDefault(),E==="basic"){if(!l.productId&&!l.customName){b.error("Please select a base product or enter a custom name");return}const r={productId:l.productId?parseInt(l.productId):null,customName:l.customName||null,customRateAmount:l.customRateAmount?parseFloat(l.customRateAmount):null,customRateUnit:l.customRateUnit||null,notes:l.notes||null};T(r)}else{if(!l.customName){b.error("Product name is required");return}if(!l.categoryId){b.error("Category is required");return}if(!l.productType){b.error("Product type is required");return}const r={productId:l.productId?parseInt(l.productId):null,customName:l.customName,customRateAmount:l.customRateAmount?parseFloat(l.customRateAmount):null,customRateUnit:l.customRateUnit||null,notes:l.notes&&l.notes.trim()?l.notes.trim():null,brand:l.brand||null,categoryId:l.categoryId?parseInt(l.categoryId):null,productType:l.productType||null,activeIngredients:l.productType==="seed"?JSON.stringify({seedBlend:q,seedRates:x||S?{new:x?parseFloat(x):null,overseed:S?parseFloat(S):null,unit:l.customRateUnit}:void 0}):l.activeIngredients||null,description:l.description||null,isAdvancedEdit:!0,...l.productType==="granular"&&C.length>0&&{spreaderSettings:C.map(n=>({equipmentId:n.equipmentId||null,spreaderBrand:n.spreaderBrand||null,spreaderModel:n.spreaderModel&&n.spreaderModel.trim()?n.spreaderModel.trim():null,settingValue:n.settingValue,rateDescription:n.rateDescription&&n.rateDescription.trim()?n.rateDescription.trim():null,notes:n.notes&&n.notes.trim()?n.notes.trim():null}))}};T(r)}},L=()=>{if(!a.equipmentId||!a.settingValue.trim())return;const t=R.find(n=>n.id===parseInt(a.equipmentId)),r={...a,equipmentId:parseInt(a.equipmentId),equipmentName:t==null?void 0:t.name,equipmentManufacturer:t==null?void 0:t.manufacturer,equipmentModel:t==null?void 0:t.model};F([...C,r]),m({equipmentId:"",settingValue:"",rateDescription:"",notes:""})},O=t=>{const r=C.filter((n,c)=>c!==t);F(r)},J=t=>{M(t)},B=()=>{M(null)},s=(t,r)=>{const n=[...C];n[t]=r,F(n),M(null)},v=({setting:t,availableSpreaders:r,onSave:n,onCancel:c})=>{const[p,$]=d.useState({equipmentId:t.equipmentId||"",settingValue:t.settingValue||"",rateDescription:t.rateDescription||"",notes:t.notes||""}),Q=()=>{if(!p.settingValue.trim())return;const h=r.find(Z=>Z.id.toString()===p.equipmentId),X={...t,equipmentId:p.equipmentId?parseInt(p.equipmentId):null,equipmentName:(h==null?void 0:h.name)||t.equipmentName,equipmentManufacturer:(h==null?void 0:h.manufacturer)||t.equipmentManufacturer,equipmentModel:(h==null?void 0:h.model)||t.equipmentModel,settingValue:p.settingValue.trim(),rateDescription:p.rateDescription.trim()||null,notes:p.notes.trim()||null};n(X)};return e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Spreader"}),e.jsxs("select",{value:p.equipmentId,onChange:h=>$({...p,equipmentId:h.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md text-sm",children:[e.jsx("option",{value:"",children:"Select equipment..."}),r.map(h=>e.jsxs("option",{value:h.id,children:[h.name," (",h.manufacturer," ",h.model,")"]},h.id))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Setting Value *"}),e.jsx("input",{type:"text",value:p.settingValue,onChange:h=>$({...p,settingValue:h.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md text-sm",placeholder:"e.g., 3.5, H, 15"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Rate Description"}),e.jsx("input",{type:"text",value:p.rateDescription,onChange:h=>$({...p,rateDescription:h.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md text-sm",placeholder:"e.g., 1 lb nitrogen per 1000 sq ft"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-gray-700 mb-1",children:"Notes"}),e.jsx("input",{type:"text",value:p.notes,onChange:h=>$({...p,notes:h.target.value}),className:"w-full px-3 py-2 border border-gray-300 rounded-md text-sm",placeholder:"Additional notes..."})]}),e.jsxs("div",{className:"flex gap-2 pt-2",children:[e.jsx("button",{type:"button",onClick:Q,disabled:!p.settingValue.trim(),className:"px-3 py-1 bg-blue-600 text-white rounded text-sm hover:bg-blue-700 disabled:bg-gray-300",children:"Save"}),e.jsx("button",{type:"button",onClick:c,className:"px-3 py-1 bg-gray-500 text-white rounded text-sm hover:bg-gray-600",children:"Cancel"})]})]})};return e.jsx("div",{className:"fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50",children:e.jsxs("div",{className:"bg-white rounded-lg p-6 w-full max-w-lg max-h-[90vh] overflow-y-auto",children:[e.jsx("h3",{className:"text-lg font-semibold mb-4",children:"Edit Product"}),e.jsx("div",{className:"mb-4",children:e.jsxs("div",{className:"flex bg-gray-100 rounded-lg p-1",children:[e.jsx("button",{type:"button",onClick:()=>I("basic"),className:`flex-1 py-2 px-4 rounded-md text-sm font-medium transition-colors ${E==="basic"?"bg-white text-gray-900 shadow-sm":"text-gray-600 hover:text-gray-900"}`,children:"Basic Edit"}),e.jsx("button",{type:"button",onClick:()=>I("advanced"),className:`flex-1 py-2 px-4 rounded-md text-sm font-medium transition-colors ${E==="advanced"?"bg-white text-gray-900 shadow-sm":"text-gray-600 hover:text-gray-900"}`,children:"Advanced Edit"})]})}),e.jsxs("form",{onSubmit:z,className:"space-y-4",children:[E==="basic"?e.jsxs(e.Fragment,{children:[e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Base Product (Optional)"}),e.jsxs("select",{className:"input",value:l.productId,onChange:t=>u({...l,productId:t.target.value}),children:[e.jsx("option",{value:"",children:"Select a base product..."}),A.map(t=>e.jsxs("option",{value:t.id,children:[t.name," ",t.brand&&`- ${t.brand}`]},t.id))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Custom Name"}),e.jsx("input",{type:"text",className:"input",value:l.customName,onChange:t=>u({...l,customName:t.target.value}),placeholder:"e.g., My 24-0-11 Blend, Custom Herbicide Mix"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Application Rate"}),e.jsx("input",{type:"number",step:"0.01",className:"input",value:l.customRateAmount,onChange:t=>u({...l,customRateAmount:t.target.value}),placeholder:"2.5"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Rate Unit"}),e.jsxs("select",{className:"input",value:l.customRateUnit,onChange:t=>u({...l,customRateUnit:t.target.value}),children:[e.jsx("option",{value:"lbs/1000 sq ft",children:"lbs/1000 sq ft"}),e.jsx("option",{value:"oz/1000 sq ft",children:"oz/1000 sq ft"}),e.jsx("option",{value:"gal/acre",children:"gal/acre"}),e.jsx("option",{value:"fl oz/1000 sq ft",children:"fl oz/1000 sq ft"}),e.jsx("option",{value:"ml/1000 sq ft",children:"ml/1000 sq ft"}),e.jsx("option",{value:"tbsp/1000 sq ft",children:"tbsp/1000 sq ft"})]})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Notes"}),e.jsx("textarea",{className:"input",rows:"3",value:l.notes,onChange:t=>u({...l,notes:t.target.value}),placeholder:"Special mixing instructions, storage notes, etc."})]})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Product Name *"}),e.jsx("input",{type:"text",className:"input",value:l.customName,onChange:t=>u({...l,customName:t.target.value}),placeholder:"Product name",required:!0})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Brand"}),e.jsx("input",{type:"text",className:"input",value:l.brand,onChange:t=>u({...l,brand:t.target.value}),placeholder:"Manufacturer or brand name"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Category *"}),e.jsxs("select",{className:"input",value:l.categoryId,onChange:t=>u({...l,categoryId:t.target.value}),required:!0,children:[e.jsx("option",{value:"",children:"Select category..."}),i.map(t=>e.jsx("option",{value:t.id,children:t.name},t.id))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Product Type *"}),e.jsxs("select",{className:"input",value:l.productType,onChange:t=>u({...l,productType:t.target.value}),required:!0,children:[e.jsx("option",{value:"",children:"Select type..."}),e.jsx("option",{value:"liquid",children:"Liquid"}),e.jsx("option",{value:"granular",children:"Granular"}),e.jsx("option",{value:"seed",children:"Seed"}),e.jsx("option",{value:"powder",children:"Powder"})]})]})]}),l.productType==="seed"?e.jsx(K,{value:q,onChange:y}):e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Active Ingredients"}),e.jsx("input",{type:"text",className:"input",value:l.activeIngredients,onChange:t=>u({...l,activeIngredients:t.target.value}),placeholder:"e.g., 2,4-D 38.3%, Dicamba 2.77%"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Description"}),e.jsx("textarea",{className:"input",rows:"3",value:l.description,onChange:t=>u({...l,description:t.target.value}),placeholder:"Product description and usage information"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-6 items-start",children:[e.jsx("div",{children:l.productType==="seed"?e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Seeding Rates"}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"label text-xs",children:"New Lawn (lbs/1000 sq ft)"}),e.jsx("input",{type:"number",step:"0.01",className:"input",value:x,onChange:t=>g(t.target.value),placeholder:"e.g., 7"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label text-xs",children:"Overseeding (lbs/1000 sq ft)"}),e.jsx("input",{type:"number",step:"0.01",className:"input",value:S,onChange:t=>f(t.target.value),placeholder:"e.g., 3"})]})]})]}):e.jsxs(e.Fragment,{children:[e.jsx("label",{className:"label",children:"Default Application Rate"}),e.jsx("input",{type:"number",step:"0.01",className:"input",value:l.customRateAmount,onChange:t=>u({...l,customRateAmount:t.target.value}),placeholder:"2.5"})]})}),e.jsxs("div",{className:"pt-6",children:[e.jsx("label",{className:"label",children:"Rate Unit"}),e.jsxs("select",{className:"input",value:l.customRateUnit,onChange:t=>u({...l,customRateUnit:t.target.value}),children:[e.jsx("option",{value:"lbs/1000 sq ft",children:"lbs/1000 sq ft"}),e.jsx("option",{value:"oz/1000 sq ft",children:"oz/1000 sq ft"}),e.jsx("option",{value:"gal/acre",children:"gal/acre"}),e.jsx("option",{value:"fl oz/1000 sq ft",children:"fl oz/1000 sq ft"}),e.jsx("option",{value:"ml/1000 sq ft",children:"ml/1000 sq ft"}),e.jsx("option",{value:"tbsp/1000 sq ft",children:"tbsp/1000 sq ft"})]})]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Notes"}),e.jsx("textarea",{className:"input",rows:"3",value:l.notes,onChange:t=>u({...l,notes:t.target.value}),placeholder:"Special mixing instructions, storage notes, etc."})]}),l.productType==="granular"&&e.jsxs("div",{children:[e.jsx("label",{className:"label",children:"Spreader Settings"}),e.jsxs("div",{className:"border border-gray-200 rounded-lg p-4 space-y-3",children:[C.length>0&&e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"text-sm font-medium text-gray-700 mb-2",children:"Current Settings:"}),C.map((t,r)=>e.jsx("div",{className:"bg-gray-50 p-3 rounded border text-sm",children:U===r?e.jsx(v,{setting:t,availableSpreaders:R,onSave:n=>s(r,n),onCancel:B}):e.jsxs("div",{className:"flex justify-between items-start",children:[e.jsxs("div",{children:[e.jsx("div",{children:e.jsx("strong",{children:t.equipmentName||`${t.spreaderBrand}${t.spreaderModel?` (${t.spreaderModel})`:""}`})}),e.jsxs("div",{children:["Setting: ",e.jsx("span",{className:"font-medium",children:t.settingValue})]}),t.rateDescription&&e.jsx("div",{className:"text-gray-600",children:t.rateDescription}),t.notes&&e.jsx("div",{className:"text-gray-600 italic",children:t.notes})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx("button",{type:"button",onClick:()=>J(r),className:"text-blue-600 hover:text-blue-800 text-sm",children:"Edit"}),e.jsx("button",{type:"button",onClick:()=>O(r),className:"text-red-600 hover:text-red-800 text-sm",children:"Remove"})]})]})},r))]}),e.jsxs("div",{children:[e.jsx("h4",{className:"text-sm font-medium text-gray-700 mb-2",children:"Add New Setting:"}),R.length===0?e.jsx("div",{className:"text-sm text-blue-800 bg-blue-100 p-3 rounded mb-3",children:e.jsx("p",{children:"No spreader equipment found. Please add spreader equipment to your inventory first."})}):e.jsx("div",{className:"mb-3",children:e.jsxs("select",{className:"input text-sm",value:a.equipmentId,onChange:t=>m({...a,equipmentId:t.target.value}),children:[e.jsx("option",{value:"",children:"Choose spreader..."}),R.map(t=>e.jsxs("option",{value:t.id,children:[t.name," ",t.spreaderType&&`(${t.spreaderType})`]},t.id))]})}),R.length>0&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"mt-3 grid grid-cols-2 gap-3",children:[e.jsx("div",{children:e.jsx("input",{type:"text",className:"input text-sm",placeholder:"Setting (e.g., #14, 2.5)",value:a.settingValue,onChange:t=>m({...a,settingValue:t.target.value})})}),e.jsx("div",{children:e.jsx("input",{type:"text",className:"input text-sm",placeholder:"Rate description (optional)",value:a.rateDescription,onChange:t=>m({...a,rateDescription:t.target.value})})})]}),e.jsx("div",{className:"mt-3",children:e.jsx("textarea",{className:"input text-sm",rows:"2",placeholder:"Notes (optional)",value:a.notes,onChange:t=>m({...a,notes:t.target.value})})})]}),e.jsx("button",{type:"button",onClick:L,disabled:!a.equipmentId||!a.settingValue.trim()||R.length===0,className:"mt-3 px-3 py-1.5 bg-blue-600 text-white rounded text-sm hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed",children:"Add Setting"})]})]})]})]}),e.jsxs("div",{className:"flex gap-3 pt-4",children:[e.jsx("button",{type:"submit",className:"btn-primary flex-1",children:"Update Product"}),e.jsx("button",{type:"button",onClick:N,className:"btn-secondary flex-1",children:"Cancel"})]})]})]})})};function K({value:o=[],onChange:T}){const[N,A]=H.useState(o||[]);H.useEffect(()=>{T&&T(N)},[N]);const i=()=>A([...N||[],{cultivar:"",percent:""}]),l=(y,x,g)=>A((N||[]).map((S,f)=>f===y?{...S,[x]:g}:S)),u=y=>A((N||[]).filter((x,g)=>g!==y)),q=(N||[]).reduce((y,x)=>y+(parseFloat(x.percent)||0),0);return e.jsxs("div",{className:"border rounded-lg p-3",children:[e.jsxs("div",{className:"flex justify-between items-center mb-2",children:[e.jsx("label",{className:"label m-0",children:"Seed Blend"}),e.jsx("button",{type:"button",className:"text-sm text-blue-600",onClick:i,children:"Add Cultivar"})]}),(N||[]).length===0?e.jsx("div",{className:"text-sm text-gray-500",children:"No cultivars added"}):e.jsxs("div",{className:"space-y-2",children:[(N||[]).map((y,x)=>e.jsxs("div",{className:"grid grid-cols-6 gap-2 items-center",children:[e.jsx("input",{className:"input col-span-4",placeholder:"Cultivar name",value:y.cultivar,onChange:g=>l(x,"cultivar",g.target.value)}),e.jsx("div",{className:"col-span-1 flex items-center gap-1",children:e.jsx("input",{type:"number",step:"0.1",className:"input",placeholder:"%",value:y.percent,onChange:g=>l(x,"percent",g.target.value)})}),e.jsx("button",{type:"button",className:"text-red-600",onClick:()=>u(x),children:"Remove"})]},x)),e.jsxs("div",{className:`text-xs ${Math.abs(q-100)<.01?"text-green-700":"text-gray-600"}`,children:["Total: ",q.toFixed(1),"%"]})]})]})}export{de as default};