import{j as e,t as x,r as g}from"./index-DiwDH9aE.js";import{A as z,D as G}from"./Alert-Cq7mXBD-.js";import{S as V,P as $,M as B}from"./Modal-tocKJOcW.js";import{C as R,T as _}from"./ConfirmModal-mJDwNEXB.js";import{u as q,a as H,h as L}from"./handleFormErrors-CaWHVJ6Q.js";import{o as W,b as X,s as f,I as S,E as b}from"./schemas-BwNPnT22.js";import{R as J}from"./RadioGroup-qqSrRVM3.js";import{B as D}from"./Button-Cu8l7rID.js";import{u as k}from"./usePermissions-Bd6Y6GNw.js";import{X as K}from"./x-D4vgwdOf.js";import{C as N}from"./Can-w-eLoK-Q.js";import{S as Q}from"./BaseService-B6zq4ARl.js";import"./search-h1vL1q1e.js";import"./triangle-alert-BxjRBxgM.js";import"./circle-check-big-CvbUXdGb.js";import"./endpoints-BUXROgJ9.js";import"./multiUserApiClient-CKPzIhIr.js";const O=W({name:f().min(1,"El nombre es obligatorio").min(3,"El nombre debe tener al menos 3 caracteres"),slug:f().min(1,"El slug es obligatorio").min(3,"El slug debe tener al menos 3 caracteres").regex(/^[a-z._]+$/,"El slug solo puede contener letras minúsculas, puntos y guiones bajos"),group:f().min(1,"El grupo es obligatorio").min(3,"El grupo debe tener al menos 3 caracteres"),description:f().optional()}),U=O,Y=O.extend({active:X().default(!0)}),Z=({initialData:m=null,onSuccess:j,onCancel:y})=>{const o=!!m,v=o?Y:U,{createPermission:E,updatePermission:n,loading:l}=k(),{register:t,handleSubmit:w,formState:{errors:s},setError:d,reset:u,watch:i,setValue:h}=q({resolver:H(v),mode:"onChange",defaultValues:m||{name:"",slug:"",group:"",description:"",active:!0}}),p=i("active"),P=[{value:!0,label:"Activo"},{value:!1,label:"Inactivo"}],C=async c=>{try{let a;o?a=await n(m.id,c):a=await E(c),a?.success?(x.success(o?"Permiso actualizado correctamente":"Permiso creado correctamente"),u(),j?.()):(console.log(a),L(a,d))}catch(a){console.error("Error al guardar permiso:",a),x.error("Error inesperado al guardar el permiso")}};return e.jsxs("form",{onSubmit:w(C),className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx(S,{label:"Nombre",placeholder:"Ej: Ver Dashboard",error:s.name?.message,...t("name")}),e.jsx(b,{message:s.name?.message})]}),e.jsxs("div",{children:[e.jsx(S,{label:"Slug",placeholder:"Ej: dashboard.view",error:s.slug?.message,...t("slug")}),e.jsx(b,{message:s.slug?.message})]}),e.jsxs("div",{children:[e.jsx(S,{label:"Grupo",placeholder:"Ej: dashboard",error:s.group?.message,...t("group")}),e.jsx(b,{message:s.group?.message})]}),e.jsxs("div",{children:[e.jsx("label",{className:"block text-sm font-medium mb-2 text-gray-700",children:"Descripción (Opcional)"}),e.jsx("textarea",{placeholder:"Descripción del permiso",rows:3,className:`
                        w-full px-3 py-3 rounded-lg border bg-white text-gray-900 
                        placeholder:text-gray-500 transition-colors duration-200
                        focus:outline-none focus:border-[#00306e] focus:ring-2 focus:ring-[#00306e]/10
                        hover:border-gray-400
                        ${s.description?.message?"border-red-500 focus:border-red-500 focus:ring-red-500/10":"border-gray-300"}
                    `,...t("description")}),e.jsx(b,{message:s.description?.message})]}),o&&e.jsxs("div",{children:[e.jsx(J,{label:"Estado",options:P,value:p,onValueChange:c=>h("active",c),error:s.active?.message,direction:"row"}),e.jsx(b,{message:s.active?.message})]}),e.jsxs("div",{className:"flex items-center justify-end gap-3 pt-4 border-t border-gray-200",children:[e.jsx(D,{type:"button",variant:"ghost",text:"Cancelar",leftIcon:e.jsx(K,{className:"w-4 h-4"}),onClick:y,disabled:l}),e.jsx(D,{type:"submit",variant:"primary",text:l?"Guardando...":o?"Actualizar":"Crear",leftIcon:!l&&e.jsx(V,{className:"w-4 h-4"}),loading:l,disabled:l})]})]})},he=()=>{const{data:m,loading:j,error:y,fetchPermissions:o,deletePermission:v}=k(),[E,n]=g.useState(!1),[l,t]=g.useState(null),[w,s]=g.useState(!1),[d,u]=g.useState(null),[i,h]=g.useState(!1),p={sort_by:"id",order:"desc",per_page:10},P=[{key:"id",label:"ID",sortable:!0,wrap:!1,width:"w-20"},{key:"name",label:"Nombre",sortable:!0,wrap:!1},{key:"slug",label:"Slug",sortable:!0,wrap:!1},{key:"group",label:"Grupo",sortable:!0,wrap:!1},{key:"description",label:"Descripción",sortable:!1,wrap:!0,maxWidth:"max-w-xs"},{key:"active",label:"Estado",sortable:!0,wrap:!1,render:r=>e.jsx("span",{className:`px-2 py-1 text-xs font-medium rounded-full ${r?"bg-green-100 text-green-800":"bg-red-100 text-red-800"}`,children:r?"Activo":"Inactivo"})}],C=()=>{t(null),n(!0)},c=r=>{t(r),n(!0)},a=()=>{n(!1),t(null)},I=()=>{n(!1),t(null),o(p)},M=r=>{u(r),s(!0)},F=()=>{i||(s(!1),u(null))},A=async()=>{if(d){h(!0);try{const r=await v(d.id);r?.success?(x.success(r.message||"Permiso eliminado correctamente"),s(!1),u(null),o(p)):(x.error(r?.message||"Error al eliminar el permiso"),s(!1))}catch(r){console.error("Error al eliminar permiso:",r),x.error("Error inesperado al eliminar el permiso"),s(!1)}finally{h(!1)}}},T=r=>e.jsxs(e.Fragment,{children:[e.jsx(N,{permission:"permissions.update",children:e.jsx("button",{onClick:()=>c(r),className:"p-2 text-[#00306e] hover:bg-blue-50 rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed",title:"Editar",disabled:i,children:e.jsx(Q,{className:"w-4 h-4"})})}),e.jsx(N,{permission:"permissions.destroy",children:e.jsx("button",{onClick:()=>M(r),className:"p-2 text-red-600 hover:bg-red-50 rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed",title:"Eliminar",disabled:i,children:e.jsx(_,{className:"w-4 h-4"})})})]});return e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center justify-between mb-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold text-gray-800",children:"Gestión de Permisos"}),e.jsx("p",{className:"text-gray-600 mt-1",children:"Administra los permisos del sistema"})]}),e.jsx(N,{permission:"permissions.store",children:e.jsxs("button",{onClick:C,className:"flex items-center gap-2 px-4 py-2 text-white bg-[#00306e] rounded-lg hover:bg-[#002555] transition-colors disabled:opacity-50 disabled:cursor-not-allowed",disabled:i,children:[e.jsx($,{className:"w-5 h-5"}),"Nuevo Permiso"]})})]}),y&&e.jsx(z,{variant:"warning",message:"⚠️ Hubo un problema al realizar la operación.",autoClose:3e3}),e.jsx(G,{columns:P,data:m,onFetchData:o,initialParams:p,renderActions:T,loading:j,loadingMessage:i?"Eliminando permiso...":"Cargando permisos...",searchable:!0,autoSearch:!1,sortable:!0,searchPlaceholder:"Buscar permisos...",emptyMessage:"No hay permisos registrados",perPageOptions:[10,25,50,100]}),e.jsx(B,{isOpen:E,onClose:a,title:l?"Editar Permiso":"Crear Permiso",size:"md",closeOnClickOutside:!1,children:e.jsx(Z,{initialData:l,onSuccess:I,onCancel:a})}),e.jsx(R,{isOpen:w,onClose:F,onConfirm:A,title:"Eliminar Permiso",message:`¿Estás seguro de eliminar el permiso "${d?.name||"seleccionado"}"? Esta acción no se puede deshacer.`,variant:"danger",confirmText:"Eliminar",cancelText:"Cancelar",loading:i})]})};export{he as default};
