@extends('layouts.master')
@section('title', 'Lawyer Statement 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); }

.report-card {
    border: none;
    border-radius: 14px;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.06);
}

.kpi-card {
    border: 1px solid #e9ecef;
    border-radius: 12px;
    padding: 1rem;
    background: #fff;
    height: 100%;
}
.kpi-title {
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #6c757d;
    font-weight: 700;
}
.kpi-value {
    font-size: 1.45rem;
    font-weight: 700;
    color: #111827;
}
.profile-chip {
    font-size: 0.8rem;
    padding: 0.2rem 0.65rem;
    border-radius: 999px;
    background: #eef6ff;
    color: #0d6efd;
    font-weight: 600;
}
.table thead th {
    white-space: nowrap;
    font-size: 0.85rem;
}
.table td {
    font-size: 0.92rem;
    vertical-align: middle;
}
@media print {
    .no-print, .btn-back, form { display: none !important; }
    .report-card, .kpi-card { box-shadow: none !important; }
}
</style>
@endsection

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

    <div class="card report-card mb-4">
        <div class="card-header bg-primary text-white py-3 d-flex justify-content-between align-items-center">
            <h5 class="mb-0"><i class="fas fa-user-tie me-2"></i> Lawyer Statement Report</h5>
            <div class="d-flex justify-content-end gap-2 no-print">
                <button type="button" class="btn btn-outline-primary btn-sm" onclick="window.print()">
                    <i class="fas fa-print me-2"></i>Print
                </button>
                @php
                    $selectedLawyerKey = $selectedLawyerId ?? ($selectedLawyer ? ($selectedLawyer->lawyer_id ?? $selectedLawyer->id ?? null) : 'all');
                    $selectedCurrencyId = old('currency', $currencyId ?? '');
                @endphp
                <form action="{{ route('modules.reports.lawyer-statement.download-pdf') }}" method="POST" style="display: inline;">
                    @csrf
                    <input type="hidden" name="customer_account" value="{{ $selectedLawyerKey ?: 'all' }}">
                    <input type="hidden" name="date_from" value="{{ old('date_from', $dateFrom ?? date('Y-m-d', strtotime('first day of this month'))) }}">
                    <input type="hidden" name="date_to" value="{{ old('date_to', $dateTo ?? date('Y-m-d')) }}">
                    <input type="hidden" name="currency" value="{{ $selectedCurrencyId }}">
                    <button type="submit" class="btn btn-success btn-sm">
                        <i class="fas fa-file-pdf me-2"></i>Download PDF
                    </button>
                </form>
            </div>
        </div>
        <div class="card-body">
            <form action="{{ route('modules.reports.lawyer-statement.generate') }}" method="POST" class="row g-3 align-items-end no-print">
                @csrf
                @php
                    $selectedLawyerKey = $selectedLawyerId ?? ($selectedLawyer ? ($selectedLawyer->lawyer_id ?? $selectedLawyer->id ?? null) : null);
                    $selectedCurrencyId = old('currency', $currencyId ?? '');
                @endphp
                <div class="col-md-4">
                    <label class="form-label"><strong>Lawyer</strong></label>
                    <select class="form-control" name="customer_account" required>
                        <option value="" disabled {{ $selectedLawyerKey ? '' : 'selected' }}>Select Lawyer</option>
                        <option value="all" {{ (string)$selectedLawyerKey === 'all' ? 'selected' : '' }}>All Lawyers</option>
                        @foreach($lawyers as $lawyer)
                            @php
                                $lawyerKey = $lawyer->lawyer_id ?? $lawyer->id ?? null;
                                $lawyerName = trim(($lawyer->first_name ?? '') . ' ' . ($lawyer->last_name ?? ''));
                                if ($lawyerName === '') {
                                    $lawyerName = $lawyer->full_name ?? 'Unnamed Lawyer';
                                }
                            @endphp
                            <option value="{{ $lawyerKey }}" {{ (string)$selectedLawyerKey === (string)$lawyerKey ? 'selected' : '' }}>
                                {{ $lawyerName }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label"><strong>Date From</strong></label>
                    <input type="date" class="form-control" name="date_from" value="{{ old('date_from', $dateFrom ?? date('Y-m-d', strtotime('first day of this month'))) }}" required>
                </div>
                <div class="col-md-2">
                    <label class="form-label"><strong>Date To</strong></label>
                    <input type="date" class="form-control" name="date_to" value="{{ old('date_to', $dateTo ?? date('Y-m-d')) }}" required>
                </div>
                <div class="col-md-2">
                    <label class="form-label"><strong>Currency</strong></label>
                    <select class="form-control" name="currency" required>
                        <option value="" disabled {{ $selectedCurrencyId ? '' : 'selected' }}>Select</option>
                        @foreach(($currencies ?? collect()) as $currency)
                            <option value="{{ $currency->currency_id }}" {{ (string)$selectedCurrencyId === (string)$currency->currency_id ? 'selected' : '' }}>
                                {{ $currency->currency_code }} - {{ $currency->currency_name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2 d-grid">
                    <button type="submit" class="btn btn-primary">Generate</button>
                </div>
            </form>
        </div>
    </div>

    <div class="card report-card 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">{{ $selectedLawyer ? (trim(($selectedLawyer->first_name ?? '') . ' ' . ($selectedLawyer->last_name ?? '')) ?: ($selectedLawyer->full_name ?? 'Selected Lawyer')) : 'All Lawyers' }}</h5>
                    <div class="text-muted">Performance and activity statement</div>
                </div>
                @if($selectedLawyer)
                    <span class="profile-chip">Lawyer Statement</span>
                @endif
            </div>
        </div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-md-3"><div class="kpi-card"><div class="kpi-title">Total Fees Generated</div><div class="kpi-value">${{ number_format($metrics['totalFeesGenerated'] ?? 0, 2) }}</div></div></div>
        <div class="col-md-3"><div class="kpi-card"><div class="kpi-title">Active Matters</div><div class="kpi-value">{{ $metrics['activeMatters'] ?? 0 }}</div></div></div>
        <div class="col-md-3"><div class="kpi-card"><div class="kpi-title">Completed Matters</div><div class="kpi-value">{{ $metrics['completedMatters'] ?? 0 }}</div></div></div>
        <div class="col-md-3"><div class="kpi-card"><div class="kpi-title">Success Rate</div><div class="kpi-value">{{ number_format($metrics['successRate'] ?? 0, 1) }}%</div></div></div>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-md-4"><div class="kpi-card"><div class="kpi-title">Billable Hours</div><div class="kpi-value">{{ number_format($metrics['billableHours'] ?? 0, 2) }}</div></div></div>
        <div class="col-md-4"><div class="kpi-card"><div class="kpi-title">Hourly Rate</div><div class="kpi-value">${{ number_format($metrics['hourlyRate'] ?? 0, 2) }}</div></div></div>
        <div class="col-md-4"><div class="kpi-card"><div class="kpi-title">Client Count</div><div class="kpi-value">{{ $metrics['clientCount'] ?? 0 }}</div></div></div>
    </div>

    <div class="card report-card">
        <div class="card-header bg-light d-flex justify-content-between align-items-center">
            <h6 class="mb-0"><i class="fas fa-briefcase me-2"></i>Matter Portfolio</h6>
            <span class="text-muted small">{{ ($portfolio ?? collect())->count() }} matter(s)</span>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Matter #</th>
                            <th>Matter Title</th>
                            <th>Client</th>
                            <th>Status</th>
                            <th>Opened Date</th>
                            <th class="text-end">Cases</th>
                            <th class="text-end">Fees Total</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse(($portfolio ?? collect()) as $row)
                            <tr>
                                <td>{{ $row['matter_number'] ?? ('MAT-' . ($row['matter_id'] ?? '')) }}</td>
                                <td>{{ $row['matter_title'] ?? 'N/A' }}</td>
                                <td>{{ $row['client'] ?? 'N/A' }}</td>
                                <td>{{ $row['status'] ?? 'N/A' }}</td>
                                <td>{{ !empty($row['opened_date']) ? \Carbon\Carbon::parse($row['opened_date'])->format('M j, Y') : 'N/A' }}</td>
                                <td class="text-end">{{ (int)($row['case_count'] ?? 0) }}</td>
                                <td class="text-end">${{ number_format((float)($row['fees_total'] ?? 0), 2) }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="text-center text-muted">No matters found for the selected criteria.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
@endsection
