import{r as a,j as e,a as i}from"./index-DM5dAVp5.js";import{I as _}from"./ImageUploadField-oqjVVSSG.js";import{T as M}from"./TextStyleWidget-CkwixV2h.js";const j={name:"",brand_id:"",image_path:"",sort_order:0};function F(){const[n,k]=a.useState([]),[x,N]=a.useState([]),[r,s]=a.useState(j),[l,m]=a.useState(null),[u,h]=a.useState(""),[d,b]=a.useState("all"),o=a.useMemo(()=>d==="all"?n:n.filter(t=>String(t.brand_id)===d),[n,d]),c=()=>{i.get("/models").then(k),i.get("/brands").then(N)};a.useEffect(c,[]);const f=t=>{m(t.id),s({name:t.name,brand_id:t.brand_id,image_path:t.image_path||"",sort_order:t.sort_order})},p=()=>{m(null),s(j)},v=async t=>{t.preventDefault(),h("");const g={...r,brand_id:Number(r.brand_id)};try{l?await i.put(`/models/${l}`,g):await i.post("/models",g),p(),c()}catch(w){h(w.message)}},y=async t=>{confirm("Modell inkl. aller Pakete/Produkte wirklich löschen?")&&(await i.delete(`/models/${t}`),c())};return e.jsxs("div",{className:"grid gap-6 lg:grid-cols-3",children:[e.jsxs("div",{className:"lg:col-span-2",children:[e.jsx("h1",{className:"mb-4 text-xl font-bold text-neutral-900 dark:text-white",children:"Modelle"}),e.jsxs("div",{className:"mb-3 flex flex-wrap items-center gap-2",children:[e.jsx("label",{className:"text-sm font-medium text-neutral-600 dark:text-neutral-300",children:"Nach Marke filtern:"}),e.jsxs("select",{value:d,onChange:t=>b(t.target.value),className:"rounded-md border border-neutral-300 bg-white px-3 py-1.5 text-sm dark:border-neutral-700 dark:bg-neutral-900",children:[e.jsx("option",{value:"all",children:"Alle Marken"}),x.map(t=>e.jsx("option",{value:String(t.id),children:t.name},t.id))]}),d!=="all"&&e.jsx("button",{onClick:()=>b("all"),className:"text-sm text-brand-600 hover:underline",children:"Filter zurücksetzen"}),e.jsxs("span",{className:"text-sm text-neutral-400",children:[o.length," von ",n.length]})]}),e.jsx("div",{className:"overflow-hidden rounded-xl border border-neutral-200 dark:border-neutral-800",children:e.jsxs("table",{className:"w-full text-left text-sm",children:[e.jsx("thead",{className:"bg-neutral-50 text-neutral-500 dark:bg-neutral-900 dark:text-neutral-400",children:e.jsxs("tr",{children:[e.jsx("th",{className:"px-4 py-2",children:"Bild"}),e.jsx("th",{className:"px-4 py-2",children:"Marke"}),e.jsx("th",{className:"px-4 py-2",children:"Modell"}),e.jsx("th",{className:"px-4 py-2 text-right",children:"Aktionen"})]})}),e.jsxs("tbody",{className:"divide-y divide-neutral-100 dark:divide-neutral-800",children:[o.map(t=>e.jsxs("tr",{className:"bg-white dark:bg-neutral-950",children:[e.jsx("td",{className:"px-4 py-2",children:t.image_path?e.jsx("img",{src:t.image_path,alt:"",className:"h-10 w-10 rounded-md object-cover"}):e.jsx("span",{className:"flex h-10 w-10 items-center justify-center rounded-md bg-neutral-100 text-xs text-neutral-400 dark:bg-neutral-800",children:"–"})}),e.jsx("td",{className:"px-4 py-2",children:t.brand_name}),e.jsx("td",{className:"px-4 py-2 font-medium text-neutral-800 dark:text-neutral-100",children:t.name}),e.jsxs("td",{className:"px-4 py-2 text-right",children:[e.jsx("button",{onClick:()=>f(t),className:"mr-3 text-brand-600 hover:underline",children:"Bearbeiten"}),e.jsx("button",{onClick:()=>y(t.id),className:"text-red-600 hover:underline",children:"Löschen"})]})]},t.id)),o.length===0&&e.jsx("tr",{children:e.jsx("td",{colSpan:4,className:"px-4 py-6 text-center text-neutral-400",children:n.length===0?"Noch keine Modelle angelegt.":"Keine Modelle für diese Marke."})})]})]})})]}),e.jsxs("form",{onSubmit:v,className:"h-fit space-y-4 rounded-xl border border-neutral-200 bg-white p-5 dark:border-neutral-800 dark:bg-neutral-900",children:[e.jsx("h2",{className:"font-bold text-neutral-900 dark:text-white",children:l?"Modell bearbeiten":"Neues Modell"}),e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300",children:"Marke *"}),e.jsxs("select",{required:!0,value:r.brand_id,onChange:t=>s({...r,brand_id:t.target.value}),className:"w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900",children:[e.jsx("option",{value:"",children:"Bitte wählen…"}),x.map(t=>e.jsx("option",{value:t.id,children:t.name},t.id))]})]}),e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300",children:"Modellname *"}),e.jsx("input",{required:!0,value:r.name,onChange:t=>s({...r,name:t.target.value}),className:"w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900"}),l&&e.jsx(M,{fieldKey:`car_models.${l}.name`,previewText:r.name||"Beispieltext"})]}),e.jsx(_,{value:r.image_path,onChange:t=>s({...r,image_path:t}),label:"Bild (optional)"}),e.jsxs("div",{children:[e.jsx("label",{className:"mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300",children:"Sortierung"}),e.jsx("input",{type:"number",value:r.sort_order,onChange:t=>s({...r,sort_order:Number(t.target.value)}),className:"w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900"})]}),u&&e.jsx("p",{className:"text-sm text-red-600",children:u}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx("button",{type:"submit",className:"rounded-md bg-brand-500 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-600",children:l?"Speichern":"Anlegen"}),l&&e.jsx("button",{type:"button",onClick:p,className:"rounded-md border border-neutral-300 px-4 py-2 text-sm dark:border-neutral-700",children:"Abbrechen"})]})]})]})}export{F as default};