@extends('layouts.master')
@section('title', 'Matter Detail Report')

@section('styles')
<style>
    .btn-back {
        text-decoration: none;
        color: #1AB9D9;
        font-weight: 500;
        display: inline-flex;
        align-items: center;
        padding: 5px 15px;
        border-radius: 6px;
        transition: all 0.3s ease;
    }
    .btn-back:hover { background-color: rgba(26, 185, 217, 0.1); text-decoration: none; }

    .metric-card {
        padding: 1.05rem;
        border-radius: 0.9rem;
        background: #fff;
        box-shadow: 0 4px 12px rgba(0,0,0,0.05);
        height: 100%;
    }
    .metric-title {
        font-size: 0.78rem;
        text-transform: uppercase;
        color: #6b7280;
        letter-spacing: 0.04em;
        font-weight: 700;
    }
    .metric-value {
        font-size: 1.35rem;
        font-weight: 800;
        color: #111827;
    }

    .pill {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        padding: 0.25rem 0.7rem;
        border-radius: 999px;
        font-size: 0.78rem;
        font-weight: 700;
        background: rgba(14,165,233,0.12);
        color: #0369a1;
    }

    @media print {
        .no-print, .btn-back, form, .card-header { display: none !important; }
        .card, .metric-card { box-shadow: none !important; }
        .table { font-size: 11px; }
    }
</style>
@endsection

