@extends('layouts.default')

@section('title', 'Dashboard')

@section('content')
<div class="container-fluid">
    @include('admin.partials.page-header')

    <div class="row">
        @foreach([
            ['label' => 'Toplam Müşteri', 'value' => $stats['clients'], 'icon' => 'flaticon-381-user-7'],
            ['label' => 'Aktif Müşteri', 'value' => $stats['active_clients'], 'icon' => 'flaticon-381-success'],
            ['label' => 'API Bağlantısı', 'value' => $stats['connections'], 'icon' => 'flaticon-381-link'],
            ['label' => 'Otomatik Rapor', 'value' => $stats['scheduled_reports'], 'icon' => 'flaticon-381-time'],
        ] as $card)
            <div class="col-xl-3 col-sm-6">
                <div class="card">
                    <div class="card-body d-flex align-items-center justify-content-between">
                        <div>
                            <h3 class="mb-1 text-black">{{ $card['value'] }}</h3>
                            <span class="text-muted">{{ $card['label'] }}</span>
                        </div>
                        <i class="{{ $card['icon'] }} fs-2 text-primary"></i>
                    </div>
                </div>
            </div>
        @endforeach
    </div>

    <div class="row">
        <div class="col-xl-8">
            <div class="card">
                <div class="card-header border-0 pb-0">
                    <h4 class="card-title mb-0">Kanal Bağlantı Özeti</h4>
                </div>
                <div class="card-body">
                    <div class="row text-center">
                        <div class="col-md-3 col-6 mb-3">
                            <h3>{{ $stats['google_connections'] }}</h3>
                            <span>Google Ads</span>
                        </div>
                        <div class="col-md-3 col-6 mb-3">
                            <h3>{{ $stats['meta_connections'] }}</h3>
                            <span>Meta Ads</span>
                        </div>
                        <div class="col-md-3 col-6 mb-3">
                            <h3>{{ $stats['gsc_connections'] }}</h3>
                            <span>Search Console</span>
                        </div>
                        <div class="col-md-3 col-6 mb-3">
                            <h3>{{ $stats['alerts'] }}</h3>
                            <span>Aktif Uyarı</span>
                        </div>
                    </div>
                    <div class="table-responsive mt-3">
                        <table class="table table-responsive-md">
                            <thead>
                            <tr>
                                <th>Platform</th>
                                <th>Müşteri</th>
                                <th>Hesap</th>
                                <th>Durum</th>
                                <th>Son Senkron</th>
                            </tr>
                            </thead>
                            <tbody>
                            @forelse($connections as $connection)
                                <tr>
                                    <td>{{ strtoupper(str_replace('_', ' ', $connection->platform)) }}</td>
                                    <td>{{ $connection->client?->company_name }}</td>
                                    <td>{{ $connection->account_name ?: $connection->connection_name }}</td>
                                    <td>@include('admin.partials.status-badge', ['status' => $connection->status])</td>
                                    <td>{{ $connection->last_sync_at?->format('d.m.Y H:i') ?? '-' }}</td>
                                </tr>
                            @empty
                                <tr><td colspan="5" class="text-center text-muted py-4">Henüz API bağlantısı eklenmedi.</td></tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-4">
            <div class="card">
                <div class="card-header border-0 pb-0">
                    <h4 class="card-title mb-0">Son Müşteriler</h4>
                </div>
                <div class="card-body">
                    @forelse($latestClients as $client)
                        <div class="d-flex justify-content-between align-items-center border-bottom py-2">
                            <div>
                                <a href="{{ route('admin.clients.show', $client) }}" class="text-black font-w600">{{ $client->company_name }}</a>
                                <div class="small text-muted">{{ $client->email ?: $client->website ?: '-' }}</div>
                            </div>
                            @include('admin.partials.status-badge', ['status' => $client->status])
                        </div>
                    @empty
                        <div class="text-center text-muted py-4">İlk müşteriyi ekleyerek başlayalım.</div>
                    @endforelse
                    <a href="{{ route('admin.clients.create') }}" class="btn btn-primary btn-sm mt-3 w-100">Yeni Müşteri Ekle</a>
                </div>
            </div>

            <div class="card">
                <div class="card-header border-0 pb-0">
                    <h4 class="card-title mb-0">Son Raporlar</h4>
                </div>
                <div class="card-body">
                    @forelse($latestReports as $report)
                        <div class="border-bottom py-2">
                            <div class="d-flex justify-content-between">
                                <strong>{{ $report->client?->company_name }}</strong>
                                @include('admin.partials.status-badge', ['status' => $report->status])
                            </div>
                            <div class="small text-muted">{{ ucfirst($report->report_type) }} · {{ $report->date_from?->format('d.m.Y') }} - {{ $report->date_to?->format('d.m.Y') }}</div>
                        </div>
                    @empty
                        <div class="text-center text-muted py-4">Henüz rapor oluşturulmadı.</div>
                    @endforelse
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
