import { T as reactExports, K as jsxRuntimeExports } from "./server-ChzsfjB9.js";
import { f as useAuth, h as useNavigate, i as useSystemConfigStore, u as uploadSystemLogo } from "./router-uu4vGjiO.js";
import { a as AppLayout, C as Card, g as CardHeader, h as CardTitle, f as CardContent, B as Building2 } from "./card-HyNxoILF.js";
import { e as createLucideIcon, B as Button, L as LoaderCircle, I as Input } from "./input-muzHTpl5.js";
import { L as Label } from "./label-2bHC70jG.js";
import { U as Upload, a as Trash2, T as Textarea, S as Switch, M as MapPin } from "./textarea-CZpcpWD8.js";
import { t as toast } from "./index-DhnCtKbR.js";
import "node:async_hooks";
import "node:stream/web";
import "node:stream";
import "./index-BxBx9O65.js";
import "./index-CZazwQDH.js";
const __iconNode$2 = [
  ["rect", { width: "18", height: "18", x: "3", y: "3", rx: "2", ry: "2", key: "1m3agn" }],
  ["circle", { cx: "9", cy: "9", r: "2", key: "af1f0g" }],
  ["path", { d: "m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21", key: "1xmnt7" }]
];
const Image = createLucideIcon("image", __iconNode$2);
const __iconNode$1 = [
  [
    "path",
    {
      d: "M13.832 16.568a1 1 0 0 0 1.213-.303l.355-.465A2 2 0 0 1 17 15h3a2 2 0 0 1 2 2v3a2 2 0 0 1-2 2A18 18 0 0 1 2 4a2 2 0 0 1 2-2h3a2 2 0 0 1 2 2v3a2 2 0 0 1-.8 1.6l-.468.351a1 1 0 0 0-.292 1.233 14 14 0 0 0 6.392 6.384",
      key: "9njp5v"
    }
  ]
];
const Phone = createLucideIcon("phone", __iconNode$1);
const __iconNode = [
  [
    "path",
    {
      d: "M15.2 3a2 2 0 0 1 1.4.6l3.8 3.8a2 2 0 0 1 .6 1.4V19a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z",
      key: "1c8476"
    }
  ],
  ["path", { d: "M17 21v-7a1 1 0 0 0-1-1H8a1 1 0 0 0-1 1v7", key: "1ydtos" }],
  ["path", { d: "M7 3v4a1 1 0 0 0 1 1h7", key: "t51u73" }]
];
const Save = createLucideIcon("save", __iconNode);
const emptyForm = {
  titulo_sistema: "",
  razao_social: "",
  nome_fantasia: "",
  cnpj: "",
  ie: "",
  email: "",
  telefone: "",
  telefone_whatsapp: "",
  cep: "",
  endereco: "",
  numero: "",
  cidade: "",
  estado: "",
  descricao: "",
  logo_url: "",
  ativo: true
};
function Page() {
  const {
    user
  } = useAuth();
  const navigate = useNavigate();
  const {
    config,
    loaded,
    load,
    save
  } = useSystemConfigStore();
  const [form, setForm] = reactExports.useState(emptyForm);
  const [uploading, setUploading] = reactExports.useState(false);
  const [saving, setSaving] = reactExports.useState(false);
  const fileRef = reactExports.useRef(null);
  reactExports.useEffect(() => {
    if (user && user.role !== "admin") {
      navigate({
        to: "/"
      });
    }
  }, [user, navigate]);
  reactExports.useEffect(() => {
    if (!loaded) load();
  }, [loaded, load]);
  reactExports.useEffect(() => {
    if (config) setForm({
      ...emptyForm,
      ...config
    });
  }, [config]);
  const setField = (k, v) => setForm((f) => ({
    ...f,
    [k]: v
  }));
  const onUpload = async (file) => {
    setUploading(true);
    try {
      const url = await uploadSystemLogo(file);
      setField("logo_url", url);
      toast.success("Logo enviada");
    } catch (e) {
      toast.error(e instanceof Error ? e.message : "Erro ao enviar logo");
    } finally {
      setUploading(false);
    }
  };
  const onSave = async () => {
    setSaving(true);
    try {
      await save(form);
      toast.success(config?.id ? "Configurações atualizadas" : "Configurações salvas");
    } catch (e) {
      toast.error(e instanceof Error ? e.message : "Erro ao salvar");
    } finally {
      setSaving(false);
    }
  };
  if (user && user.role !== "admin") return null;
  return /* @__PURE__ */ jsxRuntimeExports.jsx(AppLayout, { children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "space-y-6 max-w-5xl", children: [
    /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex items-start justify-between gap-4 flex-wrap", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx("h1", { className: "text-2xl font-bold tracking-tight", children: "Configurações do Sistema" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "text-sm text-muted-foreground", children: "Dados institucionais usados em login, sidebar, topbar e relatórios." })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(Button, { onClick: onSave, disabled: saving, className: "h-10", children: [
        saving ? /* @__PURE__ */ jsxRuntimeExports.jsx(LoaderCircle, { className: "h-4 w-4 animate-spin" }) : /* @__PURE__ */ jsxRuntimeExports.jsx(Save, { className: "h-4 w-4" }),
        config?.id ? "Atualizar" : "Salvar"
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Card, { className: "overflow-hidden border-primary/20 bg-gradient-to-br from-primary/5 via-background to-accent/10", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(CardHeader, { className: "flex flex-row items-center gap-2 space-y-0", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Image, { className: "h-4 w-4 text-primary" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(CardTitle, { className: "text-base", children: "Identidade do sistema" })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsx(CardContent, { className: "space-y-6", children: /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "grid gap-6 md:grid-cols-[220px_1fr] items-start", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex flex-col items-center gap-3", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx("div", { className: "h-40 w-40 rounded-2xl border border-dashed border-border bg-card shadow-sm flex items-center justify-center overflow-hidden", children: form.logo_url ? /* @__PURE__ */ jsxRuntimeExports.jsx("img", { src: form.logo_url, alt: "Logo", className: "h-full w-full object-contain p-2" }) : /* @__PURE__ */ jsxRuntimeExports.jsx(Image, { className: "h-10 w-10 text-muted-foreground" }) }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "text-center", children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "text-sm font-bold tracking-tight truncate max-w-[180px]", children: form.titulo_sistema || "365-BI" }),
            /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "text-[10px] text-muted-foreground", children: "Pré-visualização" })
          ] }),
          /* @__PURE__ */ jsxRuntimeExports.jsx("input", { ref: fileRef, type: "file", accept: "image/png,image/jpeg,image/jpg,image/webp,image/svg+xml", className: "hidden", onChange: (e) => {
            const f = e.target.files?.[0];
            if (f) onUpload(f);
            e.target.value = "";
          } }),
          /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "flex gap-2", children: [
            /* @__PURE__ */ jsxRuntimeExports.jsxs(Button, { type: "button", size: "sm", variant: "outline", onClick: () => fileRef.current?.click(), disabled: uploading, children: [
              uploading ? /* @__PURE__ */ jsxRuntimeExports.jsx(LoaderCircle, { className: "h-3.5 w-3.5 animate-spin" }) : /* @__PURE__ */ jsxRuntimeExports.jsx(Upload, { className: "h-3.5 w-3.5" }),
              form.logo_url ? "Substituir" : "Enviar logo"
            ] }),
            form.logo_url && /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { type: "button", size: "sm", variant: "ghost", onClick: () => setField("logo_url", ""), className: "text-destructive hover:text-destructive", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Trash2, { className: "h-3.5 w-3.5" }) })
          ] })
        ] }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "space-y-4", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsxs(Field, { label: "Título do sistema", children: [
            /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.titulo_sistema ?? "", onChange: (e) => setField("titulo_sistema", e.target.value), placeholder: "Ex: 365-BI, Insight BI, ERP Analytics", className: "h-12 text-lg font-semibold" }),
            /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "text-xs text-muted-foreground mt-1.5", children: "Nome exibido no login, sidebar, topo do navegador e relatórios." })
          ] }),
          /* @__PURE__ */ jsxRuntimeExports.jsx(Field, { label: "Descrição do sistema", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Textarea, { rows: 3, value: form.descricao ?? "", onChange: (e) => setField("descricao", e.target.value), placeholder: "Breve descrição exibida em relatórios e tela de login." }) }),
          /* @__PURE__ */ jsxRuntimeExports.jsx("p", { className: "text-xs text-muted-foreground", children: "PNG, JPG, WEBP ou SVG. Recomendado fundo transparente." })
        ] })
      ] }) })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Card, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(CardHeader, { className: "flex flex-row items-center gap-2 space-y-0", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Building2, { className: "h-4 w-4 text-primary" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(CardTitle, { className: "text-base", children: "Dados cadastrais da empresa" })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(CardContent, { className: "grid gap-4 sm:grid-cols-2", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Field, { label: "Razão social", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.razao_social ?? "", onChange: (e) => setField("razao_social", e.target.value) }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Field, { label: "Nome fantasia", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.nome_fantasia ?? "", onChange: (e) => setField("nome_fantasia", e.target.value) }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Field, { label: "CNPJ", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.cnpj ?? "", onChange: (e) => setField("cnpj", e.target.value) }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Field, { label: "Inscrição estadual", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.ie ?? "", onChange: (e) => setField("ie", e.target.value) }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: "sm:col-span-2 flex items-center gap-3 pt-2", children: [
          /* @__PURE__ */ jsxRuntimeExports.jsx(Switch, { checked: form.ativo, onCheckedChange: (v) => setField("ativo", v) }),
          /* @__PURE__ */ jsxRuntimeExports.jsx(Label, { className: "cursor-pointer", children: "Sistema ativo" })
        ] })
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Card, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(CardHeader, { className: "flex flex-row items-center gap-2 space-y-0", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Phone, { className: "h-4 w-4 text-primary" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(CardTitle, { className: "text-base", children: "Contato" })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(CardContent, { className: "grid gap-4 sm:grid-cols-2", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Field, { label: "E-mail", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { type: "email", value: form.email ?? "", onChange: (e) => setField("email", e.target.value) }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Field, { label: "Telefone", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.telefone ?? "", onChange: (e) => setField("telefone", e.target.value) }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Field, { label: "WhatsApp", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.telefone_whatsapp ?? "", onChange: (e) => setField("telefone_whatsapp", e.target.value) }) })
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntimeExports.jsxs(Card, { children: [
      /* @__PURE__ */ jsxRuntimeExports.jsxs(CardHeader, { className: "flex flex-row items-center gap-2 space-y-0", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(MapPin, { className: "h-4 w-4 text-primary" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(CardTitle, { className: "text-base", children: "Endereço" })
      ] }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(CardContent, { className: "grid gap-4 sm:grid-cols-6", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Field, { label: "CEP", className: "sm:col-span-2", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.cep ?? "", onChange: (e) => setField("cep", e.target.value) }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Field, { label: "Endereço", className: "sm:col-span-3", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.endereco ?? "", onChange: (e) => setField("endereco", e.target.value) }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Field, { label: "Número", className: "sm:col-span-1", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.numero ?? "", onChange: (e) => setField("numero", e.target.value) }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Field, { label: "Cidade", className: "sm:col-span-4", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.cidade ?? "", onChange: (e) => setField("cidade", e.target.value) }) }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Field, { label: "Estado", className: "sm:col-span-2", children: /* @__PURE__ */ jsxRuntimeExports.jsx(Input, { value: form.estado ?? "", onChange: (e) => setField("estado", e.target.value) }) })
      ] })
    ] })
  ] }) });
}
function Field({
  label,
  children,
  className
}) {
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("div", { className: `space-y-1.5 ${className ?? ""}`, children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(Label, { className: "text-xs text-muted-foreground", children: label }),
    children
  ] });
}
export {
  Page as component
};
