import{w as j,L as k,f as q,b8 as G,o as v,c as h,a as $,M as O,i as W,G as X,F as Y,A as J,E as Z,b as n,ah as ee,h as te,B as f,d as A,b9 as le,r,e as ae}from"./vendor-BDR7yMJi.js";import{_ as se,s as y,a as z}from"./index-BSaHX2oa.js";import{c as oe}from"./imageTraitements-7eElINlT.js";const ne=["onClick","onMouseenter"],ce={class:"flex items-center gap-3"},re={class:"flex-1 min-w-0"},ie={class:"flex justify-between items-start"},ue={class:"flex-1 min-w-0"},de={class:"article-code"},ve={key:0,class:"text-gray-500 font-normal"},fe={class:"article-name"},pe={class:"flex flex-col text-right ml-2"},me={class:"article-price text-green-600"},he={class:"article-price text-blue-600"},_e={class:"article-stock text-gray-600"},we={__name:"ArticleQuickSearch",props:{placeholder:{type:String,default:"Tapez le code ou nom d'article..."},disabled:{type:Boolean,default:!1},code_client:{type:String,default:""}},emits:["articleSelected"],setup(T,{expose:C,emit:L}){const V=T,D=L,p=r(null),d=r(null),s=r(null),c=r(""),t=r([]),a=r(!1),o=r(-1),_=r({top:0,left:0,width:0}),F=oe(z,y),b=r({});let m=null;const H=ae(()=>({position:"fixed",top:`${_.value.top}px`,left:`${_.value.left}px`,width:`${_.value.width}px`,zIndex:99999})),w=()=>{if(!p.value)return;const e=p.value.getBoundingClientRect();_.value={top:e.bottom+window.scrollY,left:e.left+window.scrollX,width:e.width}},M=async()=>{if(!c.value||c.value.length<1){t.value=[],a.value=!1;return}try{const e=`${y.lienApi}/Selectarticles/${y.db}/?page=1&pageSize=100`,i=await z.post(e,{searchTerm:c.value,code_client:V.code_client});t.value=i.data||[],w(),a.value=!0,o.value=t.value.length>0?0:-1;for(const u of t.value)u.code_art&&(b.value[u.code_art]=await F.loadImage(u.code_art,"article"))}catch{t.value=[],a.value=!1}},N=()=>{m&&clearTimeout(m),c.value.length>=1?m=setTimeout(M,300):(t.value=[],a.value=!1)},P=()=>{w(),t.value.length>0&&(a.value=!0)},U=e=>{setTimeout(()=>{s.value&&s.value.contains(e.relatedTarget)||(a.value=!1)},150)},E=()=>{if(!s.value||o.value<0)return;const e=s.value.children[o.value];if(!e)return;const i=e.offsetTop,u=i+e.offsetHeight,x=s.value.scrollTop,l=x+s.value.clientHeight;i<x?s.value.scrollTop=i:u>l&&(s.value.scrollTop=u-s.value.clientHeight)},K=e=>{if(!a.value||t.value.length===0){e.key==="Escape"&&(c.value="");return}switch(e.key){case"ArrowDown":e.preventDefault(),o.value=Math.min(o.value+1,t.value.length-1),k(()=>E());break;case"ArrowUp":e.preventDefault(),o.value=Math.max(o.value-1,0),k(()=>E());break;case"Enter":e.preventDefault(),o.value>=0&&t.value[o.value]&&B(t.value[o.value]);break;case"Escape":e.preventDefault(),a.value=!1,c.value="";break}},B=e=>{D("articleSelected",e),c.value="",t.value=[],a.value=!1,k(()=>{d.value&&d.value.$el&&d.value.$el.focus()})},Q=()=>{d.value&&d.value.$el.focus()},g=()=>{a.value&&w()},I=e=>{p.value&&!p.value.contains(e.target)&&s.value&&!s.value.contains(e.target)&&(a.value=!1)};return j(a,e=>{e&&k(()=>{w()})}),q(()=>{window.addEventListener("resize",g),window.addEventListener("scroll",g,!0),document.addEventListener("click",I)}),G(()=>{m&&clearTimeout(m),window.removeEventListener("resize",g),window.removeEventListener("scroll",g,!0),document.removeEventListener("click",I)}),C({focus:Q}),(e,i)=>{const u=O,x=ee;return v(),h("div",{class:"article-quick-search",ref_key:"container",ref:p},[$(u,{size:"small",ref_key:"searchInput",ref:d,modelValue:c.value,"onUpdate:modelValue":i[0]||(i[0]=l=>c.value=l),onInput:N,onKeydown:K,onFocus:P,onBlur:U,placeholder:T.placeholder,class:"w-full",disabled:T.disabled},null,8,["modelValue","placeholder","disabled"]),(v(),W(le,{to:"body"},[a.value&&t.value.length>0?(v(),h("div",{key:0,class:"search-results-portal",style:X(H.value),ref_key:"dropdown",ref:s},[(v(!0),h(Y,null,J(t.value,(l,S)=>(v(),h("div",{key:l.code_art,onClick:R=>B(l),onMouseenter:R=>o.value=S,class:Z([{selected:S===o.value},"result-item"])},[n("div",ce,[$(x,{image:b.value[l.code_art]||"/images/article.png",size:"large",shape:"circle",class:"shrink-0 bg-surface"},null,8,["image"]),n("div",re,[n("div",ie,[n("div",ue,[n("div",de,[te(f(l.code_art),1),l.reference?(v(),h("span",ve," ("+f(l.reference)+")",1)):A("",!0)]),n("div",fe,f(l.libelle),1)]),n("div",pe,[n("div",me,f(l.Prix_vente_HT?.toFixed(2))+" HT ",1),n("div",he,f(l.Prix_vente_TTC?.toFixed(2))+" TTC ",1),n("div",_e,f(l.stock_total?.toFixed(2))+" en stock ",1)])])])])],42,ne))),128))],4)):A("",!0)]))],512)}}},Te=se(we,[["__scopeId","data-v-ecd9ca51"]]);export{Te as A};
