import{_ as $,u as B,r as u,o as N,b as n,c as l,d as a,F as h,k as f,a9 as g,t as m}from"./main.37e79106.js";import{g as D,a as j,u as z}from"./RoleRepository.27008d81.js";const A={class:"layout-px-spacing"},F={class:"row layout-top-spacing"},G={class:"col-xl-12 col-lg-12 col-sm-12 layout-spacing"},L={class:"panel p-3"},M={class:"row mt-4"},S={class:"col-12"},O={key:0,class:"text-center p-5"},U={key:1},V={class:"card-header d-flex justify-content-between align-items-center"},q={class:"mb-0"},H=["onClick"],J={class:"card-body"},K={key:0},Q={class:"row"},T={class:"col-md-12 mb-3"},W={class:"row"},X={class:"form-check"},Y=["value","id","checked","onChange"],Z=["for"],I={class:"mt-3"},ss=["onClick","disabled"],es={key:1},as={class:"text-muted mb-2"},ts={key:0},is={key:1,class:"text-muted"},os={__name:"index",setup(ns){B({title:"Roles y Permisos"});const r=u([]),y=u([]),_=u(!0),d=u(null),c=u({}),v=u(!1),b=s=>s.split(".").map(t=>t.charAt(0).toUpperCase()+t.slice(1)).join(" / "),x=(s,t)=>{var e;return c.value[s.id]||(c.value[s.id]=((e=s.permissions)==null?void 0:e.map(o=>o.name))||[]),c.value[s.id].includes(t)},w=(s,t)=>{c.value[s]||(c.value[s]=[]);const e=c.value[s].indexOf(t);e>-1?c.value[s].splice(e,1):c.value[s].push(t)},P=s=>{var t,e;if(d.value===s){d.value=null;const o=r.value.find(i=>i.id===s);o&&(c.value[s]=((t=o.permissions)==null?void 0:t.map(i=>i.name))||[])}else{d.value=s;const o=r.value.find(i=>i.id===s);o&&(c.value[s]=((e=o.permissions)==null?void 0:e.map(i=>i.name))||[])}},C=async s=>{try{v.value=!0;const t=c.value[s]||[];await z(s,t),r.value.find(o=>o.id===s)&&await p(),d.value=null,g.fire({icon:"success",title:"\xC9xito",text:"Permisos actualizados correctamente",timer:2e3,showConfirmButton:!1})}catch(t){console.error("Error saving permissions:",t),g.fire({icon:"error",title:"Error",text:"No se pudieron actualizar los permisos"})}finally{v.value=!1}},p=async()=>{try{_.value=!0;const s=await D();r.value=s.data||s,r.value.forEach(t=>{var e;c.value[t.id]=((e=t.permissions)==null?void 0:e.map(o=>o.name))||[]})}catch(s){console.error("Error loading roles:",s),g.fire({icon:"error",title:"Error",text:"No se pudieron cargar los roles"})}finally{_.value=!1}},k=async()=>{try{const s=await j();y.value=s.data||s}catch(s){console.error("Error loading permissions:",s)}},E=async()=>{await p(),await k()};return N(async()=>{await p(),await k()}),(s,t)=>(n(),l("div",A,[a("div",F,[a("div",G,[a("div",L,[a("div",{class:"row"},[a("div",{class:"col-xl-12 col-lg-12 col-sm-12"},[a("div",{class:"text-center titulo-dorado"},[t[0]||(t[0]=a("h3",{class:"pull-left"},"Roles y Permisos",-1)),a("button",{class:"btn btn-black pull-right m-1",onClick:E}," Refrescar ")])])]),a("div",M,[a("div",S,[_.value?(n(),l("div",O,[...t[1]||(t[1]=[a("div",{class:"spinner-border",role:"status"},[a("span",{class:"visually-hidden"},"Cargando...")],-1)])])):(n(),l("div",U,[(n(!0),l(h,null,f(r.value,e=>{var o;return n(),l("div",{key:e.id,class:"card mb-4"},[a("div",V,[a("h5",q,m(e.name),1),a("button",{class:"btn btn-sm btn-primary",onClick:i=>P(e.id)},m(d.value===e.id?"Cancelar":"Editar Permisos"),9,H)]),a("div",J,[d.value===e.id?(n(),l("div",K,[a("div",Q,[a("div",T,[t[2]||(t[2]=a("label",{class:"form-label"},"Permisos disponibles:",-1)),a("div",W,[(n(!0),l(h,null,f(y.value,i=>(n(),l("div",{key:i.id,class:"col-md-4 mb-2"},[a("div",X,[a("input",{class:"form-check-input",type:"checkbox",value:i.name,id:`perm-${e.id}-${i.id}`,checked:x(e,i.name),onChange:R=>w(e.id,i.name)},null,40,Y),a("label",{class:"form-check-label",for:`perm-${e.id}-${i.id}`},m(b(i.name)),9,Z)])]))),128))])])]),a("div",I,[a("button",{class:"btn btn-success",onClick:i=>C(e.id),disabled:v.value},m(v.value?"Guardando...":"Guardar Cambios"),9,ss)])])):(n(),l("div",es,[a("p",as,[a("strong",null,"Permisos asignados ("+m(((o=e.permissions)==null?void 0:o.length)||0)+"):",1)]),e.permissions&&e.permissions.length>0?(n(),l("div",ts,[(n(!0),l(h,null,f(e.permissions,(i,R)=>(n(),l("span",{key:i.id,class:"badge bg-primary me-2 mb-2"},m(b(i.name)),1))),128))])):(n(),l("p",is,"No hay permisos asignados"))]))])])}),128))]))])])])])])]))}},rs=$(os,[["__scopeId","data-v-ca887890"]]);export{rs as default};
