{{-- Patient-friendly printable dental treatment plan. Standalone page (opened
     in a new tab from the plan builder). Receives $patient (ClcPatient),
     $plans (Collection keyed by plan_name of DentalTreatmentPlanItem), and
     $grandTotal. No app layout — a clean print sheet. --}}
@php
    $businessName = \Illuminate\Support\Facades\Session::get('business.name', config('app.name'));
    $currency = \Illuminate\Support\Facades\Session::get('currency');
    $symbol = is_array($currency) ? ($currency['symbol'] ?? '') : (optional($currency)->symbol ?? '');
    $urgencyLabels = [
        'elective'  => __('dentalClinic::lang.urgency_elective'),
        'soon'      => __('dentalClinic::lang.urgency_soon'),
        'urgent'    => __('dentalClinic::lang.urgency_urgent'),
        'emergency' => __('dentalClinic::lang.urgency_emergency'),
    ];
    $fmt = fn ($v) => $v === null ? '—' : ($symbol . number_format((float) $v, 2));
@endphp
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{{ __('dentalClinic::lang.treatment_plan') }} — {{ $patient->patient_name }}</title>
    <style>
        * { box-sizing: border-box; }
        body { font-family: 'Segoe UI', Arial, sans-serif; color: #222; margin: 24px; font-size: 13px; }
        h1 { font-size: 20px; margin: 0 0 2px; }
        h2 { font-size: 15px; margin: 18px 0 6px; border-bottom: 2px solid #0d6efd; padding-bottom: 3px; }
        .muted { color: #777; }
        table { width: 100%; border-collapse: collapse; margin: 6px 0 4px; }
        th, td { border: 1px solid #ddd; padding: 6px 8px; text-align: left; }
        th { background: #f4f6f9; }
        td.num, th.num { text-align: right; }
        .plan-total { text-align: right; font-weight: 700; margin: 2px 0 10px; }
        .grand { font-size: 15px; text-align: right; margin-top: 14px; border-top: 2px solid #222; padding-top: 8px; }
        .u-emergency { color: #c0392b; font-weight: 700; }
        .u-urgent { color: #e67e22; font-weight: 700; }
        .u-soon { color: #d4a107; }
        .header { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 1px solid #ccc; padding-bottom: 10px; }
        .actions { margin: 16px 0; }
        @media print { .actions { display: none; } body { margin: 8px; } }
        .note { font-size: 11px; color: #888; margin-top: 20px; }
    </style>
</head>
<body>
    <div class="header">
        <div>
            <h1>{{ __('dentalClinic::lang.treatment_plan') }}</h1>
            <div class="muted">{{ $businessName }}</div>
        </div>
        <div style="text-align:right;">
            <div><strong>{{ $patient->patient_name }}</strong></div>
            <div class="muted">{{ now()->format('Y-m-d') }}</div>
        </div>
    </div>

    <div class="actions">
        <button onclick="window.print()">Print</button>
    </div>

    @forelse($plans as $planName => $items)
        <h2>{{ $planName }}</h2>
        <table>
            <thead>
                <tr>
                    <th>#</th>
                    <th>{{ __('dentalClinic::lang.tooth') }}</th>
                    <th>{{ __('dentalClinic::lang.procedure_name') }}</th>
                    <th>{{ __('dentalClinic::lang.cdt_code') }}</th>
                    <th>{{ __('dentalClinic::lang.urgency') }}</th>
                    <th class="num">{{ __('dentalClinic::lang.estimated_cost') }}</th>
                </tr>
            </thead>
            <tbody>
                @foreach($items as $i => $item)
                    <tr>
                        <td>{{ $i + 1 }}</td>
                        <td>{{ $item->tooth_number ? 'Tooth ' . $item->tooth_number : '—' }}</td>
                        <td>{{ $item->procedure_name }}</td>
                        <td>{{ $item->cdt_code ?: '—' }}</td>
                        <td class="u-{{ $item->urgency }}">{{ $urgencyLabels[$item->urgency] ?? ucfirst($item->urgency) }}</td>
                        <td class="num">{{ $fmt($item->estimated_cost) }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
        <div class="plan-total">{{ __('dentalClinic::lang.total_estimate') }}: {{ $fmt($items->sum('estimated_cost')) }}</div>
    @empty
        <p class="muted">{{ __('dentalClinic::lang.no_plan_items') }}</p>
    @endforelse

    @if($plans->count() > 1)
        <div class="grand">{{ __('dentalClinic::lang.total_estimate') }} (all plans): {{ $fmt($grandTotal) }}</div>
    @endif

    <p class="note">Estimated costs are indicative and may change based on clinical findings and insurance coverage.</p>
</body>
</html>
