import{r as g,j as e,t as v}from"./index-DiwDH9aE.js";import{A as q,D as V}from"./Alert-Cq7mXBD-.js";import{S as U,P as _,M as H}from"./Modal-tocKJOcW.js";import{C as X,T as J}from"./ConfirmModal-mJDwNEXB.js";import{u as K,a as Q,h as W}from"./handleFormErrors-CaWHVJ6Q.js";import{o as Y,b as F,s as S,I as A,E as y}from"./schemas-BwNPnT22.js";import{R as Z}from"./RadioGroup-qqSrRVM3.js";import{B as k}from"./Button-Cu8l7rID.js";import{B as ee,S as re}from"./BaseService-B6zq4ARl.js";import{E as ae}from"./endpoints-BUXROgJ9.js";import{X as se}from"./x-D4vgwdOf.js";import{C as I}from"./Can-w-eLoK-Q.js";import"./search-h1vL1q1e.js";import"./triangle-alert-BxjRBxgM.js";import"./circle-check-big-CvbUXdGb.js";import"./multiUserApiClient-CKPzIhIr.js";const O=Y({name:S().min(1,"El nombre es requerido").min(3,"El nombre debe tener al menos 3 caracteres").max(200,"El nombre no puede exceder 200 caracteres").trim(),code:S().min(1,"El código es requerido").min(2,"El código debe tener al menos 2 caracteres").max(50,"El código no puede exceder 50 caracteres").trim().toUpperCase(),group:S().min(1,"El grupo es requerido").regex(/^[a-z]$/,"El grupo debe ser una letra minúscula (a, b, c, d, etc.)"),logo:S().optional().nullable()}),te=O.extend({active:F().default(!0)}),oe=O.extend({active:F().default(!0)});class le extends ee{constructor(){super(ae.ADMIN.CAREERS.BASE)}}const E=new le,P=()=>{const[p,h]=g.useState(null),[b,t]=g.useState(!1),[C,s]=g.useState(null),i=async(o={})=>{t(!0),s(null);try{const r=await E.getAll(o);return r.success?(h(r),r):(s(r.message),r)}catch(r){throw s(r.message||"Error al cargar carreras"),console.error("Error en fetchCareers:",r),r}finally{t(!1)}};return{data:p,loading:b,error:C,fetchCareers:i,getCareer:async o=>{t(!0),s(null);try{const r=await E.getById(o);return r.success?r.data:(s(r.message),null)}catch(r){throw s(r.message||"Error al cargar carrera"),console.error("Error en getCareer:",r),r}finally{t(!1)}},createCareer:async o=>{t(!0),s(null);try{const r=await E.create(o);return r.success?(await i(),r):(s(r.message),r)}catch(r){throw s(r.message||"Error al crear carrera"),console.error("Error en createCareer:",r),r}finally{t(!1)}},updateCareer:async(o,r)=>{t(!0),s(null);try{const n=await E.update(o,r);return n.success?(await i(),n):(s(n.message),n)}catch(n){throw s(n.message||"Error al actualizar carrera"),console.error("Error en updateCareer:",n),n}finally{t(!1)}},deleteCareer:async o=>{t(!0),s(null);try{const r=await E.delete(o);return r.success?(await i(),r):(s(r.message),r)}catch(r){throw s(r.message||"Error al eliminar carrera"),console.error("Error en deleteCareer:",r),r}finally{t(!1)}},clearError:()=>{s(null)},reset:()=>{h(null),t(!1),s(null)}}},ne=({initialData:p=null,onSuccess:h,onCancel:b})=>{const t=!!p,C=t?oe:te,{createCareer:s,updateCareer:i,loading:d}=P(),{register:u,handleSubmit:w,formState:{errors:l},setError:x,reset:f,watch:o,setValue:r}=K({resolver:Q(C),mode:"onChange",defaultValues:p||{name:"",code:"",group:"",logo:null,active:!0}}),n=o("active"),j=[{value:!0,label:"Activo"},{value:!1,label:"Inactivo"}],N=[{value:"a",label:"Grupo A"},{value:"b",label:"Grupo B"},{value:"c",label:"Grupo C"},{value:"d",label:"Grupo D"}],D=async m=>{try{let c;t?c=await i(p.id,m):c=await s(m),c?.success||c?.data?(v.success(c.message||(t?"Carrera actualizada correctamente":"Carrera creada correctamente")),f(),h?.()):(console.log(c),W(c,x))}catch(c){console.error("Error al guardar carrera:",c),v.error("Error inesperado al guardar la carrera")}};return e.jsxs("form",{onSubmit:w(D),className:"space-y-4",children:[e.jsxs("div",{children:[e.jsx(A,{label:"Nombre de la Carrera",placeholder:"Ej: Matemáticas y Estadísticas Aplicadas",error:l.name?.message,...u("name")}),e.jsx(y,{message:l.name?.message})]}),e.jsxs("div",{children:[e.jsx(A,{label:"Código",placeholder:"Ej: EPMEA",error:l.code?.message,...u("code"),style:{textTransform:"uppercase"}}),e.jsx(y,{message:l.code?.message}),e.jsx("p",{className:"text-xs text-gray-500 mt-1",children:"El código se convertirá automáticamente a mayúsculas"})]}),e.jsxs("div",{children:[e.jsxs("label",{className:"block text-sm font-medium mb-2 text-gray-700",children:["Grupo ",e.jsx("span",{className:"text-red-500",children:"*"})]}),e.jsxs("select",{className:`
            w-full px-3 py-3 rounded-lg border bg-white text-gray-900 
            transition-colors duration-200
            focus:outline-none focus:border-[#00306e] focus:ring-2 focus:ring-[#00306e]/10
            hover:border-gray-400
            ${l.group?.message?"border-red-500 focus:border-red-500 focus:ring-red-500/10":"border-gray-300"}
          `,...u("group"),children:[e.jsx("option",{value:"",children:"Seleccione un grupo"}),N.map(m=>e.jsx("option",{value:m.value,children:m.label},m.value))]}),e.jsx(y,{message:l.group?.message})]}),e.jsxs("div",{children:[e.jsx(A,{label:"Logo (URL)",placeholder:"https://ejemplo.com/logo.png",error:l.logo?.message,...u("logo")}),e.jsx(y,{message:l.logo?.message}),e.jsx("p",{className:"text-xs text-gray-500 mt-1",children:"Campo opcional. Ingrese la URL del logo de la carrera"})]}),e.jsxs("div",{children:[e.jsx(Z,{label:"Estado",options:j,value:n,onValueChange:m=>r("active",m),error:l.active?.message,direction:"row"}),e.jsx(y,{message:l.active?.message})]}),e.jsxs("div",{className:"flex items-center justify-end gap-3 pt-4 border-t border-gray-200",children:[e.jsx(k,{type:"button",variant:"ghost",text:"Cancelar",leftIcon:e.jsx(se,{className:"w-4 h-4"}),onClick:b,disabled:d}),e.jsx(k,{type:"submit",variant:"primary",text:d?"Guardando...":t?"Actualizar":"Crear",leftIcon:!d&&e.jsx(U,{className:"w-4 h-4"}),loading:d,disabled:d})]})]})},we=()=>{const{data:p,loading:h,error:b,fetchCareers:t,deleteCareer:C}=P(),[s,i]=g.useState(!1),[d,u]=g.useState(null),[w,l]=g.useState(!1),[x,f]=g.useState(null),[o,r]=g.useState(!1),n={page:1,sort_by:"id",order:"desc",per_page:10},j=g.useRef(n),N=[{key:"id",label:"ID",sortable:!0,wrap:!1,width:"w-20"},{key:"code",label:"Código",sortable:!0,wrap:!1,width:"w-32"},{key:"name",label:"Nombre de la Carrera",sortable:!0,wrap:!1},{key:"group",label:"Grupo",sortable:!0,wrap:!1,width:"w-24",render:a=>e.jsxs("span",{className:"uppercase font-medium text-gray-700",children:["Grupo ",a]})},{key:"active",label:"Estado",sortable:!0,wrap:!1,width:"w-28",render:a=>e.jsx("span",{className:`px-2 py-1 text-xs font-medium rounded-full ${a?"bg-green-100 text-green-800":"bg-red-100 text-red-800"}`,children:a?"Activo":"Inactivo"})}],D=()=>{u(null),i(!0)},m=async a=>(j.current=a,await t(a)),c=()=>{t(j.current)},G=()=>{t({...j.current,page:1})},R=a=>{u(a),i(!0)},M=()=>{i(!1),u(null)},T=()=>{const a=!d;i(!1),u(null),a?G():c()},B=a=>{f(a),l(!0)},z=()=>{o||(l(!1),f(null))},L=async()=>{if(x){r(!0);try{const a=await C(x.id);a?.success||a?.data?(v.success(a.message||"Carrera eliminada correctamente"),l(!1),f(null),c()):(v.error(a?.message||"Error al eliminar la carrera"),l(!1))}catch(a){console.error("Error al eliminar carrera:",a),v.error("Error inesperado al eliminar la carrera"),l(!1)}finally{r(!1)}}},$=a=>e.jsxs(e.Fragment,{children:[e.jsx(I,{permission:"careers.update",children:e.jsx("button",{onClick:()=>R(a),className:"p-2 text-[#00306e] hover:bg-blue-50 rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed",title:"Editar",disabled:o,children:e.jsx(re,{className:"w-4 h-4"})})}),e.jsx(I,{permission:"careers.destroy",children:e.jsx("button",{onClick:()=>B(a),className:"p-2 text-red-600 hover:bg-red-50 rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed",title:"Eliminar",disabled:o,children:e.jsx(J,{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 Carreras"}),e.jsx("p",{className:"text-gray-600 mt-1",children:"Administra las carreras profesionales del sistema"})]}),e.jsx(I,{permission:"careers.store",children:e.jsxs("button",{onClick:D,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:o,children:[e.jsx(_,{className:"w-5 h-5"}),"Nueva Carrera"]})})]}),b&&e.jsx(q,{variant:"warning",message:"⚠️ Hubo un problema al realizar la operación.",autoClose:3e3}),e.jsx(V,{columns:N,data:p,onFetchData:m,initialParams:n,renderActions:$,loading:h,loadingMessage:o?"Eliminando carrera...":"Cargando carreras...",searchable:!0,autoSearch:!1,sortable:!0,searchPlaceholder:"Buscar carreras...",emptyMessage:"No hay carreras registradas",perPageOptions:[10,25,50,100]}),e.jsx(H,{isOpen:s,onClose:M,title:d?"Editar Carrera":"Crear Carrera",size:"md",closeOnClickOutside:!1,children:e.jsx(ne,{initialData:d,onSuccess:T,onCancel:M})}),e.jsx(X,{isOpen:w,onClose:z,onConfirm:L,title:"Eliminar Carrera",message:`¿Estás seguro de eliminar la carrera "${x?.name||"seleccionada"}" (${x?.code||""})? Esta acción no se puede deshacer.`,variant:"danger",confirmText:"Eliminar",cancelText:"Cancelar",loading:o})]})};export{we as default};
