@extends('layouts.default')
@section('title', $page_title)
@section('content')
<div class="container-fluid">
    @include('admin.partials.page-header', ['actions' => new \Illuminate\Support\HtmlString('
        <div class="d-flex gap-2">
            <form method="POST" action="'.route('admin.alert-rules.evaluate', $alertRule).'">'.csrf_field().'<button class="btn btn-warning">Kuralı Çalıştır</button></form>
            <a href="'.route('admin.alert-rules.edit', $alertRule).'" class="btn btn-primary">Düzenle</a>
        </div>
    ')])

    <div class="row">
        <div class="col-xl-4">
            <div class="card">
                <div class="card-header"><h4 class="card-title">Kural Özeti</h4></div>
                <div class="card-body">
                    <dl class="row mb-0">
                        <dt class="col-5">Müşteri</dt><dd class="col-7">{{ $alertRule->client?->company_name ?: 'Tüm müşteriler' }}</dd>
                        <dt class="col-5">Platform</dt><dd class="col-7">{{ strtoupper(str_replace('_',' ', $alertRule->platform)) }}</dd>
                        <dt class="col-5">Koşul</dt><dd class="col-7"><code>{{ $alertRule->metric }}</code> {{ $alertRule->operator }} {{ $alertRule->threshold }}</dd>
                        <dt class="col-5">Periyot</dt><dd class="col-7">{{ $alertRule->period }}</dd>
                        <dt class="col-5">Önem</dt><dd class="col-7">{{ $alertRule->severity }}</dd>
                        <dt class="col-5">Son değer</dt><dd class="col-7">{{ $alertRule->last_value ?? '-' }}</dd>
                        <dt class="col-5">Son kontrol</dt><dd class="col-7">{{ $alertRule->last_evaluated_at?->format('d.m.Y H:i') ?: '-' }}</dd>
                        <dt class="col-5">Son tetikleme</dt><dd class="col-7">{{ $alertRule->last_triggered_at?->format('d.m.Y H:i') ?: '-' }}</dd>
                    </dl>
                </div>
            </div>
        </div>

        <div class="col-xl-8">
            <div class="card">
                <div class="card-header"><h4 class="card-title">Bu Kurala Ait Uyarılar</h4></div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-responsive-md align-middle">
                            <thead><tr><th>Tarih</th><th>Müşteri</th><th>Değer</th><th>Durum</th><th>Bildirim</th><th class="text-end">İşlem</th></tr></thead>
                            <tbody>
                            @forelse($logs as $log)
                                <tr>
                                    <td>{{ $log->triggered_at?->format('d.m.Y H:i') }}</td>
                                    <td>{{ $log->client?->company_name }}</td>
                                    <td>{{ $log->current_value }} / eşik {{ $log->threshold }}</td>
                                    <td><span class="badge badge-{{ $log->status === 'open' ? 'warning' : 'success' }}">{{ $log->status }}</span></td>
                                    <td>{{ $log->notified_at?->format('d.m.Y H:i') ?: '-' }}</td>
                                    <td class="text-end">
                                        @if($log->status === 'open')
                                            <form method="POST" action="{{ route('admin.alert-logs.resolve', $log) }}" class="d-inline">@csrf<button class="btn btn-xs btn-outline-success">Çözüldü</button></form>
                                        @endif
                                    </td>
                                </tr>
                            @empty
                                <tr><td colspan="6" class="text-center text-muted py-5">Bu kurala ait uyarı yok.</td></tr>
                            @endforelse
                            </tbody>
                        </table>
                    </div>
                    {{ $logs->links() }}
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
