<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>#{{ $wo->number }} — {{ $tenant->display_name ?? $tenant->name }}</title>
@php
    $themeColor = $tenant->settings['theme_color'] ?? '#3B82F6';
    $logoUrl = method_exists($tenant, 'getLogoUrl') ? $tenant->getLogoUrl() : null;
    $faviconUrl = method_exists($tenant, 'getFaviconUrl') ? $tenant->getFaviconUrl() : null;
    $statuses = App\Models\Tenant\WorkOrder::STATUSES;
    $flow = ['new', 'diagnosis', 'agreement', 'approved', 'in_work', 'awaiting_parts', 'ready', 'done'];
    $currentIdx = array_search($wo->status, $flow, true);
@endphp
@if ($faviconUrl)
    <link rel="icon" href="{{ $faviconUrl }}">
@endif
<style>
    * { box-sizing: border-box; margin: 0; padding: 0; }
    body { font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif; color: #1f2937; background: #f3f4f6; line-height: 1.5; }
    .wrap { max-width: 720px; margin: 0 auto; padding: 24px 16px 64px; }

    header { background: {{ $themeColor }}; color: #fff; padding: 28px 16px; text-align: center; }
    header img { max-height: 56px; margin-bottom: 12px; }
    header h1 { font-size: 22px; font-weight: 700; }
    header .num { font-size: 14px; opacity: .85; margin-top: 4px; }

    .card { background: #fff; border-radius: 12px; padding: 20px; margin-top: 16px; box-shadow: 0 1px 3px rgba(0,0,0,.05); }
    .card h2 { font-size: 16px; font-weight: 600; margin-bottom: 14px; color: {{ $themeColor }}; }
    .row { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid #f3f4f6; font-size: 14px; }
    .row:last-child { border-bottom: 0; }
    .row .k { color: #6b7280; }
    .row .v { font-weight: 500; color: #111827; text-align: right; }

    .status-badge {
        display: inline-block; padding: 6px 14px; border-radius: 999px;
        background: {{ $themeColor }}; color: #fff; font-size: 13px; font-weight: 600;
    }

    .timeline { list-style: none; padding: 0; }
    .timeline li {
        position: relative; padding: 10px 0 10px 36px; font-size: 14px;
        color: #9ca3af;
    }
    .timeline li::before {
        content: ''; position: absolute; left: 10px; top: 16px;
        width: 12px; height: 12px; border-radius: 50%;
        background: #e5e7eb; border: 2px solid #fff; box-shadow: 0 0 0 2px #e5e7eb;
    }
    .timeline li.done { color: #111827; }
    .timeline li.done::before { background: {{ $themeColor }}; box-shadow: 0 0 0 2px {{ $themeColor }}; }
    .timeline li.current { color: #111827; font-weight: 600; }
    .timeline li.current::before { background: {{ $themeColor }}; box-shadow: 0 0 0 2px {{ $themeColor }}33; }
    .timeline li:not(:last-child)::after {
        content: ''; position: absolute; left: 15px; top: 28px; bottom: -4px;
        width: 2px; background: #e5e7eb;
    }
    .timeline li.done:not(:last-child)::after { background: {{ $themeColor }}; }

    .photos { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
    .photos a { display: block; aspect-ratio: 1; overflow: hidden; border-radius: 8px; background: #f3f4f6; }
    .photos img { width: 100%; height: 100%; object-fit: cover; }

    .note { background: #fefce8; border-left: 3px solid #facc15; padding: 12px; border-radius: 4px; font-size: 14px; color: #713f12; }
    .totals { display: flex; justify-content: space-between; align-items: center; padding-top: 12px; border-top: 2px solid #e5e7eb; margin-top: 12px; }
    .totals .lbl { font-size: 14px; color: #6b7280; }
    .totals .amt { font-size: 22px; font-weight: 700; color: {{ $themeColor }}; }
    footer { text-align: center; padding: 24px 16px; color: #9ca3af; font-size: 12px; }

    @media (prefers-color-scheme: dark) {
        body { background: #0f172a; color: #e5e7eb; }
        .card { background: #1e293b; box-shadow: none; }
        .row { border-color: #334155; }
        .row .k { color: #94a3b8; }
        .row .v { color: #f1f5f9; }
        .timeline li::before { border-color: #1e293b; box-shadow: 0 0 0 2px #475569; }
        .timeline li.done { color: #e5e7eb; }
        .timeline li:not(:last-child)::after { background: #475569; }
        .note { background: #422006; border-left-color: #ca8a04; color: #fef9c3; }
        footer { color: #64748b; }
    }
</style>
</head>
<body>
<header style="position:relative;">
    <div style="position:absolute;top:14px;right:16px;">
        @include('partials.lang-switcher')
    </div>
    @if ($logoUrl)<img src="{{ $logoUrl }}" alt="">@endif
    <h1>{{ $tenant->display_name ?? $tenant->name }}</h1>
    <div class="num">{{ __('portal.tracking.title_fisa', ['number' => $wo->number]) }}</div>
</header>

<style>
.approval-banner { background:#fef3c7; border:1px solid #fcd34d; border-radius:10px; padding:14px 16px; margin-bottom:16px; }
.approval-banner h3 { font-size:14px; font-weight:700; color:#92400e; margin-bottom:8px; display:flex; align-items:center; gap:6px; }
.approval-line { background:white; border:1px solid #fde68a; border-radius:8px; padding:10px 12px; margin-bottom:8px; }
.approval-line:last-child { margin-bottom:0; }
.approval-line-title { font-weight:600; font-size:14px; color:#1a202c; }
.approval-line-amount { color:#92400e; font-weight:600; margin-top:2px; font-size:13px; }
.approval-line-actions { display:flex; gap:8px; margin-top:8px; }
.btn-approve { flex:1; padding:8px; background:#16a34a; color:white; border:none; border-radius:6px; font-weight:600; cursor:pointer; font-size:13px; }
.btn-approve:hover { background:#15803d; }
.btn-decline { flex:1; padding:8px; background:white; color:#dc2626; border:1px solid #dc2626; border-radius:6px; font-weight:600; cursor:pointer; font-size:13px; }
.btn-decline:hover { background:#fee2e2; }
.flash-success { background:#dcfce7; border:1px solid #86efac; color:#166534; padding:12px 14px; border-radius:8px; margin-bottom:16px; font-size:13px; }
.flash-error { background:#fee2e2; border:1px solid #fca5a5; color:#991b1b; padding:12px 14px; border-radius:8px; margin-bottom:16px; font-size:13px; }
</style>

<div class="wrap">

    @if (isset($approvalStatus) && $approvalStatus)
        <div class="flash-{{ $approvalStatus['kind'] === 'error' ? 'error' : 'success' }}">{{ $approvalStatus['message'] }}</div>
    @endif

    @if ($pendingWorks->isNotEmpty() || $pendingParts->isNotEmpty())
        <div class="approval-banner">
            <h3>{{ __('portal.tracking.approval_needed_title') }}</h3>
            <p style="font-size:13px;color:#92400e;margin-bottom:10px;">{{ __('portal.tracking.approval_needed_body') }}</p>
            @foreach ($pendingWorks as $w)
                <div class="approval-line">
                    <div class="approval-line-title">{{ $w->name }}</div>
                    <div class="approval-line-amount">{{ rtrim(rtrim(number_format($w->hours, 2), '0'), '.') }} {{ __('portal.tracking.hours') }} · {{ number_format($w->total, 0, '.', ' ') }} {{ __('portal.common.currency_mdl') }}</div>
                    <div class="approval-line-actions">
                        <form method="POST" action="{{ route('tracking.approve', ['token' => $wo->tracking_token, 'kind' => 'work', 'lineToken' => $w->approval_token]) }}" style="flex:1;display:flex;gap:8px;">
                            @csrf
                            <button type="submit" name="decision" value="approve" class="btn-approve">{{ __('portal.tracking.approve') }}</button>
                            <button type="submit" name="decision" value="decline" class="btn-decline">{{ __('portal.tracking.decline') }}</button>
                        </form>
                    </div>
                </div>
            @endforeach
            @foreach ($pendingParts as $p)
                <div class="approval-line">
                    <div class="approval-line-title">{{ $p->name }} @if ($p->article) <span style="font-family:monospace;color:#6b7280;font-weight:400;">· {{ $p->article }}</span>@endif</div>
                    <div class="approval-line-amount">{{ rtrim(rtrim(number_format($p->qty, 2), '0'), '.') }} {{ $p->unit ?? __('portal.tracking.unit_pcs') }} · {{ number_format($p->total, 0, '.', ' ') }} {{ __('portal.common.currency_mdl') }}</div>
                    <div class="approval-line-actions">
                        <form method="POST" action="{{ route('tracking.approve', ['token' => $wo->tracking_token, 'kind' => 'part', 'lineToken' => $p->approval_token]) }}" style="flex:1;display:flex;gap:8px;">
                            @csrf
                            <button type="submit" name="decision" value="approve" class="btn-approve">{{ __('portal.tracking.approve') }}</button>
                            <button type="submit" name="decision" value="decline" class="btn-decline">{{ __('portal.tracking.decline') }}</button>
                        </form>
                    </div>
                </div>
            @endforeach
        </div>
    @endif

    <div class="card" style="text-align:center;">
        <span class="status-badge">{{ $statuses[$wo->status] ?? $wo->status }}</span>
        @if ($wo->eta_at && in_array($wo->status, ['in_work', 'awaiting_parts', 'approved', 'diagnosis'], true))
            <p style="margin-top:10px;color:#6b7280;font-size:14px;">
                {{ __('portal.tracking.ready_estimated') }} <strong style="color:#111827">{{ $wo->eta_at->isoFormat('D MMM YYYY, HH:mm') }}</strong>
            </p>
        @endif
    </div>

    <div class="card">
        <h2>{{ __('portal.tracking.details') }}</h2>
        @if ($wo->vehicle)
            <div class="row">
                <span class="k">{{ __('portal.tracking.auto') }}</span>
                <span class="v">{{ trim($wo->vehicle->make . ' ' . $wo->vehicle->model) }}
                    @if ($wo->vehicle->plate) · {{ $wo->vehicle->plate }} @endif
                </span>
            </div>
        @endif
        @if ($wo->mileage_in)
            <div class="row"><span class="k">{{ __('portal.tracking.mileage') }}</span><span class="v">{{ number_format($wo->mileage_in, 0, '.', ' ') }} km</span></div>
        @endif
        <div class="row"><span class="k">{{ __('portal.tracking.opened') }}</span><span class="v">{{ $wo->opened_at?->isoFormat('D MMM YYYY') }}</span></div>
        @if ($wo->master)
            <div class="row"><span class="k">{{ __('portal.tracking.master') }}</span><span class="v">{{ $wo->master->name }}</span></div>
        @endif
    </div>

    <div class="card">
        <h2>{{ __('portal.tracking.stages') }}</h2>
        <ul class="timeline">
            @foreach ($flow as $i => $st)
                @php
                    $cls = '';
                    if ($currentIdx !== false && $i < $currentIdx) $cls = 'done';
                    elseif ($currentIdx !== false && $i === $currentIdx) $cls = 'current';
                @endphp
                <li class="{{ $cls }}">{{ $statuses[$st] }}</li>
            @endforeach
        </ul>
    </div>

    @if ($wo->complaint)
        <div class="card">
            <h2>{{ __('portal.tracking.what_asked') }}</h2>
            <p style="font-size:14px;white-space:pre-wrap;">{{ $wo->complaint }}</p>
        </div>
    @endif

    @if ($wo->recommendations)
        <div class="card">
            <h2>{{ __('portal.tracking.recommendations') }}</h2>
            <div class="note" style="white-space:pre-wrap;">{{ $wo->recommendations }}</div>
        </div>
    @endif

    @if ($photos->count())
        <div class="card">
            <h2>{{ __('portal.tracking.photos') }}</h2>
            <div class="photos">
                @foreach ($photos as $p)
                    <a href="{{ $p->getUrl() }}" target="_blank" rel="noopener">
                        <img src="{{ $p->getUrl() }}" alt="" loading="lazy">
                    </a>
                @endforeach
            </div>
        </div>
    @endif

    @if ((float) $wo->total > 0)
        <div class="card">
            <div class="totals">
                <span class="lbl">{{ __('portal.common.total') }}</span>
                <span class="amt">{{ number_format((float) $wo->total, 2, '.', ' ') }} {{ $tenant->settings['currency'] ?? 'MDL' }}</span>
            </div>
        </div>
    @endif

    <footer>
        {{ __('portal.common.powered_by') }}
    </footer>
</div>
</body>
</html>
