import{g as R,w as j}from"./primevue-CfIk0_DY.js";import{_ as G,s as y,b as $}from"./index-BxIwIJfy.js";import{c as O}from"./imageTraitements-BoTBAXYr.js";import{w as W,n as k,o as X,a0 as Y,b as v,c as h,v as z,k as J,G as Z,F as ee,C as te,q as le,d as n,f as ae,t as f,p as A,T as se,a as r,y as oe}from"./vue-core-Di3IuVqe.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:V}){const D=T,F=V,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}),H=O($,y),b=r({});let m=null;const L=oe(()=>({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 $.post(e,{searchTerm:c.value,code_client:D.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 H.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)},q=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]&&I(t.value[o.value]);break;case"Escape":e.preventDefault(),a.value=!1,c.value="";break}},I=e=>{F("articleSelected",e),c.value="",t.value=[],a.value=!1,k(()=>{d.value&&d.value.$el&&d.value.$el.focus()})},K=()=>{d.value&&d.value.$el.focus()},g=()=>{a.value&&w()},S=e=>{p.value&&!p.value.contains(e.target)&&s.value&&!s.value.contains(e.target)&&(a.value=!1)};return W(a,e=>{e&&k(()=>{w()})}),X(()=>{window.addEventListener("resize",g),window.addEventListener("scroll",g,!0),document.addEventListener("click",S)}),Y(()=>{m&&clearTimeout(m),window.removeEventListener("resize",g),window.removeEventListener("scroll",g,!0),document.removeEventListener("click",S)}),C({focus:K}),(e,i)=>{const u=R,x=j;return v(),h("div",{class:"article-quick-search",ref_key:"container",ref:p},[z(u,{size:"small",ref_key:"searchInput",ref:d,modelValue:c.value,"onUpdate:modelValue":i[0]||(i[0]=l=>c.value=l),onInput:N,onKeydown:q,onFocus:P,onBlur:U,placeholder:T.placeholder,class:"w-full",disabled:T.disabled},null,8,["modelValue","placeholder","disabled"]),(v(),J(se,{to:"body"},[a.value&&t.value.length>0?(v(),h("div",{key:0,class:"search-results-portal",style:Z(L.value),ref_key:"dropdown",ref:s},[(v(!0),h(ee,null,te(t.value,(l,B)=>(v(),h("div",{key:l.code_art,onClick:Q=>I(l),onMouseenter:Q=>o.value=B,class:le([{selected:B===o.value},"result-item"])},[n("div",ce,[z(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,[ae(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)}}},ye=G(we,[["__scopeId","data-v-2bf0e56b"]]);export{ye as A};