@section('content')
<div class="container-fluid py-3">
    <div class="d-flex justify-content-end mb-3 gap-2 no-print">
        <a href="{{ route('modules.reports.matter-reports') }}" class="btn-back">
            <i class="fas fa-arrow-left me-2"></i> Back to Matter Reports
        </a>
        <a href="{{ route('modules.reports') }}" class="btn-back">
            <i class="fas fa-list me-2"></i> Reports Menu
        </a>
    </div>

    <div class="card shadow-sm mb-4">
        <div class="card-header bg-primary text-white py-3">
            <h5 class="mb-0"><i class="fas fa-clipboard-list me-2"></i> Matter Detail Report</h5>
        </div>
        <div class="card-body">
            @if($errors->any())
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        @foreach($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <form method="GET" action="{{ route('modules.reports.matter-detail') }}" class="row g-3 align-items-end no-print">
                <div class="col-md-8">
                    <label class="form-label"><strong>Select Matter</strong></label>
                    <select class="form-control" name="matter_id" required>
                        <option value="" disabled {{ $matterId ? '' : 'selected' }}>Select a Matter</option>
                        @foreach($matters as $m)
                            <option value="{{ $m->id }}" {{ (string)$matterId === (string)$m->id ? 'selected' : '' }}>
                                {{ $m->matter_number }} — {{ \Illuminate\Support\Str::limit($m->matter_title, 70) }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-4 d-grid">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-play me-2"></i> Generate
                    </button>
                </div>
            </form>
        </div>
    </div>

    @if($matter)
        <div class="d-flex justify-content-end gap-2 mb-3 no-print">
            <button type="button" class="btn btn-outline-primary" onclick="window.print()">
                <i class="fas fa-print me-2"></i>Print
            </button>
            <a href="{{ route('modules.reports.matter-detail.download-pdf', ['matter_id' => $matter->id]) }}" class="btn btn-success">
                <i class="fas fa-file-pdf me-2"></i>Download PDF
            </a>
        </div>

        <div class="card shadow-sm mb-4">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start flex-wrap gap-3">
                    <div>
                        <h5 class="mb-1">{{ $matter->matter_number }} — {{ $matter->matter_title }}</h5>
                        <div class="text-muted">Matter detail statement</div>
                    </div>
                    <div class="d-flex gap-2 flex-wrap">
                        <span class="pill"><i class="fas fa-flag"></i> {{ $matter->status?->status_name ?? 'N/A' }}</span>
                        <span class="pill"><i class="fas fa-tag"></i> {{ $matter->type?->type_name ?? 'N/A' }}</span>
                    </div>
                </div>
                <hr>
                <div class="row g-3">
                    <div class="col-md-3"><strong>Opened:</strong> {{ $matter->opened_date ? \Carbon\Carbon::parse($matter->opened_date)->format('M j, Y') : 'N/A' }}</div>
                    <div class="col-md-3"><strong>Closed:</strong> {{ $matter->closed_date ? \Carbon\Carbon::parse($matter->closed_date)->format('M j, Y') : '-' }}</div>
                    <div class="col-md-3"><strong>Priority:</strong> {{ $matter->priority?->priority_name ?? 'N/A' }}</div>
                    <div class="col-md-3"><strong>Category:</strong> {{ $matter->category?->category_name ?? 'N/A' }}</div>
                </div>
                @if(!empty($matter->description))
                    <div class="mt-3"><strong>Description:</strong> {!! nl2br(e($matter->description)) !!}</div>
                @endif
            </div>
        </div>

        <div class="row g-3 mb-4">
            <div class="col-md-3"><div class="metric-card text-center"><div class="metric-title">Invoices Total</div><div class="metric-value">${{ number_format($totals['invoices_total'] ?? 0, 2) }}</div></div></div>
            <div class="col-md-3"><div class="metric-card text-center"><div class="metric-title">Receipts Total</div><div class="metric-value text-success">${{ number_format($totals['receipts_total'] ?? 0, 2) }}</div></div></div>
            <div class="col-md-3"><div class="metric-card text-center"><div class="metric-title">Balance</div><div class="metric-value {{ ($totals['balance'] ?? 0) > 0 ? 'text-danger' : 'text-success' }}">${{ number_format($totals['balance'] ?? 0, 2) }}</div></div></div>
            <div class="col-md-3"><div class="metric-card text-center"><div class="metric-title">Cases</div><div class="metric-value">{{ $matterCases->count() }}</div></div></div>
        </div>

        <div class="card shadow-sm mb-4">
            <div class="card-header bg-light d-flex justify-content-between align-items-center">
                <h6 class="mb-0"><i class="fas fa-users me-2"></i>Clients &amp; Lawyers</h6>
            </div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-6">
                        <h6 class="fw-bold mb-2">Clients</h6>
                        @if(($matter->clients ?? collect())->count() > 0)
                            <ul class="mb-0">
                                @foreach($matter->clients as $c)
                                    <li>{{ $c->full_name ?? $c->fullName ?? $c->name ?? 'Client' }}</li>
                                @endforeach
                            </ul>
                        @else
                            <div class="text-muted">No clients linked.</div>
                        @endif
                    </div>
                    <div class="col-md-6">
                        <h6 class="fw-bold mb-2">Lawyers</h6>
                        @if(($matterLawyers ?? collect())->count() > 0)
                            <ul class="mb-0">
                                @foreach($matterLawyers as $l)
                                    @php
                                        $nm = trim(($l->first_name ?? '') . ' ' . ($l->last_name ?? ''));
                                        if ($nm === '') $nm = (string) ($l->lawyer_id ?? 'Lawyer');
                                    @endphp
                                    <li>{{ $nm }}{!! !empty($l->is_lead_lawyer) ? ' <span class="badge bg-primary">Lead</span>' : '' !!}</li>
                                @endforeach
                            </ul>
                        @else
                            <div class="text-muted">No lawyers linked.</div>
                        @endif
                    </div>
                </div>
            </div>
        </div>

        <div class="card shadow-sm mb-4">
            <div class="card-header bg-primary text-white">
                <h6 class="mb-0"><i class="fas fa-gavel me-2"></i>Cases (subset of Matter)</h6>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Case #</th>
                                <th>Case Name</th>
                                <th>Client</th>
                                <th>Type</th>
                                <th>Status</th>
                                <th>Court Appearance</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($matterCases 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>
                                    <td>{{ $case->court_appearance_date ? \Carbon\Carbon::parse($case->court_appearance_date)->format('M j, Y') : '-' }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="6" class="text-center text-muted">No cases linked to this matter.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="card shadow-sm mb-4">
            <div class="card-header bg-secondary text-white">
                <h6 class="mb-0"><i class="fas fa-file-invoice me-2"></i>Invoices</h6>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Invoice #</th>
                                <th>Date</th>
                                <th>Client</th>
                                <th>Description</th>
                                <th>Status</th>
                                <th class="text-end">Total</th>
                                <th>Currency</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($invoices as $inv)
                                <tr>
                                    <td>{{ $inv->invoice_number }}</td>
                                    <td>{{ $inv->invoice_date ? $inv->invoice_date->format('Y-m-d') : 'N/A' }}</td>
                                    <td>{{ optional($inv->client)->full_name ?? 'N/A' }}</td>
                                    <td>{{ $inv->description ?? $inv->narration ?? '' }}</td>
                                    <td>{{ $inv->status ?? 'N/A' }}</td>
                                    <td class="text-end">{{ number_format((float)($inv->total ?? 0), 2) }}</td>
                                    <td>{{ $inv->currency->currency_code ?? $inv->currency_id ?? 'N/A' }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="7" class="text-center text-muted">No invoices found for this matter.</td></tr>
                            @endforelse
                        </tbody>
                        @if($invoices->count() > 0)
                            <tfoot class="table-secondary">
                                <tr>
                                    <td colspan="5" class="text-end"><strong>Total</strong></td>
                                    <td class="text-end"><strong>{{ number_format((float)$invoices->sum('total'), 2) }}</strong></td>
                                    <td></td>
                                </tr>
                            </tfoot>
                        @endif
                    </table>
                </div>
            </div>
        </div>

        <div class="card shadow-sm mb-4">
            <div class="card-header bg-success text-white">
                <h6 class="mb-0"><i class="fas fa-receipt me-2"></i>Receipts</h6>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Receipt #</th>
                                <th>Date</th>
                                <th>Client</th>
                                <th>Method</th>
                                <th>Status</th>
                                <th class="text-end">Amount</th>
                                <th>Currency</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($receipts as $rec)
                                <tr>
                                    <td>{{ $rec->receipt_number }}</td>
                                    <td>{{ $rec->receipt_date ? $rec->receipt_date->format('Y-m-d') : 'N/A' }}</td>
                                    <td>{{ optional($rec->client)->full_name ?? 'N/A' }}</td>
                                    <td>{{ $rec->payment_methods_label ?: ($rec->payment_method ?? 'N/A') }}</td>
                                    <td>{{ $rec->status ?? 'N/A' }}</td>
                                    <td class="text-end">{{ number_format((float)($rec->total_amount ?? 0), 2) }}</td>
                                    <td>{{ $rec->currency_id ?? 'N/A' }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="7" class="text-center text-muted">No receipts found for this matter.</td></tr>
                            @endforelse
                        </tbody>
                        @if($receipts->count() > 0)
                            <tfoot class="table-secondary">
                                <tr>
                                    <td colspan="5" class="text-end"><strong>Total</strong></td>
                                    <td class="text-end"><strong>{{ number_format((float)$receipts->sum('total_amount'), 2) }}</strong></td>
                                    <td></td>
                                </tr>
                            </tfoot>
                        @endif
                    </table>
                </div>
            </div>
        </div>

        <div class="card shadow-sm mb-4">
            <div class="card-header bg-dark text-white">
                <h6 class="mb-0"><i class="fas fa-cash-register me-2"></i>Cashbook Entries</h6>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th>Date</th>
                                <th>Cashbook</th>
                                <th>Reference</th>
                                <th>Description</th>
                                <th>Type</th>
                                <th class="text-end">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($cashbookEntries as $e)
                                <tr>
                                    <td>{{ $e->transaction_date ? $e->transaction_date->format('Y-m-d') : 'N/A' }}</td>
                                    <td>{{ $e->cashbook?->name ?? 'N/A' }}</td>
                                    <td>{{ $e->reference ?? '' }}</td>
                                    <td>{{ $e->description ?? '' }}</td>
                                    <td>{{ $e->transaction_type ?? '' }}</td>
                                    <td class="text-end">{{ number_format((float)($e->amount ?? 0), 2) }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="6" class="text-center text-muted">No cashbook entries linked to this matter.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    @else
        <div class="alert alert-info mb-0">
            Select a matter and generate the report.
        </div>
    @endif
</div>
@endsection

