import{q as E,r as l,s as V,j as e,c as x}from"./index-CUr9vJx-.js";import O from"./ImageUploadField-OEGY-f0l.js";import{T}from"./TextStyleWidget-BOGJRJrl.js";import{S as z,s as D}from"./SortableList-DTcW7IV9.js";import{V as L}from"./ViewOnSite-DL1pMLmR.js";import{V as J}from"./VehicleCatalogDialog-DjXB6vYN.js";import"./resizeImage-C6PvAwh0.js";import"./sortable.esm-BriK72Y6.js";import"./grip-vertical-ChbhtBmG.js";const g={name:"",brand_id:"",image_path:"",sort_order:0};function Y(){const c=E(),[i,h]=l.useState([]),[v,F]=l.useState([]),[n,u]=l.useState(g),[R,y]=l.useState(g),[d,w]=l.useState(null),[_,f]=l.useState(""),[o,S]=l.useState("all"),[B,M]=l.useState(!1),j=l.useRef(null);V(JSON.stringify(n)!==JSON.stringify(R),"models");const m=o!=="all",k=l.useMemo(()=>o==="all"?i:i.filter(t=>String(t.brand_id)===o),[i,o]),b=()=>{x.get("/models").then(h),x.get("/brands").then(F)};l.useEffect(b,[]);const I=t=>{const r={name:t.name,brand_id:t.brand_id,image_path:t.image_path||"",sort_order:t.sort_order};w(t.id),u(r),y(r),f(""),requestAnimationFrame(()=>{var a,s,p;(a=j.current)==null||a.scrollIntoView({behavior:"smooth",block:"start"}),(p=(s=j.current)==null?void 0:s.querySelector("select, input"))==null||p.focus({preventScroll:!0})})},N=()=>{w(null),u(g),y(g)},$=async t=>{t.preventDefault(),f("");const r={...n,brand_id:Number(n.brand_id)};try{if(d)await x.put(`/models/${d}`,r);else{const a=i.filter(s=>Number(s.brand_id)===r.brand_id).reduce((s,p)=>Math.max(s,Number(p.sort_order)||0),0)+10;await x.post("/models",{...r,sort_order:a})}c.success(d?"Modell gespeichert.":"Modell angelegt."),N(),b()}catch(a){f(a.message)}},q=async t=>{if(confirm("Modell inkl. aller Pakete/Produkte wirklich löschen?"))try{await x.delete(`/models/${t}`),d===t&&N(),c.success("Modell gelöscht."),b()}catch(r){c.error(`Löschen fehlgeschlagen: ${r.message}`)}},C=(t,r)=>{const a=[...r];return t.map(s=>String(s.brand_id)===o?a.shift():s)},A=async t=>{const r=i;h(C(i,t));try{const a=await D("models",t);h(s=>C(s,a)),c.success("Reihenfolge gespeichert.")}catch(a){h(r),c.error(`Reihenfolge konnte nicht gespeichert werden: ${a.message}`)}};return e.jsxs("div",{className:"grid gap-6 lg:grid-cols-3",children:[e.jsxs("div",{className:"lg:col-span-2",children:[e.jsxs("div",{className:"mb-4 flex items-center justify-between gap-3",children:[e.jsx("h1",{className:"text-xl font-bold text-neutral-900 dark:text-white",children:"Modelle"}),e.jsx("button",{type:"button",onClick:()=>M(!0),className:"shrink-0 rounded-md border border-neutral-300 px-3 py-1.5 text-sm font-medium text-neutral-700 hover:bg-neutral-50 dark:border-neutral-700 dark:text-neutral-200 dark:hover:bg-neutral-800",children:"Aus Vorlage laden"})]}),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:o,onChange:t=>S(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"}),v.map(t=>e.jsx("option",{value:String(t.id),children:t.name},t.id))]}),o!=="all"&&e.jsx("button",{onClick:()=>S("all"),className:"text-sm text-brand-600 hover:underline",children:"Filter zurücksetzen"}),e.jsxs("span",{className:"text-sm text-neutral-400",children:[k.length," von ",i.length]})]}),e.jsx("p",{className:"mb-3 text-xs text-neutral-500 dark:text-neutral-400",children:m?"Reihenfolge am Griff links per Ziehen ändern – so erscheinen die Modelle auf der Website.":"Zum Sortieren erst eine Marke auswählen."}),e.jsx("div",{className:"overflow-x-auto 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:[m&&e.jsx("th",{className:"w-8 px-2 py-2","aria-label":"Reihenfolge"}),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.jsx(z,{items:k,onReorder:A,as:"tbody",disabled:!m,className:"divide-y divide-neutral-100 dark:divide-neutral-800",children:(t,r)=>e.jsxs(e.Fragment,{children:[m&&e.jsx("td",{className:"bg-white px-2 py-2 dark:bg-neutral-950",children:r}),e.jsx("td",{className:"bg-white px-4 py-2 dark:bg-neutral-950",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:"bg-white px-4 py-2 dark:bg-neutral-950",children:t.brand_name}),e.jsx("td",{className:"bg-white px-4 py-2 font-medium text-neutral-800 dark:bg-neutral-950 dark:text-neutral-100",children:t.name}),e.jsxs("td",{className:"whitespace-nowrap bg-white px-4 py-2 text-right dark:bg-neutral-950",children:[e.jsx(L,{to:`/fahrzeuge/${t.brand_slug}/${t.slug}`,className:"mr-3"}),e.jsx("button",{onClick:()=>I(t),className:"mr-3 text-brand-600 hover:underline",children:"Bearbeiten"}),e.jsx("button",{onClick:()=>q(t.id),className:"text-red-600 hover:underline",children:"Löschen"})]})]})}),k.length===0&&e.jsx("tbody",{children:e.jsx("tr",{children:e.jsx("td",{colSpan:m?5:4,className:"px-4 py-6 text-center text-neutral-400",children:i.length===0?"Noch keine Modelle angelegt.":"Keine Modelle für diese Marke."})})})]})})]}),e.jsxs("form",{ref:j,onSubmit:$,className:"h-fit scroll-mt-20 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:d?"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:n.brand_id,onChange:t=>u({...n,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…"}),v.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:n.name,onChange:t=>u({...n,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"}),d&&e.jsx(T,{fieldKey:`car_models.${d}.name`,previewText:n.name||"Beispieltext"})]}),e.jsx(O,{value:n.image_path,onChange:t=>u({...n,image_path:t}),label:"Bild (optional)"}),_&&e.jsx("p",{className:"text-sm text-red-600",children:_}),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:d?"Speichern":"Anlegen"}),d&&e.jsx("button",{type:"button",onClick:N,className:"rounded-md border border-neutral-300 px-4 py-2 text-sm dark:border-neutral-700",children:"Abbrechen"})]})]}),e.jsx(J,{open:B,onClose:()=>M(!1),onImported:b})]})}export{Y as default};