PageSourceSearch

https://skrotahusvagn.lovable.app/assets/PriceOptimizationPage-Dlq3Y9zs.js

js skrotahusvagn.lovable.app collected 2026-10-03 03:45:59 UTC 45,743 bytes, 26 lines download raw bytes

1var Te=Object.defineProperty,Le=Object.defineProperties;var $e=Object.getOwnPropertyDescriptors;var je=Object.getOwnPropertySymbols;var De=Object.prototype.hasOwnProperty,Fe=Object.prototype.propertyIsEnumerable;var ge=(n,a,r)=>a in n?Te(n,a,{enumerable:!0,configurable:!0,writable:!0,value:r}):n[a]=r,G=(n,a)=>{for(var r in a||(a={}))De.call(a,r)&&ge(n,r,a[r]);if(je)for(var r of je(a))Fe.call(a,r)&&ge(n,r,a[r]);return n},W=(n,a)=>Le(n,$e(a));import{r as A,j as e}from"./react-vendor-zBl7e66q.js";import{T as qe,a as Be,b as B,c as R}from"./tabs-AVx_OMx6.js";import{J as $,c as T,h as u,j as N,k as v,i as p,m as F,B as q,I as O,S as fe}from"./index-DDJP99TE.js";import{C as Ne}from"./checkbox-DNFHEEsF.js";import{T as le,a as de,b as E,c as _,d as oe,e as b}from"./table-Bdoq30Sb.js";import{A as Re,h as Ie,a as Ee,b as Ue,c as Ve,d as ze,e as Oe,f as Qe,g as Ge}from"./alert-dialog-K630ZDvn.js";import{u as z,a as Z,b as ee}from"./query-CqHJrFw1.js";import{f as K}from"./formatters-C1kxxdiU.js";import{L as D,Y as me,az as se,X as We,r as Xe,N as xe,ay as U,Z as H,a_ as J,b$ as ue,bM as ae,z as Ye,j as ne,P as ie,w as He,t as Je,k as Ze,b0 as es,bR as ss,c5 as rs,bX as ts,cT as as,l as ns,S as is}from"./icons-ddqYUsFI.js";import{S as cs}from"./switch-BYkbqiUr.js";import{B as C}from"./badge-B6PfYjAf.js";import{R as ce,o as ls,n as we,m as Ce,X as Pe,Y as Se,T as Me,B as ds}from"./generateCategoricalChart-iQL_5rf8.js";import{P as os,a as ms}from"./PieChart-ByJeVIZ4.js";import{B as xs}from"./BarChart-DYbDaQfV.js";import{P as us}from"./progress-DZ16lMUu.js";import{u as hs,a as ps,b as js}from"./useCompetitorProducts-DqjJCx1Q.js";import{f as gs,s as Y,b as fs,a as re,p as te}from"./date-fns-DKRY5Ax4.js";import{S as ve,a as ke,b as ye,c as _e,d as I}from"./select-Bf6QQWwL.js";import{m as V}from"./framer-CVxE5Mud.js";import{L as Ns}from"./LineChart-DxToTGFH.js";import{L as be}from"./Line-Blrg2Q85.js";import"./radix-BsWLCLsF.js";import"./supabase-CPiqIBkr.js";import"./router-DxihJGWf.js";import"./forms-BZpIKt6_.js";import"./index-DYCccshK.js";function vs(){return z({queryKey:["pricing-rules"],queryFn:async()=>{const{data:n,error:a}=await T.from("pricing_rules").select("*").order("priority");if(a)throw a;return n}})}function ks(){const n=Z();return ee({mutationFn:async({id:a,updates:r})=>{const{data:y,error:m}=await T.from("pricing_rules").update(r).eq("id",a).select().single();if(m)throw m;return y},onSuccess:()=>{n.invalidateQueries({queryKey:["pricing-rules"]}),$.success("Regel uppdaterad")},onError:a=>{$.error(`Kunde inte uppdatera regel: ${a.message}`)}})}function ys(){return z({queryKey:["product-competitor-matches"],queryFn:async()=>{const{data:n,error:a}=await T.from("product_competitor_matches").select(`
2          *,
3          competitor_products (
4            id,
5            name,
6            price,
7            sku,
8            competitor_id,
9            competitors (name)
10          )
11        `).order("created_at",{ascending:!1});if(a)throw a;return n}})}function _s(){const n=Z();return ee({mutationFn:async()=>{const{data:a,error:r}=await T.functions.invoke("match-competitor-products",{body:{action:"auto_match"}});if(r)throw r;if(!a.success)throw new Error(a.error);return a},onSuccess:a=>{n.invalidateQueries({queryKey:["product-competitor-matches"]}),$.success(`Matchning klar: ${a.summary.newMatchesAdded} nya matchningar`)},onError:a=>{$.error(`Matchning misslyckades: ${a.message}`)}})}function bs(n){return z({queryKey:["price-suggestions",n],queryFn:async()=>{let a=T.from("price_suggestions").select("*").order("created_at",{ascending:!1});a=a.eq("status",n);const{data:r,error:y}=await a;if(y)throw y;return r}})}function Ke(){const n=Z();return ee({mutationFn:async({skus:a,clearPending:r=!1}={})=>{const{data:y,error:m}=await T.functions.invoke("analyze-product-pricing",{body:{skus:a,generateSuggestions:!0,clearPending:r}});if(m)throw m;if(!y.success)throw new Error(y.error);return y},onSuccess:a=>{n.invalidateQueries({queryKey:["price-suggestions"]}),$.success(`Analys klar: ${a.summary.suggestionsGenerated} förslag genererade`)},onError:a=>{$.error(`Analys misslyckades: ${a.message}`)}})}function ws(){const n=Z();return ee({mutationFn:async({suggestion_ids:a,action:r})=>{const{data:y,error:m}=await T.functions.invoke("bulk-update-prices",{body:{suggestion_ids:a,action:r}});if(m)throw m;if(!y.success)throw new Error(y.error);return y},onSuccess:(a,r)=>
11{n.invalidateQueries({queryKey:["price-suggestions"]}),r.action==="apply"?$.success(`${a.successCount} priser uppdaterade i Magento`):$.success(`${a.rejectedCount} förslag avvisade`)},onError:a=>{$.error(`Åtgärd misslyckades: ${a.message}`)}})}function Cs(){const[n,a]=A.useState([]),[r,y]=A.useState(""),{data:m,isLoading:h}=bs("pending"),g=Ke(),d=ws(),x=(m==null?void 0:m.filter(t=>{var o;return t.product_sku.toLowerCase().includes(r.toLowerCase())||((o=t.product_name)==null?void 0:o.toLowerCase().includes(r.toLowerCase()))}))||[],L=()=>{n.length===x.length?a([]):a(x.map(t=>t.id))},c=t=>{a(o=>o.includes(t)?o.filter(P=>P!==t):[...o,t])},j=()=>{n.length!==0&&(d.mutate({suggestion_ids:n,action:"apply"}),a([]))},l=()=>{n.length!==0&&(d.mutate({suggestion_ids:n,action:"reject"}),a([]))},s=()=>{g.mutate({clearPending:!0})},i=x.reduce((t,o)=>t+(o.potential_extra_profit||0),0),f=x.length>0?x.reduce((t,o)=>t+(o.suggested_price-o.current_price),0)/x.length:0,k=t=>t.current_margin_percent!==null&&t.current_margin_percent<0?e.jsx(xe,{className:"h-4 w-4 text-destructive"}):t.suggested_price>t.current_price?e.jsx(U,{className:"h-4 w-4 text-green-500"}):e.jsx(H,{className:"h-4 w-4 text-orange-500"}),S=t=>t===null?"text-muted-foreground":t<0?"text-destructive font-bold":t<15?"text-orange-500":t<25?"text-yellow-500":"text-green-500",w=()=>x.filter(o=>n.includes(o.id)).slice(0,3);return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"grid gap-4 md:grid-cols-4",children:[e.jsxs(u,{children:[e.jsx(N,{className:"pb-2",children:e.jsx(v,{className:"text-sm font-medium",children:"Väntande förslag"})}),e.jsx(p,{children:e.jsx("div",{className:"text-2xl font-bold",children:x.length})})]}),e.jsxs(u,{children:[e.jsx(N,{className:"pb-2",children:e.jsx(v,{className:"text-sm font-medium",children:"Potentiell extra marginal"})}),e.jsxs(p,{children:[e.jsxs("div",{className:"text-2xl font-bold text-green-500",children:["+",K(i)," kr"]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"per såld enhet"})]})]}),e.jsxs(u,{children:[e.jsx(N,{className:"pb-2",children:e.jsx(v,{className:"text-sm font-medium",children:"Genomsnittlig prisändring"})}),e.jsx(p,{children:e.jsxs("div",{className:`text-2xl font-bold ${f>=0?"text-green-500":"text-orange-500"}`,children:[f>=0?"+":"",K(f)," kr"]})})]}),e.jsxs(u,{children:[e.jsx(N,{className:"pb-2",children:e.jsx(v,{className:"text-sm font-medium",children:"Markerade"})}),e.jsx(p,{children:e.jsx("div",{className:"text-2xl font-bold",children:n.length})})]})]}),e.jsxs(u,{children:[e.jsx(N,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(v,{children:"Prisförslag"}),e.jsx(F,{children:"Granska och godkänn prisändringar baserat på marginalmål och konkurrentpriser."})]}),e.jsxs(q,{onClick:s,disabled:g.isPending,children:[g.isPending?e.jsx(D,{className:"h-4 w-4 mr-2 animate-spin"}):e.jsx(me,{className:"h-4 w-4 mr-2"}),"Analysera produkter"]})]})}),e.jsxs(p,{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center justify-between gap-4",children:[e.jsxs("div",{className:"relative flex-1 max-w-sm",children:[e.jsx(se,{className:"absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground"}),e.jsx(O,{placeholder:"Sök SKU eller produktnamn...",value:r,onChange:t=>y(t.target.value),className:"pl-8"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(q,{variant:"outline",size:"sm",onClick:l,disabled:n.length===0||d.isPending,children:[e.jsx(We,{className:"h-4 w-4 mr-1"}),"Avvisa (",n.length,")"]}),e.jsxs(Re,{children:[e.jsx(Ie,{asChild:!0,children:e.jsxs(q,{size:"sm",disabled:n.length===0||d.isPending,children:[d.isPending?e.jsx(D,{className:"h-4 w-4 mr-1 animate-spin"}):e.jsx(Xe,{className:"h-4 w-4 mr-1"}),"Tillämpa (",n.length,")"]})}),e.jsxs(Ee,{children:[e.jsxs(Ue,{children:[e.jsx(Ve,{children:"Bekräfta prisändring"}),e.jsx(ze,{asChild:!0,children:e.jsxs("div",{className:"space-y-4",children:[e.jsxs("p",{children:["Du är på väg att ändra pris på ",e.jsx("strong",{children:n.length})," produkter i Magento."]}),w().length>0&&e.jsxs("div",{className:"bg-muted p-3 rounded-lg space-y-2",children:[e.jsx("p",{className:"font-medium text-sm",children:"Exempel på ändringar:"}),w().map(t=>e.jsxs("div",{className:"text-sm flex justify-between",children:[e.jsx("span",{className:"truncate max-w-[200px]",children:t.product_name||t.product_sku}),e.jsxs("span",{className:"font-mono",children:[K(t.current_price)," → ",K(t.suggested_price)," kr"]})]},t.id)),n.length>3&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:["...och ",n.length-3," produkter till"]})]}),e.jsx("div",{className:"bg-destructive/10 border border-destructive/20 p-3 rounded-lg",children:e.jsx("p",{className:"text-destructive text-sm font-medium",children:"⚠️ Denna åtgärd uppdaterar priser direkt i Magento och kan inte ångras automatiskt."})})]})})]}),e.jsxs(Oe,{children:[e.jsx(Qe,{children:"Avbryt"}),e.jsx(Ge,{onClick:j,children:"Bekräfta ändring"})]})]})]})]})]}),h?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(D,{className:"h-8 w-8 animate-spin text-muted-foreground"})}):x.length===0?e.jsxs("div",{className:"text-center py-8 text-muted-foreground",children:[e.jsx("p",{children:"Inga väntande prisförslag."}),e.jsx("p",{className:"text-sm mt-1",children:'Klicka på "Analysera produkter" för att generera förslag.'})]}):e.jsx("div",{className:"rounded-md border",children:e.jsxs(le,{children:[e.jsx(de,{children:e.jsxs(E,{children:[e.jsx(_,{className:"w-10",children:e.jsx(Ne,{checked:n.length===x.length&&x.length>0,onCheckedChange:L})}),e.jsx(_,{children:"Produkt"}),e.jsx(_,{className:"text-right",children:"Nuv. pris"}),e.jsx(_,{className:"text-right",children:"Kostnad"}),e.jsx(_,{className:"text-right",children:"Marginal"}),e.jsx(_,{className:"text-right",children:"Konkurrentsnitt"}),e.jsx(_,{className:"text-right",children:"Föreslaget pris"}),e.jsx(_,{children:"Anledning"})]})}),e.jsx(oe,{children:x.map(t=>e.jsxs(E,{children:[e.jsx(b,{children:e.jsx(Ne,{checked:n.includes(t.id),onCheckedChange:()=>c(t.id)})}),e.jsx(b,{children:e.jsxs("div",{className:"flex items-center gap-2",children:[k(t),e.jsxs("div",{children:[e.jsx("div",{className:"font-medium",children:t.product_name||t.product_sku}),e.jsx("div",{className:"text-xs text-muted-foreground",children:t.product_sku})]})]})}),e.jsxs(b,{className:"text-right font-medium",children:[K(t.current_price)," kr"]}),e.jsx(b,{className:"text-right",children:t.current_cost!==null?`${K(t.current_cost)} kr`:e.jsx("span",{className:"text-muted-foreground",children:"-"})}),e.jsx(b,{className:`text-right ${S(t.current_margin_percent)}
11`,children:t.current_margin_percent!==null?`${t.current_margin_percent.toFixed(1)}%`:e.jsx("span",{className:"text-muted-foreground",children:"-"})}),e.jsx(b,{className:"text-right",children:t.competitor_avg_price!==null?`${K(t.competitor_avg_price)} kr`:e.jsx("span",{className:"text-muted-foreground",children:"-"})}),e.jsxs(b,{className:"text-right",children:[e.jsxs("div",{className:"font-bold text-primary",children:[K(t.suggested_price)," kr"]}),e.jsxs("div",{className:`text-xs ${t.suggested_price>t.current_price?"text-green-500":"text-orange-500"}`,children:[t.suggested_price>t.current_price?"+":"",K(t.suggested_price-t.current_price)," kr"]})]}),e.jsx(b,{children:e.jsx("p",{className:"text-sm max-w-xs truncate",title:t.reason,children:t.reason})})]},t.id))})]})})]})]})]})}const Ps={min_margin:{label:"Minimum marginal",description:"Produkter med marginal under detta värde varnas",icon:H,unit:"%"},max_competitor_diff:{label:"Max avvikelse från konkurrenter",description:"Tillåten prisavvikelse över konkurrenternas snitt",icon:U,unit:"%"},target_margin:{label:"Målmarginal",description:"Önskad marginal för prisförslag",icon:ue,unit:"%"},min_price:{label:"Minimipris",description:"Produkter under detta pris ignoreras",icon:J,unit:"kr"}};function Ss(){const{data:n,isLoading:a}=vs(),r=ks(),y=(h,g)=>{const d=parseFloat(g);!isNaN(d)&&d>=0&&r.mutate({id:h.id,updates:{value:d}})},m=h=>{r.mutate({id:h.id,updates:{is_active:!h.is_active}})};return a?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(D,{className:"h-8 w-8 animate-spin text-muted-foreground"})}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs(u,{children:[e.jsxs(N,{children:[e.jsx(v,{children:"Prisregler"}),e.jsx(F,{children:"Konfigurera regler som styr hur prisförslag genereras och vilka varningar som visas."})]}),e.jsx(p,{className:"space-y-6",children:n==null?void 0:n.map(h=>{const g=Ps[h.rule_type]||{label:h.rule_name,description:"",icon:ue,unit:"%"},d=g.icon;return e.jsxs("div",{className:`flex items-start justify-between p-4 border rounded-lg ${h.is_active?"bg-card":"bg-muted/50 opacity-60"}`,children:[e.jsxs("div",{className:"flex items-start gap-4",children:[e.jsx("div",{className:`p-2 rounded-lg ${h.is_active?"bg-primary/10":"bg-muted"}`,children:e.jsx(d,{className:`h-5 w-5 ${h.is_active?"text-primary":"text-muted-foreground"}`})}),e.jsxs("div",{className:"space-y-1",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("h4",{className:"font-medium",children:g.label}),!h.is_active&&e.jsx(C,{variant:"outline",children:"Inaktiv"})]}),e.jsx("p",{className:"text-sm text-muted-foreground",children:g.description})]})]}),e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(O,{type:"number",value:h.value,onChange:x=>y(h,x.target.value),className:"w-24 text-right",disabled:r.isPending}),e.jsx("span",{className:"text-sm text-muted-foreground w-8",children:g.unit})]}),e.jsx(cs,{checked:h.is_active,onCheckedChange:()=>m(h),disabled:r.isPending})]})]},h.id)})})]}),e.jsxs(u,{children:[e.jsx(N,{children:e.jsx(v,{children:"Hur fungerar prisoptimeringen?"})}),e.jsxs(p,{className:"space-y-4 text-sm text-muted-foreground",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium text-foreground",children:"1. Marginalberäkning"}),e.jsxs("p",{children:["Marginalen beräknas som (pris ex moms - inköpspris) / pris ex moms. Eftersom ",e.jsx("code",{className:"bg-muted px-1 rounded",children:"price"})," i Magento är inkl moms och"," ",e.jsx("code",{className:"bg-muted px-1 rounded",children:"cost"})," är ex moms, konverterar vi automatiskt:"]}),e.jsxs("pre",{className:"bg-muted p-2 rounded text-xs",children:["pris_ex_moms = price / 1.25",`
12`,"marginal = (pris_ex_moms - cost) / pris_ex_moms × 100%"]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium text-foreground",children:"2. Prisförslag genereras när:"}),e.jsxs("ul",{className:"list-disc list-inside space-y-1",children:[e.jsxs("li",{children:[e.jsx("strong",{children:"Negativ marginal:"})," Föreslår pris baserat på målmarginal"]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Låg marginal:"})," Under minimum marginal → höj priset"]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Billigare än konkurrenter:"})," 20%+ under snitt → möjlighet att höja"]}),e.jsxs("li",{children:[e.jsx("strong",{children:"Dyrare än konkurrenter:"})," Över max avvikelse + hög marginal → överväg sänkning"]})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("h4",{className:"font-medium text-foreground",children:"3. Produktmatchning"}),e.jsx("p",{children:"Systemet matchar automatiskt era produkter mot konkurrenternas baserat på SKU och produktnamn. Högre konfidens = säkrare matchning."})]})]})]})]})}function Ms(){const[n,a]=A.useState(""),{data:r,isLoading:y}=ys(),m=_s(),h=(r==null?void 0:r.filter(l=>{var s,i;
12return l.own_sku.toLowerCase().includes(n.toLowerCase())||((i=(s=l.competitor_products)==null?void 0:s.name)==null?void 0:i.toLowerCase().includes(n.toLowerCase()))}))||[],g=(l,s)=>{switch(l){case"auto_sku":return e.jsxs(C,{className:"bg-green-500",children:["SKU-match (",s,"%)"]});case"auto_name":return e.jsxs(C,{className:"bg-yellow-500 text-black",children:["Namn (",s,"%)"]});case"manual":return e.jsx(C,{variant:"secondary",children:"Manuell"});default:return e.jsx(C,{variant:"outline",children:l})}},d=(r==null?void 0:r.length)||0,x=(r==null?void 0:r.filter(l=>l.match_type==="auto_sku").length)||0,L=(r==null?void 0:r.filter(l=>l.match_type==="auto_name").length)||0,c=(r==null?void 0:r.filter(l=>l.match_type==="manual").length)||0,j=(r==null?void 0:r.filter(l=>l.is_verified).length)||0;return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"grid gap-4 md:grid-cols-5",children:[e.jsxs(u,{children:[e.jsx(N,{className:"pb-2",children:e.jsx(v,{className:"text-sm font-medium",children:"Totalt matchningar"})}),e.jsx(p,{children:e.jsx("div",{className:"text-2xl font-bold",children:d})})]}),e.jsxs(u,{children:[e.jsx(N,{className:"pb-2",children:e.jsx(v,{className:"text-sm font-medium",children:"SKU-matchningar"})}),e.jsx(p,{children:e.jsx("div",{className:"text-2xl font-bold text-green-500",children:x})})]}),e.jsxs(u,{children:[e.jsx(N,{className:"pb-2",children:e.jsx(v,{className:"text-sm font-medium",children:"Namn-matchningar"})}),e.jsx(p,{children:e.jsx("div",{className:"text-2xl font-bold text-yellow-500",children:L})})]}),e.jsxs(u,{children:[e.jsx(N,{className:"pb-2",children:e.jsx(v,{className:"text-sm font-medium",children:"Manuella"})}),e.jsx(p,{children:e.jsx("div",{className:"text-2xl font-bold",children:c})})]}),e.jsxs(u,{children:[e.jsx(N,{className:"pb-2",children:e.jsx(v,{className:"text-sm font-medium",children:"Verifierade"})}),e.jsx(p,{children:e.jsx("div",{className:"text-2xl font-bold",children:j})})]})]}),e.jsxs(u,{children:[e.jsx(N,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(v,{children:"Produktmatchningar"}),e.jsx(F,{children:"Kopplingar mellan era produkter och konkurrenternas för prisjämförelse."})]}),e.jsxs(q,{onClick:()=>m.mutate(),disabled:m.isPending,children:[m.isPending?e.jsx(D,{className:"h-4 w-4 mr-2 animate-spin"}):e.jsx(ae,{className:"h-4 w-4 mr-2"}),"Kör automatisk matchning"]})]})}),e.jsxs(p,{className:"space-y-4",children:[e.jsxs("div",{className:"relative max-w-sm",children:[e.jsx(se,{className:"absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground"}),e.jsx(O,{placeholder:"Sök SKU eller produktnamn...",value:n,onChange:l=>a(l.target.value),className:"pl-8"})]}),y?e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx(D,{className:"h-8 w-8 animate-spin text-muted-foreground"})}):h.length===0?e.jsxs("div",{className:"text-center py-8 text-muted-foreground",children:[e.jsx(ae,{className:"h-12 w-12 mx-auto mb-4 opacity-50"}),e.jsx("p",{children:"Inga produktmatchningar ännu."}),e.jsx("p",{className:"text-sm mt-1",children:'Klicka på "Kör automatisk matchning" för att hitta matchande produkter.'})]}):e.jsx("div",{className:"rounded-md border max-h-[500px] overflow-auto",children:e.jsxs(le,{children:[e.jsx(de,{children:e.jsxs(E,{children:[e.jsx(_,{children:"Er produkt (SKU)"}),e.jsx(_,{children:"Konkurrentprodukt"}),e.jsx(_,{children:"Konkurrent"}),e.jsx(_,{className:"text-right",children:"Konkurrentpris"}),e.jsx(_,{children:"Matchningstyp"}),e.jsx(_,{children:"Status"})]})}),e.jsx(oe,{children:h.map(l=>{var s,i,f,k,S,w;return e.jsxs(E,{children:[e.jsx(b,{className:"font-mono font-medium",children:l.own_sku}),e.jsxs(b,{children:[e.jsx("div",{className:"max-w-xs truncate",title:(s=l.competitor_products)==null?void 0:s.name,children:((i=l.competitor_products)==null?void 0:i.name)||"-"}),((f=l.competitor_products)==null?void 0:f.sku)&&e.jsx("div",{className:"text-xs text-muted-foreground font-mono",children:l.competitor_products.sku})]}),e.jsx(b,{children:((S=(k=l.competitor_products)==null?void 0:k.competitors)==null?void 0:S.name)||"-"}),e.jsx(b,{className:"text-right font-medium",children:(w=l.competitor_products)!=null&&w.price?`${K(l.competitor_products.price)} kr`:"-"}),e.jsx(b,{children:g(l.match_type,l.match_confidence)}),e.jsx(b,{children:l.is_verified?e.jsxs("div",{className:"flex items-center gap-1 text-green-500",children:[e.jsx(Ye,{className:"h-4 w-4"}),e.jsx("span",{className:"text-sm",children:"Verifierad"})]}):e.jsxs("div",{className:"flex items-center gap-1 text-muted-foreground",children:[e.jsx(ne,{className:"h-4 w-4"}),e.jsx("span",{className:"text-sm",children:"Ej verifierad"})]})})]},l.id)})})]})})]})]})]})}const X={negative:"#ef4444",low:"#f97316",medium:"#eab308",good:"#22c55e"};function Ks(){const[n,a]=A.useState(""),[r,y]=A.useState(null),m=Ke(),h=async()=>{const s=await m.mutateAsync({clearPending:!1});y({summary:s.summary,products:s.products})},g=(r==null?void 0:r.products)||[],d=r==null?void 0:r.summary,x=g.filter(s=>s.sku.toLowerCase().includes(n.toLowerCase())||s.name.toLowerCase().includes(n.toLowerCase())),L=[{name:"Negativ",value:g.filter(s=>s.currentMarginPercent!==null&&s.currentMarginPercent<0).length,color:X.negative},{name:"<15%",value:g.filter(s=>s.currentMarginPercent!==null&&s.currentMarginPercent>=0&&s.currentMarginPercent<15).length,color:X.low},{name:"15-30%",value:g.filter(s=>s.currentMarginPercent!==null&&s.currentMarginPercent>=15&&s.currentMarginPercent<30).length,color:X.medium},{name:">30%",value:g.filter(s=>s.currentMarginPercent!==null&&s.currentMarginPercent>=30).length,color:X.good}],c=[...g].filter(s=>s.currentMarginPercent!==null).sort((s,i)=>(i.currentMarginPercent||0)-(s.currentMarginPercent||0));c.slice(0,10).map(s=>({name:s.sku.substring(0,12),margin:s.currentMarginPercent}));const j=c.slice(-10).reverse().map(s=>({name:s.sku.substring(0,12),margin:s.currentMarginPercent})),l=s=>s===null?e.jsx(C,{variant:"outline",children:"Okänd"}):s<0?e.jsx(C,{variant:"destructive",children:"Negativ"}):s<15?e.jsx(C,{className:"bg-orange-500",children:"Låg"}):s<30?e.jsx(C,{className:"bg-yellow-500 text-black",children:"Medium"}):e.jsx(C,{className:"bg-green-500",children:"Bra"});return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"grid gap-4 md:grid-cols-5",children:[e.jsxs(u,{children:[e.jsxs(N,{className:"pb-2 flex flex-row items-center justify-between space-y-0",children:[e.jsx(v,{className:"text-sm font-medium",children:"Analyserade produkter"}),e.jsx(ie,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsxs(p,{children:[e.jsx("div",{className:"text-2xl font-bold",children:(d==null?void 0:d.totalProducts)||0}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[(d==null?void 0:d.productsOnPage)||0," på denna sida"]})]})]}),e.jsxs(u,{children:[e.jsxs(N,{className:"pb-2 flex flex-row items-center justify-between space-y-0",children:[e.jsx(v,{className:"text-sm font-medium",children:"Med kostnadspris"}),e.jsx(J,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsxs(p,{children:[e.jsx("div",{className:"text-2xl font-bold",children:(d==null?void 0:d.productsWithCost)||0}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[(d==null?void 0:d.productsWithoutCost)||0," saknar kostnadspris"]})]})]}),e.jsxs(u,{children:[e.jsxs(N,{className:"pb-2 flex flex-row items-center justify-between space-y-0",children:[e.jsx(v,{className:"text-sm font-medium",children:"Genomsnittlig marginal"}),e.jsx(U,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(p,{children:e.jsx("div",{className:"text-2xl font-bold",children:(d==null?void 0:d.averageMargin)!=null?`${d.averageMargin.toFixed(1)}%`:"-"})})]}),e.jsxs(u,{children:[e.jsxs(N,{className:"pb-2 flex flex-row items-center justify-between space-y-0",children:[e.jsx(v,{className:"text-sm font-medium",children:"Negativ marginal"}),e.jsx(xe,{className:"h-4 w-4 text-destructive"})]}),e.jsxs(p,{children:[e.jsx("div",{className:"text-2xl font-bold text-destructive",children:(d==null?void 0:d.negativeMarginCount)||0}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"produkter med förlust"})]})]}),e.jsxs(u,{children:[e.jsxs(N,{className:"pb-2 flex flex-row items-center justify-between space-y-0",children:[e.jsx(v,{className:"text-sm font-medium",children:"Matchade konkurrenter"}),e.jsx(J,{className:"h-4 w-4 text-muted-foreground"})]}),e.jsx(p,{children:e.jsx("div",{className:"text-2xl font-bold",children:(d==null?void 0:d.productsWithCompetitorMatch)||0})})]})]}),e.jsx(u,{children:e.jsx(N,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(v,{children:"Lönsamhetsanalys"}),e.jsx(F,{children:"Analysera alla produkter för att se marginaler och lönsamhet."})]}),e.jsxs(q,{onClick:h,disabled:m.isPending,children:[m.isPending?e.jsx(D,{className:"h-4 w-4 mr-2 animate-spin"}):e.jsx(me,{className:"h-4 w-4 mr-2"}),"Kör analys"]})]})})}),g.length>0&&e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"grid gap-4 md:grid-cols-2",children:[e.jsxs(u,{children:[e.jsx(N,{children:e.jsx(v,{children:"Marginalfördelning"})}),e.jsx(p,{children:e.jsx(ce,{width:"100%",height:250,children:e.jsxs(os,{children:[e.jsx(ms,{data:L,cx:"50%",cy:"50%",innerRadius:60,outerRadius:80,paddingAngle:5,dataKey:"value",label:({name:s,value:i})=>`${s}: ${i}`,children:L.map((s,i)=>e.jsx(ls,{fill:s.color},`cell-${i}`))}),e.jsx(we,{})]})})})]}),e.jsxs(u,{children:[e.jsx(N,{children:e.jsx(v,{children:"Sämsta marginaler (åtgärda först)"})}),e.jsx(p,{children:e.jsx(ce,{width:"100%",height:250,children:e.jsxs(xs,{data:j,layout:"vertical",children:[e.jsx(Ce,{strokeDasharray:"3 3"}),e.jsx(Pe,{type:"number",unit:"%"}),e.jsx(Se,{type:"category",dataKey:"name",width:80}),e.jsx(Me,{formatter:s=>[`${Number(s).toFixed(1)}%`,"Marginal"]}
12),e.jsx(ds,{dataKey:"margin",fill:"#ef4444",radius:[0,4,4,0]})]})})})]})]}),e.jsxs(u,{children:[e.jsxs(N,{children:[e.jsx(v,{children:"Produktdetaljer"}),e.jsxs("div",{className:"relative max-w-sm",children:[e.jsx(se,{className:"absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground"}),e.jsx(O,{placeholder:"Sök SKU eller produktnamn...",value:n,onChange:s=>a(s.target.value),className:"pl-8"})]})]}),e.jsxs(p,{children:[e.jsx("div",{className:"rounded-md border max-h-96 overflow-auto",children:e.jsxs(le,{children:[e.jsx(de,{children:e.jsxs(E,{children:[e.jsx(_,{children:"Produkt"}),e.jsx(_,{className:"text-right",children:"Pris (inkl moms)"}),e.jsx(_,{className:"text-right",children:"Kostnad"}),e.jsx(_,{className:"text-right",children:"Bruttovinst"}),e.jsx(_,{className:"text-right",children:"Marginal"}),e.jsx(_,{children:"Status"}),e.jsx(_,{className:"text-right",children:"Konkurrentsnitt"})]})}),e.jsx(oe,{children:x.slice(0,50).map(s=>e.jsxs(E,{children:[e.jsx(b,{children:e.jsxs("div",{children:[e.jsx("div",{className:"font-medium truncate max-w-48",children:s.name}),e.jsx("div",{className:"text-xs text-muted-foreground",children:s.sku})]})}),e.jsxs(b,{className:"text-right",children:[K(s.currentPrice)," kr"]}),e.jsx(b,{className:"text-right",children:s.cost!==null?`${K(s.cost)} kr`:e.jsx(C,{variant:"outline",className:"text-orange-500 border-orange-500",children:"Saknas"})}),e.jsx(b,{className:"text-right",children:s.currentProfitKr!==null?`${K(s.currentProfitKr)} kr`:e.jsx("span",{className:"text-muted-foreground",children:"-"})}),e.jsx(b,{className:"text-right font-medium",children:s.currentMarginPercent!==null?`${s.currentMarginPercent.toFixed(1)}%`:e.jsx("span",{className:"text-muted-foreground",children:"-"})}),e.jsx(b,{children:l(s.currentMarginPercent)}),e.jsx(b,{className:"text-right",children:s.avgCompetitorPrice!==null?`${K(s.avgCompetitorPrice)} kr`:e.jsx("span",{className:"text-muted-foreground",children:"-"})})]},s.sku))})]})}),x.length>50&&e.jsxs("p",{className:"text-sm text-muted-foreground mt-2 text-center",children:["Visar 50 av ",x.length," produkter. Använd sökfältet för att filtrera."]})]})]})]})]})}function As(){const{data:n,isLoading:a}=hs(),r=ps(),[y,m]=A.useState({}),[h,g]=A.useState(null),{data:d}=js(h),x=async c=>{m(j=>W(G({},j),{[c.id]:{competitorId:c.id,status:"scraping",message:"Startar skrapning..."}}));try{await r.mutateAsync({competitorId:c.id}),m(j=>W(G({},j),{[c.id]:{competitorId:c.id,status:"done",message:"Skrapning klar!"}}))}catch(j){m(l=>W(G({},l),{[c.id]:{competitorId:c.id,status:"error",message:j instanceof Error?j.message:"Okänt fel"}}))}};if(a)return e.jsx("div",{className:"flex items-center justify-center py-12",children:e.jsx(D,{className:"h-8 w-8 animate-spin text-muted-foreground"})});const L=(n==null?void 0:n.filter(c=>c.is_active))||[];return e.jsxs("div",{className:"space-y-6",children:[e.jsxs(u,{children:[e.jsxs(N,{children:[e.jsxs(v,{className:"flex items-center gap-2",children:[e.jsx(ie,{className:"h-5 w-5"}),"Konkurrentövervakning"]}),e.jsx(F,{children:"Skrapa produkter från konkurrenter för prisjämförelse och konkurrensanalys. Data används för att generera AI-förslag på prisoptimering."})]}),e.jsx(p,{children:L.length===0?e.jsxs("div",{className:"text-center py-8 text-muted-foreground",children:[e.jsx(ne,{className:"h-12 w-12 mx-auto mb-4 opacity-50"}),e.jsx("p",{children:"Inga aktiva konkurrenter hittades."}),e.jsx("p",{className:"text-sm mt-2",children:"Lägg till konkurrenter i SEO-verktyg → Konkurrentanalys"})]}):e.jsx("div",{className:"grid gap-4",children:L.map(c=>{const j=y[c.id],l=(j==null?void 0:j.status)==="scraping",s=c.latest_snapshot;return e.jsx(u,{className:`cursor-pointer transition-colors ${h===c.id?"ring-2 ring-primary":""}`,onClick:()=>g(h===c.id?null:c.id),children:e.jsxs(p,{className:"pt-4",children:[e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsxs("div",{className:"flex-1",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[c.logo_url&&e.jsx("img",{src:c.logo_url,alt:c.name,className:"h-8 w-8 rounded object-contain bg-muted"}),e.jsxs("div",{children:[e.jsxs("h3",{className:"font-semibold flex items-center gap-2",children:[c.name,e.jsx("a",{href:c.website_url,target:"_blank",rel:"noopener noreferrer",onClick:i=>i.stopPropagation(),className:"text-muted-foreground hover:text-primary",children:e.jsx(He,{className:"h-4 w-4"})})]}),e.jsx("p",{className:"text-sm text-muted-foreground",children:c.website_url.replace(/^https?:\/\//,"")})]})]}),e.jsxs("div",{className:"mt-3 flex flex-wrap gap-2",children:[(s==null?void 0:s.product_count)&&e.jsxs(C,{variant:"secondary",children:[e.jsx(ie,{className:"h-3 w-3 mr-1"}),s.product_count," produkter"]}),(s==null?void 0:s.snapshot_date)&&e.jsxs(C,{variant:"outline",children:[e.jsx(Je,{className:"h-3 w-3 mr-1"}),"Senast: ",gs(new Date(s.snapshot_date),{addSuffix:!0,locale:Y})]}),(j==null?void 0:j.status)==="done"&&e.jsxs(C,{variant:"default",children:[e.jsx(Ze,{className:"h-3 w-3 mr-1"}),"Skrapning klar"]}),(j==null?void 0:j.status)==="error"&&e.jsxs(C,{variant:"destructive",children:[e.jsx(ne,{className:"h-3 w-3 mr-1"}),"Fel: ",j.message]})]})]}),e.jsx(q,{variant:"outline",size:"sm",disabled:l||r.isPending,onClick:i=>{i.stopPropagation(),x(c)},children:l?e.jsxs(e.Fragment,{children:[e.jsx(D,{className:"h-4 w-4 mr-2 animate-spin"}),"Skrapar..."]}):e.jsxs(e.Fragment,{children:[e.jsx(me,{className:"h-4 w-4 mr-2"}),"Skrapa produkter"]})})]}),l&&e.jsxs("div",{className:"mt-4",children:[e.jsx(us,{value:void 0,className:"h-2"}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:j.message})]})]})},c.id)})})})]}),h&&d&&d.length>0&&e.jsxs(u,{children:[e.jsxs(N,{children:[e.jsxs(v,{className:"text-lg",children:["Skrapade produkter (",d.length,")"]}),e.jsx(F,{children:"De senaste produkterna från denna konkurrent"})]}),e.jsx(p,{children:e.jsxs("div",{className:"max-h-[400px] overflow-y-auto",children:[e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{className:"sticky top-0 bg-background border-b",children:e.jsxs("tr",{children:[e.jsx("th",{className:"text-left py-2 px-2",children:"Produkt"}),e.jsx("th",{className:"text-right py-2 px-2",children:"Pris"}),e.jsx("th",{className:"text-center py-2 px-2",children:"I lager"})]})}),e.jsx("tbody",{children:d.slice(0,50).map(c=>e.jsxs("tr",{className:"border-b last:border-0",children:[e.jsx("td",{className:"py-2 px-2",children:e.jsxs("div",{className:"flex items-center gap-2",children:[c.image_url&&e.jsx("img",{src:c.image_url,alt:c.name||"Konkurrentprodukt",className:"h-8 w-8 rounded object-cover bg-muted"}),e.jsxs("div",{children:[e.jsx("p",{className:"font-medium truncate max-w-[300px]",children:c.name}),c.sku&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:["SKU: ",c.sku]})]})]})}),e.jsx("td",{className:"text-right py-2 px-2 font-mono",children:c.price?`${c.price} ${c.currency||"SEK"}`:"-"}),e.jsxs("td",{className:"text-center py-2 px-2",children:[c.in_stock===!0&&e.jsx(C,{variant:"default",children:"Ja"}),c.in_stock===!1&&e.jsx(C,{variant:"destructive",children:"Nej"}),c.in_stock===null&&e.jsx("span",{className:"text-muted-foreground",children:"-"})]})]},c.id))})]}),d.length>50&&e.jsxs("p",{className:"text-center text-sm text-muted-foreground py-4",children:["Visar 50 av ",d.length," produkter"]})]})})]})]})}function Ts(){var s;const[n,a]=A.useState(""),[r,y]=A.useState("diff_desc"),[m,h]=A.useState(null),[g,d]=A.useState("30d"),{data:x,isLoading:L}=z({queryKey:["competitor-price-matches"],queryFn:async()=>{const{data:i,error:f}=await T.from("product_matchings").select(`
13          id,
14          our_sku,
15          confidence_score,
16          competitor_products!inner (
17            id,
18            name,
19            price,
20            competitor_id,
21            created_at,
22            competitors!inner (
23              name
24            )
25          )
26        `).eq("status","confirmed").not("competitor_products.price","is",null).order("confidence_score",{ascending:!1}).limit(200);if(f)throw f;const k=(i==null?void 0:i.map(o=>o.our_sku))||[],{data:S}=await T.from("products_cache").select("sku, name, price").in("sku",k),w=new Map((S==null?void 0:S.map(o=>[o.sku,o]))||[]);return(i==null?void 0:i.map(o=>{var pe;const P=w.get(o.our_sku),M=o.competitor_products,Q=(P==null?void 0:P.price)||0,he=(M==null?void 0:M.price)||0,Ae=Q>0?(he-Q)/Q*100:0;return{our_sku:o.our_sku,our_name:(P==null?void 0:P.name)||o.our_sku,our_price:Q,competitor_name:((pe=M==null?void 0:M.competitors)==null?void 0:pe.name)||"Okänd",competitor_price:he,competitor_product_name:(M==null?void 0:M.name)||"",price_diff_percent:Ae,last_updated:(M==null?void 0:M.created_at)||""}}).filter(o=>o.our_price>0&&o.competitor_price>0))||[]},staleTime:5*60*1e3}),{data:c}=z({queryKey:["competitor-price-history",m,g],queryFn:async()=>
26{if(!m)return[];const f=fs(new Date,g==="7d"?7:g==="30d"?30:90).toISOString(),{data:k}=await T.from("product_price_history").select("price, recorded_at").eq("sku",m).gte("recorded_at",f).order("recorded_at",{ascending:!0}),{data:S}=await T.from("product_matchings").select("competitor_product_id").eq("our_sku",m).eq("status","confirmed").limit(1).single();if(!(S!=null&&S.competitor_product_id))return(k==null?void 0:k.map(o=>({date:re(te(o.recorded_at),"dd MMM",{locale:Y}),our_price:o.price,competitor_price:0})))||[];const{data:w}=await T.from("competitor_products").select("price, created_at").eq("id",S.competitor_product_id).gte("created_at",f).order("created_at",{ascending:!0}),t=new Map;return k==null||k.forEach(o=>{const P=re(te(o.recorded_at),"dd MMM",{locale:Y});t.set(P,{date:P,our_price:o.price,competitor_price:0})}),w==null||w.forEach(o=>{const P=re(te(o.created_at),"dd MMM",{locale:Y}),M=t.get(P);M?M.competitor_price=o.price||0:t.set(P,{date:P,our_price:0,competitor_price:o.price||0})}),Array.from(t.values()).sort((o,P)=>new Date(o.date).getTime()-new Date(P.date).getTime())},enabled:!!m,staleTime:5*60*1e3}),j=A.useMemo(()=>{let i=x||[];if(n){const f=n.toLowerCase();i=i.filter(k=>k.our_sku.toLowerCase().includes(f)||k.our_name.toLowerCase().includes(f)||k.competitor_name.toLowerCase().includes(f))}switch(r){case"diff_desc":i.sort((f,k)=>k.price_diff_percent-f.price_diff_percent);break;case"diff_asc":i.sort((f,k)=>f.price_diff_percent-k.price_diff_percent);break;case"name":i.sort((f,k)=>f.our_name.localeCompare(k.our_name,"sv"));break}return i},[x,n,r]),l=A.useMemo(()=>{if(!(x!=null&&x.length))return null;const i=x.filter(w=>w.price_diff_percent>5).length,f=x.filter(w=>w.price_diff_percent<-5).length,k=x.filter(w=>Math.abs(w.price_diff_percent)<=5).length,S=x.reduce((w,t)=>w+t.price_diff_percent,0)/x.length;return{total:x.length,cheaperCount:i,expensiveCount:f,similarCount:k,avgDiff:S}},[x]);return L?e.jsxs("div",{className:"space-y-4",children:[e.jsx(fe,{className:"h-32 w-full"}),e.jsx(fe,{className:"h-64 w-full"})]}):e.jsxs("div",{className:"space-y-6",children:[l&&e.jsxs("div",{className:"grid grid-cols-2 md:grid-cols-4 gap-4",children:[e.jsx(V.div,{initial:{opacity:0,y:10},animate:{opacity:1,y:0},children:e.jsx(u,{className:"bg-card/50 border-border/50",children:e.jsx(p,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Matchade produkter"}),e.jsx("p",{className:"text-2xl font-bold",children:l.total})]}),e.jsx(es,{className:"h-8 w-8 text-primary/50"})]})})})}),e.jsx(V.div,{initial:{opacity:0,y:10},animate:{opacity:1,y:0},transition:{delay:.1},children:e.jsx(u,{className:"bg-green-500/10 border-green-500/30",children:e.jsx(p,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-green-400",children:"Vi är billigast"}),e.jsx("p",{className:"text-2xl font-bold text-green-400",children:l.cheaperCount})]}),e.jsx(H,{className:"h-8 w-8 text-green-500/50"})]})})})}),e.jsx(V.div,{initial:{opacity:0,y:10},animate:{opacity:1,y:0},transition:{delay:.2},children:e.jsx(u,{className:"bg-red-500/10 border-red-500/30",children:e.jsx(p,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-red-400",children:"Vi är dyrast"}),e.jsx("p",{className:"text-2xl font-bold text-red-400",children:l.expensiveCount})]}),e.jsx(U,{className:"h-8 w-8 text-red-500/50"})]})})})}),e.jsx(V.div,{initial:{opacity:0,y:10},animate:{opacity:1,y:0},transition:{delay:.3},children:e.jsx(u,{className:"bg-card/50 border-border/50",children:e.jsx(p,{className:"p-4",children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Genomsnittlig diff"}),e.jsxs("p",{className:`text-2xl font-bold ${l.avgDiff>0?"text-green-400":l.avgDiff<0?"text-red-400":""}`,children:[l.avgDiff>0?"+":"",l.avgDiff.toFixed(1),"%"]})]}
26),e.jsx(ue,{className:"h-8 w-8 text-muted-foreground/50"})]})})})})]}),m&&c&&c.length>0&&e.jsxs(u,{className:"bg-card/50 border-border/50",children:[e.jsx(N,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsxs("div",{children:[e.jsx(v,{className:"text-lg",children:"Prishistorik"}),e.jsx(F,{children:(s=j.find(i=>i.our_sku===m))==null?void 0:s.our_name})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(ve,{value:g,onValueChange:i=>d(i),children:[e.jsx(ke,{className:"w-24",children:e.jsx(ye,{})}),e.jsxs(_e,{children:[e.jsx(I,{value:"7d",children:"7 dagar"}),e.jsx(I,{value:"30d",children:"30 dagar"}),e.jsx(I,{value:"90d",children:"90 dagar"})]})]}),e.jsx(q,{variant:"ghost",size:"sm",onClick:()=>h(null),children:"Stäng"})]})]})}),e.jsx(p,{children:e.jsx("div",{className:"h-[250px]",children:e.jsx(ce,{width:"100%",height:"100%",children:e.jsxs(Ns,{data:c,children:[e.jsx(Ce,{strokeDasharray:"3 3",stroke:"hsl(var(--border))"}),e.jsx(Pe,{dataKey:"date",tick:{fill:"hsl(var(--muted-foreground))",fontSize:12}}),e.jsx(Se,{tick:{fill:"hsl(var(--muted-foreground))",fontSize:12},tickFormatter:i=>`${i} kr`}),e.jsx(Me,{contentStyle:{backgroundColor:"hsl(var(--card))",border:"1px solid hsl(var(--border))",borderRadius:"8px"},labelStyle:{color:"hsl(var(--foreground))"}}),e.jsx(we,{}),e.jsx(be,{type:"monotone",dataKey:"our_price",name:"Vårt pris",stroke:"hsl(var(--primary))",strokeWidth:2,dot:!1}),e.jsx(be,{type:"monotone",dataKey:"competitor_price",name:"Konkurrentpris",stroke:"hsl(var(--destructive))",strokeWidth:2,dot:!1})]})})})})]}),e.jsxs(u,{className:"bg-card/50 border-border/50",children:[e.jsxs(N,{children:[e.jsxs(v,{className:"flex items-center gap-2",children:[e.jsx(J,{className:"h-5 w-5"}),"Prisjämförelser"]}),e.jsx(F,{children:"Matchade produkter med konkurrentpriser"})]}),e.jsxs(p,{children:[e.jsxs("div",{className:"flex flex-col sm:flex-row gap-4 mb-4",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx(se,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(O,{placeholder:"Sök SKU, produkt eller konkurrent...",value:n,onChange:i=>a(i.target.value),className:"pl-10"})]}),e.jsxs(ve,{value:r,onValueChange:i=>y(i),children:[e.jsxs(ke,{className:"w-[180px]",children:[e.jsx(ss,{className:"h-4 w-4 mr-2"}),e.jsx(ye,{placeholder:"Sortera"})]}),e.jsxs(_e,{children:[e.jsx(I,{value:"diff_desc",children:"Billigast först"}),e.jsx(I,{value:"diff_asc",children:"Dyrast först"}),e.jsx(I,{value:"name",children:"Namn A-Ö"})]})]})]}),e.jsx("div",{className:"max-h-[500px] overflow-y-auto space-y-2",children:j.length===0?e.jsxs("div",{className:"text-center py-8 text-muted-foreground",children:[e.jsx(xe,{className:"h-12 w-12 mx-auto mb-4 opacity-50"}),e.jsx("p",{children:"Inga prisjämförelser hittades"}),e.jsx("p",{className:"text-sm mt-2",children:'Matcha produkter i "Matchning"-fliken först'})]}):j.map((i,f)=>e.jsx(V.div,{initial:{opacity:0,x:-10},animate:{opacity:1,x:0},transition:{delay:f*.02},children:e.jsx(u,{className:`cursor-pointer transition-colors hover:bg-muted/30 ${m===i.our_sku?"ring-2 ring-primary":""}`,onClick:()=>h(i.our_sku),children:e.jsx(p,{className:"p-3",children:e.jsxs("div",{className:"flex items-center justify-between gap-4",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"font-medium truncate",children:i.our_name}),e.jsxs("div",{className:"flex items-center gap-2 text-sm text-muted-foreground",children:[e.jsx("span",{className:"font-mono",children:i.our_sku}),e.jsx("span",{children:"•"}),e.jsxs("span",{children:["vs ",i.competitor_name]})]})]}),e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsxs("div",{className:"text-right",children:[e.jsxs("p",{className:"font-mono text-sm",children:[e.jsx("span",{className:"text-muted-foreground",children:"Vi: "}),e.jsxs("span",{className:"font-semibold",children:[i.our_price.toFixed(0)," kr"]})]}),e.jsxs("p",{className:"font-mono text-sm",children:[e.jsx("span",{className:"text-muted-foreground",children:"De: "}),e.jsxs("span",{children:[i.competitor_price.toFixed(0)," kr"]})]})]}),e.jsxs(C,{variant:i.price_diff_percent>5?"default":i.price_diff_percent<-5?"destructive":"secondary",className:`w-20 justify-center ${i.price_diff_percent>5?"bg-green-500/20 text-green-400 border-green-500/30":(i.price_diff_percent<-5,"")}`,children:[i.price_diff_percent>0?e.jsx(H,{className:"h-3 w-3 mr-1"}):i.price_diff_percent<0?e.jsx(U,{className:"h-3 w-3 mr-1"}):e.jsx(rs,{className:"h-3 w-3 mr-1"}),i.price_diff_percent>0?"+":"",i.price_diff_percent.toFixed(0),"%"]})]})]})})})},`${i.our_sku}-${i.competitor_name}`))})]})]})]})}function ir(){return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold tracking-tight",children:"Prisoptimering"}),e.jsx("p",{className:"text-muted-foreground",children:"Analysera marginaler, jämför med konkurrenter och optimera priser för maximal lönsamhet."})]}),e.jsxs(qe,{defaultValue:"suggestions",className:"space-y-4",children:[e.jsxs(Be,{className:"grid w-full grid-cols-6",children:[e.jsxs(B,{value:"suggestions",className:"flex items-center gap-2",children:[e.jsx(ts,{className:"h-4 w-4"}),e.jsx("span",{className:"hidden sm:inline",children:"Prisförslag"})]}),e.jsxs(B,{value:"overview",className:"flex items-center gap-2",children:[e.jsx(U,{className:"h-4 w-4"}),e.jsx("span",{className:"hidden sm:inline",children:"Lönsamhet"})]}),e.jsxs(B,{value:"price-tracker",className:"flex items-center gap-2",children:[e.jsx(as,{className:"h-4 w-4"}),e.jsx("span",{className:"hidden sm:inline",children:"Prisspårning"})]}),e.jsxs(B,{value:"matching",className:"flex items-center gap-2",children:[e.jsx(ae,{className:"h-4 w-4"}),e.jsx("span",{className:"hidden sm:inline",children:"Matchning"})]}),e.jsxs(B,{value:"competitors",className:"flex items-center gap-2",children:[e.jsx(ns,{className:"h-4 w-4"}),e.jsx("span",{className:"hidden sm:inline",children:"Konkurrenter"})]}),e.jsxs(B,{value:"rules",className:"flex items-center gap-2",children:[e.jsx(is,{className:"h-4 w-4"}),e.jsx("span",{className:"hidden sm:inline",children:"Regler"})]})]}),e.jsx(R,{value:"suggestions",children:e.jsx(Cs,{})}),e.jsx(R,{value:"overview",children:e.jsx(Ks,{})}),e.jsx(R,{value:"price-tracker",children:e.jsx(Ts,{})}),e.jsx(R,{value:"matching",children:e.jsx(Ms,{})}),e.jsx(R,{value:"competitors",children:e.jsx(As,{})}),e.jsx(R,{value:"rules",children:e.jsx(Ss,{})})]})]})}export{ir as PriceOptimizationPage};

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.