import{b as q,f as B}from"./primevue-CfIk0_DY.js";import{b as $,s as C,o as N}from"./index-BxIwIJfy.js";import{h as H,b as k,k as R,l as E,c as M,d as b,t as S,v as F,p as A,a as i,y as L}from"./vue-core-Di3IuVqe.js";const z={key:0,class:"flex flex-col gap-4"},Q={class:"bg-blue-50 p-3 rounded-lg"},W={class:"font-semibold"},j={class:"text-sm text-gray-600"},I={style:{height:"350px"}},K={class:"ag-theme-quartz",style:{height:"100%",width:"100%"}},X={__name:"GammeSelectionDialog",setup(T,{expose:w}){const s=i(!1),o=i(null),c=i([]),v=i(!1),x=i(null);let n=null;const d=i(""),h=i(""),u={},g=async t=>{if(!t||t<=0)return"";if(u[t]!==void 0)return u[t];try{const r=(await $.post(`${C.lienApi}/ungamme_art/${C.db}/${t}`)).data?.libelle||"";return u[t]=r,r}catch{return""}},_=t=>{x.value=t.api,setTimeout(()=>x.value?.sizeColumnsToFit(),100)},P=()=>{v.value=c.value.some(t=>t.qte_saisie>0)},e=L(()=>{const t=o.value?.id_gamme_2>0;return[{field:"id_lgamme1",hide:!0},{field:"id_lgamme2",hide:!0},{field:"libelle_gamme_1",headerName:d.value||"Gamme 1",sortable:!0,filter:"agTextColumnFilter",width:180},...t?[{field:"libelle_gamme_2",headerName:h.value||"Gamme 2",sortable:!0,filter:"agTextColumnFilter",width:180}]:[],{field:"Prix_vente_HT",headerName:"Prix HT",width:130,filter:"agNumberColumnFilter",valueFormatter:l=>N(l.value)},{field:"Prix_vente_TTC",headerName:"Prix TTC",width:130,filter:"agNumberColumnFilter",valueFormatter:l=>N(l.value)},{field:"stock",headerName:"Stock",width:110,filter:"agNumberColumnFilter",valueFormatter:l=>N(l.value)},{field:"qte_saisie",headerName:"Quantité",width:120,editable:!0,filter:!1,cellEditor:"agNumberCellEditor",cellEditorParams:{min:0,precision:2},cellStyle:{backgroundColor:"#fffde7",fontWeight:"bold"},valueSetter:l=>(l.data.qte_saisie=l.newValue||0,!0)}]}),a=i({sortable:!0,filter:!0,floatingFilter:!1,resizable:!0,suppressHeaderMenuButton:!0}),m=async t=>{try{const l=await $.post(`${C.lienApi}/larticle_gamme/${C.db}/${t.code_art}`,{},{headers:{"Content-Type":"application/json"}});return!l.data||l.data.length===0?null:(o.value=t,c.value=l.data.map(r=>({...r,qte_saisie:0})),v.value=!1,d.value="",h.value="",[d.value,h.value]=await Promise.all([g(t.id_gamme_1),g(t.id_gamme_2)]),s.value=!0,new Promise(r=>{n=r}))}catch{return null}},p=()=>{const t=c.value.filter(r=>r.qte_saisie>0),l=n;n=null,s.value=!1,o.value=null,l&&l(t)},f=()=>{s.value=!1},y=()=>{const t=n;n=null,o.value=null,t&&t(null)};return w({open:m}),(t,l)=>{const r=H("ag-grid-vue"),D=q,V=B;return k(),R(V,{visible:s.value,"onUpdate:visible":l[0]||(l[0]=G=>s.value=G),header:"Sélection des gammes - "+(o.value?.code_art||""),modal:!0,style:{width:"750px"},closable:!0,onHide:y},{footer:E(()=>[F(D,{label:"Annuler",icon:"pi pi-times",onClick:f,text:""}),F(D,{label:"Valider",icon:"pi pi-check",onClick:p,disabled:!v.value,class:"p-button-success"},null,8,["disabled"])]),default:E(()=>[o.value?(k(),M("div",z,[b("div",Q,[b("p",W,S(o.value.code_art),1),b("p",j,S(o.value.libelle),1)]),b("div",I,[b("div",K,[F(r,{rowData:c.value,columnDefs:e.value,defaultColDef:a.value,theme:t.$agGridTheme,localeText:t.$agGridLocale,stopEditingWhenCellsLoseFocus:!0,onGridReady:_,onCellValueChanged:P,class:"ag-theme-quartz",style:{height:"100%",width:"100%"}},null,8,["rowData","columnDefs","defaultColDef","theme","localeText"])])])])):A("",!0)]),_:1},8,["visible","header"])}}};function Y(T,w){const{modeModificationRef:s,numGamme:o,valueListMaxWidth:c=220,valueListMaxHeight:v=280,cellHeight:x=32}=w,n=`id_gamme_${o}`,d=`libelle_gamme_${o}`,h=`gamme${o}`,u=e=>e?.code_art?T.value?.[e.code_art]?.[h]||[]:[],g=e=>!s.value||!e?!1:!!e.code_art,_=(e,a)=>{if(a==null||a==="")return"";const p=u(e).find(f=>f.value===a);return p?p.text:e?.[d]||""};return{columnPartialDef:{editable:e=>g(e.data),cellEditor:"agRichSelectCellEditor",cellEditorParams:e=>({values:u(e.data).map(a=>a.value),searchType:"matchAny",highlightMatch:!0,cellHeight:x,valueListMaxHeight:v,valueListMaxWidth:c,formatValue:a=>_(e.data,a)}),valueSetter:e=>{const a=e.newValue;if(a==null||a==="")return!1;const m=Number(a);if(e.data[n]===m)return!1;e.data[n]=m;const f=u(e.data).find(y=>y.value===m);return e.data[d]=f?f.text:"",!0},valueFormatter:e=>e.node?.rowPinned?"":_(e.data,e.value),cellRenderer:e=>e.node?.rowPinned?"":`<div class="code-cell"><span class="code-cell-text">${_(e.data,e.value)}</span><i class="pi pi-chevron-down code-cell-chevron"></i></div>`,cellStyle:e=>{const a={padding:"4px 8px",display:"flex",alignItems:"center",justifyContent:"center"};return g(e.data)?{...a,cursor:"pointer"}:a},suppressMenu:!0}}}export{X as _,Y as u};
