<x-filament-panels::page>
    @php
        $data = $this->data();
        $tabs = $this->tabs();
        $periods = $this->periods();
    @endphp

    <div class="space-y-6">
        {{-- Period selector --}}
        <div class="flex flex-wrap gap-2 items-center">
            <span class="text-sm font-medium">Perioadă:</span>
            @foreach ($periods as $key => $label)
                <button
                    type="button"
                    wire:click="setPeriod('{{ $key }}')"
                    @class([
                        'px-3 py-1.5 rounded-md text-sm border',
                        'bg-primary-600 text-white border-primary-600' => $period === $key,
                        'bg-white border-gray-200 hover:bg-gray-50 dark:bg-gray-800 dark:border-gray-700' => $period !== $key,
                    ])
                >{{ $label }}</button>
            @endforeach
        </div>

        {{-- Tabs --}}
        <div class="flex flex-wrap gap-2 border-b border-gray-200 dark:border-gray-700">
            @foreach ($tabs as $key => $label)
                <button
                    type="button"
                    wire:click="setTab('{{ $key }}')"
                    @class([
                        'px-4 py-2 text-sm font-medium -mb-px border-b-2',
                        'border-primary-600 text-primary-600' => $tab === $key,
                        'border-transparent text-gray-500 hover:text-gray-700' => $tab !== $key,
                    ])
                >{{ $label }}</button>
            @endforeach
        </div>

        {{-- Content per tab --}}
        @if ($tab === 'finance')
            <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
                @foreach ([
                    ['Încasări', $data['income'], 'success'],
                    ['Cheltuieli', $data['expenses'], 'danger'],
                    ['Profit', $data['profit'], $data['profit'] >= 0 ? 'success' : 'danger'],
                    ['Datorii clienți', $data['debt'], 'warning'],
                ] as [$label, $value, $color])
                    <div class="bg-white dark:bg-gray-800 rounded-lg p-4 border border-gray-200 dark:border-gray-700">
                        <div class="text-xs text-gray-500">{{ $label }}</div>
                        <div class="text-2xl font-bold text-{{ $color }}-600 mt-1">
                            {{ number_format((float)$value, 2, '.', ' ') }} MDL
                        </div>
                    </div>
                @endforeach
            </div>
            <div class="grid md:grid-cols-2 gap-4">
                <div class="bg-white dark:bg-gray-800 rounded-lg p-4 border border-gray-200 dark:border-gray-700">
                    <h3 class="font-semibold mb-3">Încasări pe metodă</h3>
                    <table class="w-full text-sm">
                        <thead><tr class="border-b"><th class="text-left py-2">Metodă</th><th class="text-right">Tranz.</th><th class="text-right">Total</th></tr></thead>
                        <tbody>
                            @forelse ($data['by_method'] as $row)
                                <tr class="border-b last:border-0">
                                    <td class="py-2">{{ \App\Models\Tenant\Payment::METHODS[$row->method] ?? $row->method }}</td>
                                    <td class="text-right">{{ $row->cnt }}</td>
                                    <td class="text-right font-semibold">{{ number_format((float)$row->total, 2, '.', ' ') }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="3" class="py-4 text-gray-400 text-center">Nicio plată în perioada selectată.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                <div class="bg-white dark:bg-gray-800 rounded-lg p-4 border border-gray-200 dark:border-gray-700">
                    <h3 class="font-semibold mb-3">Cheltuieli pe categorie</h3>
                    <table class="w-full text-sm">
                        <thead><tr class="border-b"><th class="text-left py-2">Categorie</th><th class="text-right">Nr.</th><th class="text-right">Total</th></tr></thead>
                        <tbody>
                            @forelse ($data['by_category'] as $row)
                                <tr class="border-b last:border-0">
                                    <td class="py-2">{{ \App\Models\Tenant\Expense::CATEGORIES[$row->category] ?? $row->category }}</td>
                                    <td class="text-right">{{ $row->cnt }}</td>
                                    <td class="text-right font-semibold text-red-600">{{ number_format((float)$row->total, 2, '.', ' ') }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="3" class="py-4 text-gray-400 text-center">Nicio cheltuială.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="text-sm text-gray-500">Marjă profit: <b>{{ $data['margin_pct'] }}%</b></div>
        @elseif ($tab === 'workload')
            <div class="grid md:grid-cols-2 gap-4">
                <div class="bg-white dark:bg-gray-800 rounded-lg p-4 border border-gray-200 dark:border-gray-700">
                    <div class="text-xs text-gray-500">Fișe deschise</div>
                    <div class="text-3xl font-bold">{{ $data['opened'] }}</div>
                </div>
                <div class="bg-white dark:bg-gray-800 rounded-lg p-4 border border-gray-200 dark:border-gray-700">
                    <div class="text-xs text-gray-500">Fișe închise</div>
                    <div class="text-3xl font-bold text-success-600">{{ $data['closed'] }}</div>
                </div>
            </div>
            <div class="bg-white dark:bg-gray-800 rounded-lg p-4 border border-gray-200 dark:border-gray-700">
                <h3 class="font-semibold mb-3">Pe status</h3>
                <table class="w-full text-sm">
                    <tbody>
                        @foreach ($data['by_status'] as $row)
                            <tr class="border-b last:border-0">
                                <td class="py-2">{{ \App\Models\Tenant\WorkOrder::STATUSES[$row->status] ?? $row->status }}</td>
                                <td class="text-right font-bold">{{ $row->cnt }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @elseif ($tab === 'masters')
            <div class="bg-white dark:bg-gray-800 rounded-lg p-4 border border-gray-200 dark:border-gray-700 overflow-x-auto">
                <table class="w-full text-sm">
                    <thead><tr class="border-b">
                        <th class="text-left py-2">Mecanic</th>
                        <th class="text-left">Specializare</th>
                        <th class="text-right">Manopere</th>
                        <th class="text-right">Ore</th>
                        <th class="text-right">Venit</th>
                    </tr></thead>
                    <tbody>
                        @forelse ($data['rows'] as $r)
                            <tr class="border-b last:border-0">
                                <td class="py-2 font-medium">{{ $r['name'] }}</td>
                                <td class="text-gray-500">{{ $r['specialization'] ?? '—' }}</td>
                                <td class="text-right">{{ $r['works'] }}</td>
                                <td class="text-right">{{ number_format($r['hours'], 1) }}</td>
                                <td class="text-right font-semibold text-success-600">{{ number_format($r['revenue'], 2, '.', ' ') }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="5" class="py-4 text-gray-400 text-center">Niciun mecanic activ.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        @elseif ($tab === 'works')
            <div class="bg-white dark:bg-gray-800 rounded-lg p-4 border border-gray-200 dark:border-gray-700 overflow-x-auto">
                <table class="w-full text-sm">
                    <thead><tr class="border-b">
                        <th class="text-left py-2">Manoperă</th>
                        <th class="text-right">Nr.</th>
                        <th class="text-right">Ore total</th>
                        <th class="text-right">Venit</th>
                    </tr></thead>
                    <tbody>
                        @forelse ($data['rows'] as $r)
                            <tr class="border-b last:border-0">
                                <td class="py-2">{{ $r->name }}</td>
                                <td class="text-right font-semibold">{{ $r->cnt }}</td>
                                <td class="text-right">{{ number_format((float)$r->hours, 1) }}</td>
                                <td class="text-right text-success-600">{{ number_format((float)$r->revenue, 2, '.', ' ') }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="py-4 text-gray-400 text-center">Nicio manoperă efectuată.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        @elseif ($tab === 'parts')
            <div class="grid md:grid-cols-2 gap-4">
                <div class="bg-white dark:bg-gray-800 rounded-lg p-4 border border-gray-200 dark:border-gray-700 overflow-x-auto">
                    <h3 class="font-semibold mb-3">Top piese vândute</h3>
                    <table class="w-full text-sm">
                        <thead><tr class="border-b"><th class="text-left py-2">Piesă</th><th class="text-right">Cant.</th><th class="text-right">Venit</th><th class="text-right">Marjă</th></tr></thead>
                        <tbody>
                            @forelse ($data['sold'] as $r)
                                <tr class="border-b last:border-0">
                                    <td class="py-2">{{ $r->name }} <span class="text-gray-400">{{ $r->brand }}</span></td>
                                    <td class="text-right">{{ number_format((float)$r->qty, 2) }}</td>
                                    <td class="text-right">{{ number_format((float)$r->revenue, 2, '.', ' ') }}</td>
                                    <td class="text-right text-success-600">{{ number_format((float)$r->margin, 2, '.', ' ') }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="4" class="py-4 text-gray-400 text-center">Nicio piesă montată.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                <div class="bg-white dark:bg-gray-800 rounded-lg p-4 border border-gray-200 dark:border-gray-700">
                    <h3 class="font-semibold mb-3">⚠️ Stoc minim atins</h3>
                    <table class="w-full text-sm">
                        <thead><tr class="border-b"><th class="text-left py-2">Piesă</th><th class="text-right">Stoc</th><th class="text-right">Min.</th></tr></thead>
                        <tbody>
                            @forelse ($data['low'] as $p)
                                <tr class="border-b last:border-0">
                                    <td class="py-2">{{ $p->name }}</td>
                                    <td class="text-right font-bold {{ $p->qty <= 0 ? 'text-red-600' : 'text-yellow-600' }}">{{ $p->qty }}</td>
                                    <td class="text-right">{{ $p->min_qty }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="3" class="py-4 text-gray-400 text-center">Toate piesele sunt în stoc.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        @elseif ($tab === 'clients')
            <div class="grid md:grid-cols-2 gap-4">
                <div class="bg-white dark:bg-gray-800 rounded-lg p-4 border border-gray-200 dark:border-gray-700">
                    <div class="text-xs text-gray-500">Clienți noi în perioada</div>
                    <div class="text-3xl font-bold">{{ $data['new_count'] }}</div>
                </div>
                <div class="bg-white dark:bg-gray-800 rounded-lg p-4 border border-gray-200 dark:border-gray-700">
                    <h3 class="font-semibold mb-3">Lead-uri pe sursă</h3>
                    <table class="w-full text-sm">
                        @foreach ($data['by_source'] as $row)
                            <tr class="border-b last:border-0">
                                <td class="py-1">{{ \App\Models\Tenant\Lead::SOURCES[$row->source] ?? ($row->source ?? '—') }}</td>
                                <td class="text-right font-bold">{{ $row->cnt }}</td>
                            </tr>
                        @endforeach
                    </table>
                </div>
            </div>
        @endif
    </div>
</x-filament-panels::page>
