import{_ as Ie}from"./ComboDynamique-CzrtYPe1.js";import{r as n,f as we,o as f,c as P,b as i,i as I,z as he,d as y,j as T,a as r,av as $e,M as Be,an as _e,ap as Ce,q as Le,F as je,ar as ke,e as k}from"./vendor-0kGcmQpG.js";import{u as Me,a as m,s as a,S as M}from"./index-Bn9WoKhP.js";import{_ as S}from"./DataGrid-tb6QVwpZ.js";import"./useAgGridConfig-DjXMc6wb.js";const Ae={key:0},Re={class:"w-full pb-3 flex flex-row items-center justify-start gap-3 pt-2 ml-2"},De={class:"w-full bg-white p-4"},Ee={class:"grid grid-cols-1 md:grid-cols-2 gap-4"},Ve={class:"flex flex-col gap-3"},Pe={class:"field"},Te={class:"p-float-label"},Se={class:"field"},Ue={class:"flex gap-6 w-full"},Ge={style:"height: calc(100vh - 350px)",class:"mt-4"},ze={class:"w-full flex flex-row items-center gap-3 p-2"},Ne={style:{height:"calc(100vh - 180px)"}},Je={__name:"Budget",props:{ndoc:{type:String,default:""}},setup(U){const{showError:b,showInfo:$,showWarn:w}=Me(),A=U,B=n(),G=n(),x=n(),z=n(),N=n([]),F=n([]),d=n({}),q=n(null),p=n(null),_=n([]),l=n([]),g=n(!1),C=n(null),c=n(!1),v=n(!1),O=n(!1),H=n(!1),W=n(!1),J=n(""),K=n(""),h=n(A.ndoc||""),R=n(null),D=n(null),Q={IdLigne:0,IdBudget:1,IdCategorie:0,MontantPrevu:0,MontantEngage:0,MontantRealise:0,Periode:""},u=ke({lecture:!1,ajout:!1,modif:!1,supp:!1,devalidation:!1,validation:!1}),X=k(()=>["MontantPrevu","MontantEngage","MontantRealise"]),Y=({event:t,ndoc:e})=>{h.value=e,C.value=rowIndex,j()},Z=({selectedRows:t})=>{N.value=t,q.value=t.length>0?t[0]:null},ee=t=>{x.value=t.api},te=({event:t,row:e})=>{},le=({selectedRows:t})=>{F.value=t,p.value=t.length>0?t[0]:null},ne=t=>{z.value=t.api},ae=[{label:"Ajouter",icon:"pi pi-plus-circle",command:()=>E(),disabled:k(()=>!u.ajout)},{label:"Supprimer",icon:"pi pi-trash",command:()=>ve(),disabled:k(()=>!u.supp)}],se=[{label:"Exporter vers Excel",icon:"pi pi-file-excel",command:()=>ce()},{label:"Imprimer la liste",icon:"pi pi-print",command:()=>pe()}];we(async()=>{await oe(),h.value?(j(),c.value=!0,v.value=!0,setTimeout(()=>{document.getElementById("projetInput")?.focus()},200)):L()});const oe=async()=>{try{const e=(await m.post(`${a.lienApi}/verifierDroit/${a.state.dbCommun6}`,{id_utilisateur:a.userId,id_page:902,mail:a.state.userCloud.mail})).data;e&&Object.keys(e).length>0&&(u.lecture=e.lecture,u.ajout=e.ajout,u.modif=e.modif,u.supp=e.supp,u.devalidation=e.devalidation,u.validation=e.validation),u.lecture||(w("Vous n'avez pas le droit, veuillez consulter l'administrateur"),M("Budget"))}catch(t){b("Une erreur s'est produite lors de la vérification des droits."+t)}},L=async()=>{a.state.afficher_in_progress=!0;try{g.value=!1,J.value=K.value;const t=await m.post(`${a.lienApi}/budgets/${a.db}`,{},{headers:{"Content-Type":"application/json"}});_.value=t.data}catch(t){b(`Une erreur est survenue lors de l'affichage des données.
 ${t}`)}finally{a.state.afficher_in_progress=!1}},E=async()=>{try{const t=await m.post(`${a.lienApi}/ajouterEtRenvoyerBudget/${a.db}`);d.value=t.data,l.value=[],g.value=!0,c.value=!0,v.value=!0,setTimeout(()=>{document.getElementById("projetInput")?.focus()},200)}catch(t){b(`Une erreur est survenue lors de l'ajout du budget. 
 ${t}`)}finally{a.state.afficher_in_progress=!1}},ie=async()=>{c.value=!0,v.value=!0,setTimeout(()=>{document.getElementById("projetInput")?.focus()},200)},ue=async()=>{try{const t=`${a.lienApi}/enregistrerBudget/${a.db}/${d.value.IdBudget}`;await m.post(t,d.value,{headers:{"Content-Type":"application/json"}});const e=`${a.lienApi}/enregistrerLignesBudget/${a.db}/${d.value.IdBudget}`;await m.post(e,l.value,{headers:{"Content-Type":"application/json"}}),c.value=!1,v.value=!1}catch(t){const e=t.response?.data?.message||t.message;b(`Une erreur est survenue : 
 ${e}`)}finally{a.state.afficher_in_progress=!1}},re=()=>{c.value=!1,v.value=!1,h.value&&j()},de=()=>{A.ndoc?M("Budget"):(g.value=!1,L(),C.value!=null&&x.value&&setTimeout(()=>{const t=x.value.getDisplayedRowCount();if(t>0){const e=Math.min(C.value,t-1),s=x.value.getDisplayedRowAtIndex(e);s&&(x.value.deselectAll(),s.setSelected(!0),x.value.ensureNodeVisible(s,"middle"))}},100))},ce=()=>{if(!B.value){$("Grille non initialisée");return}if(_.value.length===0){$("Aucune donnée à exporter");return}const e=new Date().toISOString().slice(0,19).replace(/[-:]/g,"").replace("T","_");B.value.exportToExcel(`Liste des budgets_${a.state.enreg_societe.Nom}_${e}.xlsx`)},pe=()=>{W.value=!0},j=async()=>{try{l.value=[],p.value=null;const t=await m.post(`${a.lienApi}/unbudget/${a.db}/${h.value}`);d.value=t.data,g.value=!0}catch(t){b(`Une erreur est survenue lors de l'affichage des détails du budget. 
 ${t}`)}finally{a.state.afficher_in_progress=!1}},ve=async()=>{try{const t=`${a.lienApi}/supprimerBudgets/${a.db}/${d.value.IdBudget}`;await m.post(t),g.value=!1,c.value=!1,O.value=!1,L()}catch(t){const e=t.response?.data?.message||t.message;b(`Une erreur est survenue : ${e}`)}},ge=t=>{const e={...Q,IdLigne:l.value.length>0?Math.max(...l.value.map(s=>s.IdLigne))+1:1,IdBudget:d.value.IdBudget};l.value.push(e),p.value=e,$("Une nouvelle ligne a été ajoutée. Veuillez remplir les informations.")},fe=()=>{if(!p.value){w("Veuillez sélectionner une ligne à supprimer.");return}const t=l.value.findIndex(e=>e.IdLigne===p.value.IdLigne);t!==-1&&(l.value.splice(t,1),p.value=null,H.value=!1)},me=()=>{if(!p.value){w("Veuillez sélectionner une ligne à déplacer.");return}const t=l.value.findIndex(e=>e.IdLigne===p.value.IdLigne);if(t>0){const e=l.value[t];l.value[t]=l.value[t-1],l.value[t-1]=e,l.value=[...l.value]}},be=()=>{if(!p.value){w("Veuillez sélectionner une ligne à déplacer.");return}const t=l.value.findIndex(e=>e.IdLigne===p.value.IdLigne);if(t<l.value.length-1){const e=l.value[t];l.value[t]=l.value[t+1],l.value[t+1]=e,l.value=[...l.value]}};return(t,e)=>{const s=he,V=$e,xe=Be,ye=Ie;return f(),P(je,null,[g.value&&u.lecture?(f(),P("div",Ae,[i("div",Re,[v.value?y("",!0):(f(),I(s,{key:0,icon:"pi pi-pencil",label:"Modifier",text:"",onClick:e[0]||(e[0]=o=>ie()),disabled:!u.modif},null,8,["disabled"])),v.value?y("",!0):(f(),I(V,{key:1,ref_key:"actionsMenu",ref:R,model:ae,popup:!0},{button:T(()=>[r(s,{icon:"pi pi-cog",label:"Actions",onClick:e[1]||(e[1]=o=>R.value.toggle(o))})]),_:1},512)),v.value?y("",!0):(f(),I(s,{key:2,icon:"pi pi-arrow-left",label:"Retour",text:"",onClick:de})),v.value?(f(),I(s,{key:3,icon:"pi pi-save",label:"Enregistrer",text:"",onClick:e[2]||(e[2]=o=>ue())})):y("",!0),v.value?(f(),I(s,{key:4,icon:"pi pi-times",label:"Annuler",text:"",onClick:e[3]||(e[3]=o=>re())})):y("",!0)]),i("div",De,[i("div",Ee,[i("div",Ve,[i("div",Pe,[i("span",Te,[e[13]||(e[13]=i("label",{for:"idBudget"},"Numéro",-1)),r(xe,{size:"small",id:"idBudget",modelValue:d.value.IdBudget,"onUpdate:modelValue":e[4]||(e[4]=o=>d.value.IdBudget=o),class:"w-full",disabled:""},null,8,["modelValue"])])]),i("div",Se,[e[14]||(e[14]=i("label",{for:"projetInput"},"Projets",-1)),r(ye,{id:"projetInput",modelValue:d.value.IdProjet,"onUpdate:modelValue":e[5]||(e[5]=o=>d.value.IdProjet=o),placeholder:"Projet",table:"Projets",enabled:c.value,class:"w-full"},null,8,["modelValue","enabled"])])]),e[15]||(e[15]=i("div",null,null,-1))])]),e[16]||(e[16]=i("div",{class:"h-2"},null,-1)),i("div",Ue,[r(s,{label:"",text:"",icon:"pi pi-plus-circle",onClick:e[6]||(e[6]=o=>ge()),disabled:!c.value,title:"Ajouter une ligne",style:{color:"#10b981 !important"}},null,8,["disabled"]),r(s,{label:"",text:"",icon:"pi pi-trash",onClick:e[7]||(e[7]=o=>fe()),disabled:!c.value,title:"Supprimer la ligne",style:{color:"#ef4444 !important"}},null,8,["disabled"]),r(s,{label:"",text:"",icon:"pi pi-arrow-up",onClick:e[8]||(e[8]=o=>me()),disabled:!c.value,title:"Déplacer vers le haut",style:{color:"#6366f1 !important"}},null,8,["disabled"]),r(s,{label:"",text:"",icon:"pi pi-arrow-down",onClick:e[9]||(e[9]=o=>be()),disabled:!c.value,title:"Déplacer vers le bas",style:{color:"#a5b4fc !important"}},null,8,["disabled"])]),i("div",Ge,[r(S,{ref_key:"dataGridLignes",ref:G,"excel-file-name":"Budgets","excel-sheet-name":"Budgets","row-data":l.value,"document-number-fields":["ndoc"],"excluded-columns":["ndoc"],"column-widths":{IdLigne:80,IdCategorie:200,MontantPrevu:150,MontantEngage:150,MontantRealise:150,Periode:120},"enable-totals":!0,"show-checkbox":!1,"numeric-columns":X.value,"totals-columns":["MontantPrevu","MontantEngage","MontantRealise"],onRowClicked:te,onSelectionChanged:le,onGridReady:ne},null,8,["row-data","numeric-columns"])])])):y("",!0),_e(i("div",null,[i("div",ze,[r(s,{icon:"pi pi-plus-circle",label:"Ajouter",text:"",onClick:e[10]||(e[10]=o=>E()),disabled:!u.ajout},null,8,["disabled"]),r(V,{ref_key:"listeMenu",ref:D,model:se,popup:!0},{button:T(()=>[r(s,{icon:"pi pi-cog",label:"Actions",text:"",onClick:e[11]||(e[11]=o=>D.value.toggle(o))})]),_:1},512),r(s,{icon:"pi pi-times",label:"Fermer",text:"",onClick:e[12]||(e[12]=o=>Le(M)("Budget"))})]),i("div",Ne,[r(S,{ref_key:"dataGridListe",ref:B,"excel-file-name":"Budgets","excel-sheet-name":"Budgets","row-data":_.value,"document-number-fields":["ndoc"],"excluded-columns":["ndoc"],"column-widths":{IdBudget:150,CategoriesBudget:250,projet:250,utilisateur:200},"enable-totals":!1,"numeric-columns":[],onRowClicked:Y,onSelectionChanged:Z,onGridReady:ee},null,8,["row-data"])])],512),[[Ce,!g.value&&u.lecture]])],64)}}};export{Je as default};
