<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Matter Detail</title>
    <style>
        body { font-family: Arial, sans-serif; font-size: 11px; margin: 16px; color: #111; }
        h3, h4 { margin: 0; }
        .line { border-top: 1px solid #888; margin: 7px 0; }
        .section { margin-top: 12px; }
        table { width: 100%; border-collapse: collapse; margin-top: 6px; }
        th, td { border: 1px solid #ddd; padding: 5px; vertical-align: top; }
        th { background: #f2f4f7; text-align: left; }
        .text-right { text-align: right; }
        .muted { color: #555; }
    </style>
</head>
<body>
    @include('modules.reports.partials.pdf-company-details')
    <div style="text-align:center;">
        <h3>MATTER DETAIL REPORT</h3>
    </div>
    <div class="line"></div>

    <div><strong>Matter:</strong> {{ $matter->matter_number }} — {{ $matter->matter_title }}</div>
    <div><strong>Status:</strong> {{ $matter->status?->status_name ?? 'N/A' }}</div>
    <div><strong>Type:</strong> {{ $matter->type?->type_name ?? 'N/A' }}</div>
    <div><strong>Opened:</strong> {{ $matter->opened_date ? \Carbon\Carbon::parse($matter->opened_date)->format('M j, Y') : 'N/A' }}</div>
    <div><strong>Closed:</strong> {{ $matter->closed_date ? \Carbon\Carbon::parse($matter->closed_date)->format('M j, Y') : '-' }}</div>
    <div><strong>Generated:</strong> {{ now()->format('M j, Y H:i') }}</div>

    <div class="section">
        <strong>Summary</strong>
        <table>
            <tbody>
                <tr><td>Invoices Total</td><td class="text-right">{{ number_format((float)($totals['invoices_total'] ?? 0), 2) }}</td></tr>
                <tr><td>Receipts Total</td><td class="text-right">{{ number_format((float)($totals['receipts_total'] ?? 0), 2) }}</td></tr>
                <tr><td><strong>Balance</strong></td><td class="text-right"><strong>{{ number_format((float)($totals['balance'] ?? 0), 2) }}</strong></td></tr>
                <tr><td>Cases</td><td class="text-right">{{ count($matterCases ?? []) }}</td></tr>
            </tbody>
        </table>
    </div>

    <div class="section">
        <strong>Clients</strong>
        <table>
            <tbody>
                @forelse(($matter->clients ?? collect()) as $c)
                    <tr><td>{{ $c->full_name ?? $c->fullName ?? $c->name ?? 'Client' }}</td></tr>
                @empty
                    <tr><td class="muted">No clients linked.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="section">
        <strong>Lawyers</strong>
        <table>
            <tbody>
                @forelse(($matterLawyers ?? collect()) as $l)
                    @php
                        $nm = trim(($l->first_name ?? '') . ' ' . ($l->last_name ?? ''));
                        if ($nm === '') $nm = (string) ($l->lawyer_id ?? 'Lawyer');
                    @endphp
                    <tr>
                        <td>{{ $nm }}</td>
                        <td style="width: 18%;">{{ !empty($l->is_lead_lawyer) ? 'Lead' : '' }}</td>
                    </tr>
                @empty
                    <tr><td class="muted">No lawyers linked.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="section">
        <strong>Cases</strong>
        <table>
            <thead>
                <tr>
                    <th>Case #</th>
                    <th>Case Name</th>
                    <th>Client</th>
                    <th>Type</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                @forelse(($matterCases ?? collect()) as $case)
                    <tr>
                        <td>{{ $case->case_id ?? ('CASE-' . $case->id) }}</td>
                        <td>{{ $case->case_name ?? 'N/A' }}</td>
                        <td>{{ optional($case->client)->full_name ?? 'N/A' }}</td>
                        <td>{{ optional($case->caseType)->type_name ?? ($case->type_id ?? 'N/A') }}</td>
                        <td>{{ optional($case->caseStatus)->status_name ?? ($case->status_code ?? 'N/A') }}</td>
                    </tr>
                @empty
                    <tr><td colspan="5" style="text-align:center;">No cases linked to this matter.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="section">
        <strong>Invoices</strong>
        <table>
            <thead>
                <tr>
                    <th>Invoice #</th>
                    <th>Date</th>
                    <th>Status</th>
                    <th class="text-right">Total</th>
                    <th>Currency</th>
                </tr>
            </thead>
            <tbody>
                @forelse(($invoices ?? collect()) as $inv)
                    <tr>
                        <td>{{ $inv->invoice_number }}</td>
                        <td>{{ $inv->invoice_date ? $inv->invoice_date->format('Y-m-d') : 'N/A' }}</td>
                        <td>{{ $inv->status ?? 'N/A' }}</td>
                        <td class="text-right">{{ number_format((float)($inv->total ?? 0), 2) }}</td>
                        <td>{{ $inv->currency->currency_code ?? $inv->currency_id ?? 'N/A' }}</td>
                    </tr>
                @empty
                    <tr><td colspan="5" style="text-align:center;">No invoices found for this matter.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="section">
        <strong>Receipts</strong>
        <table>
            <thead>
                <tr>
                    <th>Receipt #</th>
                    <th>Date</th>
                    <th>Status</th>
                    <th class="text-right">Amount</th>
                    <th>Currency</th>
                </tr>
            </thead>
            <tbody>
                @forelse(($receipts ?? collect()) as $rec)
                    <tr>
                        <td>{{ $rec->receipt_number }}</td>
                        <td>{{ $rec->receipt_date ? $rec->receipt_date->format('Y-m-d') : 'N/A' }}</td>
                        <td>{{ $rec->status ?? 'N/A' }}</td>
                        <td class="text-right">{{ number_format((float)($rec->total_amount ?? 0), 2) }}</td>
                        <td>{{ $rec->currency_id ?? 'N/A' }}</td>
                    </tr>
                @empty
                    <tr><td colspan="5" style="text-align:center;">No receipts found for this matter.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</body>
</html>

