import React from 'react';

const rows = [
  { process: '#4711', type: 'TÜV', equipment: 'DT-1004 · Flasche', customer: 'Maria Keller', due: 'overdue', workflow: 'in Prüfung', next: 'Dokumente prüfen', action: 'UC02', dueTone: 'red', workflowTone: 'blue' },
  { process: '#4720', type: 'TÜV', equipment: 'DT-1088 · Flasche', customer: 'Sara Frei', due: 'due_soon', workflow: 'wartend', next: 'Kundeninfo offen', action: 'FR-07', dueTone: 'yellow', workflowTone: 'yellow' },
  { process: '#4802', type: 'Service', equipment: 'DT-1220 · Regler', customer: 'Unbekannt', due: 'unknown', workflow: 'blockiert', next: 'Daten prüfen', action: 'UC04', dueTone: 'gray', workflowTone: 'red' },
];

const metrics = [
  { label: 'laufend', value: 12, tone: 'blue' },
  { label: 'kritisch', value: 3, tone: 'red' },
  { label: 'offener Schritt', value: 5, tone: 'yellow' },
  { label: 'blockiert', value: 2, tone: 'red' },
  { label: 'nicht zugewiesen', value: 1, tone: 'gray' },
];

function Badge({ children, tone = 'gray' }) {
  const tones = {
    blue: 'bg-blue-100 text-blue-800 border-blue-200',
    yellow: 'bg-yellow-100 text-yellow-800 border-yellow-200',
    red: 'bg-red-100 text-red-800 border-red-200',
    gray: 'bg-slate-100 text-slate-700 border-slate-200',
  };
  return <span className={`inline-flex rounded-full border px-3 py-1 text-xs font-semibold ${tones[tone]}`}>{children}</span>;
}

function MetricCard({ label, value, tone }) {
  const tones = {
    blue: 'border-blue-200 bg-blue-50 text-blue-900',
    yellow: 'border-yellow-200 bg-yellow-50 text-yellow-900',
    red: 'border-red-200 bg-red-50 text-red-900',
    gray: 'border-slate-200 bg-slate-50 text-slate-900',
  };
  return (
    <div className={`rounded-2xl border p-4 ${tones[tone]}`}>
      <p className="text-3xl font-bold">{value}</p>
      <p className="mt-1 text-sm font-semibold">{label}</p>
    </div>
  );
}

export default function FR06LaufendePruefServiceprozesse() {
  return (
    <div className="min-h-screen bg-slate-50 p-6 text-slate-900">
      <div className="mx-auto max-w-7xl space-y-5">
        <header className="rounded-2xl border border-sky-200 bg-sky-50 p-6 shadow-sm">
          <div className="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
            <div>
              <p className="text-sm font-semibold uppercase tracking-wide text-sky-700">UC05 · FR-06</p>
              <h1 className="mt-1 text-3xl font-bold">Laufende Prüf- und Serviceprozesse</h1>
              <p className="mt-2 max-w-3xl text-sm text-slate-600">Koordinierende Übersicht aktiver Workflows mit Fristbezug.</p>
            </div>
            <div className="flex flex-wrap gap-2">
              <button className="rounded-xl border border-slate-300 bg-white px-4 py-2 text-sm font-semibold text-slate-800">FR-02</button>
              <button className="rounded-xl border border-slate-300 bg-white px-4 py-2 text-sm font-semibold text-slate-800">FR-07</button>
              <button className="rounded-xl bg-sky-700 px-4 py-2 text-sm font-semibold text-white">Export</button>
            </div>
          </div>
        </header>

        <section className="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm">
          <h2 className="text-lg font-bold">Prozesskennzahlen</h2>
          <div className="mt-4 grid gap-4 sm:grid-cols-2 lg:grid-cols-5">
            {metrics.map((metric) => <MetricCard key={metric.label} {...metric} />)}
          </div>
          <p className="mt-3 text-sm text-slate-500">UC04 ist aktuell nur vorbereitet und noch nicht priorisiert.</p>
        </section>

        <section className="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm">
          <h2 className="text-lg font-bold">Filter</h2>
          <div className="mt-4 grid gap-3 lg:grid-cols-6">
            {['TÜV-Prüfung', 'in Prüfung', 'Friststatus', 'Verantwortlich', 'Kunde'].map((label) => (
              <div key={label} className="rounded-xl border border-slate-300 bg-slate-50 px-3 py-2 text-sm font-semibold text-slate-700">{label}</div>
            ))}
            <button className="rounded-xl bg-slate-900 px-3 py-2 text-sm font-semibold text-white">Filtern</button>
          </div>
        </section>

        <section className="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm">
          <div className="border-b border-slate-200 px-5 py-4">
            <h2 className="text-lg font-bold">Laufende Prozesse</h2>
            <p className="text-sm text-slate-500">Read-only: Workflow-Bearbeitung erfolgt in UC02 / UC04.</p>
          </div>
          <div className="overflow-x-auto">
            <table className="w-full min-w-[1100px] text-left text-sm">
              <thead className="bg-slate-100 text-xs uppercase tracking-wide text-slate-600">
                <tr>
                  <th className="px-5 py-3">Prozess</th>
                  <th className="px-5 py-3">Art</th>
                  <th className="px-5 py-3">Equipment</th>
                  <th className="px-5 py-3">Kunde</th>
                  <th className="px-5 py-3">Frist</th>
                  <th className="px-5 py-3">Workflow</th>
                  <th className="px-5 py-3">nächster Schritt</th>
                  <th className="px-5 py-3">Aktion</th>
                </tr>
              </thead>
              <tbody className="divide-y divide-slate-100">
                {rows.map((row) => (
                  <tr key={row.process} className="hover:bg-slate-50">
                    <td className="px-5 py-4 font-semibold">{row.process}</td>
                    <td className="px-5 py-4 text-slate-600">{row.type}</td>
                    <td className="px-5 py-4 font-semibold">{row.equipment}</td>
                    <td className="px-5 py-4 text-slate-600">{row.customer}</td>
                    <td className="px-5 py-4"><Badge tone={row.dueTone}>{row.due}</Badge></td>
                    <td className="px-5 py-4"><Badge tone={row.workflowTone}>{row.workflow}</Badge></td>
                    <td className="px-5 py-4 text-slate-600">{row.next}</td>
                    <td className="px-5 py-4"><button className="rounded-lg border border-slate-300 px-3 py-1 text-xs font-semibold text-slate-700">{row.action}</button></td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        </section>

        <section className="rounded-2xl border border-slate-200 bg-white p-5 shadow-sm">
          <h2 className="text-lg font-bold">Schnellaktionen</h2>
          <div className="mt-4 flex flex-wrap gap-2">
            <button className="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700">FR-02 Liste</button>
            <button className="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700">FR-07 Vorschau</button>
            <button className="rounded-xl border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700">FR-08 Export</button>
          </div>
        </section>
      </div>
    </div>
  );
}
