import React from 'react';

const rows = [
  { prio: 'hoch', status: 'blocked', equipment: 'DT-1011 · Flasche', customer: 'Luca Meier', due: '2026-04-18', days: '-19', reason: 'Prüfung abgelaufen', workflow: 'in Prüfung', action: 'FR-03', tone: 'red' },
  { prio: 'hoch', status: 'overdue', equipment: 'DT-1004 · Flasche', customer: 'Maria Keller', due: '2026-04-25', days: '-12', reason: 'Frist überschritten', workflow: 'keiner', action: 'FR-03', tone: 'red' },
  { prio: 'hoch', status: 'blocked', equipment: 'DT-1191 · Flasche', customer: 'Nina Graf', due: '2026-02-05', days: '-91', reason: 'manuell gesperrt', workflow: 'keiner', action: 'UC01', tone: 'red' },
];

const metrics = [
  { label: 'kritisch gesamt', value: 10, tone: 'red' },
  { label: 'überfällig', value: 7, tone: 'red' },
  { label: 'gesperrt', value: 3, tone: 'red' },
  { label: 'ohne Workflow', value: 4, tone: 'yellow' },
  { label: 'mit Workflow', value: 6, tone: 'blue' },
];

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',
  };
  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 FR04UeberfaelligeGesperrteFristen() {
  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-04</p>
              <h1 className="mt-1 text-3xl font-bold">Überfällige / gesperrte Fristen</h1>
              <p className="mt-2 max-w-3xl text-sm text-slate-600">Fokussierte Risikoansicht für kritische Equipment-Fristen.</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">Risiko-Kennzahlen</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">Ältester Fall: 91 Tage überfällig.</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">
            {['blocked', 'overdue', 'Tage > 0', 'Kunde', 'Workflow'].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">Kritische Fristen</h2>
            <p className="text-sm text-slate-500">Read-only: keine direkte Änderung an Sperren oder Fristgrundlagen.</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">Prio</th>
                  <th className="px-5 py-3">Status</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">Tage</th>
                  <th className="px-5 py-3">Sperrgrund</th>
                  <th className="px-5 py-3">Workflow</th>
                  <th className="px-5 py-3">Aktion</th>
                </tr>
              </thead>
              <tbody className="divide-y divide-slate-100">
                {rows.map((row) => (
                  <tr key={`${row.equipment}-${row.status}`} className="hover:bg-slate-50">
                    <td className="px-5 py-4"><Badge tone={row.tone}>{row.prio}</Badge></td>
                    <td className="px-5 py-4"><Badge tone={row.tone}>{row.status}</Badge></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 text-slate-600">{row.due}</td>
                    <td className="px-5 py-4 font-semibold text-red-700">{row.days}</td>
                    <td className="px-5 py-4 text-slate-600">{row.reason}</td>
                    <td className="px-5 py-4 text-slate-600">{row.workflow}</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>
  );
}
