- CotizacionForm: "Nuevo servicio" (general y por fase) lo crea en el catalogo y lo deja seleccionado; el lapiz de cada servicio lo edita y actualiza la partida (el precio solo si no se ajusto a mano). - ServicioFormModal compartido con /catalogo: hace el POST/PATCH y muestra el error de la API sin cerrarse (antes un alta "Sin categoria" fallaba en silencio). - /api/catalogo: validacion con defaults, filtros en GET, GET por id, PATCH/PUT parciales, campos desconocidos rechazados con su nombre, errores con detalles por campo. - POST /api/cotizaciones acepta cuerpo minimo (numero, asesor, fechas y precio de Bucefalo CRM los completa el servidor); GET con filtros. - GET /api/clientes con busqueda. - Seguridad: las respuestas de cotizaciones ya no incluyen el hash de la contrasena del asesor. - cli/cotizador.mjs: CLI sin dependencias, JSON en stdout, codigos de salida fijos, `esquema` autodescriptivo, --dry-run y --confirmar. Docs en docs/CLI.md y skill en .claude/skills/cotizador-cli. Co-Authored-By: Claude Opus 5.5 <[email protected]>
631 lines
26 KiB
TypeScript
631 lines
26 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useCallback } from "react";
|
|
import {
|
|
Plus,
|
|
Pencil,
|
|
Trash2,
|
|
X,
|
|
ChevronDown,
|
|
ChevronRight,
|
|
Loader2,
|
|
Package,
|
|
LayoutGrid,
|
|
Settings,
|
|
FileDown,
|
|
Upload,
|
|
} from "lucide-react";
|
|
import { formatCurrency } from "@/lib/calculators";
|
|
import { useConfirm, usePrompt, useToast } from "@/components/ui/DialogProvider";
|
|
import { ServicioFormModal } from "@/components/ServicioFormModal";
|
|
import clsx from "clsx";
|
|
|
|
interface Servicio {
|
|
id: string;
|
|
nombre: string;
|
|
descripcion: string | null;
|
|
fase: number;
|
|
tipoPago: string;
|
|
precioBase: number;
|
|
tiempoEntrega: string;
|
|
entregablesDefault: string[];
|
|
categoriaId: string | null;
|
|
categoriaNombre: string;
|
|
categoriaColor: string;
|
|
variante: string | null;
|
|
nivel: string | null;
|
|
activo: boolean;
|
|
orden: number;
|
|
}
|
|
|
|
interface Categoria {
|
|
id: string;
|
|
nombre: string;
|
|
descripcion: string | null;
|
|
color: string;
|
|
activo: boolean;
|
|
orden: number;
|
|
}
|
|
|
|
interface FasePaqueteUI {
|
|
id: string;
|
|
nombre: string;
|
|
orden: number;
|
|
servicios: { id: string; nombre: string; precioBase: number; tipoPago: string; nivel: string | null }[];
|
|
}
|
|
|
|
interface PaqueteUI {
|
|
id: string;
|
|
nombre: string;
|
|
descripcion: string | null;
|
|
fases: FasePaqueteUI[];
|
|
}
|
|
|
|
interface Props {
|
|
initialServicios: Servicio[];
|
|
initialCategorias: Categoria[];
|
|
initialPaquetes: PaqueteUI[];
|
|
}
|
|
|
|
type Tab = "categorias" | "paquetes" | "servicios";
|
|
|
|
export function CatalogoClient({ initialServicios, initialCategorias, initialPaquetes }: Props) {
|
|
const showConfirm = useConfirm();
|
|
const showPrompt = usePrompt();
|
|
const showToast = useToast();
|
|
const [tab, setTab] = useState<Tab>("servicios");
|
|
const [servicios, setServicios] = useState<Servicio[]>(initialServicios);
|
|
const [categorias, setCategorias] = useState<Categoria[]>(initialCategorias);
|
|
const [paquetes, setPaquetes] = useState<PaqueteUI[]>(initialPaquetes);
|
|
const [loading, setLoading] = useState(false);
|
|
const [importing, setImporting] = useState(false);
|
|
|
|
const refresh = useCallback(async () => {
|
|
setLoading(true);
|
|
try {
|
|
const [sRes, cRes, pRes] = await Promise.all([
|
|
fetch("/api/catalogo"),
|
|
fetch("/api/categorias"),
|
|
fetch("/api/paquetes"),
|
|
]);
|
|
if (sRes.ok) setServicios(await sRes.json());
|
|
if (cRes.ok) setCategorias(await cRes.json());
|
|
if (pRes.ok) setPaquetes(await pRes.json());
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, []);
|
|
|
|
const handleImport = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
const file = e.target.files?.[0];
|
|
if (!file) return;
|
|
setImporting(true);
|
|
try {
|
|
const form = new FormData();
|
|
form.append("file", file);
|
|
const res = await fetch("/api/import/catalogo", { method: "POST", body: form });
|
|
const data = await res.json();
|
|
if (res.ok) {
|
|
let msg = `${data.creados} servicio(s) importado(s).`;
|
|
if (data.omitidos > 0) msg += ` ${data.omitidos} omitido(s).`;
|
|
showToast(msg, "success");
|
|
if (data.errores?.length) showToast(`Errores:\n${data.errores.join("\n")}`, "error");
|
|
await refresh();
|
|
} else {
|
|
showToast(data.error || "Error al importar", "error");
|
|
}
|
|
} catch {
|
|
showToast("Error al importar CSV", "error");
|
|
} finally {
|
|
setImporting(false);
|
|
e.target.value = "";
|
|
}
|
|
};
|
|
|
|
const deleteCategoria = async (id: string) => {
|
|
const res = await fetch(`/api/categorias/${id}`, { method: "DELETE" });
|
|
if (res.ok) setCategorias((p) => p.filter((c) => c.id !== id));
|
|
else { const e = await res.json(); showToast(e.error, "error"); }
|
|
};
|
|
|
|
const toggleCategoriaActivo = async (cat: Categoria) => {
|
|
const res = await fetch(`/api/categorias/${cat.id}`, {
|
|
method: "PUT",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ ...cat, activo: !cat.activo }),
|
|
});
|
|
if (res.ok) {
|
|
const updated = await res.json();
|
|
setCategorias((p) => p.map((c) => (c.id === cat.id ? updated : c)));
|
|
}
|
|
};
|
|
|
|
const deletePaquete = async (id: string) => {
|
|
const ok = await showConfirm({
|
|
title: "Eliminar paquete",
|
|
message: "¿Eliminar este paquete y todas sus fases?",
|
|
confirmText: "Eliminar",
|
|
danger: true,
|
|
});
|
|
if (!ok) return;
|
|
const res = await fetch(`/api/paquetes/${id}`, { method: "DELETE" });
|
|
if (res.ok) setPaquetes((p) => p.filter((pk) => pk.id !== id));
|
|
};
|
|
|
|
const handleCreatePaquete = async () => {
|
|
const nombre = await showPrompt({
|
|
title: "Nuevo paquete",
|
|
label: "Nombre del paquete",
|
|
placeholder: "Ej. Paquete Emprendedor",
|
|
confirmText: "Crear",
|
|
});
|
|
if (!nombre) return;
|
|
const res = await fetch("/api/paquetes", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ nombre, fases: [] }),
|
|
});
|
|
if (res.ok) {
|
|
const created = await res.json();
|
|
setPaquetes((p) => [...p, { ...created, fases: [] }]);
|
|
}
|
|
};
|
|
|
|
const addFaseToPaquete = async (paqueteId: string) => {
|
|
const nombre = await showPrompt({
|
|
title: "Nueva fase",
|
|
label: "Nombre de la fase",
|
|
placeholder: "Ej. Fase 1 - Setup",
|
|
confirmText: "Agregar",
|
|
});
|
|
if (!nombre) return;
|
|
const pk = paquetes.find((p) => p.id === paqueteId);
|
|
const orden = pk ? pk.fases.length : 0;
|
|
const res = await fetch(`/api/paquetes/${paqueteId}/manage`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ action: "addFase", nombre, orden }),
|
|
});
|
|
if (res.ok) {
|
|
const fase = await res.json();
|
|
setPaquetes((p) =>
|
|
p.map((pk) =>
|
|
pk.id === paqueteId ? { ...pk, fases: [...pk.fases, { ...fase, servicios: [] }] } : pk
|
|
)
|
|
);
|
|
}
|
|
};
|
|
|
|
const deleteFase = async (paqueteId: string, faseId: string) => {
|
|
const ok = await showConfirm({
|
|
title: "Eliminar fase",
|
|
message: "¿Eliminar esta fase?",
|
|
confirmText: "Eliminar",
|
|
danger: true,
|
|
});
|
|
if (!ok) return;
|
|
const res = await fetch(`/api/paquetes/${paqueteId}/manage`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ action: "deleteFase", faseId }),
|
|
});
|
|
if (res.ok) {
|
|
setPaquetes((p) =>
|
|
p.map((pk) =>
|
|
pk.id === paqueteId ? { ...pk, fases: pk.fases.filter((f) => f.id !== faseId) } : pk
|
|
)
|
|
);
|
|
}
|
|
};
|
|
|
|
const removeServFromFase = async (paqueteId: string, faseId: string, servicioId: string) => {
|
|
const res = await fetch(`/api/paquetes/${paqueteId}/manage`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ action: "removeServicio", servicioCatalogoId: servicioId, fasePaqueteId: faseId }),
|
|
});
|
|
if (res.ok) {
|
|
setPaquetes((p) =>
|
|
p.map((pk) =>
|
|
pk.id === paqueteId
|
|
? {
|
|
...pk,
|
|
fases: pk.fases.map((f) =>
|
|
f.id === faseId
|
|
? { ...f, servicios: f.servicios.filter((s) => s.id !== servicioId) }
|
|
: f
|
|
),
|
|
}
|
|
: pk
|
|
)
|
|
);
|
|
}
|
|
};
|
|
|
|
const addServToFase = async (paqueteId: string, faseId: string, servicioId: string) => {
|
|
const res = await fetch(`/api/paquetes/${paqueteId}/manage`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ action: "addServicio", servicioCatalogoId: servicioId, fasePaqueteId: faseId }),
|
|
});
|
|
if (res.ok) await refresh();
|
|
};
|
|
|
|
const [addModal, setAddModal] = useState<{ paqueteId: string; faseId: string } | null>(null);
|
|
const [servicioModal, setServicioModal] = useState<Servicio | null>(null);
|
|
const [showNewServicio, setShowNewServicio] = useState(false);
|
|
const [expandedPk, setExpandedPk] = useState<Set<string>>(new Set(paquetes.map((p) => p.id)));
|
|
|
|
const tabs: { key: Tab; label: string; icon: React.ReactNode }[] = [
|
|
{ key: "servicios", label: "Servicios", icon: <LayoutGrid className="w-4 h-4" /> },
|
|
{ key: "categorias", label: "Categorias", icon: <Settings className="w-4 h-4" /> },
|
|
{ key: "paquetes", label: "Paquetes", icon: <Package className="w-4 h-4" /> },
|
|
];
|
|
|
|
return (
|
|
<div>
|
|
<div className="flex items-center justify-between mb-6">
|
|
<h1 className="text-2xl font-bold">Catalogo de Servicios</h1>
|
|
<div className="flex items-center gap-3">
|
|
<a
|
|
href="/api/export/catalogo/plantilla"
|
|
download
|
|
className="flex items-center gap-2 px-3 py-2 border border-border rounded-lg text-sm hover:bg-gray-50 transition-colors"
|
|
>
|
|
<FileDown className="w-4 h-4" />
|
|
Plantilla CSV
|
|
</a>
|
|
<label className={`flex items-center gap-2 px-3 py-2 border border-border rounded-lg text-sm hover:bg-gray-50 transition-colors cursor-pointer ${importing ? "opacity-50 pointer-events-none" : ""}`}>
|
|
<Upload className="w-4 h-4" />
|
|
{importing ? "Importando..." : "Importar CSV"}
|
|
<input type="file" accept=".csv" onChange={handleImport} className="hidden" />
|
|
</label>
|
|
<a
|
|
href="/api/export/catalogo"
|
|
download
|
|
className="flex items-center gap-2 px-3 py-2 border border-border rounded-lg text-sm hover:bg-gray-50 transition-colors"
|
|
>
|
|
<FileDown className="w-4 h-4" />
|
|
Exportar CSV
|
|
</a>
|
|
<button onClick={refresh} disabled={loading} className="text-xs text-muted hover:text-dark">
|
|
{loading ? <Loader2 className="w-4 h-4 animate-spin" /> : "Actualizar"}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex gap-1 mb-6 bg-gray-100 p-1 rounded-lg w-fit">
|
|
{tabs.map((t) => (
|
|
<button
|
|
key={t.key}
|
|
onClick={() => setTab(t.key)}
|
|
className={clsx(
|
|
"flex items-center gap-2 px-4 py-2 rounded-md text-sm font-medium transition-colors",
|
|
tab === t.key ? "bg-white shadow text-dark" : "text-muted hover:text-dark"
|
|
)}
|
|
>
|
|
{t.icon}{t.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
|
|
{/* ── TAB: SERVICIOS (por categoria) ── */}
|
|
{tab === "servicios" && (
|
|
<div>
|
|
<div className="flex items-center justify-between mb-4">
|
|
<p className="text-sm text-muted">{servicios.filter((s) => s.activo).length} servicios activos</p>
|
|
<button
|
|
onClick={() => setShowNewServicio(true)}
|
|
className="flex items-center gap-2 px-3 py-1.5 bg-primary text-white rounded-lg text-sm font-medium hover:bg-primary-dark"
|
|
>
|
|
<Plus className="w-4 h-4" /> Nuevo Servicio
|
|
</button>
|
|
</div>
|
|
{categorias.filter((c) => c.activo).map((cat) => {
|
|
const catServs = servicios.filter((s) => s.categoriaNombre === cat.nombre);
|
|
if (!catServs.length) return null;
|
|
return (
|
|
<div key={cat.id} className="mb-6">
|
|
<div className="flex items-center gap-2 mb-3">
|
|
<div className="w-3 h-3 rounded-full" style={{ backgroundColor: cat.color }} />
|
|
<h2 className="font-semibold text-lg">{cat.nombre}</h2>
|
|
<span className="text-xs text-muted">({catServs.length})</span>
|
|
</div>
|
|
<div className="bg-card-bg rounded-xl border border-border overflow-hidden">
|
|
<table className="w-full text-sm table-fixed">
|
|
<thead>
|
|
<tr className="border-b border-border bg-gray-50">
|
|
<th className="text-left px-4 py-3 font-medium text-muted">Servicio</th>
|
|
<th className="text-left px-4 py-3 font-medium text-muted w-28">Tipo</th>
|
|
<th className="text-left px-4 py-3 font-medium text-muted w-32">Tiempo</th>
|
|
<th className="text-right px-4 py-3 font-medium text-muted w-32">Precio</th>
|
|
<th className="text-center px-4 py-3 font-medium text-muted w-28">Entregables</th>
|
|
<th className="text-center px-4 py-3 font-medium text-muted w-24">Acciones</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{catServs.map((s) => (
|
|
<tr key={s.id} className={clsx("border-b border-border hover:bg-gray-50", !s.activo && "opacity-50")}>
|
|
<td className="px-4 py-3 align-top">
|
|
<div className="font-medium flex flex-wrap items-center gap-2 min-w-0">
|
|
<span className="break-words">{s.nombre}</span>
|
|
{s.nivel && <span className="inline-block px-2 py-0.5 rounded text-xs font-medium bg-amber-100 text-amber-700">{s.nivel}</span>}
|
|
</div>
|
|
{s.descripcion && <div className="text-xs text-muted mt-0.5 break-words">{s.descripcion}</div>}
|
|
</td>
|
|
<td className="px-4 py-3 align-top">
|
|
<span className={clsx("inline-block px-2 py-0.5 rounded text-xs font-medium", s.tipoPago === "unico" ? "bg-blue-100 text-blue-700" : "bg-green-100 text-green-700")}>
|
|
{s.tipoPago === "unico" ? "Unico" : "Mensual"}
|
|
</span>
|
|
</td>
|
|
<td className="px-4 py-3 text-muted text-xs align-top">{s.tiempoEntrega}</td>
|
|
<td className="px-4 py-3 text-right font-medium align-top">{formatCurrency(s.precioBase)}</td>
|
|
<td className="px-4 py-3 text-center text-muted align-top">{s.entregablesDefault.length}</td>
|
|
<td className="px-4 py-3 align-top">
|
|
<div className="flex items-center justify-center gap-1">
|
|
<button onClick={() => setServicioModal(s)} className="p-1.5 text-muted hover:text-primary rounded" title="Editar">
|
|
<Pencil className="w-3.5 h-3.5" />
|
|
</button>
|
|
<button onClick={async () => {
|
|
const ok = await showConfirm({ title: "Eliminar servicio", message: `¿Eliminar "${s.nombre}"?`, confirmText: "Eliminar", danger: true });
|
|
if (!ok) return;
|
|
const res = await fetch(`/api/catalogo/${s.id}`, { method: "DELETE" });
|
|
if (res.ok) { const d = await res.json(); if (d.archived) { setServicios((p) => p.map((x) => x.id === s.id ? { ...x, activo: false } : x)); } else { setServicios((p) => p.filter((x) => x.id !== s.id)); } }
|
|
}} className="p-1.5 text-muted hover:text-red-500 rounded" title="Eliminar">
|
|
<Trash2 className="w-3.5 h-3.5" />
|
|
</button>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
|
|
{/* ── TAB: CATEGORIAS ── */}
|
|
{tab === "categorias" && (
|
|
<div className="space-y-3">
|
|
{categorias.map((cat) => (
|
|
<div key={cat.id} className="bg-card-bg rounded-xl border border-border p-4 flex items-center gap-4">
|
|
<div className="w-5 h-5 rounded-full shrink-0" style={{ backgroundColor: cat.color }} />
|
|
<div className="flex-1 min-w-0">
|
|
<div className="font-medium">{cat.nombre}</div>
|
|
{cat.descripcion && <div className="text-xs text-muted">{cat.descripcion}</div>}
|
|
</div>
|
|
<span className="text-xs text-muted">
|
|
{servicios.filter((s) => s.categoriaNombre === cat.nombre).length} servicios
|
|
</span>
|
|
<button
|
|
onClick={() => toggleCategoriaActivo(cat)}
|
|
className={clsx("px-3 py-1 rounded text-xs font-medium", cat.activo ? "bg-green-100 text-green-700" : "bg-gray-100 text-gray-500")}
|
|
>
|
|
{cat.activo ? "Activa" : "Inactiva"}
|
|
</button>
|
|
<button onClick={() => deleteCategoria(cat.id)} className="p-1.5 text-muted hover:text-red-500 rounded">
|
|
<Trash2 className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
))}
|
|
<CreateCategoriaInline onCreated={refresh} />
|
|
</div>
|
|
)}
|
|
|
|
{/* ── TAB: PAQUETES ── */}
|
|
{tab === "paquetes" && (
|
|
<div className="space-y-6">
|
|
<div className="flex justify-end">
|
|
<button onClick={handleCreatePaquete} className="flex items-center gap-2 px-4 py-2 bg-primary text-white rounded-lg text-sm font-medium hover:bg-primary-dark">
|
|
<Plus className="w-4 h-4" /> Nuevo Paquete
|
|
</button>
|
|
</div>
|
|
|
|
{paquetes.map((pk) => {
|
|
const isOpen = expandedPk.has(pk.id);
|
|
return (
|
|
<div key={pk.id} className="bg-card-bg rounded-xl border border-border overflow-hidden">
|
|
<div
|
|
onClick={() => setExpandedPk((prev) => { const n = new Set(prev); if (n.has(pk.id)) n.delete(pk.id); else n.add(pk.id); return n; })}
|
|
className="w-full flex items-center gap-3 px-5 py-4 text-left hover:bg-gray-50 cursor-pointer"
|
|
>
|
|
{isOpen ? <ChevronDown className="w-4 h-4" /> : <ChevronRight className="w-4 h-4" />}
|
|
<div className="flex-1">
|
|
<div className="font-semibold text-lg">{pk.nombre}</div>
|
|
{pk.descripcion && <div className="text-xs text-muted">{pk.descripcion}</div>}
|
|
</div>
|
|
<span className="text-xs text-muted">{pk.fases.length} fases</span>
|
|
<button onClick={(e) => { e.stopPropagation(); deletePaquete(pk.id); }} className="p-1.5 text-muted hover:text-red-500 rounded">
|
|
<Trash2 className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
|
|
{isOpen && (
|
|
<div className="px-5 pb-4 space-y-4">
|
|
{pk.fases.map((fase) => (
|
|
<div key={fase.id} className="border border-border rounded-lg overflow-hidden">
|
|
<div className="flex items-center gap-2 px-4 py-2 bg-gray-50">
|
|
<span className="font-medium text-sm">{fase.nombre}</span>
|
|
<span className="text-xs text-muted">({fase.servicios.length} servicios)</span>
|
|
<div className="ml-auto">
|
|
<button onClick={() => setAddModal({ paqueteId: pk.id, faseId: fase.id })} className="text-xs text-primary hover:underline mr-3">
|
|
+ Agregar servicio
|
|
</button>
|
|
<button onClick={() => deleteFase(pk.id, fase.id)} className="text-xs text-red-500 hover:underline">
|
|
Eliminar fase
|
|
</button>
|
|
</div>
|
|
</div>
|
|
{fase.servicios.length > 0 ? (
|
|
<table className="w-full text-sm table-fixed">
|
|
<tbody>
|
|
{fase.servicios.map((s) => (
|
|
<tr key={s.id} className="border-t border-border">
|
|
<td className="px-4 py-2 align-top">
|
|
<div className="font-medium flex flex-wrap items-center gap-2 min-w-0">
|
|
<span className="break-words">{s.nombre}</span>
|
|
{s.nivel && <span className="inline-block px-2 py-0.5 rounded text-xs font-medium bg-amber-100 text-amber-700">{s.nivel}</span>}
|
|
</div>
|
|
</td>
|
|
<td className="px-4 py-2 align-top w-28">
|
|
<span className={clsx("text-xs px-2 py-0.5 rounded", s.tipoPago === "unico" ? "bg-blue-100 text-blue-700" : "bg-green-100 text-green-700")}>
|
|
{s.tipoPago === "unico" ? "Unico" : "Mensual"}
|
|
</span>
|
|
</td>
|
|
<td className="px-4 py-2 text-right font-medium text-sm align-top w-32">{formatCurrency(s.precioBase)}</td>
|
|
<td className="px-4 py-2 w-10 align-top">
|
|
<button onClick={() => removeServFromFase(pk.id, fase.id, s.id)} className="text-muted hover:text-red-500">
|
|
<X className="w-4 h-4" />
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
) : (
|
|
<p className="px-4 py-3 text-xs text-muted">Sin servicios asignados</p>
|
|
)}
|
|
</div>
|
|
))}
|
|
<button onClick={() => addFaseToPaquete(pk.id)} className="text-sm text-primary hover:underline flex items-center gap-1">
|
|
<Plus className="w-3.5 h-3.5" /> Agregar fase
|
|
</button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
|
|
{/* ── MODAL: Add service to phase ── */}
|
|
{addModal && (
|
|
<AddServicioModal
|
|
servicios={servicios.filter((s) => s.activo)}
|
|
onAdd={(servicioId) => {
|
|
addServToFase(addModal.paqueteId, addModal.faseId, servicioId);
|
|
setAddModal(null);
|
|
}}
|
|
onClose={() => setAddModal(null)}
|
|
/>
|
|
)}
|
|
|
|
{(servicioModal || showNewServicio) && (
|
|
<ServicioFormModal
|
|
servicio={servicioModal}
|
|
categorias={categorias}
|
|
onSaved={(guardado) => {
|
|
if (servicioModal) {
|
|
setServicios((p) => p.map((s) => (s.id === guardado.id ? { ...s, ...guardado } : s)));
|
|
showToast("Servicio actualizado", "success");
|
|
} else {
|
|
setServicios((p) => [...p, guardado]);
|
|
showToast("Servicio creado", "success");
|
|
}
|
|
setServicioModal(null);
|
|
setShowNewServicio(false);
|
|
}}
|
|
onClose={() => { setServicioModal(null); setShowNewServicio(false); }}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function CreateCategoriaInline({ onCreated }: { onCreated: () => void }) {
|
|
const [nombre, setNombre] = useState("");
|
|
const [color, setColor] = useState("#6b7280");
|
|
const [saving, setSaving] = useState(false);
|
|
const showToast = useToast();
|
|
|
|
const handleSave = async () => {
|
|
if (!nombre.trim()) return;
|
|
setSaving(true);
|
|
try {
|
|
const res = await fetch("/api/categorias", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ nombre: nombre.trim(), color }),
|
|
});
|
|
if (res.ok) { setNombre(""); onCreated(); }
|
|
else { const e = await res.json(); showToast(e.error, "error"); }
|
|
} finally { setSaving(false); }
|
|
};
|
|
|
|
return (
|
|
<div className="flex items-center gap-3 bg-card-bg rounded-xl border border-dashed border-border p-4">
|
|
<input type="color" value={color} onChange={(e) => setColor(e.target.value)} className="w-8 h-8 rounded cursor-pointer p-0 border-0" />
|
|
<input
|
|
type="text"
|
|
value={nombre}
|
|
onChange={(e) => setNombre(e.target.value)}
|
|
onKeyDown={(e) => e.key === "Enter" && handleSave()}
|
|
placeholder="Nueva categoria..."
|
|
className="flex-1 px-3 py-2 border border-border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary"
|
|
/>
|
|
<button onClick={handleSave} disabled={saving || !nombre.trim()} className="flex items-center gap-2 px-3 py-2 bg-primary text-white rounded-lg text-sm disabled:opacity-50">
|
|
{saving ? <Loader2 className="w-4 h-4 animate-spin" /> : <Plus className="w-4 h-4" />}
|
|
Crear
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function AddServicioModal({
|
|
servicios,
|
|
onAdd,
|
|
onClose,
|
|
}: {
|
|
servicios: Servicio[];
|
|
onAdd: (id: string) => void;
|
|
onClose: () => void;
|
|
}) {
|
|
const [search, setSearch] = useState("");
|
|
const filtered = servicios.filter(
|
|
(s) =>
|
|
s.nombre.toLowerCase().includes(search.toLowerCase()) ||
|
|
s.categoriaNombre.toLowerCase().includes(search.toLowerCase())
|
|
);
|
|
|
|
return (
|
|
<div className="fixed inset-0 bg-black/40 z-50 flex items-center justify-center p-4" onClick={onClose}>
|
|
<div className="bg-white rounded-xl shadow-xl w-full max-w-lg max-h-[70vh] flex flex-col" onClick={(e) => e.stopPropagation()}>
|
|
<div className="px-5 py-4 border-b border-border flex items-center justify-between">
|
|
<h3 className="font-semibold">Agregar servicio a la fase</h3>
|
|
<button onClick={onClose}><X className="w-5 h-5 text-muted" /></button>
|
|
</div>
|
|
<div className="px-5 py-3 border-b border-border">
|
|
<input
|
|
type="text"
|
|
value={search}
|
|
onChange={(e) => setSearch(e.target.value)}
|
|
placeholder="Buscar por nombre o categoria..."
|
|
className="w-full px-3 py-2 border border-border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20"
|
|
autoFocus
|
|
/>
|
|
</div>
|
|
<div className="overflow-y-auto flex-1">
|
|
{filtered.map((s) => (
|
|
<button
|
|
key={s.id}
|
|
onClick={() => onAdd(s.id)}
|
|
className="w-full text-left px-5 py-3 hover:bg-gray-50 border-b border-border/50 flex items-center gap-3"
|
|
>
|
|
<div className="w-2 h-2 rounded-full shrink-0" style={{ backgroundColor: s.categoriaColor }} />
|
|
<div className="flex-1 min-w-0">
|
|
<div className="font-medium text-sm truncate">{s.nombre}</div>
|
|
<div className="text-xs text-muted">{s.categoriaNombre} · {s.tipoPago === "unico" ? "Unico" : "Mensual"}</div>
|
|
</div>
|
|
<span className="text-sm font-medium">{formatCurrency(s.precioBase)}</span>
|
|
</button>
|
|
))}
|
|
{filtered.length === 0 && <p className="text-center text-muted text-sm py-8">Sin resultados</p>}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|