import{r as n,f as Y,o as w,c as y,b as a,a as r,z as Z,av as ee,M as se,d as te,an as le,ap as ae,q as ie,F as oe,ar as ne,e as S}from"./vendor-BDR7yMJi.js";import{u as re,a as g,s as t,S as _}from"./index-BSaHX2oa.js";import{_ as ue}from"./DataGrid-DbeHIyBR.js";import"./useAgGridConfig-DxO2r97K.js";const ce={key:0},de={class:"flex flex-col gap-3 sm:flex-row sm:justify-between sm:items-center mb-3"},pe={class:"flex flex-col gap-2 sm:flex-row sm:gap-3"},fe={key:0,class:"flex flex-wrap gap-2 sm:gap-3"},me={key:1,class:"flex flex-wrap gap-2 sm:gap-3"},ve={class:"w-full bg-white p-3"},ge={class:"grid grid-cols-1 lg:grid-cols-2 gap-4"},be={class:"flex flex-col gap-2"},xe={class:"field"},we={class:"field"},ye={class:"flex flex-col gap-3 sm:flex-row sm:justify-between sm:items-center mb-3"},_e={class:"flex flex-col gap-2 sm:flex-row sm:gap-3 lg:w-1/2 lg:justify-start"},$e={class:"flex flex-wrap gap-2 sm:gap-3"},he={style:{height:"calc(100vh - 200px)"}},je={__name:"Unite",props:["ndoc"],setup(R){const{confirmAction:U,showSuccess:$,showError:c,showInfo:j,showWarn:L}=re(),d=R,M=n(d.ndoc),l=n({}),h=n([]),p=n(!1),A=n(null),f=n(!1),m=n(!1),x=n(!1),V=n(""),T=n(""),k=n(),v=n(),F=n([]),i=ne({lecture:!1,ajout:!1,modif:!1,supp:!1,devalidation:!1,validation:!1}),z=({ndoc:s,rowIndex:e})=>{M.value=s,A.value=e,C()},B=({selectedRows:s})=>{F.value=s},G=s=>{v.value=s.api},N=S(()=>[{label:"Supprimer",icon:"pi pi-trash",command:K,disabled:!i.supp},{label:"Ajouter",icon:"pi pi-plus",command:D,disabled:!i.ajout},{label:"Définir par défaut",icon:"pi pi-star",command:Q,disabled:!i.modif}]),P=S(()=>[{label:"Exporter vers Excel",icon:"pi pi-file-excel",command:J}]);Y(async()=>{await O(),d.ndoc?d.ndoc==="9999"?(f.value=!0,m.value=!0,p.value=!0,x.value=!0,l.value={}):(C(),f.value=!0,m.value=!0,setTimeout(()=>{document.getElementById("inputFocus").focus()},200)):b()});const O=async()=>{try{const e=(await g.post(`${t.lienApi}/verifierDroit/${t.state.dbCommun6}`,{id_utilisateur:t.userId,id_page:226,mail:t.state.userCloud.mail})).data;e&&Object.keys(e).length>0&&(i.lecture=e.lecture,i.ajout=e.ajout,i.modif=e.modif,i.supp=e.supp,i.devalidation=e.devalidation,i.validation=e.validation),i.lecture||(L("Vous n'avez pas le droit, veuillez consulter l'administrateur"),_("unite"))}catch(s){c(`Une erreur s'est produite lors de la vérification des droits. ${s}`)}},b=async()=>{t.state.afficher_in_progress=!0,p.value=!1,V.value=T.value;try{const s=await g.post(`${t.lienApi}/unite/${t.db}`,{condition:V.value});h.value=s.data}catch(s){c("Une erreur est survenue : "+(s.response?.data?.message||s.message))}finally{t.state.afficher_in_progress=!1}},D=async()=>{l.value={},p.value=!0,f.value=!0,m.value=!0,x.value=!0,setTimeout(()=>{document.getElementById("inputFocus").focus()},200)},q=async()=>{f.value=!0,m.value=!0,setTimeout(()=>{document.getElementById("inputFocus").focus()},200)},W=async()=>{if(!l.value.unite||l.value.unite.trim()==="")return c("Le code est obligatoire"),!1;if(!l.value.Libelle||l.value.Libelle.trim()==="")return c("Le libellé est obligatoire"),!1;try{t.state.afficher_in_progress=!0;const s=`${t.lienApi}/enregistrerUnite/${t.db}/${l.value.unite}`;return await g.post(s,l.value,{headers:{"Content-Type":"application/json"}}),x.value=!1,f.value=!1,m.value=!1,d.ndoc||(p.value=!1,b()),$("Enregistrement réussi"),!0}catch(s){const e=s.response?.data?.message||s.response?.data?.error||s.message||"Une erreur est survenue lors de l'enregistrement";return c("Erreur d'enregistrement : "+e),!1}finally{t.state.afficher_in_progress=!1}},H=()=>{f.value=!1,m.value=!1,d.ndoc&&d.ndoc!=="9999"?C():d.ndoc==="9999"&&_("Unites")},J=()=>{if(!k.value){j("Grille non initialisée");return}if(h.value.length===0){j("Aucune donnée à exporter");return}const e=new Date().toISOString().slice(0,19).replace(/[-:]/g,"").replace("T","_");k.value.exportToExcel(`Liste des unites_${t.state.enreg_societe.Nom}_${e}.xlsx`)},C=async()=>{try{const s=await g.post(`${t.lienApi}/ununite/${t.db}/${M.value}`);l.value=s.data,p.value=!0}catch(s){c("Une erreur est survenue : "+(s.response?.data?.message||s.message))}},K=async()=>{if(await U({message:"Voulez-vous vraiment supprimer cette unité ?"}))try{const e=`${t.lienApi}/supprimerUnite/${t.db}/${l.value.unite}`;await g.post(e),$("Suppression réussie"),p.value=!1,f.value=!1,b()}catch(e){c("Une erreur est survenue : "+(e.response?.data?.message||e.message))}},Q=async()=>{if(await U({message:`Voulez-vous définir l'unité "${l.value.unite}" par défaut ?`}))try{const e=`${t.lienApi}/definirUniteParDefaut/${t.db}/${l.value.unite}`;await g.post(e),$("L'unité a été définie par défaut avec succès"),b()}catch(e){const o=e.response?.data?.message||e.message;c(`Une erreur est survenue :
  ${o}`)}},X=()=>{d.ndoc?_("Unites"):(b(),A.value!=null&&v.value&&setTimeout(()=>{const s=v.value.getDisplayedRowCount();if(s>0){const e=Math.min(A.value,s-1),o=v.value.getDisplayedRowAtIndex(e);o&&(v.value.deselectAll(),o.setSelected(!0),v.value.ensureNodeVisible(o,"middle"))}},100))};return(s,e)=>{const o=Z,E=ee,I=se;return w(),y(oe,null,[p.value&&i.lecture?(w(),y("div",ce,[a("div",de,[a("div",pe,[m.value?(w(),y("div",me,[r(o,{icon:"pi pi-save",label:"Enregistrer",text:"",onClick:e[2]||(e[2]=u=>W())}),r(o,{icon:"pi pi-times",label:"Annuler",text:"",onClick:e[3]||(e[3]=u=>H())})])):(w(),y("div",fe,[r(o,{icon:"pi pi-pencil",label:"Modifier",text:"",onClick:q,disabled:!i.modif},null,8,["disabled"]),r(E,{ref:"actionsMenu",popup:!0,model:N.value},null,8,["model"]),r(o,{icon:"pi pi-cog",label:"Actions",text:"",onClick:e[0]||(e[0]=u=>s.$refs.actionsMenu.toggle(u))}),r(o,{icon:"pi pi-arrow-left",label:"Retour",text:"",onClick:e[1]||(e[1]=u=>X())})]))])]),a("div",ve,[a("div",ge,[a("div",be,[a("div",xe,[e[9]||(e[9]=a("label",{for:"code_unite",class:"block mb-1"},"Code",-1)),r(I,{size:"small",id:"code_unite",modelValue:l.value.unite,"onUpdate:modelValue":e[4]||(e[4]=u=>l.value.unite=u),disabled:!x.value,class:"w-full",maxlength:"10"},null,8,["modelValue","disabled"])]),a("div",we,[e[10]||(e[10]=a("label",{for:"inputFocus",class:"block mb-1"},"Libellé",-1)),r(I,{size:"small",id:"inputFocus",modelValue:l.value.Libelle,"onUpdate:modelValue":e[5]||(e[5]=u=>l.value.Libelle=u),disabled:!f.value,class:"w-full",maxlength:"20"},null,8,["modelValue","disabled"])])]),e[11]||(e[11]=a("div",{class:"flex flex-col gap-2"},null,-1))])])])):te("",!0),le(a("div",null,[a("div",ye,[a("div",_e,[a("div",$e,[r(o,{icon:"pi pi-plus",label:"Ajouter",text:"",onClick:e[6]||(e[6]=u=>D()),disabled:!i.ajout},null,8,["disabled"]),r(E,{ref:"listeActionsMenu",popup:!0,model:P.value},null,8,["model"]),r(o,{icon:"pi pi-cog",label:"Actions",text:"",onClick:e[7]||(e[7]=u=>s.$refs.listeActionsMenu.toggle(u))}),r(o,{icon:"pi pi-times",label:"Fermer",text:"",onClick:e[8]||(e[8]=u=>ie(_)("unite"))})])]),e[12]||(e[12]=a("div",{class:"flex items-center gap-3 lg:w-1/2 lg:justify-end whitespace-nowrap"},null,-1))]),a("div",he,[r(ue,{ref_key:"dataGrid",ref:k,"excel-file-name":"Unites","excel-sheet-name":"Unites","row-data":h.value,"document-number-fields":["ndoc"],"excluded-columns":["ndoc"],"column-widths":{unite:150,libelle:200},onRowClicked:z,onSelectionChanged:B,onGridReady:G},null,8,["row-data"])])],512),[[ae,!p.value&&i.lecture]])],64)}}};export{je as default};
