1import{r as t,s as l,j as e,l as v,I as D,e as z,W as H,Y as V,Z as A,_ as G,$ as b,B as N,L as Q,u as h,a3 as F,A as _,E as C,z as $,a7 as k,x as L,f as J,F as R,G as U,H as W,J as T,T as Y}from"./index-CCnUCdHR.js";import{c as Z}from"./PageHeader-CS0virIR.js";import{D as K,a as X,b as ee,c as se,e as ae}from"./dialog-2XYhsBjp.js";import{S as re}from"./switch-DVcoMpMR.js";import{D as te}from"./DeleteConfirmDialog-DKj86kks.js";import{S as ie}from"./search-DiAJ7DLn.js";import{E as ne}from"./ellipsis-vertical-DWqy_ybX.js";import{S as ce}from"./square-pen-DiFcvkVX.js";import"./formatDistanceToNow-DmwmhgR7.js";import"./alert-dialog-CNdBgshb.js";function oe({open:x,onOpenChange:p,service:i,onSuccess:y}){const[d,S]=t.useState(!1),[w,m]=t.useState([]),[r,n]=t.useState({name:"",description:"",duration_minutes:60,category_id:"",is_active:!0}),g=!!i;t.useEffect(()=>{x&&(l.from("service_categories").select("*").order("name").then(({data:a})=>{a&&m(a)}),n(i?{name:i.name,description:i.description||"",duration_minutes:i.duration_minutes||60,category_id:i.category_id||"",is_active:i.is_active??!0}:{name:"",description:"",duration_minutes:60,category_id:"",is_active:!0}))},[x,i]);const j=async a=>{if(a.preventDefault(),!r.name.trim()){h.error("El nombre es requerido");return}S(!0);try{const{data:{user:o}}=await l.auth.getUser();if(!o)throw new Error("No autenticado");const{data:u}=await l.from("profiles").select("tenant_id").eq("user_id",o.id).single();if(!(u!=null&&u.tenant_id))throw new Error("No se encontró el tenant");const f={name:r.name.trim(),description:r.description.trim()||null,duration_minutes:r.duration_minutes,category_id:r.category_id||null,is_active:r.is_active,tenant_id:u.tenant_id};if(g&&i){const{error:c}=await l.from("services").update(f).eq("id",i.id);if(c)throw c;h.success("Servicio actualizado")}else{const{error:c}=await l.from("services").insert(f);if(c)throw c;h.success("Servicio creado")}y(),p(!1)}catch(o){console.error("Error saving service:",o),h.error("Error al guardar el servicio")}finally{S(!1)}};return e.jsx(K,{open:x,onOpenChange:p,children:e.jsxs(X,{className:"max-w-md",children:[e.jsx(ee,{children:e.jsx(se,{children:g?"Editar servicio":"Nuevo servicio"})}),e.jsxs("form",{onSubmit:j,className:"space-y-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(v,{htmlFor:"name",children:"Nombre *"}),e.jsx(D,{id:"name",value:r.name,onChange:a=>n({...r,name:a.target.value}),placeholder:"Ej: Botox facial"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(v,{htmlFor:"description",children:"Descripción"}),e.jsx(z,{id:"description",value:r.description,onChange:a=>n({...r,description:a.target.value}),placeholder:"Descripción del servicio...",rows:3})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(v,{htmlFor:"duration",children:"Duración (min)"}),e.jsx(D,{id:"duration",type:"number",min:5,max:480,value:r.duration_minutes,onChange:a=>n({...r,duration_minutes:parseInt(a.target.value)||60})})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(v,{htmlFor:"category",children:"CategorÃa"}),e.jsxs(H,{value:r.category_id||"none",onValueChange:a=>n({...r,category_id:a==="none"?"":a}),children:[e.jsx(V,{children:e.jsx(A,{placeholder:"Sin categorÃa"})}),e.jsxs(G,{children:[e.jsx(b,{value:"none",children:"Sin categorÃa"}),w.map(a=>e.jsx(b,{value:a.id,children:a.name},a.id))]})]})]})]}),e.jsxs("div",{className:"flex items-center justify-between py-2",children:[e.jsx(v,{htmlFor:"active",className:"cursor-pointer",children:"Servicio activo"}),e.jsx(re,{id:"active",checked:r.is_active,onCheckedChange:a=>n({...r,is_active:a})})]}),e.jsxs(ae,{children:[e.jsx(N,{type:"button",variant:"outline",onClick:()=>p(!1),children:"Cancelar"}),e.jsxs(N,{type:"submit",disabled:d,children:[d&&e.jsx(Q,{className:"w-4 h-4 mr-2 animate-spin"}),g?"Guardar cambios":"Crear servicio"]})]})]})]})})}function ve(){const[x,p]=t.useState([]),[i,y]=t.useState(!0),[d,S]=t.useState(""),[w,m]=t.useState(!1),[r,n]=t.useState(null),[g,j]=t.useState(!1),[a,o]=t.useState(null),[u,f]=t.useState(!1),c=async()=>{y(!0);const{data:s,error:P}=await l.from("services").select(` 2 *, 3 category:service_categories(*) 4 `).order("name",{ascending:!0});!P&&s&&p(s),y(!1)};t.useEffect(()=>{c()},[]);const I=s=>{n(s),m(!0)},q=s=>{o(s),j(!0)},B=async()=>
4{if(a){f(!0);try{const{error:s}=await l.from("services").delete().eq("id",a.id);if(s)throw s;h.success("Servicio eliminado"),j(!1),o(null),c()}catch(s){console.error("Error deleting service:",s),h.error("Error al eliminar el servicio")}finally{f(!1)}}},M=()=>{n(null),c()},O=s=>{m(s),s||n(null)},E=x.filter(s=>s.name.toLowerCase().includes(d.toLowerCase()));return e.jsxs(e.Fragment,{children:[e.jsx(Z,{title:"Catálogo de servicios",description:"Servicios, categorÃas y precios por sede",backPath:"/settings"}),e.jsxs("div",{className:"flex-1 overflow-y-auto px-4 py-4 md:px-6 md:py-5 space-y-4",children:[e.jsxs("div",{className:"flex gap-3",children:[e.jsxs("div",{className:"relative flex-1",children:[e.jsx(ie,{className:"absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-muted-foreground"}),e.jsx(D,{placeholder:"Buscar servicios...",value:d,onChange:s=>S(s.target.value),className:"pl-9"})]}),e.jsxs(N,{className:"gap-2",onClick:()=>m(!0),children:[e.jsx(F,{className:"w-4 h-4"}),e.jsx("span",{className:"hidden sm:inline",children:"Nuevo servicio"})]})]}),i?e.jsx("div",{className:"space-y-3",children:[1,2,3].map(s=>e.jsx(_,{children:e.jsx(C,{className:"p-4",children:e.jsx($,{className:"h-16 w-full"})})},s))}):E.length===0?e.jsx(_,{children:e.jsxs(C,{className:"py-12 text-center",children:[e.jsx(k,{className:"w-12 h-12 mx-auto text-muted-foreground/50 mb-4"}),e.jsx("h3",{className:"font-medium text-lg mb-1",children:"No hay servicios"}),e.jsx("p",{className:"text-muted-foreground text-sm mb-4",children:d?"No se encontraron resultados":"Crea tu primer servicio"}),e.jsxs(N,{onClick:()=>m(!0),children:[e.jsx(F,{className:"w-4 h-4 mr-2"}),"Nuevo servicio"]})]})}):e.jsx("div",{className:"space-y-3",children:E.map(s=>e.jsx(_,{className:"hover:shadow-md transition-shadow",children:e.jsx(C,{className:"p-4",children:e.jsxs("div",{className:"flex items-start justify-between gap-4",children:[e.jsxs("div",{className:"flex items-start gap-4",children:[e.jsx("div",{className:"p-3 rounded-xl bg-primary/10",children:e.jsx(k,{className:"w-5 h-5 text-primary"})}),e.jsxs("div",{children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("h3",{className:"font-medium",children:s.name}),e.jsx(L,{variant:s.is_active?"success":"secondary",children:s.is_active?"Activo":"Inactivo"})]}),e.jsxs("div",{className:"flex flex-wrap gap-x-4 gap-y-1 mt-2 text-sm text-muted-foreground",children:[s.category&&e.jsx(L,{variant:"outline",children:s.category.name}),s.duration_minutes&&e.jsxs("span",{className:"flex items-center gap-1",children:[e.jsx(J,{className:"w-3.5 h-3.5"}),s.duration_minutes," min"]})]}),s.description&&e.jsx("p",{className:"text-sm text-muted-foreground mt-2 line-clamp-2",children:s.description})]})]}),e.jsxs(R,{children:[e.jsx(U,{asChild:!0,children:e.jsx(N,{variant:"ghost",size:"icon",className:"h-8 w-8",children:e.jsx(ne,{className:"w-4 h-4"})})}),e.jsxs(W,{align:"end",children:[e.jsxs(T,{onClick:()=>I(s),children:[e.jsx(ce,{className:"w-4 h-4 mr-2"}),"Editar"]}),e.jsxs(T,{className:"text-destructive",onClick:()=>q(s),children:[e.jsx(Y,{className:"w-4 h-4 mr-2"}),"Eliminar"]})]})]})]})})},s.id))})]}),e.jsx(oe,{open:w,onOpenChange:O,service:r,onSuccess:M}),e.jsx(te,{open:g,onOpenChange:j,title:"¿Eliminar servicio?",description:`Esta acción eliminará "${a==null?void 0:a.name}" permanentemente. Esta acción no se puede deshacer.`,onConfirm:B,loading:u})]})}
4export{ve as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.