@extends('layouts.default')

@section('content')
<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 Yönetim Raporu #{{ $report->id }}</h4>
                <span>{{ $report->periodLabel() }} · {{ optional($report->date_from)->format('d.m.Y') }} - {{ optional($report->date_to)->format('d.m.Y') }}</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.index') }}" class="btn btn-outline-secondary btn-sm mr-2">Geri</a>
            <form method="POST" action="{{ route('admin.management-reports.regenerate', $report) }}" class="mr-2">
                @csrf
                <button class="btn btn-outline-primary btn-sm">Yeniden Oluştur</button>
            </form>
            @if($report->htmlExists())
                <a href="{{ route('admin.management-reports.download', [$report, 'html']) }}" class="btn btn-primary btn-sm mr-2">HTML İndir</a>
            @endif
            @if($report->csvExists())
                <a href="{{ route('admin.management-reports.download', [$report, 'csv']) }}" class="btn btn-success btn-sm mr-2">CSV İndir</a>
            @endif
            <a href="{{ route('admin.management-reports.mail-settings') }}" class="btn btn-outline-dark btn-sm">Mail Ayarları</a>
        </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

    @if($report->status === 'failed')
        <div class="alert alert-danger">{{ $report->error_message }}</div>
    @endif


    <div class="card">
        <div class="card-header"><h4 class="card-title">Yönetici Raporunu Mail Gönder</h4></div>
        <div class="card-body">
            <form method="POST" action="{{ route('admin.management-reports.send', $report) }}">
                @csrf
                <div class="form-row">
                    <div class="form-group col-md-4">
                        <label>Alıcılar</label>
                        <textarea name="recipients" class="form-control" rows="2" placeholder="Boş bırakılırsa varsayılan alıcılar kullanılır"></textarea>
                    </div>
                    <div class="form-group col-md-4">
                        <label>CC</label>
                        <textarea name="cc" class="form-control" rows="2"></textarea>
                    </div>
                    <div class="form-group col-md-4">
                        <label>Not</label>
                        <textarea name="note" class="form-control" rows="2" placeholder="Mail giriş notu"></textarea>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <div class="custom-control custom-checkbox mr-4">
                        <input type="checkbox" class="custom-control-input" id="include_html_send" name="include_html" value="1" checked>
                        <label class="custom-control-label" for="include_html_send">HTML ekle</label>
                    </div>
                    <div class="custom-control custom-checkbox mr-4">
                        <input type="checkbox" class="custom-control-input" id="include_csv_send" name="include_csv" value="1" checked>
                        <label class="custom-control-label" for="include_csv_send">CSV ekle</label>
                    </div>
                    <button class="btn btn-success">Mail Olarak Gönder</button>
                </div>
            </form>
        </div>
    </div>

    <div class="row">
        <div class="col-xl-3 col-lg-6 col-sm-6">
            <div class="widget-stat card"><div class="card-body"><p class="mb-1">Genel Skor</p><h3>{{ $report->overall_score }}/100</h3></div></div>
        </div>
        <div class="col-xl-3 col-lg-6 col-sm-6">
            <div class="widget-stat card"><div class="card-body"><p class="mb-1">Müşteri</p><h3>{{ $report->kpis['total_clients'] ?? 0 }}</h3></div></div>
        </div>
        <div class="col-xl-3 col-lg-6 col-sm-6">
            <div class="widget-stat card"><div class="card-body"><p class="mb-1">Riskli Müşteri</p><h3>{{ $report->kpis['risk_clients'] ?? 0 }}</h3></div></div>
        </div>
        <div class="col-xl-3 col-lg-6 col-sm-6">
            <div class="widget-stat card"><div class="card-body"><p class="mb-1">API Hatası</p><h3>{{ $report->kpis['api_errors'] ?? 0 }}</h3></div></div>
        </div>
    </div>

    <div class="card">
        <div class="card-header"><h4 class="card-title">Yönetici Özeti</h4></div>
        <div class="card-body">
            <p>{{ $report->summary['text'] ?? '' }}</p>
            <div class="row">
                @foreach(($report->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="row">
        <div class="col-xl-6">
            <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 mb-0">
                            <thead><tr><th>Müşteri</th><th>Skor</th><th>Durum</th><th>Risk</th></tr></thead>
                            <tbody>
                                @forelse(($report->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></tr>
                                @empty
                                    <tr><td colspan="4" class="text-center text-muted p-4">Riskli müşteri yok.</td></tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <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(($report->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>
</div>
@endsection
