@extends('layouts.portal')
@section('title', 'Müşteri Dashboard')
@section('content')
<div class="d-flex align-items-start justify-content-between flex-wrap gap-3 mb-4">
    <div>
        <h2 class="mb-1">Dashboard</h2>
        <p class="portal-muted mb-0">{{ $client?->company_name ?: 'Aktif müşteri bulunamadı' }}</p>
    </div>
    @if($client)
        <form method="GET" class="d-flex gap-2 align-items-center flex-wrap">
            @if($clients->count() > 1)
                <select name="client_id" class="form-control" style="min-width:240px;">
                    @foreach($clients as $portalClient)
                        <option value="{{ $portalClient->id }}" @selected($client->id === $portalClient->id)>{{ $portalClient->company_name }}</option>
                    @endforeach
                </select>
            @endif
            <input type="date" name="from" value="{{ request('from', $metrics['date_from'] ?? now()->subDays(29)->toDateString()) }}" class="form-control">
            <input type="date" name="to" value="{{ request('to', $metrics['date_to'] ?? now()->toDateString()) }}" class="form-control">
            <button class="btn btn-primary" type="submit">Filtrele</button>
        </form>
    @endif
</div>

@if(! $client)
    <div class="card soft-panel"><div class="card-body text-center py-5 text-muted">Bu kullanıcıya bağlı aktif müşteri bulunamadı.</div></div>
@else
    @if($client->portal_welcome_text)
        <div class="alert alert-primary">{{ $client->portal_welcome_text }}</div>
    @endif

    <div class="row">
        @php
            $totals = $metrics['totals'];
            $currency = $metrics['currency'];
            $cards = [
                ['label' => 'Toplam Harcama', 'value' => number_format($totals['spend'], 2, ',', '.').' '.$currency],
                ['label' => 'Reklam Tıklaması', 'value' => number_format($totals['clicks'], 0, ',', '.')],
                ['label' => 'Dönüşüm', 'value' => number_format($totals['conversions'], 2, ',', '.')],
                ['label' => 'Organik Tıklama', 'value' => number_format($totals['organic_clicks'], 0, ',', '.')],
                ['label' => 'GA4 Oturum', 'value' => number_format($totals['sessions'], 0, ',', '.')],
                ['label' => 'Açık Uyarı', 'value' => number_format($totals['open_alerts'], 0, ',', '.')],
            ];
        @endphp
        @foreach($cards as $card)
            <div class="col-sm-6 col-xl-2 mb-4">
                <div class="card metric-card h-100"><div class="card-body">
                    <div class="portal-muted small mb-2">{{ $card['label'] }}</div>
                    <div class="metric-value">{{ $card['value'] }}</div>
                </div></div>
            </div>
        @endforeach
    </div>

    <div class="row">
        @foreach($metrics['channels'] as $channelKey => $channel)
            <div class="col-xl-3 col-md-6 mb-4">
                <div class="card soft-panel h-100">
                    <div class="card-body">
                        <h4 class="mb-3">{{ $channel['label'] }}</h4>
                        @if($channelKey === 'google_ads')
                            <p class="mb-1"><strong>Harcama:</strong> {{ number_format($channel['cost'], 2, ',', '.') }} {{ $metrics['currency'] }}</p>
                            <p class="mb-1"><strong>Tıklama:</strong> {{ number_format($channel['clicks'], 0, ',', '.') }}</p>
                            <p class="mb-1"><strong>CTR:</strong> %{{ number_format($channel['ctr'], 2, ',', '.') }}</p>
                            <p class="mb-0"><strong>Dönüşüm:</strong> {{ number_format($channel['conversions'], 2, ',', '.') }}</p>
                        @elseif($channelKey === 'meta_ads')
                            <p class="mb-1"><strong>Harcama:</strong> {{ number_format($channel['spend'], 2, ',', '.') }} {{ $metrics['currency'] }}</p>
                            <p class="mb-1"><strong>Tıklama:</strong> {{ number_format($channel['clicks'], 0, ',', '.') }}</p>
                            <p class="mb-1"><strong>Lead:</strong> {{ number_format($channel['leads'], 2, ',', '.') }}</p>
                            <p class="mb-0"><strong>Dönüşüm:</strong> {{ number_format($channel['conversions'], 2, ',', '.') }}</p>
                        @elseif($channelKey === 'search_console')
                            <p class="mb-1"><strong>Tıklama:</strong> {{ number_format($channel['clicks'], 0, ',', '.') }}</p>
                            <p class="mb-1"><strong>Gösterim:</strong> {{ number_format($channel['impressions'], 0, ',', '.') }}</p>
                            <p class="mb-1"><strong>CTR:</strong> %{{ number_format($channel['ctr'], 2, ',', '.') }}</p>
                            <p class="mb-0"><strong>Pozisyon:</strong> {{ number_format($channel['position'], 2, ',', '.') }}</p>
                        @else
                            <p class="mb-1"><strong>Oturum:</strong> {{ number_format($channel['sessions'], 0, ',', '.') }}</p>
                            <p class="mb-1"><strong>Kullanıcı:</strong> {{ number_format($channel['users'], 0, ',', '.') }}</p>
                            <p class="mb-1"><strong>Dönüşüm:</strong> {{ number_format($channel['conversions'], 2, ',', '.') }}</p>
                            <p class="mb-0"><strong>Gelir:</strong> {{ number_format($channel['revenue'], 2, ',', '.') }} {{ $metrics['currency'] }}</p>
                        @endif
                    </div>
                </div>
            </div>
        @endforeach
    </div>

    <div class="row">
        <div class="col-xl-8 mb-4">
            <div class="card soft-panel h-100">
                <div class="card-header border-0 pb-0 d-flex justify-content-between align-items-center">
                    <h4 class="card-title mb-0">Son Raporlar</h4>
                    <a href="{{ route('portal.reports', ['client_id' => $client->id]) }}" class="btn btn-sm btn-light">Tümünü Gör</a>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table">
                            <thead><tr><th>Rapor</th><th>Tarih Aralığı</th><th>Durum</th><th></th></tr></thead>
                            <tbody>
                            @forelse($latestReports as $report)
                                <tr>
                                    <td>{{ ucfirst($report->report_type) }}</td>
                                    <td>{{ optional($report->date_from)->format('d.m.Y') }} - {{ optional($report->date_to)->format('d.m.Y') }}</td>
                                    <td><span class="badge badge-light">{{ $report->status }}</span></td>
                                    <td class="text-end"><a href="{{ route('portal.reports.show', $report) }}" class="btn btn-sm btn-primary">Aç</a></td>
                                </tr>
                            @empty
                                <tr><td colspan="4" class="text-center text-muted py-4">Henüz rapor yok.</td></tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-4 mb-4">
            <div class="card soft-panel h-100">
                <div class="card-header border-0 pb-0"><h4 class="card-title mb-0">Açık Uyarılar</h4></div>
                <div class="card-body">
                    @forelse($openAlerts as $alert)
                        <div class="border-bottom pb-3 mb-3">
                            <div class="d-flex justify-content-between gap-2">
                                <strong>{{ $alert->title }}</strong>
                                <span class="badge badge-warning">{{ $alert->severity }}</span>
                            </div>
                            <p class="portal-muted mb-0 small">{{ $alert->message }}</p>
                        </div>
                    @empty
                        <p class="text-muted mb-0">Açık uyarı yok.</p>
                    @endforelse
                </div>
            </div>
        </div>
    </div>
@endif
@endsection
