import { S as reactExports, J as jsxRuntimeExports } from "./server-CZT6KZsz.js";
import { u as useSuspenseQuery } from "./useSuspenseQuery-CRUy30GM.js";
import { A as AppShell } from "./AppShell-CCqmKL3X.js";
import { U as useQueryClient, O as toast, H as supabase, x as particularesQO, w as ordenesQO, o as getInitials } from "./router-Bsssaenw.js";
import { E as useMutation, b as Dialog, c as DialogContent, f as DialogHeader, g as DialogTitle, L as Label, I as Input, T as Textarea, e as DialogFooter, B as Button, P as Plus } from "./textarea-BUwzaNv6.js";
import { M as Mail, P as Phone } from "./phone-CwN74hQO.js";
import { M as MapPin } from "./map-pin-2TMtQ5Pc.js";
import "node:async_hooks";
import "node:stream/web";
import "node:stream";
import "./package-D5atLoEx.js";
import "./wrench-CccwRKu3.js";
const empty = { nombre: "", email: "", telefono: "", direccion: "", notas: "" };
function NuevoParticularDialog({ open, onOpenChange }) {
  const qc = useQueryClient();
  const [form, setForm] = reactExports.useState(empty);
  const set = (k) => (e) => setForm((f) => ({ ...f, [k]: e.target.value }));
  const mut = useMutation({
    mutationFn: async () => {
      if (!form.nombre.trim()) throw new Error("El nombre es obligatorio");
      const payload = {
        nombre: form.nombre.trim(),
        email: form.email.trim() || null,
        telefono: form.telefono.trim() || null,
        direccion: form.direccion.trim() || null,
        notas: form.notas.trim() || null
      };
      const { error } = await supabase.from("clientes_particulares").insert(payload);
      if (error) throw error;
    },
    onSuccess: () => {
      toast.success("Cliente creado");
      qc.invalidateQueries({ queryKey: ["particulares"] });
      setForm(empty);
      onOpenChange(false);
    },
    onError: (e) => toast.error(e.message ?? "Error al crear")
  });
  return /* @__PURE__ */ jsxRuntimeExports.jsx(Dialog, { open, onOpenChange, children: /* @__PURE__ */ jsxRuntimeExports.jsxs(DialogContent, { className: "max-w-lg", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(DialogHeader, { children: /* @__PURE__ */ jsxRuntimeExports.jsx(DialogTitle, { children: "Nuevo Cliente Particular" }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "grid gap-3 py-2", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "grid gap-1.5", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Label, { children: "Nombre *" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.nombre, onChange: set("nombre"), autoFocus: true })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "grid grid-cols-2 gap-3", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "grid gap-1.5", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(Label, { children: "Email" }),
          /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { type: "email", value: form.email, onChange: set("email") })
        ] }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "grid gap-1.5", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(Label, { children: "Teléfono" }),
          /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.telefono, onChange: set("telefono") })
        ] })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "grid gap-1.5", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Label, { children: "Dirección" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.direccion, onChange: set("direccion") })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "grid gap-1.5", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Label, { children: "Notas" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Textarea, { rows: 3, value: form.notas, onChange: set("notas") })
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(DialogFooter, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { variant: "outline", onClick: () => onOpenChange(false), disabled: mut.isPending, children: "Cancelar" }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { onClick: () => mut.mutate(), disabled: mut.isPending, children: mut.isPending ? "Creando..." : "Crear" })
    ] })
  ] }) });
}
function ParticularesPage() {
  const {
    data: particulares
  } = useSuspenseQuery(particularesQO());
  const {
    data: ordenes
  } = useSuspenseQuery(ordenesQO());
  const [open, setOpen] = reactExports.useState(false);
  return /* @__PURE__ */ jsxRuntimeExports.jsx(AppShell, { children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "space-y-6", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(NuevoParticularDialog, { open, onOpenChange: setOpen }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex items-end justify-between gap-4 flex-wrap", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "text-xs uppercase tracking-widest text-muted-foreground font-mono", children: "Micro · On-demand" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("h1", { className: "text-3xl font-bold mt-1", children: "Clientes Particulares" }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs("p", { className: "text-muted-foreground mt-1", children: [
          particulares.length,
          " clientes registrados"
        ] })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs("button", { onClick: () => setOpen(true), className: "inline-flex items-center gap-2 px-4 py-2.5 rounded-md gradient-primary text-primary-foreground font-medium text-sm shadow-elevated", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Plus, { className: "size-4" }),
        " Nuevo Cliente"
      ] })
    ] }),
    particulares.length === 0 && /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "bg-card border border-border rounded-xl p-10 text-center text-sm text-muted-foreground", children: "No hay clientes particulares cargados." }),
    /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "grid md:grid-cols-2 lg:grid-cols-3 gap-4", children: particulares.map((p) => {
      const trabajos = ordenes.filter((o) => o.cliente_particular_id === p.id).length;
      return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "bg-card border border-border rounded-xl shadow-card p-5", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex items-center gap-3", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "size-12 rounded-full bg-accent text-accent-foreground grid place-items-center font-bold", children: getInitials(p.nombre) }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex-1 min-w-0", children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx("h3", { className: "font-semibold truncate", children: p.nombre }),
            /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "text-xs text-muted-foreground", children: [
              trabajos,
              " servicios históricos"
            ] })
          ] })
        ] }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "mt-4 space-y-1.5 text-xs text-muted-foreground", children: [
          p.email && /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "inline-flex items-center gap-2", children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx(Mail, { className: "size-3.5" }),
            " ",
            p.email
          ] }),
          p.telefono && /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex items-center gap-2", children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx(Phone, { className: "size-3.5" }),
            " ",
            p.telefono
          ] }),
          p.direccion && /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex items-start gap-2", children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx(MapPin, { className: "size-3.5 mt-0.5" }),
            " ",
            /* @__PURE__ */ jsxRuntimeExports.jsx("span", { children: p.direccion })
          ] })
        ] })
      ] }, p.id);
    }) })
  ] }) });
}
export {
  ParticularesPage as component
};
