import{r as a,j as e}from"./vendor-react-Ci7aBfa0.js";import{t as c}from"./vendor-ui-X4tvFhT0.js";import{J as E,i,U as C,K as k,N as R}from"./vendor-icons-4GGNjGXS.js";const N="/api";async function F(l){const s=await fetch(`${N}/reviews?productId=${encodeURIComponent(l)}`);if(!s.ok)throw new Error("Erreur lors du chargement des avis");return s.json()}async function z(l){const s=await fetch(`${N}/reviews`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(l)});if(!s.ok){const o=await s.json().catch(()=>({error:"Erreur"}));throw new Error(o.error||"Erreur lors de l'envoi")}return s.json()}function T({productId:l}){const[s,o]=a.useState([]),[y,u]=a.useState(!0),[w,n]=a.useState(!1),[d,h]=a.useState(0),[A,g]=a.useState(0),[m,p]=a.useState(""),[f,j]=a.useState(""),[x,b]=a.useState("");a.useEffect(()=>{u(!0),F(l).then(o).catch(()=>o([])).finally(()=>u(!1))},[l]);const v=s.length>0?(s.reduce((t,r)=>t+r.rating,0)/s.length).toFixed(1):"0",S=[5,4,3,2,1].map(t=>({stars:t,count:s.filter(r=>r.rating===t).length,pct:s.length>0?Math.round(s.filter(r=>r.rating===t).length/s.length*100):0})),D=async t=>{if(t.preventDefault(),d===0){c.error("Veuillez sélectionner une note");return}if(!m.trim()||!x.trim()){c.error("Veuillez remplir tous les champs");return}try{await z({productId:l,customerName:m.trim(),rating:d,comment:x.trim(),title:f.trim()||void 0}),c.success("Avis soumis ! Il sera visible après modération."),n(!1),h(0),p(""),j(""),b("")}catch(r){c.error(r.message||"Erreur")}};return y?e.jsx("div",{className:"mt-12 pt-8 border-t border-gray-200",children:e.jsx("div",{className:"w-8 h-8 border-4 border-[#D45A00] border-t-transparent rounded-full animate-spin mx-auto"})}):e.jsxs("div",{className:"mt-12 pt-8 border-t border-gray-200",children:[e.jsxs("h3",{className:"text-2xl font-bold text-[#333333] mb-6 flex items-center gap-2",children:[e.jsx(E,{className:"w-6 h-6 text-[#D45A00]"}),"Avis clients"]}),s.length===0?e.jsxs("div",{className:"text-center py-8 bg-gray-50 rounded-xl",children:[e.jsx(i,{className:"w-12 h-12 text-gray-300 mx-auto mb-3"}),e.jsx("p",{className:"text-gray-500 mb-4",children:"Soyez le premier à donner votre avis"}),e.jsx("button",{onClick:()=>n(!0),className:"px-6 py-2.5 bg-[#D45A00] text-white rounded-lg font-medium hover:bg-[#E55A00]",children:"Donner mon avis"})]}):e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-3 gap-8",children:[e.jsx("div",{className:"lg:col-span-1",children:e.jsxs("div",{className:"bg-gray-50 rounded-xl p-6",children:[e.jsxs("div",{className:"text-center mb-4",children:[e.jsx("p",{className:"text-5xl font-extrabold text-[#333333]",children:v}),e.jsx("div",{className:"flex items-center justify-center gap-1 my-2",children:[1,2,3,4,5].map(t=>e.jsx(i,{className:`w-5 h-5 ${t<=Math.round(Number(v))?"text-yellow-400 fill-yellow-400":"text-gray-300"}`},t))}),e.jsxs("p",{className:"text-sm text-gray-500",children:[s.length," avis"]})]}),e.jsx("div",{className:"space-y-1.5",children:S.map(t=>e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:"text-xs text-gray-500 w-3",children:t.stars}),e.jsx(i,{className:"w-3 h-3 text-gray-400"}),e.jsx("div",{className:"flex-1 h-2 bg-gray-200 rounded-full overflow-hidden",children:e.jsx("div",{className:"h-full bg-yellow-400 rounded-full",style:{width:`${t.pct}%`}})}),e.jsx("span",{className:"text-xs text-gray-400 w-6 text-right",children:t.count})]},t.stars))}),e.jsx("button",{onClick:()=>n(!0),className:"w-full mt-4 py-2.5 bg-[#D45A00] text-white rounded-lg font-medium hover:bg-[#E55A00] transition-colors",children:"Donner mon avis"})]})}),e.jsx("div",{className:"lg:col-span-2 space-y-4",children:s.map(t=>e.jsxs("div",{className:"bg-white border border-gray-100 rounded-xl p-5",children:[e.jsxs("div",{className:"flex items-start justify-between mb-2",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("div",{className:"w-10 h-10 rounded-full bg-[#D45A00]/10 flex items-center justify-center",children:e.jsx(C,{className:"w-5 h-5 text-[#D45A00]"})}),e.jsxs("div",{children:[e.jsx("p",{className:"font-semibold text-[#333333] text-sm",children:t.customerName}),e.jsx("div",{className:"flex items-center gap-1",children:[1,2,3,4,5].map(r=>e.jsx(i,{className:`w-3.5 h-3.5 ${r<=t.rating?"text-yellow-400 fill-yellow-400":"text-gray-300"}`},r))})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[t.verified&&e.jsxs("span",{className:"text-xs flex items-center gap-0.5 text-green-600 bg-green-50 px-2 py-0.5 rounded-full",children:[e.jsx(k,{className:"w-3 h-3"})," Achat vérifié"]}),e.jsx("span",{className:"text-xs text-gray-400",children:new Date(t.createdAt).toLocaleDateString("fr-FR")})]})]}),t.title&&e.jsx("h4",{className:"font-bold text-[#333333] mb-1",children:t.title}),e.jsx("p",{className:"text-gray-600 text-sm leading-relaxed",children:t.comment}),e.jsx("div",{className:"mt-3 flex items-center gap-4",children:e.jsxs("span",{className:"text-xs text-gray-400 flex items-center gap-1",children:[e.jsx(R,{className:"w-3.5 h-3.5"})," Utile (",t.helpful||0,")"]})})]},t.id))})]}),w&&e.jsx("div",{className:"fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4",children:e.jsxs("div",{className:"bg-white rounded-xl shadow-2xl w-full max-w-md max-h-[90vh] overflow-y-auto",children:[e.jsxs("div",{className:"flex items-center justify-between p-4 border-b",children:[e.jsx("h3",{className:"font-bold text-[#333333]",children:"Donner mon avis"}),e.jsx("button",{onClick:()=>n(!1),className:"p-1.5 hover:bg-[#F2F2F2] rounded-lg text-gray-500",children:"✕"})]}),e.jsxs("form",{onSubmit:D,className:"p-4 space-y-4",children:[e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-[#333333] mb-2",children:"Votre note *"}),e.jsx("div",{className:"flex items-center gap-1",children:[1,2,3,4,5].map(t=>e.jsx("button",{type:"button",onClick:()=>h(t),onMouseEnter:()=>g(t),onMouseLeave:()=>g(0),className:"p-1 transition-transform hover:scale-110",children:e.jsx(i,{className:`w-8 h-8 ${t<=(A||d)?"text-yellow-400 fill-yellow-400":"text-gray-300"}`})},t))})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-[#333333] mb-1",children:"Votre nom *"}),e.jsx("input",{type:"text",value:m,onChange:t=>p(t.target.value),className:"w-full px-4 py-2.5 border rounded-lg focus:ring-2 focus:ring-[#D45A00] outline-none",placeholder:"Jean K.",required:!0})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-[#333333] mb-1",children:"Titre (optionnel)"}),e.jsx("input",{type:"text",value:f,onChange:t=>j(t.target.value),className:"w-full px-4 py-2.5 border rounded-lg focus:ring-2 focus:ring-[#D45A00] outline-none",placeholder:"Résumez votre expérience"})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium text-[#333333] mb-1",children:"Votre commentaire *"}),e.jsx("textarea",{rows:4,value:x,onChange:t=>b(t.target.value),className:"w-full px-4 py-2.5 border rounded-lg focus:ring-2 focus:ring-[#D45A00] outline-none resize-none",placeholder:"Partagez votre expérience avec ce produit...",required:!0})]}),e.jsxs("div",{className:"flex gap-3 pt-2",children:[e.jsx("button",{type:"button",onClick:()=>n(!1),className:"flex-1 py-2.5 border rounded-lg font-medium hover:bg-[#F2F2F2]",children:"Annuler"}),e.jsx("button",{type:"submit",className:"flex-1 py-2.5 bg-[#D45A00] text-white rounded-lg font-medium hover:bg-[#E55A00]",children:"Envoyer"})]})]})]})})]})}export{T as P};