@extends('layouts.default')
@section('title', $client->company_name)
@section('content')
<div class="container-fluid">
    @include('admin.partials.page-header', ['actions' => new \Illuminate\Support\HtmlString(
        '<a href="'.route('admin.clients.portal-users.index', $client).'" class="btn btn-outline-primary me-2">Portal Kullanıcıları</a>'.
        '<a href="'.route('admin.clients.edit', $client).'" class="btn btn-primary">Müşteriyi Düzenle</a>'
    )])

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif

    <div class="row">
        <div class="col-xl-4">
            <div class="card">
                <div class="card-body">
                    <h4 class="mb-1">{{ $client->company_name }}</h4>
                    <div class="mb-3">@include('admin.partials.status-badge', ['status' => $client->status])</div>
                    <p class="mb-1"><strong>Yetkili:</strong> {{ $client->contact_person ?: '-' }}</p>
                    <p class="mb-1"><strong>E-posta:</strong> {{ $client->email ?: '-' }}</p>
                    <p class="mb-1"><strong>Telefon:</strong> {{ $client->phone ?: '-' }}</p>
                    <p class="mb-1"><strong>Site:</strong> {{ $client->website ?: '-' }}</p>
                    <p class="mb-0"><strong>Para / Saat:</strong> {{ $client->currency_code }} · {{ $client->timezone }}</p>
                </div>
            </div>

            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start">
                        <div>
                            <h4 class="mb-1">Müşteri Portalı</h4>
                            <p class="text-muted mb-2">Müşterinin göreceği rapor ve uyarı alanı.</p>
                        </div>
                        <span class="badge {{ $client->portal_enabled ?? true ? 'badge-success' : 'badge-secondary' }}">
                            {{ $client->portal_enabled ?? true ? 'Aktif' : 'Pasif' }}
                        </span>
                    </div>
                    <p class="mb-2"><strong>Kullanıcı:</strong> {{ $client->users()->count() }}</p>
                    <a href="{{ route('admin.clients.portal-users.index', $client) }}" class="btn btn-sm btn-primary">Portal Kullanıcılarını Yönet</a>
                    <a href="{{ route('portal.login') }}" target="_blank" class="btn btn-sm btn-light">Portal Girişini Aç</a>
                </div>
            </div>
        </div>
        <div class="col-xl-8">
            <div class="card">
                <div class="card-header border-0 pb-0"><h4 class="card-title mb-0">Bağlantılar</h4></div>
                <div class="card-body">
                    <div class="row">
                        @foreach(['google_ads' => 'Google Ads', 'meta_ads' => 'Meta Ads', 'search_console' => 'Search Console', 'ga4' => 'GA4'] as $platform => $label)
                            @php $count = $client->platformConnections->where('platform', $platform)->count(); @endphp
                            <div class="col-md-3 col-6 mb-3">
                                <div class="border rounded p-3 text-center">
                                    <h3 class="mb-1">{{ $count }}</h3>
                                    <span>{{ $label }}</span>
                                </div>
                            </div>
                        @endforeach
                    </div>
                    <div class="table-responsive mt-2">
                        <table class="table table-responsive-md">
                            <thead><tr><th>Platform</th><th>Bağlantı</th><th>Hesap ID</th><th>Durum</th></tr></thead>
                            <tbody>
                            @forelse($client->platformConnections as $connection)
                                <tr>
                                    <td>{{ strtoupper(str_replace('_',' ', $connection->platform)) }}</td>
                                    <td>{{ $connection->connection_name }}</td>
                                    <td>{{ $connection->account_id ?: '-' }}</td>
                                    <td>@include('admin.partials.status-badge', ['status' => $connection->status])</td>
                                </tr>
                            @empty
                                <tr><td colspan="4" class="text-center text-muted py-4">Henüz bağlantı yok.</td></tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-xl-6">
            <div class="card"><div class="card-body"><h4>Rapor Alıcıları</h4><p class="text-muted mb-0">{{ implode(', ', $client->report_recipients ?? []) ?: 'Tanımlı alıcı yok.' }}</p></div></div>
        </div>
        <div class="col-xl-6">
            <div class="card"><div class="card-body"><h4>Notlar</h4><p class="text-muted mb-0">{{ $client->notes ?: 'Müşteri notu yok.' }}</p></div></div>
        </div>
    </div>
</div>
@endsection
