@extends('layouts.default')

@section('content')
@php
    $live = $data['live'];
    $kpis = $live['kpis'];
    $summary = $live['summary'];
@endphp

<div class="container-fluid">
    <div class="row page-titles mx-0">
        <div class="col-sm-6 p-md-0">
            <div class="welcome-text">
                <h4>Ajans KPI / Yönetim Raporu</h4>
                <span>{{ $summary['range_label'] ?? '' }} aralığı için canlı operasyon özeti</span>
            </div>
        </div>
        <div class="col-sm-6 p-md-0 justify-content-sm-end mt-2 mt-sm-0 d-flex">
            <a href="{{ route('admin.management-reports.mail-settings') }}" class="btn btn-outline-primary btn-sm mr-2">Mail Ayarları</a>
            <ol class="breadcrumb mb-0">
                <li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">Dashboard</a></li>
                <li class="breadcrumb-item active">Ajans Yönetim Raporu</li>
            </ol>
        </div>
    </div>

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

    <div class="row">
        <div class="col-xl-3 col-lg-6 col-sm-6">
            <div class="widget-stat card">
                <div class="card-body p-4">
                    <div class="media ai-icon">
                        <span class="mr-3 bgl-primary text-primary"><i class="flaticon-381-heart"></i></span>
                        <div class="media-body">
                            <p class="mb-1">Ajans Sağlığı</p>
                            <h4 class="mb-0">{{ $live['overall_score'] }}/100</h4>
                            <span>{{ $summary['title'] }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-lg-6 col-sm-6">
            <div class="widget-stat card">
                <div class="card-body p-4">
                    <div class="media ai-icon">
                        <span class="mr-3 bgl-warning text-warning"><i class="flaticon-381-user-7"></i></span>
                        <div class="media-body">
                            <p class="mb-1">Riskli Müşteri</p>
                            <h4 class="mb-0">{{ $kpis['risk_clients'] ?? 0 }}</h4>
                            <span>Kritik: {{ $kpis['critical_clients'] ?? 0 }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-lg-6 col-sm-6">
            <div class="widget-stat card">
                <div class="card-body p-4">
                    <div class="media ai-icon">
                        <span class="mr-3 bgl-info text-info"><i class="flaticon-381-file"></i></span>
                        <div class="media-body">
                            <p class="mb-1">Üretilen Rapor</p>
                            <h4 class="mb-0">{{ $kpis['reports_generated'] ?? 0 }}</h4>
                            <span>Hatalı: {{ $kpis['reports_failed'] ?? 0 }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-3 col-lg-6 col-sm-6">
            <div class="widget-stat card">
                <div class="card-body p-4">
                    <div class="media ai-icon">
                        <span class="mr-3 bgl-danger text-danger"><i class="flaticon-381-warning-1"></i></span>
                        <div class="media-body">
                            <p class="mb-1">Operasyon Riski</p>
                            <h4 class="mb-0">{{ $kpis['api_errors'] ?? 0 }}</h4>
                            <span>Geciken görev: {{ $kpis['overdue_tasks'] ?? 0 }}</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-xl-4">
            <div class="card">
                <div class="card-header"><h4 class="card-title">Rapor Oluştur</h4></div>
                <div class="card-body">
                    <form method="GET" action="{{ route('admin.management-reports.index') }}" class="mb-3">
                        <div class="form-group">
                            <label>Dönem</label>
                            <select name="period_type" class="form-control">
                                @foreach(['daily' => 'Günlük', 'weekly' => 'Haftalık', 'monthly' => 'Aylık', 'custom' => 'Özel'] as $key => $label)
                                    <option value="{{ $key }}" @selected(($data['filters']['period_type'] ?? 'weekly') === $key)>{{ $label }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="form-row">
                            <div class="form-group col-md-6">
                                <label>Başlangıç</label>
                                <input type="date" name="date_from" class="form-control" value="{{ $data['filters']['date_from'] ?? '' }}">
                            </div>
                            <div class="form-group col-md-6">
                                <label>Bitiş</label>
                                <input type="date" name="date_to" class="form-control" value="{{ $data['filters']['date_to'] ?? '' }}">
                            </div>
                        </div>
                        <button class="btn btn-outline-primary btn-block">Canlı Önizle</button>
                    </form>
                    <form method="POST" action="{{ route('admin.management-reports.generate') }}" class="mb-2">
                        @csrf
                        <input type="hidden" name="period_type" value="{{ $data['filters']['period_type'] }}">
                        <input type="hidden" name="date_from" value="{{ $data['filters']['date_from'] }}">
                        <input type="hidden" name="date_to" value="{{ $data['filters']['date_to'] }}">
                        <button class="btn btn-primary btn-block">Bu Özeti Arşivle</button>
                    </form>
                    <form method="POST" action="{{ route('admin.management-reports.send-latest') }}">
                        @csrf
                        <input type="hidden" name="period_type" value="{{ $data['filters']['period_type'] }}">
                        <input type="hidden" name="generate_if_missing" value="1">
                        <button class="btn btn-success btn-block">Oluştur ve Yöneticiye Mail Gönder</button>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-xl-8">
            <div class="card">
                <div class="card-header"><h4 class="card-title">Yönetici Özeti</h4></div>
                <div class="card-body">
                    <p>{{ $summary['text'] }}</p>
                    <div class="row">
                        @foreach($live['recommendations'] as $recommendation)
                            <div class="col-md-6 mb-2">
                                <div class="alert alert-light border mb-0">{{ $recommendation }}</div>
                            </div>
                        @endforeach
                    </div>
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h4 class="card-title">Riskli Müşteriler</h4></div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-striped mb-0">
                            <thead><tr><th>Müşteri</th><th>Skor</th><th>Durum</th><th>Risk</th><th></th></tr></thead>
                            <tbody>
                                @forelse($live['risk_clients'] as $client)
                                    <tr>
                                        <td>{{ $client['company_name'] }}</td>
                                        <td>{{ $client['score'] }}/100</td>
                                        <td>{{ $client['status_label'] }}</td>
                                        <td>{{ $client['risk_count'] }}</td>
                                        <td><a href="{{ $client['url'] }}" class="btn btn-xs btn-outline-primary">Detay</a></td>
                                    </tr>
                                @empty
                                    <tr><td colspan="5" class="text-center text-muted p-4">Riskli müşteri 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-header"><h4 class="card-title">Kanal Sağlığı</h4></div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table mb-0">
                            <thead><tr><th>Kanal</th><th>Toplam</th><th>Aktif</th><th>Hata</th><th>Skor</th></tr></thead>
                            <tbody>
                                @foreach($live['channel_health'] as $item)
                                    <tr>
                                        <td>{{ $item['label'] }}</td>
                                        <td>{{ $item['total'] }}</td>
                                        <td>{{ $item['active'] }}</td>
                                        <td>{{ $item['errors'] }}</td>
                                        <td>{{ $item['score'] }}/100</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-6">
            <div class="card">
                <div class="card-header"><h4 class="card-title">Ekip İş Yükü</h4></div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table mb-0">
                            <thead><tr><th>Kullanıcı</th><th>Açık</th><th>Tamamlanan</th><th>Kritik</th><th>Geciken</th></tr></thead>
                            <tbody>
                                @forelse($live['team_workload']['users'] as $user)
                                    <tr>
                                        <td>{{ $user['name'] }}</td>
                                        <td>{{ $user['open'] }}</td>
                                        <td>{{ $user['completed'] }}</td>
                                        <td>{{ $user['critical'] }}</td>
                                        <td>{{ $user['overdue'] }}</td>
                                    </tr>
                                @empty
                                    <tr><td colspan="5" class="text-center text-muted p-4">Görev verisi yok.</td></tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-header"><h4 class="card-title">Arşivlenen Yönetim Raporları</h4></div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead><tr><th>#</th><th>Dönem</th><th>Tarih</th><th>Skor</th><th>Durum</th><th>Oluşturma</th><th></th></tr></thead>
                    <tbody>
                        @forelse($data['reports'] as $report)
                            <tr>
                                <td>#{{ $report->id }}</td>
                                <td>{{ $report->periodLabel() }}</td>
                                <td>{{ optional($report->date_from)->format('d.m.Y') }} - {{ optional($report->date_to)->format('d.m.Y') }}</td>
                                <td>{{ $report->overall_score }}/100</td>
                                <td>{{ $report->statusLabel() }}</td>
                                <td>{{ optional($report->generated_at)->format('d.m.Y H:i') }}</td>
                                <td><a href="{{ route('admin.management-reports.show', $report) }}" class="btn btn-xs btn-primary">Aç</a></td>
                            </tr>
                        @empty
                            <tr><td colspan="7" class="text-center text-muted p-4">Henüz arşivlenmiş yönetim raporu yok.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        <div class="card-footer">{{ $data['reports']->links() }}</div>
    </div>
</div>
@endsection
