import { jsxs, Fragment, jsx } from "react/jsx-runtime";
import { Link } from "@tanstack/react-router";
import * as React from "react";
import { FileText, ShieldCheck, Truck, Clock, Trash2, Send, MessageCircle } from "lucide-react";
import { z as useQuote, A as useSiteSettings, B as Button, o as formatKsh, s as supabase, H as waLinkFor } from "./router-D4ixsMHb.js";
import { I as Input } from "./input-Cu7YTZO_.js";
import { L as Label, T as Textarea } from "./label-GLGnKkqd.js";
import { toast } from "sonner";
import { h as heroImg } from "./hero-TkSUT3Ve.js";
import "@tanstack/react-query";
import "@radix-ui/react-slot";
import "class-variance-authority";
import "clsx";
import "tailwind-merge";
import "cmdk";
import "@radix-ui/react-dialog";
import "@supabase/supabase-js";
import "@radix-ui/react-label";
function QuotePage() {
  const {
    items,
    remove,
    setQty,
    total,
    clear
  } = useQuote();
  const [submitting, setSubmitting] = React.useState(false);
  const site = useSiteSettings();
  async function handleSubmit(e) {
    e.preventDefault();
    setSubmitting(true);
    const form = e.currentTarget;
    const fd = new FormData(form);
    const payload = {
      name: String(fd.get("name") || "").trim(),
      email: String(fd.get("email") || "").trim() || null,
      phone: String(fd.get("phone") || "").trim() || null,
      clinic: String(fd.get("clinic") || "").trim() || null,
      message: [String(fd.get("city") || ""), String(fd.get("notes") || "")].filter(Boolean).join("\n") || null,
      items: items.map((i) => ({
        id: i.product.id,
        name: i.product.name,
        price: i.product.price,
        quantity: i.quantity
      }))
    };
    const {
      error
    } = await supabase.from("quote_requests").insert(payload);
    setSubmitting(false);
    if (error) {
      toast.error("Could not send", {
        description: error.message
      });
      return;
    }
    toast.success("Quote request received!", {
      description: "Our team will respond to you within one business day."
    });
    form.reset();
    clear();
  }
  function whatsappQuote() {
    if (items.length === 0) return;
    const lines = items.map((i) => `• ${i.product.name} x${i.quantity} — ${formatKsh(i.product.price * i.quantity)}`);
    const msg = `Hello ${site.name}, I'd like a quote for:

${lines.join("\n")}

Estimated total: ${formatKsh(total)}`;
    window.open(waLinkFor(site.whatsapp, msg), "_blank");
  }
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsxs("section", { className: "relative overflow-hidden", children: [
      /* @__PURE__ */ jsx("div", { className: "absolute inset-0 bg-cover bg-center", style: {
        backgroundImage: `url(${heroImg})`
      } }),
      /* @__PURE__ */ jsx("div", { className: "absolute inset-0 bg-gradient-to-r from-brand/95 via-brand/80 to-brand/30" }),
      /* @__PURE__ */ jsx("div", { className: "relative container mx-auto px-4 py-14 md:px-6 md:py-20", children: /* @__PURE__ */ jsxs("div", { className: "max-w-2xl text-white", children: [
        /* @__PURE__ */ jsxs("span", { className: "inline-flex items-center gap-1.5 rounded-full bg-white/20 px-3 py-1 text-xs font-medium backdrop-blur-sm", children: [
          /* @__PURE__ */ jsx(FileText, { className: "h-3.5 w-3.5" }),
          "Official Quotation"
        ] }),
        /* @__PURE__ */ jsx("h1", { className: "mt-4 text-3xl font-bold md:text-4xl lg:text-5xl", children: "Request a Quote" }),
        /* @__PURE__ */ jsx("p", { className: "mt-3 text-base text-white/90 md:text-lg", children: "Add items while browsing, then submit for an official quotation. We'll respond within one business day." }),
        /* @__PURE__ */ jsxs("div", { className: "mt-6 flex flex-wrap items-center gap-4 text-sm text-white/80", children: [
          /* @__PURE__ */ jsxs("span", { className: "inline-flex items-center gap-1.5", children: [
            /* @__PURE__ */ jsx(ShieldCheck, { className: "h-4 w-4" }),
            "Authentic stock"
          ] }),
          /* @__PURE__ */ jsxs("span", { className: "inline-flex items-center gap-1.5", children: [
            /* @__PURE__ */ jsx(Truck, { className: "h-4 w-4" }),
            "Nationwide delivery"
          ] }),
          /* @__PURE__ */ jsxs("span", { className: "inline-flex items-center gap-1.5", children: [
            /* @__PURE__ */ jsx(Clock, { className: "h-4 w-4" }),
            "24hr response"
          ] })
        ] })
      ] }) })
    ] }),
    /* @__PURE__ */ jsx("section", { className: "container mx-auto px-4 py-10 md:px-6 md:py-14", children: /* @__PURE__ */ jsxs("div", { className: "mt-10 grid gap-8 lg:grid-cols-[1fr_420px]", children: [
      /* @__PURE__ */ jsxs("div", { className: "rounded border border-border bg-card p-4 shadow-[var(--shadow-card)] md:p-6", children: [
        /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
          /* @__PURE__ */ jsxs("h2", { className: "text-lg font-semibold", children: [
            "Your items (",
            items.length,
            ")"
          ] }),
          items.length > 0 && /* @__PURE__ */ jsx("button", { onClick: clear, className: "text-xs font-medium text-muted-foreground hover:text-destructive", children: "Clear all" })
        ] }),
        items.length === 0 ? /* @__PURE__ */ jsxs("div", { className: "mt-8 rounded-sm border border-dashed border-border p-10 text-center", children: [
          /* @__PURE__ */ jsx(FileText, { className: "mx-auto h-8 w-8 text-muted-foreground" }),
          /* @__PURE__ */ jsx("p", { className: "mt-3 font-medium text-foreground", children: "Your quote list is empty" }),
          /* @__PURE__ */ jsx("p", { className: "mt-1 text-sm text-muted-foreground", children: "Browse products and tap the quote button to add items." }),
          /* @__PURE__ */ jsx(Link, { to: "/products", className: "mt-5 inline-block", children: /* @__PURE__ */ jsx(Button, { children: "Browse products" }) })
        ] }) : /* @__PURE__ */ jsx("ul", { className: "mt-4 divide-y divide-border", children: items.map(({
          product,
          quantity
        }) => /* @__PURE__ */ jsxs("li", { className: "flex items-center gap-4 py-4", children: [
          /* @__PURE__ */ jsx("img", { src: product.image, alt: product.name, loading: "lazy", className: "h-16 w-16 flex-shrink-0 rounded-sm object-cover" }),
          /* @__PURE__ */ jsxs("div", { className: "min-w-0 flex-1", children: [
            /* @__PURE__ */ jsx(Link, { to: "/products/$slug", params: {
              slug: product.slug
            }, className: "line-clamp-1 font-medium text-foreground hover:text-brand-bright", children: product.name }),
            /* @__PURE__ */ jsxs("p", { className: "text-xs text-muted-foreground", children: [
              formatKsh(product.price),
              " each"
            ] })
          ] }),
          /* @__PURE__ */ jsx("input", { type: "number", min: 1, value: quantity, onChange: (e) => setQty(product.id, Number(e.target.value) || 1), className: "h-9 w-16 rounded-md border border-input bg-background px-2 text-center text-sm" }),
          /* @__PURE__ */ jsx("p", { className: "hidden w-28 text-right text-sm font-semibold text-brand sm:block", children: formatKsh(product.price * quantity) }),
          /* @__PURE__ */ jsx("button", { onClick: () => remove(product.id), className: "text-muted-foreground hover:text-destructive", "aria-label": "Remove", children: /* @__PURE__ */ jsx(Trash2, { className: "h-4 w-4" }) })
        ] }, product.id)) }),
        items.length > 0 && /* @__PURE__ */ jsxs("div", { className: "mt-4 flex items-center justify-between border-t border-border pt-4", children: [
          /* @__PURE__ */ jsx("span", { className: "text-sm text-muted-foreground", children: "Estimated total" }),
          /* @__PURE__ */ jsx("span", { className: "text-xl font-bold text-brand", children: formatKsh(total) })
        ] })
      ] }),
      /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, className: "space-y-4 rounded border border-border bg-card p-4 shadow-[var(--shadow-card)] md:p-6", children: [
        /* @__PURE__ */ jsx("h2", { className: "text-lg font-semibold", children: "Your details" }),
        /* @__PURE__ */ jsxs("div", { className: "grid gap-3 sm:grid-cols-2", children: [
          /* @__PURE__ */ jsxs("div", { className: "space-y-1.5", children: [
            /* @__PURE__ */ jsx(Label, { htmlFor: "name", children: "Full name *" }),
            /* @__PURE__ */ jsx(Input, { id: "name", required: true, placeholder: "Dr. Jane Mwangi" })
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "space-y-1.5", children: [
            /* @__PURE__ */ jsx(Label, { htmlFor: "clinic", children: "Clinic / Organisation" }),
            /* @__PURE__ */ jsx(Input, { id: "clinic", placeholder: "Bright Smile Dental" })
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "space-y-1.5", children: [
            /* @__PURE__ */ jsx(Label, { htmlFor: "email", children: "Email *" }),
            /* @__PURE__ */ jsx(Input, { id: "email", type: "email", required: true, placeholder: "you@clinic.co.ke" })
          ] }),
          /* @__PURE__ */ jsxs("div", { className: "space-y-1.5", children: [
            /* @__PURE__ */ jsx(Label, { htmlFor: "phone", children: "Phone *" }),
            /* @__PURE__ */ jsx(Input, { id: "phone", required: true, placeholder: "+254 7xx xxx xxx" })
          ] })
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "space-y-1.5", children: [
          /* @__PURE__ */ jsx(Label, { htmlFor: "city", children: "Delivery city" }),
          /* @__PURE__ */ jsx(Input, { id: "city", placeholder: "Nairobi" })
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "space-y-1.5", children: [
          /* @__PURE__ */ jsx(Label, { htmlFor: "notes", children: "Notes" }),
          /* @__PURE__ */ jsx(Textarea, { id: "notes", rows: 3, placeholder: "Any specific brand, sizes or delivery timeline." })
        ] }),
        /* @__PURE__ */ jsxs("div", { className: "grid gap-2 pt-2", children: [
          /* @__PURE__ */ jsxs(Button, { type: "submit", size: "lg", disabled: submitting || items.length === 0, className: "h-auto min-w-0 flex-wrap justify-center gap-2 whitespace-normal px-4 py-3 text-center leading-tight", children: [
            /* @__PURE__ */ jsx(Send, { className: "h-4 w-4" }),
            " ",
            submitting ? "Sending…" : "Submit Quote Request"
          ] }),
          /* @__PURE__ */ jsxs(Button, { type: "button", size: "lg", onClick: whatsappQuote, disabled: items.length === 0, className: "h-auto min-w-0 flex-wrap justify-center gap-2 whitespace-normal px-4 py-3 text-center leading-tight text-white hover:opacity-90", style: {
            background: "#25D366"
          }, children: [
            /* @__PURE__ */ jsx(MessageCircle, { className: "h-4 w-4" }),
            " Send via WhatsApp"
          ] })
        ] }),
        items.length === 0 && /* @__PURE__ */ jsx("p", { className: "text-xs text-muted-foreground", children: "Add at least one product to submit." })
      ] })
    ] }) })
  ] });
}
export {
  QuotePage as component
};
