"use client";
import { useState } from "react";
import type { CompanyProfile as CompanyProfileType } from "../lib/types";
type Props = {
profile: CompanyProfileType;
onSave: (profile: CompanyProfileType) => void;
};
export default function CompanyProfile({ profile, onSave }: Props) {
const [form, setForm] = useState(profile);
// Use local strings for editing lists
const [servicesStr, setServicesStr] = useState(profile.services.join(", "));
const [certsStr, setCertsStr] = useState(profile.certifications?.join(", ") || "");
const [regionsStr, setRegionsStr] = useState(profile.regions?.join(", ") || "");
const [docsStr, setDocsStr] = useState(profile.documents_available?.join(", ") || "");
const [keywordsStr, setKeywordsStr] = useState(profile.keywords?.join(", ") || "");
const [saving, setSaving] = useState(false);
const [saveStatus, setSaveStatus] = useState<"idle" | "saving" | "success" | "error">("idle");
const handleSave = async () => {
console.log("[CompanyProfile] Clicked Save Profile");
setSaving(true);
setSaveStatus("saving");
try {
const updatedProfile = {
...form,
services: servicesStr.split(",").map((s) => s.trim()).filter(Boolean),
certifications: certsStr.split(",").map((s) => s.trim()).filter(Boolean),
regions: regionsStr.split(",").map((s) => s.trim()).filter(Boolean),
documents_available: docsStr.split(",").map((s) => s.trim()).filter(Boolean),
keywords: keywordsStr.split(",").map((s) => s.trim()).filter(Boolean),
};
console.log("[CompanyProfile] Sending to onSave:", updatedProfile);
await onSave(updatedProfile);
setSaveStatus("success");
setTimeout(() => setSaveStatus("idle"), 3000);
} catch (e) {
console.error("[CompanyProfile] Save failed", e);
setSaveStatus("error");
setTimeout(() => setSaveStatus("idle"), 3000);
} finally {
setSaving(false);
}
};
return (
);
}