import{b as de,y as me,K as pe,f as fe}from"./primevue-CfIk0_DY.js";import{C as ve}from"./ComboDynamique-BzWI4ISP.js";import{_ as be,u as ge,s as i,S as M,o as _,b as $}from"./index-BxIwIJfy.js";import{a as n,o as he,h as F,b as d,c as m,d as l,v as r,k as ye,a7 as b,p as D,t as p,G as _e,l as I,F as T,C as xe,y as c}from"./vue-core-Di3IuVqe.js";import"./cjs-helpers-psZwOBky.js";import"./vue-cal-DKMVGERa.js";import"./moment-BeHQNF8T.js";const je={class:"flex min-w-full flex-col lg:flex-row lg:items-center lg:justify-between gap-2 p-1"},we={class:"btns flex items-center gap-2 py-2 flex-wrap"},Ae={class:"flex items-center gap-3 flex-wrap"},ke={key:0,style:{width:"190px"}},Ce={class:"whitespace-nowrap",style:{color:"#64748b"}},$e={key:0,class:"kpi-row"},De={class:"kpi-card"},Re={class:"kpi-value"},Ne={class:"kpi-card"},Oe={class:"kpi-value"},Ve={class:"kpi-card"},Me={class:"kpi-card"},Fe={class:"kpi-value"},Ie={style:"height:calc(100vh - 300px)",class:"mt-2"},Te={key:0,style:{height:"100%",width:"100%"}},ze={key:1,class:"ag-theme-quartz",style:{height:"100%",width:"100%"}},Se={class:"grid grid-cols-2 gap-3"},qe={class:"block mb-1"},Ge={__name:"ObjectifsVsRealise",props:["masquerFermer"],setup(z){const{showError:x,showWarn:S,showSuccess:q}=ge(),G=z,B=n(),g=n(),j=n([]),h=n(!1),f=n(i.state.annee_fiscale),w=n(0),E=c(()=>!!i.state.enreg_option_comm?.numerotation_par_agence),R=c(()=>i.state.userCommun6?.id_agence||0),U=c(()=>!!i.state.userCommun6?.Access_Agence),J=c(()=>R.value>0&&!U.value),K=c(()=>E.value),L=["Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre"],P=["Jan","Fév","Mar","Avr","Mai","Juin","Juil","Août","Sep","Oct","Nov","Déc"],N=e=>e==null?"#64748b":e>=100?"#16a34a":e>=75?"#ca8a04":"#dc2626",u=c(()=>{const e=new Map;for(const s of j.value){let a=e.get(s.IDcommercial);a||(a={IDcommercial:s.IDcommercial,Commercial:s.Commercial,objectif:0,realise:0,mois:{}},e.set(s.IDcommercial,a)),a.objectif+=Number(s.Objectif)||0,a.realise+=Number(s.Realise)||0,a.mois[s.Mois]={objectif:Number(s.Objectif)||0,realise:Number(s.Realise)||0}}const t=[...e.values()];return t.forEach(s=>{s.ecart=s.realise-s.objectif,s.pct=s.objectif>0?s.realise/s.objectif*100:null}),t.sort((s,a)=>a.realise-s.realise),t}),O=c(()=>{if(!u.value.length)return[];const e=u.value.reduce((t,s)=>({objectif:t.objectif+s.objectif,realise:t.realise+s.realise}),{objectif:0,realise:0});return[{Commercial:"TOTAL",objectif:e.objectif,realise:e.realise,ecart:e.realise-e.objectif,pct:e.objectif>0?e.realise/e.objectif*100:null}]}),v=c(()=>{const e=O.value[0]||{objectif:0,realise:0,pct:null},t=u.value.filter(s=>s.pct!=null&&s.pct>=100).length;return{objectif:e.objectif,realise:e.realise,pct:e.pct,nbOk:t}}),H={sortable:!0,filter:!0,resizable:!0,suppressHeaderMenuButton:!0},k=(e,t=!1)=>{const s=Number(e.value)||0;let a=e.node?.rowPinned?"font-weight:700;":"";return t&&(a+=s<0?"color:#dc2626;":s>0?"color:#16a34a;":""),`<span style="${a}">${_(s)}</span>`},W=n([{field:"Commercial",headerName:"Commercial",flex:1,minWidth:220},{field:"objectif",headerName:"Objectif",width:150,type:"rightAligned",cellRenderer:e=>k(e)},{field:"realise",headerName:"Réalisé",width:150,type:"rightAligned",cellRenderer:e=>k(e)},{field:"ecart",headerName:"Écart",width:150,type:"rightAligned",cellRenderer:e=>k(e,!0)},{field:"pct",headerName:"% Atteinte",width:220,cellRenderer:e=>{const t=e.value;if(t==null)return'<span style="color:#94a3b8">—</span>';const s=Math.max(0,Math.min(100,t)),a=N(t);return`<div style="display:flex;align-items:center;gap:6px;">
      <div style="flex:1;min-width:40px;background:#e5e7eb;border-radius:6px;height:10px;overflow:hidden;">
        <div style="width:${s}%;height:100%;background:${a};"></div>
      </div>
      <span style="min-width:56px;text-align:right;font-weight:700;color:${a};">${_(t)} %</span>
    </div>`}}]),Y=e=>{g.value=e.api},Q=()=>{g.value&&g.value.sizeColumnsToFit()},X=({value:e})=>new Intl.NumberFormat("fr-FR",{maximumFractionDigits:0}).format(e),Z=c(()=>{const e=Array(12).fill(0),t=Array(12).fill(0);for(const s of j.value)e[s.Mois-1]+=Number(s.Objectif)||0,t[s.Mois-1]+=Number(s.Realise)||0;return P.map((s,a)=>({mois:s,realise:t[a],objectif:e[a]}))}),ee=c(()=>({data:Z.value,title:{text:`Réalisé vs Objectif — ${f.value}`},series:[{type:"bar",xKey:"mois",yKey:"realise",yName:"Réalisé",fill:"#1675C7"},{type:"line",xKey:"mois",yKey:"objectif",yName:"Objectif",stroke:"#ef4444",marker:{fill:"#ef4444",stroke:"#ef4444"}}],axes:[{type:"category",position:"bottom"},{type:"number",position:"left",label:{formatter:X}}]})),y=n(!1),A=n(null),C=n([]),te=e=>{if(e.node?.rowPinned)return;const t=e.data;t?.IDcommercial&&(A.value=t,C.value=Array.from({length:12},(s,a)=>({mois:a+1,objectif:t.mois?.[a+1]?.objectif||0})),y.value=!0)},se=async()=>{if(A.value){i.state.afficher_in_progress=!0;try{for(const e of C.value)await $.post(`${i.lienApi}/enregistrerObjectifCommercial/${i.db}`,{IDcommercial:A.value.IDcommercial,Annee:f.value,mois:e.mois,IDfamille:0,Objectif:e.objectif});q("Objectifs enregistrés"),y.value=!1,await V()}catch(e){x(`Erreur lors de l'enregistrement.
 ${e}`)}finally{i.state.afficher_in_progress=!1}}},V=async()=>{i.state.afficher_in_progress=!0;try{const e=await $.post(`${i.lienApi}/ObjectifsVsRealise/${i.db}`,{Annee:f.value,id_agence:w.value});j.value=Array.isArray(e.data)?e.data:[]}catch(e){x(`Une erreur s'est produite lors de la récupération des données.
 ${e}`),j.value=[]}finally{i.state.afficher_in_progress=!1}},ae=()=>{if(!g.value||u.value.length===0){x("Aucune donnée à exporter");return}const e=new Date,t=a=>String(a).padStart(2,"0"),s=`${e.getFullYear()}${t(e.getMonth()+1)}${t(e.getDate())}_${t(e.getHours())}${t(e.getMinutes())}`;g.value.exportDataAsExcel({fileName:`ObjectifsVsRealise_${i.state.enreg_societe?.Nom||""}_${s}.xlsx`,sheetName:"Objectifs vs Realise"})},le=async()=>{try{const t=(await $.post(`${i.lienApi}/verifierDroit/${i.state.dbCommun6}`,{id_utilisateur:i.userId,id_page:828,mail:i.state.userCloud.mail},{headers:{"Content-Type":"application/json"}})).data;t&&Object.keys(t).length>0&&!t.lecture&&(S("Vous n'avez pas le droit, veuillez consulter l'administrateur"),M("ObjectifsVsRealise"))}catch(e){x("Une erreur est survenue : "+(e.response?.data?.message||e.message))}};return he(async()=>{await le(),w.value=R.value}),(e,t)=>{const s=de,a=me,ie=ve,oe=F("AgCharts"),re=F("ag-grid-vue"),ne=pe,ce=fe;return d(),m(T,null,[l("div",je,[l("div",we,[r(s,{icon:"pi pi-refresh",label:"Afficher",text:"",onClick:t[0]||(t[0]=o=>V())}),r(s,{icon:h.value?"pi pi-table":"pi pi-chart-bar",label:h.value?"Tableau":"Graphique",text:"",onClick:t[1]||(t[1]=o=>h.value=!h.value)},null,8,["icon","label"]),r(s,{icon:"pi pi-file-excel",label:"Excel",text:"",onClick:t[2]||(t[2]=o=>ae())}),G.masquerFermer?D("",!0):(d(),ye(s,{key:0,icon:"pi pi-times",label:"Fermer",text:"",onClick:t[3]||(t[3]=o=>b(M)("ObjectifsVsRealise"))}))]),l("div",Ae,[r(a,{size:"small",modelValue:f.value,"onUpdate:modelValue":t[4]||(t[4]=o=>f.value=o),options:b(i).state.Annees,optionLabel:"Annee",optionValue:"Annee",placeholder:"Année",style:{width:"120px"}},null,8,["modelValue","options"]),K.value?(d(),m("div",ke,[r(ie,{modelValue:w.value,"onUpdate:modelValue":t[5]||(t[5]=o=>w.value=o),placeholder:"Toutes les agences",table:"agence",class:"w-full",size:"small",disabled:J.value},null,8,["modelValue","disabled"])])):D("",!0),l("span",Ce,p(u.value.length)+" commercial(aux)",1)])]),u.value.length?(d(),m("div",$e,[l("div",De,[t[9]||(t[9]=l("div",{class:"kpi-label"},"Objectif total",-1)),l("div",Re,p(b(_)(v.value.objectif)),1)]),l("div",Ne,[t[10]||(t[10]=l("div",{class:"kpi-label"},"Réalisé total",-1)),l("div",Oe,p(b(_)(v.value.realise)),1)]),l("div",Ve,[t[11]||(t[11]=l("div",{class:"kpi-label"},"Taux d'atteinte global",-1)),l("div",{class:"kpi-value",style:_e({color:N(v.value.pct)})},p(v.value.pct==null?"—":b(_)(v.value.pct)+" %"),5)]),l("div",Me,[t[12]||(t[12]=l("div",{class:"kpi-label"},"Commerciaux à l'objectif",-1)),l("div",Fe,p(v.value.nbOk)+" / "+p(u.value.length),1)])])):D("",!0),t[13]||(t[13]=l("div",{style:{color:"#64748b",margin:"2px 0 8px 4px"}}," Cliquez sur un commercial pour saisir ses objectifs mensuels. ",-1)),l("div",Ie,[h.value?(d(),m("div",Te,[r(oe,{options:ee.value,style:{height:"100%",width:"100%"}},null,8,["options"])])):(d(),m("div",ze,[r(re,{ref_key:"agGrid",ref:B,rowData:u.value,pinnedBottomRowData:O.value,columnDefs:W.value,defaultColDef:H,theme:e.$agGridTheme,localeText:e.$agGridLocale,onGridReady:Y,onFirstDataRendered:Q,onRowClicked:te,class:"ag-theme-quartz",style:{height:"100%",width:"100%"}},null,8,["rowData","pinnedBottomRowData","columnDefs","theme","localeText"])]))]),r(ce,{visible:y.value,"onUpdate:visible":t[8]||(t[8]=o=>y.value=o),modal:"",header:`Objectifs ${f.value} — ${A.value?.Commercial||""}`,style:{width:"520px"}},{footer:I(()=>[r(s,{label:"Annuler",text:"",onClick:t[6]||(t[6]=o=>y.value=!1)}),r(s,{label:"Enregistrer",icon:"pi pi-check",onClick:t[7]||(t[7]=o=>se()),loading:b(i).state.afficher_in_progress},null,8,["loading"])]),default:I(()=>[l("div",Se,[(d(!0),m(T,null,xe(C.value,o=>(d(),m("div",{key:o.mois,class:"field"},[l("label",qe,p(L[o.mois-1]),1),r(ne,{modelValue:o.objectif,"onUpdate:modelValue":ue=>o.objectif=ue,min:0,mode:"decimal",useGrouping:!0,size:"small",class:"w-full"},null,8,["modelValue","onUpdate:modelValue"])]))),128))])]),_:1},8,["visible","header"])],64)}}},We=be(Ge,[["__scopeId","data-v-51aac659"]]);export{We as default};
