1import{u as F,o as G,r,s as n,j as e,B as o,g as q,a6 as B,U as J,e as S}from"./index-CJAQG0mx.js";import{u as W}from"./useAdminAuth-BsozdsBx.js";import{T as X}from"./textarea-FB0AUn4o.js";import{B as L}from"./badge-C6vkf5dU.js";import{C as m,a as x,b as f,c as h}from"./card-BmKXugNV.js";import{S as Y,a as ee,b as se,c as ae,d as j}from"./select-oesuobRU.js";import{T as te,a as re,b as M}from"./tabs-D9T7aGSY.js";import{S as ne}from"./separator-DBSHnziv.js";import{A as le}from"./arrow-left-D3BVqJ5Q.js";import{S as ie}from"./send-DNEQgUDS.js";import{C as ce}from"./clock-BzcuSu8j.js";import{S as de}from"./shopping-cart-BM4HoeHB.js";import{f as A}from"./format-Ds7tieJC.js";import{s as w}from"./sv-DMRbwygk.js";import{f as oe}from"./formatDistanceToNow-DBzp6msJ.js";import"./index-BdQq_4o_.js";import"./index-CJjE5PV9.js";import"./check-Cfj_Q-47.js";import"./en-US-BvK8AWMH.js";import"./constructNow-C0YS1KEP.js";import"./endOfMonth-LmV9cV7M.js";const qe=()=>{const{ticketId:i}=F(),u=G(),{user:b}=W(),g=r.useRef(null),[t,N]=r.useState(null),[v,C]=r.useState([]),[a,E]=r.useState(null);r.useState("");const[T,H]=r.useState([]),[_,V]=r.useState([]),[p,k]=r.useState(""),[c,R]=r.useState(!1),[z,I]=r.useState(!1),[D,P]=r.useState(!0),O=async()=>{if(!i)return;const{data:s}=await n.from("support_tickets").select("*").eq("id",i).single();if(!s){u("/admin/support");return}N(s);const[l,d,Q,Z]=await Promise.all([n.from("support_messages").select("*").eq("ticket_id",i).order("created_at",{ascending:!0}),n.from("profiles").select("first_name, last_name, phone, city, street_address, postal_code").eq("user_id",s.user_id
1).single(),n.from("orders").select("id, order_number, status, price_sek").eq("user_id",s.user_id).order("created_at",{ascending:!1}).limit(5),n.from("quick_reply_templates").select("id, title, content, category").eq("is_active",!0).order("sort_order")]);C(l.data??[]),E(d.data),H(Q.data??[]),V(Z.data??[]),s.has_unread&&await n.from("support_tickets").update({has_unread:!1}).eq("id",i),P(!1)};r.useEffect(()=>{O()},[i]),r.useEffect(()=>{if(!i)return;const s=n.channel(`ticket-${i}`).on("postgres_changes",{event:"INSERT",schema:"public",table:"support_messages",filter:`ticket_id=eq.${i}`},l=>{C(d=>[...d,l.new])}).subscribe();return()=>{n.removeChannel(s)}},[i]),r.useEffect(()=>{var s;(s=g.current)==null||s.scrollTo({top:g.current.scrollHeight,behavior:"smooth"})},[v]);const K=async()=>{var l;if(!p.trim()||!t||!b)return;I(!0);const{error:s}=await n.from("support_messages").insert({ticket_id:t.id,message:p.trim(),sender_type:"staff",sender_name:((l=b.email)==null?void 0:l.split("@")[0])??"Personal",is_internal:c});s?S.error("Kunde inte skicka meddelande"):(c||(await n.from("support_tickets").update({status:"Besvarad",has_unread:!0}).eq("id",t.id),N(d=>d&&{...d,status:"Besvarad"})),k(""),S.success(c?"Intern anteckning sparad":"Svar skickat")),I(!1)},U=async s=>{t&&(await n.from("support_tickets").update({status:s}).eq("id",t.id),N(l=>l&&{...l,status:s}),S.success(`Status ändrad till ${s}`))},$=s=>{k(s.content),R(!1)};if(D)return e.jsx("div",{className:"flex items-center justify-center h-64 text-muted-foreground",children:"Laddar ärende..."});if(!t)return null;const y=a&&`${a.first_name??""} ${a.last_name??""}`.trim()||"Okänd";return e.jsxs("div",{className:"space-y-4",children:[e.jsxs("div",{className:"flex items-center gap-4 flex-wrap",children:[e.jsx(o,{variant:"ghost",size:"icon",onClick:()=>u("/admin/support"),children:e.jsx(le,{className:"h-5 w-5"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("h1",{className:"text-xl font-bold text-foreground",children:[t.ticket_number,": ",t.subject]}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[y," · ",t.category]})]}),e.jsxs(Y,{value:t.status,onValueChange:U,children:[e.jsx(ee,{className:"w-[180px]",children:e.jsx(se,{})}),e.jsxs(ae,{children:[e.jsx(j,{value:"Ãppet",children:"Ãppet"}),e.jsx(j,{value:"Väntar pÃ¥ svar",children:"Väntar pÃ¥ svar"}),e.jsx(j,{value:"Besvarad",children:"Besvarad"}),e.jsx(j,{value:"Löst",children:"Löst"})]})]})]}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-3 gap-4",children:[e.jsx("div",{className:"lg:col-span-2 flex flex-col",children:e.jsx(m,{className:"flex-1 flex flex-col",children:e.jsxs(x,{className:"flex-1 flex flex-col p-0",children:[e.jsx("div",{ref:g,className:"flex-1 overflow-y-auto p-4 space-y-4 max-h-[50vh]",children:v.map(s=>e.jsx("div",{className:`flex ${s.sender_type==="staff"?"justify-end":"justify-start"}`,children:e.jsxs("div",{className:`max-w-[75%] rounded-lg p-3 text-sm ${s.is_internal?"bg-yellow-500/10 border border-yellow-500/30 text-foreground":s.sender_type==="staff"?"bg-primary/10 text-foreground":"bg-muted text-foreground"}`,children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[s.is_internal&&e.jsx(q,{className:"h-3 w-3 text-yellow-500"}),e.jsx("span",{className:"font-medium text-xs",children:s.is_internal?"Intern":s.sender_type==="staff"?s.sender_name??"Personal":y}),e.jsx("span",{className:"text-xs text-muted-foreground",children:A(new Date(s.created_at),"d MMM HH:mm",{locale:w})})]}),e.jsx("p",{className:"whitespace-pre-wrap",children:s.message})]})},s.id))}),e.jsx(ne,{}),e.jsxs("div",{className:"p-4 space-y-3",children:[e.jsx(te,{value:c?"internal":"reply",onValueChange:s=>R(s==="internal"),children:e.jsxs(re,{className:"h-8",children:[e.jsx(M,{value:"reply",className:"text-xs px-3 h-6",children:"Svara kund"}),e.jsxs(M,{value:"internal",className:"text-xs px-3 h-6",children:[e.jsx(q,{className:"h-3 w-3 mr-1"})," Intern anteckning"]})]})}),e.jsx(X,{placeholder:c?"Skriv en intern anteckning...":"Skriv ditt svar till kunden...",value:p,onChange:s=>k(s.target.value),rows:3,className:c?"border-yellow-500/30":""}),e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("div",{className:"flex gap-2 flex-wrap",children:_.slice(0,3).map(s=>e.jsxs(o,{variant:"outline",size:"sm",className:"text-xs h-7",onClick:()=>$(s),children:[e.jsx(B,{className:"h-3 w-3 mr-1"})," ",s.title]},s.id))}),e.jsxs(o,{onClick:K,disabled:!p.trim()||z,size:"sm",children:[e.jsx(ie,{className:"h-4 w-4 mr-1"}),c?"Spara anteckning":"Skicka svar"]})]})]})]})})}),e.jsxs("div",{className:"space-y-4",children:[e.jsxs(m,{children:[e.jsx(f,{className:"pb-3",children:e.jsxs(h,{className:"text-sm flex items-center gap-2",children:[e.jsx(J,{className:"h-4 w-4"})," Kundinformation"]})}),e.jsxs(x,{className:"space-y-2 text-sm",children:[e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Namn:"})," ",e.jsx("span",{className:"text-foreground",children:y})]}),(a==null?void 0:a.phone)&&e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Telefon:"})," ",e.jsx("span",{className:"text-foreground",children:a.phone})]}),(a==null?void 0:a.city)&&e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Ort:"})," ",e.jsx("span",{className:"text-foreground",children:a.city})]}),(a==null?void 0:a.street_address)&&e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Adress:"})," ",e.jsxs("span",{className:"text-foreground",children:[a.street_address,", ",a.postal_code," ",a.city]})]}),e.jsx(o,{variant:"outline",size:"sm",className:"w-full mt-2 text-xs",onClick:()=>u(`/admin/kunder/${t.user_id}`),children:"Visa kundprofil"})]})]}),e.jsxs(m,{children:[e.jsx(f,{className:"pb-3",children:e.jsxs(h,{className:"text-sm flex items-center gap-2",children:[e.jsx(ce,{className:"h-4 w-4"})," Ãrendedetaljer"]})}),e.jsxs(x,{className:"space-y-2 text-sm",children:[e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Skapad:"})," ",e.jsx("span",{className:"text-foreground",children:A(new Date(t.created_at),"d MMM yyyy HH:mm",{locale:w})})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Uppdaterad:"})," ",e.jsx("span",{className:"text-foreground",children:oe(new Date(t.updated_at),{addSuffix:!0,locale:w})})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Kategori:"})," ",e.jsx(L,{variant:"outline",className:"text-xs ml-1",children:t.category})]}),e.jsxs("div",{children:[e.jsx("span",{className:"text-muted-foreground",children:"Meddelanden:"})," ",e.jsx("span",{className:"text-foreground",children:v.length})]})]})]}),T.length>0&&e.jsxs(m,{children:[e.jsx(f,{className:"pb-3",children:e.jsxs(h,{className:"text-sm flex items-center gap-2",children:[e.jsx(de,{className:"h-4 w-4"})," Senaste ordrar"]})}),e.jsx(x,{className:"space-y-2",children:T.map(s=>e.jsxs("div",{className:"flex items-center justify-between text-sm cursor-pointer hover:bg-muted/50 rounded p-1.5 -mx-1.5",onClick:()=>u(`/admin/ordrar/${s.id}`),children:[e.jsx("span",{className:"text-foreground font-medium",children:s.order_number}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("span",{className:"text-muted-foreground",children:[s.price_sek.toLocaleStr
1ing("sv-SE")," kr"]}),e.jsx(L,{variant:"outline",className:"text-xs",children:s.status})]})]},s.id))})]}),_.length>3&&e.jsxs(m,{children:[e.jsx(f,{className:"pb-3",children:e.jsxs(h,{className:"text-sm flex items-center gap-2",children:[e.jsx(B,{className:"h-4 w-4"})," Snabbsvar"]})}),e.jsx(x,{className:"space-y-1",children:_.map(s=>e.jsx(o,{variant:"ghost",size:"sm",className:"w-full justify-start text-xs h-7",onClick:()=>$(s),children:s.title},s.id))})]})]})]})]})};export{qe as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.