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

    <style>
        .rp-bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 16px; }
        .rp-label { font-size: 13px; font-weight: 500; }
        .rp-pill {
            padding: 6px 12px; border-radius: 6px; font-size: 13px;
            background: #fff; border: 1px solid #e5e7eb; cursor: pointer; color: #374151;
            transition: all .15s;
        }
        .dark .rp-pill { background: #1f2937; border-color: #374151; color: #d1d5db; }
        .rp-pill:hover { background: #f9fafb; }
        .dark .rp-pill:hover { background: #374151; }
        .rp-pill.active { background: #3b82f6; color: #fff; border-color: #3b82f6; }

        .rp-tabs {
            display: flex; flex-wrap: wrap; gap: 4px;
            border-bottom: 1px solid #e5e7eb; margin-bottom: 16px;
        }
        .dark .rp-tabs { border-color: #374151; }
        .rp-tab {
            padding: 10px 16px; font-size: 14px; font-weight: 500;
            border: none; background: transparent; cursor: pointer;
            border-bottom: 2px solid transparent; margin-bottom: -1px;
            color: #6b7280;
        }
        .rp-tab:hover { color: #1f2937; }
        .dark .rp-tab:hover { color: #f9fafb; }
        .rp-tab.active { color: #3b82f6; border-bottom-color: #3b82f6; }

        .rp-grid { display: grid; gap: 16px; }
        .rp-grid-4 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
        .rp-grid-2 { grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)); }

        .rp-card {
            background: #fff; border: 1px solid #e5e7eb; border-radius: 10px;
            padding: 16px;
        }
        .dark .rp-card { background: #1f2937; border-color: #374151; }
        .rp-stat-label { font-size: 11px; color: #6b7280; text-transform: uppercase; letter-spacing: .5px; }
        .rp-stat-value { font-size: 28px; font-weight: 700; margin-top: 6px; }

        .rp-h3 { font-size: 14px; font-weight: 600; margin-bottom: 12px; }

        .rp-tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
        .rp-tbl thead th {
            text-align: left; padding: 8px 4px; border-bottom: 1px solid #e5e7eb;
            font-weight: 600; color: #6b7280;
        }
        .dark .rp-tbl thead th { border-color: #374151; }
        .rp-tbl tbody td { padding: 8px 4px; border-bottom: 1px solid #f3f4f6; }
        .dark .rp-tbl tbody td { border-color: #374151; }
        .rp-tbl tbody tr:last-child td { border-bottom: none; }
        .rp-right { text-align: right; }
        .rp-bold { font-weight: 600; }

        .rp-success { color: #059669; }
        .rp-danger { color: #dc2626; }
        .rp-warning { color: #d97706; }
        .rp-muted { color: #9ca3af; text-align: center; padding: 16px 0; }

        .rp-stack { display: flex; flex-direction: column; gap: 16px; }
    </style>

    <div class="rp-stack">
        <div class="rp-bar">
            <span class="rp-label">Perioadă:</span>
            @foreach ($periods as $key => $label)
                <button type="button" wire:click="setPeriod('{{ $key }}')"
                        class="rp-pill {{ $period === $key ? 'active' : '' }}">{{ $label }}</button>
            @endforeach
        </div>

        <div class="rp-tabs">
            @foreach ($tabs as $key => $label)
                <button type="button" wire:click="setTab('{{ $key }}')"
                        class="rp-tab {{ $tab === $key ? 'active' : '' }}">{{ $label }}</button>
            @endforeach
        </div>

        @if ($tab === 'finance')
            <div class="rp-grid rp-grid-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="rp-card">
                        <div class="rp-stat-label">{{ $label }}</div>
                        <div class="rp-stat-value rp-{{ $color }}">
                            {{ number_format((float)$value, 2, '.', ' ') }} MDL
                        </div>
                    </div>
                @endforeach
            </div>

            <div class="rp-grid rp-grid-2">
                <div class="rp-card">
                    <div class="rp-h3">Încasări pe metodă</div>
                    <table class="rp-tbl">
                        <thead><tr><th>Metodă</th><th class="rp-right">Tranz.</th><th class="rp-right">Total</th></tr></thead>
                        <tbody>
                            @forelse ($data['by_method'] as $row)
                                <tr>
                                    <td>{{ \App\Models\Tenant\Payment::METHODS[$row->method] ?? $row->method }}</td>
                                    <td class="rp-right">{{ $row->cnt }}</td>
                                    <td class="rp-right rp-bold">{{ number_format((float)$row->total, 2, '.', ' ') }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="3" class="rp-muted">Nicio plată în perioada selectată.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                <div class="rp-card">
                    <div class="rp-h3">Cheltuieli pe categorie</div>
                    <table class="rp-tbl">
                        <thead><tr><th>Categorie</th><th class="rp-right">Nr.</th><th class="rp-right">Total</th></tr></thead>
                        <tbody>
                            @forelse ($data['by_category'] as $row)
                                <tr>
                                    <td>{{ \App\Models\Tenant\Expense::CATEGORIES[$row->category] ?? $row->category }}</td>
                                    <td class="rp-right">{{ $row->cnt }}</td>
                                    <td class="rp-right rp-bold rp-danger">{{ number_format((float)$row->total, 2, '.', ' ') }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="3" class="rp-muted">Nicio cheltuială.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
            <div style="font-size: 13px; color: #6b7280;">Marjă profit: <b>{{ $data['margin_pct'] }}%</b></div>

        @elseif ($tab === 'workload')
            <div class="rp-grid rp-grid-2">
                <div class="rp-card">
                    <div class="rp-stat-label">Fișe deschise</div>
                    <div class="rp-stat-value">{{ $data['opened'] }}</div>
                </div>
                <div class="rp-card">
                    <div class="rp-stat-label">Fișe închise</div>
                    <div class="rp-stat-value rp-success">{{ $data['closed'] }}</div>
                </div>
            </div>
            <div class="rp-card">
                <div class="rp-h3">Pe status</div>
                <table class="rp-tbl">
                    <tbody>
                        @forelse ($data['by_status'] as $row)
                            <tr>
                                <td>{{ \App\Models\Tenant\WorkOrder::STATUSES[$row->status] ?? $row->status }}</td>
                                <td class="rp-right rp-bold">{{ $row->cnt }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="2" class="rp-muted">Nicio fișă în perioada selectată.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

        @elseif ($tab === 'masters')
            <div class="rp-card" style="overflow-x: auto;">
                <table class="rp-tbl">
                    <thead><tr>
                        <th>Mecanic</th>
                        <th>Specializare</th>
                        <th class="rp-right">Manopere</th>
                        <th class="rp-right">Ore</th>
                        <th class="rp-right">Venit</th>
                    </tr></thead>
                    <tbody>
                        @forelse ($data['rows'] as $r)
                            <tr>
                                <td class="rp-bold">{{ $r['name'] }}</td>
                                <td style="color:#6b7280">{{ $r['specialization'] ?? '—' }}</td>
                                <td class="rp-right">{{ $r['works'] }}</td>
                                <td class="rp-right">{{ number_format($r['hours'], 1) }}</td>
                                <td class="rp-right rp-bold rp-success">{{ number_format($r['revenue'], 2, '.', ' ') }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="5" class="rp-muted">Niciun mecanic activ.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

        @elseif ($tab === 'works')
            <div class="rp-card" style="overflow-x: auto;">
                <table class="rp-tbl">
                    <thead><tr>
                        <th>Manoperă</th>
                        <th class="rp-right">Nr.</th>
                        <th class="rp-right">Ore total</th>
                        <th class="rp-right">Venit</th>
                    </tr></thead>
                    <tbody>
                        @forelse ($data['rows'] as $r)
                            <tr>
                                <td>{{ $r->name }}</td>
                                <td class="rp-right rp-bold">{{ $r->cnt }}</td>
                                <td class="rp-right">{{ number_format((float)$r->hours, 1) }}</td>
                                <td class="rp-right rp-success">{{ number_format((float)$r->revenue, 2, '.', ' ') }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="rp-muted">Nicio manoperă efectuată.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

        @elseif ($tab === 'parts')
            <div class="rp-grid rp-grid-2">
                <div class="rp-card" style="overflow-x: auto;">
                    <div class="rp-h3">Top piese vândute</div>
                    <table class="rp-tbl">
                        <thead><tr><th>Piesă</th><th class="rp-right">Cant.</th><th class="rp-right">Venit</th><th class="rp-right">Marjă</th></tr></thead>
                        <tbody>
                            @forelse ($data['sold'] as $r)
                                <tr>
                                    <td>{{ $r->name }} <span style="color:#9ca3af">{{ $r->brand }}</span></td>
                                    <td class="rp-right">{{ number_format((float)$r->qty, 2) }}</td>
                                    <td class="rp-right">{{ number_format((float)$r->revenue, 2, '.', ' ') }}</td>
                                    <td class="rp-right rp-success">{{ number_format((float)$r->margin, 2, '.', ' ') }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="4" class="rp-muted">Nicio piesă montată.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                <div class="rp-card">
                    <div class="rp-h3">⚠️ Stoc minim atins</div>
                    <table class="rp-tbl">
                        <thead><tr><th>Piesă</th><th class="rp-right">Stoc</th><th class="rp-right">Min.</th></tr></thead>
                        <tbody>
                            @forelse ($data['low'] as $p)
                                <tr>
                                    <td>{{ $p->name }}</td>
                                    <td class="rp-right rp-bold {{ $p->qty <= 0 ? 'rp-danger' : 'rp-warning' }}">{{ $p->qty }}</td>
                                    <td class="rp-right">{{ $p->min_qty }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="3" class="rp-muted">Toate piesele sunt în stoc.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>

        @elseif ($tab === 'clients')
            <div class="rp-grid rp-grid-2">
                <div class="rp-card">
                    <div class="rp-stat-label">Clienți noi în perioadă</div>
                    <div class="rp-stat-value">{{ $data['new_count'] }}</div>
                </div>
                <div class="rp-card">
                    <div class="rp-h3">Lead-uri pe sursă</div>
                    <table class="rp-tbl">
                        <tbody>
                            @forelse ($data['by_source'] as $row)
                                <tr>
                                    <td>{{ \App\Models\Tenant\Lead::SOURCES[$row->source] ?? ($row->source ?? '—') }}</td>
                                    <td class="rp-right rp-bold">{{ $row->cnt }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="2" class="rp-muted">Niciun lead.</td></tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        @endif
    </div>
</x-filament-panels::page>
