import{f as G,g as T,o as U,c as M,b as f,a as h,M as F,z as H,G as O,d as q,r as s,e as D}from"./vendor-BDR7yMJi.js";import{u as K,s as r,a as L}from"./index-BSaHX2oa.js";const P={class:"flex flex-col p-4"},W={class:"mb-3"},X={class:"mb-3",style:{height:"220px"}},Y={class:"flex gap-2 justify-end"},Z={__name:"SelectCompte",props:{show:Boolean,modifierLigne:Boolean,searchvalue:String,position:Object},emits:["close","ajouter","annuler"],setup(R,{emit:$}){const{showError:g}=K(),c=$,S=s(!1),d=R,w=D({get(){return d.show},set(e){e||c("close")}}),y=s([]),o=s([]),B=s(null),a=s(null),b=D(()=>({left:`${d.position?.X||0}px`,top:`${d.position?.Y||0}px`})),i=s(""),v=s(null),l=s(0),C=s(!0),A=s([{field:"Code",headerName:"Code",width:120,sortable:!0},{field:"Libelle",headerName:"Libellé",flex:1,sortable:!0}]),I=s({resizable:!0,suppressHeaderMenuButton:!0});G(async()=>{i.value=d.searchvalue,await z(),await x(),C.value&&setTimeout(()=>{const e=document.getElementById("Recherche");e&&(e.focus(),e.select()),C.value=!1},50)});const N=e=>{a.value=e.api,o.value.length>0&&a.value.getRowNode(l.value)?.setSelected(!0)},j=()=>{const e=a.value?.getSelectedNodes();e&&e.length>0&&(v.value=e[0].data,l.value=e[0].rowIndex)},z=async()=>{r.state.afficher_in_progress=!0;try{const e=await L.post(`${r.lienApi}/SelectCompte/${r.db}`);y.value=e.data}catch(e){g(`Une erreur est survenue lors de la récupération des comptes ${e}`)}finally{r.state.afficher_in_progress=!1}},x=async()=>{o.value=y.value.filter(e=>{const t=i.value.toLowerCase(),u=(e.Code||"").toLowerCase(),m=(e.Libelle||"").toLowerCase();let p=u.startsWith(t),n=m.includes(t);return p||n}),_()},_=()=>{setTimeout(()=>{o.value.length>0&&(v.value=o.value[l.value]);const e=document.getElementById("Recherche");e&&e.focus()},50)},k=e=>{const t=e?.data||v.value;if(!t)return;const u=[t.Code];c("ajouter",u),w.value=!1},E=e=>{if(e.key==="ArrowUp"&&(l.value===0?l.value=0:l.value--),e.key==="ArrowDown"&&(l.value===o.value.length-1?l.value=o.value.length-1:l.value++),e.key==="Enter"){k();return}if(e.key==="Escape"){c("annuler");return}if(o.value.length>0&&a.value){v.value=o.value[l.value],a.value.deselectAll();const t=a.value.getRowNode(l.value);t&&(t.setSelected(!0),a.value.ensureIndexVisible(l.value,"middle"))}_()},V=async()=>{if(o.value.length===0)try{await L.post(`${r.lienApi}/AjouterUnCompte/${r.db}`,{id:i.value},{headers:{"Content-Type":"application/json"}})}catch(e){const t=e.response?.data?.message||e.message;g(`Une erreur est survenue :  ${t}`)}S.value=!0};return(e,t)=>{const u=F,m=T("ag-grid-vue"),p=H;return w.value?(U(),M("div",{key:0,style:O({position:"fixed",left:b.value.left,top:b.value.top,width:"600px",height:"auto",zIndex:9999,backgroundColor:"white",border:"1px solid #d1d5db",borderRadius:"8px",boxShadow:"0 10px 25px rgba(0, 0, 0, 0.15)"}),onKeyup:E},[f("div",P,[f("div",W,[h(u,{size:"small",id:"Recherche",modelValue:i.value,"onUpdate:modelValue":t[0]||(t[0]=n=>i.value=n),onInput:t[1]||(t[1]=n=>x()),placeholder:"Rechercher un compte...",class:"w-full"},null,8,["modelValue"])]),f("div",X,[h(m,{ref_key:"agGrid",ref:B,class:"ag-theme-quartz",style:{height:"100%",width:"100%"},rowData:o.value,columnDefs:A.value,defaultColDef:I.value,rowSelection:{mode:"singleRow",checkboxes:!1},rowHeight:30,onRowClicked:k,onSelectionChanged:j,onGridReady:N},null,8,["rowData","columnDefs","defaultColDef"])]),f("div",Y,[h(p,{disabled:o.value.length>0,severity:o.value.length>0?"secondary":"primary",onClick:t[2]||(t[2]=n=>V()),label:"Ajouter",size:"small"},null,8,["disabled","severity"]),h(p,{severity:"secondary",outlined:"",onClick:t[3]||(t[3]=n=>c("annuler")),label:"Annuler",size:"small"})])])],36)):q("",!0)}}};export{Z as _};
