'use client';

import React from 'react';
import { Card } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
import { Eye } from 'lucide-react';
import { Enterprise, AuditStatus } from '../types';

interface EnterpriseListProps {
  enterprises: Enterprise[];
  onViewDetail: (enterprise: Enterprise) => void;
}

export function EnterpriseList({ enterprises, onViewDetail }: EnterpriseListProps) {
  const getAuditStatusBadge = (status: AuditStatus) => {
    switch (status) {
      case 'pending':
        return <Badge className="bg-yellow-100 text-yellow-700">待审核</Badge>;
      case 'approved':
        return <Badge className="bg-green-100 text-green-700">已通过</Badge>;
      case 'rejected':
        return <Badge className="bg-red-100 text-red-700">已驳回</Badge>;
      default:
        return <Badge>{status}</Badge>;
    }
  };

  return (
    <Card>
      <Table>
        <TableHeader>
          <TableRow>
            <TableHead>企业名称</TableHead>
            <TableHead>企业类型</TableHead>
            <TableHead>社会信用代码</TableHead>
            <TableHead>法人</TableHead>
            <TableHead>所在地区</TableHead>
            <TableHead>审核状态</TableHead>
            <TableHead>提交时间</TableHead>
            <TableHead>操作</TableHead>
          </TableRow>
        </TableHeader>
        <TableBody>
          {enterprises.length === 0 ? (
            <TableRow>
              <TableCell colSpan={8} className="text-center text-muted-foreground py-8">
                暂无数据
              </TableCell>
            </TableRow>
          ) : (
            enterprises.map((enterprise) => (
              <TableRow key={enterprise.id}>
                <TableCell className="font-medium">{enterprise.name}</TableCell>
                <TableCell className="text-muted-foreground">{enterprise.type}</TableCell>
                <TableCell className="text-muted-foreground">
                  <code className="text-xs">{enterprise.socialCreditCode}</code>
                </TableCell>
                <TableCell>{enterprise.legalPerson || '-'}</TableCell>
                <TableCell>{`${enterprise.province} ${enterprise.city}`}</TableCell>
                <TableCell>{getAuditStatusBadge(enterprise.auditStatus)}</TableCell>
                <TableCell className="text-muted-foreground">
                  {new Date(enterprise.createdAt).toLocaleDateString('zh-CN')}
                </TableCell>
                <TableCell>
                  <Button
                    variant="ghost"
                    size="sm"
                    onClick={() => onViewDetail(enterprise)}
                  >
                    <Eye className="w-4 h-4 mr-1" />
                    查看
                  </Button>
                </TableCell>
              </TableRow>
            ))
          )}
        </TableBody>
      </Table>
    </Card>
  );
}