import Link from 'next/link';
import type { Order } from '@prisma/client';
import { StatusBadge } from './StatusBadge';
import { templateById } from '@/lib/templates';
import { apShort } from '@/lib/ap';
export function OrderTable({ orders, admin = false }: { orders: (Order & { user?: { email: string } })[]; admin?: boolean }) {
  if (!orders.length) return <p className="border-t border-ink/15 px-4 py-6 text-[14.5px] text-ink-soft">Henüz sipariş yok.</p>;
  return (
    <div className="overflow-x-auto border-t border-ink/15">
      <table className="w-full text-left text-[13.5px]">
        <thead className="border-b border-ink/15"><tr className="[&>th]:tag [&>th]:px-3 [&>th]:py-2">
          <th>Parsel</th>{admin && <th>Kullanıcı</th>}<th>Şablon</th><th>Durum</th><th>Tarih</th></tr></thead>
        <tbody>
          {orders.map((o) => (
            <tr key={o.id} className="border-b border-ink/10 hover:bg-paper-2 [&>td]:px-3 [&>td]:py-2.5">
              <td><Link href={admin ? `/panel/siparisler/${o.id}` : `/panel/siparisler/${o.id}`} className="font-bold">{apShort(o.ada, o.parsel)}</Link><div className="text-[12px] text-ink-soft">{o.mahalle}, {o.ilce}</div></td>
              {admin && <td className="text-[12.5px]">{o.user?.email}</td>}
              <td>{templateById(o.template).name}</td>
              <td><StatusBadge s={o.status} />{o.status === 'RENDERING' && <span className="num ml-1 text-[12px]">%{o.progress}</span>}</td>
              <td className="num whitespace-nowrap text-[12px] text-ink-soft">{o.createdAt.toLocaleString('tr-TR', { day: '2-digit', month: '2-digit', hour: '2-digit', minute: '2-digit', timeZone: 'Europe/Istanbul' })}</td>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  );
}
