import { useEffect, useState } from "react";

import { useAuth } from "../../auth/AuthProvider";
import { api } from "../../lib/api";

export function VendorsPage() {
  const { token } = useAuth();
  const [vendors, setVendors] = useState<Array<Record<string, string>>>([]);

  useEffect(() => {
    if (!token) {
      return;
    }
    api.getVendors(token).then(setVendors);
  }, [token]);

  return (
    <section className="rounded-[28px] border border-line/70 bg-surface/90 p-8 shadow-panel">
      <p className="text-xs font-semibold uppercase tracking-[0.24em] text-muted">CRM</p>
      <h3 className="mt-3 text-2xl font-bold text-text">Vendors</h3>
      <div className="mt-6 grid gap-4 md:grid-cols-2">
        {vendors.map((vendor) => (
          <article key={vendor.id} className="rounded-2xl border border-line/70 bg-page/70 p-5">
            <h4 className="text-lg font-bold text-text">{vendor.name}</h4>
            <p className="mt-2 text-sm text-muted">{vendor.email}</p>
            <p className="text-sm text-muted">{vendor.phone}</p>
            <p className="mt-3 text-sm text-muted">{vendor.city}, {vendor.state}</p>
          </article>
        ))}
      </div>
    </section>
  );
}

