/**
 * filekorolheader: 企业审核表格组件 - 企业数据列表展示和管理界面
 * 功能：企业数据表格展示、搜索过滤、分页管理、操作按钮
 * 路径：/central-config/tenant/enterprise-audit/components/EnterpriseAuditTable
 * 规范：遵循crop-x/docs/开发项目规范.md，使用shadcn/ui组件，TypeScript类型安全
 */

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

interface EnterpriseAuditTableProps {
  enterprises: Enterprise[];
  loading: boolean;
  onViewDetails: (enterprise: Enterprise) => void;
}

export function EnterpriseAuditTable({
  enterprises,
  loading,
  onViewDetails
}: EnterpriseAuditTableProps) {
  const getAuditStatusBadge = (status: Enterprise['auditStatus']) => {
    switch (status) {
      case '草稿':
        return <Badge variant="secondary">草稿</Badge>;
      case '待审核':
        return <Badge className="bg-yellow-100 text-yellow-700">待审核</Badge>;
      case '已通过':
        return <Badge className="bg-green-100 text-green-700">已通过</Badge>;
      case '已驳回':
        return <Badge className="bg-red-100 text-red-700">已驳回</Badge>;
      default:
        return <Badge variant="outline">{status}</Badge>;
    }
  };

  const getStatusBadge = (status: Enterprise['status']) => {
    return status === 'active' ? (
      <Badge variant="default">启用</Badge>
    ) : (
      <Badge variant="secondary">禁用</Badge>
    );
  };

  if (loading) {
    return (
      <Card>
        <div className="flex items-center justify-center h-96">
          <div className="text-muted-foreground">加载中...</div>
        </div>
      </Card>
    );
  }

  return (
    <Card>
      <Table>
        <TableHeader>
          <TableRow>
            <TableHead>企业名称</TableHead>
            <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={9} 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 bg-gray-100 px-1 py-0.5 rounded">
                    {enterprise.socialCreditCode}
                  </code>
                </TableCell>
                <TableCell>{enterprise.legalPerson || '-'}</TableCell>
                <TableCell className="text-muted-foreground">
                  {enterprise.province && enterprise.city ?
                    `${enterprise.province} ${enterprise.city}` :
                    '-'
                  }
                </TableCell>
                <TableCell>{getAuditStatusBadge(enterprise.auditStatus)}</TableCell>
                <TableCell>{getStatusBadge(enterprise.status)}</TableCell>
                <TableCell className="text-muted-foreground">
                  {enterprise.submitTime || '-'}
                </TableCell>
                <TableCell>
                  <Button
                    variant="ghost"
                    size="sm"
                    onClick={() => onViewDetails(enterprise)}
                  >
                    <Eye className="w-4 h-4 mr-1" />
                    查看
                  </Button>
                </TableCell>
              </TableRow>
            ))
          )}
        </TableBody>
      </Table>
    </Card>
  );
}