<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Matters Report - {{ config('app.name', 'Prolex') }}</title>
    <style>
        body { font-family: 'DejaVu Sans', sans-serif; font-size: 12px; line-height: 1.4; color: #333; }
        .header { text-align: center; margin-bottom: 30px; }
        .header h1 { font-size: 18px; margin-bottom: 5px; color: #1a1a1a; }
        .header h2 { font-size: 14px; margin-bottom: 2px; color: #666; }
        .meta-table { width: 100%; margin-bottom: 20px; border-collapse: collapse; }
        .meta-table th, .meta-table td { padding: 8px 4px; text-align: left; border-bottom: 1px solid #eee; font-size: 11px; }
        .meta-table th { font-weight: bold; color: #555; width: 30%; }
        .summary-section { margin-bottom: 25px; }
        .summary-title { font-size: 13px; font-weight: bold; margin-bottom: 8px; color: #1a1a1a; text-transform: uppercase; letter-spacing: 0.5px; }
        .summary-table { width: 100%; margin-bottom: 15px; border-collapse: collapse; }
        .summary-table th, .summary-table td { padding: 6px 8px; text-align: left; border: 1px solid #ddd; font-size: 11px; }
        .summary-table th { background: #f8f9fa; font-weight: bold; text-align: left; }
        .main-table { width: 100%; border-collapse: collapse; margin-top: 15px; }
        .main-table th, .main-table td { padding: 6px 8px; text-align: left; border: 1px solid #ddd; font-size: 10px; }
        .main-table th { background: #e9ecef; font-weight: bold; font-size: 11px; }
        .status-active { color: #28a745; font-weight: bold; }
        .status-inactive { color: #6c757d; }
        .text-right { text-align: right; }
        .text-center { text-align: center; }
        .page-break { page-break-before: always; }
        @page { margin: 20mm; }
    </style>
</head>
<body>
    <div class="header">
        <h1>{{ config('app.name', 'Prolex Law Firm') }}</h1>
        <h2>Matter Status Report</h2>

    </div>

    <table class="meta-table">
        <tr><th>Period</th><td>{{ \Carbon\Carbon::parse($reportData['pfrom'])->format('F j, Y') }} to {{ \Carbon\Carbon::parse($reportData['pto'])->format('F j, Y') }}</td></tr>
        <tr><th>Generated On</th><td>{{ now()->format('F j, Y \\a\\t g:i A') }}</td></tr>
        <tr><th>Total Matters</th><td class="text-right">{{ number_format($reportData['total_matters']) }}</td></tr>
        <tr><th>Open Matters</th><td class="text-right">{{ number_format($reportData['open_matters']) }}</td></tr>
        <tr><th>Closed Matters</th><td class="text-right">{{ number_format($reportData['closed_matters']) }}</td></tr>
    </table>

    <div class="summary-section">
        <div class="summary-title">Status Summary</div>
        <table class="summary-table">
            <thead>
                <tr><th>Status</th><th class="text-right">Count</th></tr>
            </thead>
            <tbody>
                @forelse($reportData['status_summary'] as $status => $count)
                    <tr><td>{{ $status }}</td><td class="text-right">{{ $count }}</td></tr>
                @empty
                    <tr><td colspan="2" class="text-center">No data</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="summary-section">
        <div class="summary-title">Type Summary</div>
        <table class="summary-table">
            <thead>
                <tr><th>Type</th><th class="text-right">Count</th></tr>
            </thead>
            <tbody>
                @forelse($reportData['type_summary'] as $type => $count)
                    <tr><td>{{ $type }}</td><td class="text-right">{{ $count }}</td></tr>
                @empty
                    <tr><td colspan="2" class="text-center">No data</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="summary-section">
        <div class="summary-title">Lawyer Summary (Top 5)</div>
        <table class="summary-table">
            <thead>
                <tr><th>Lawyer</th><th class="text-right">Count</th></tr>
            </thead>
            <tbody>
                @php $topLawyers = collect($reportData['lawyer_summary'])->sortDesc()->take(5); @endphp
                @forelse($topLawyers as $lawyer => $count)
                    <tr><td>{{ $lawyer }}</td><td class="text-right">{{ $count }}</td></tr>
                @empty
                    <tr><td colspan="2" class="text-center">No data</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="summary-section">
        <div class="summary-title">Matters Detail</div>
        <table class="main-table">
            <thead>
                <tr>
                    <th>Matter #</th>
                    <th>Title</th>
                    <th>Client</th>
                    <th>Lawyer</th>
                    <th>Status</th>
                    <th>Type</th>
                    <th>Priority</th>
                    <th>Category</th>
                    <th>Opened</th>
                    <th>Closed</th>
                </tr>
            </thead>
            <tbody>
                @forelse($reportData['matters'] as $matter)
                    <tr>
                        <td>{{ $matter->matter_number }}</td>
                        <td>{{ Str::limit($matter->matter_title, 40) }}</td>
                        <td>{{ $matter->client_display ?? 'N/A' }}</td>
                        <td>{{ $matter->lawyer_display ?? 'Unassigned' }}</td>
                        <td class="status-{{ $matter->closed_date ? 'inactive' : 'active' }}">{{ $matter->status?->name ?? 'N/A' }}</td>
                        <td>{{ $matter->type?->name ?? 'N/A' }}</td>
                        <td>{{ $matter->priority?->name ?? 'N/A' }}</td>
                        <td>{{ $matter->category?->name ?? 'N/A' }}</td>
                        <td>{{ $matter->opened_date ? \Carbon\Carbon::parse($matter->opened_date)->format('M j, Y') : 'N/A' }}</td>
                        <td>{{ $matter->closed_date ? \Carbon\Carbon::parse($matter->closed_date)->format('M j, Y') : '-' }}</td>
                    </tr>
                @empty
                    <tr><td colspan="10" class="text-center">No matters found for the selected criteria.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</body>
</html>
