@extends('layouts.default')
@section('title', $page_title)
@section('content')
<div class="container-fluid">
    @include('admin.partials.page-header', ['actions' => new \Illuminate\Support\HtmlString('<a href="'.route('admin.alert-rules.index').'" class="btn btn-primary">Kurallara Dön</a>')])

    <div class="card">
        <div class="card-body">
            <form method="GET" class="row g-3 align-items-end">
                <div class="col-md-3">
                    <label class="form-label">Müşteri</label>
                    <select name="client_id" class="form-control default-select">
                        <option value="">Tümü</option>
                        @foreach($clients as $client)
                            <option value="{{ $client->id }}" @selected(request('client_id') == $client->id)>{{ $client->company_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2"><label class="form-label">Platform</label><select name="platform" class="form-control"><option value="">Tümü</option>@foreach(['google_ads','meta_ads','search_console','ga4','system'] as $platform)<option value="{{ $platform }}" @selected(request('platform')===$platform)>{{ strtoupper(str_replace('_',' ', $platform)) }}</option>@endforeach</select></div>
                <div class="col-md-2"><label class="form-label">Önem</label><select name="severity" class="form-control"><option value="">Tümü</option>@foreach(['info','warning','critical'] as $severity)<option value="{{ $severity }}" @selected(request('severity')===$severity)>{{ ucfirst($severity) }}</option>@endforeach</select></div>
                <div class="col-md-2"><label class="form-label">Durum</label><select name="status" class="form-control"><option value="">Tümü</option><option value="open" @selected(request('status')==='open')>Açık</option><option value="resolved" @selected(request('status')==='resolved')>Çözüldü</option></select></div>
                <div class="col-md-3"><button class="btn btn-primary">Filtrele</button><a href="{{ route('admin.alert-logs.index') }}" class="btn btn-light">Temizle</a></div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-responsive-md align-middle">
                    <thead><tr><th>Tarih</th><th>Başlık</th><th>Müşteri</th><th>Platform</th><th>Koşul</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><strong>{{ $log->title }}</strong><div class="text-muted fs-12">{{ $log->message }}</div></td>
                            <td>{{ $log->client?->company_name ?: '-' }}</td>
                            <td>{{ strtoupper(str_replace('_',' ', $log->platform)) }}</td>
                            <td><code>{{ $log->metric }}</code> {{ $log->operator }} {{ $log->threshold }}<div class="text-muted fs-12">Değer: {{ $log->current_value }}</div></td>
                            <td><span class="badge badge-{{ $log->status === 'open' ? 'warning' : 'success' }}">{{ $log->status }}</span><div class="text-muted fs-12">{{ $log->severity }}</div></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="8" class="text-center text-muted py-5">Uyarı kaydı yok.</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            {{ $logs->links() }}
        </div>
    </div>
</div>
@endsection
