@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>Bildirim Merkezi</h4>
                <span>API, mail, uyarı, sağlık skoru ve görev bildirimleri</span>
            </div>
        </div>
        <div class="col-sm-6 p-md-0 justify-content-sm-end mt-2 mt-sm-0 d-flex">
            <form method="POST" action="{{ route('admin.notifications.build') }}" class="mr-2">
                @csrf
                <button class="btn btn-primary btn-sm" type="submit">Bildirimleri Güncelle</button>
            </form>
            <form method="POST" action="{{ route('admin.notifications.mark-all-read') }}">
                @csrf
                <button class="btn btn-outline-secondary btn-sm" type="submit">Tümünü Okundu Yap</button>
            </form>
        </div>
    </div>

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

    <div class="row">
        <div class="col-xl-3 col-sm-6">
            <div class="card"><div class="card-body"><h6>Okunmamış</h6><h3>{{ $summary['unread'] }}</h3></div></div>
        </div>
        <div class="col-xl-3 col-sm-6">
            <div class="card"><div class="card-body"><h6>Kritik</h6><h3>{{ $summary['danger'] }}</h3></div></div>
        </div>
        <div class="col-xl-3 col-sm-6">
            <div class="card"><div class="card-body"><h6>Uyarı</h6><h3>{{ $summary['warning'] }}</h3></div></div>
        </div>
        <div class="col-xl-3 col-sm-6">
            <div class="card"><div class="card-body"><h6>Toplam Aktif</h6><h3>{{ $summary['total'] }}</h3></div></div>
        </div>
    </div>

    <div class="card">
        <div class="card-header d-block">
            <h4 class="card-title">Bildirimler</h4>
            <form method="GET" class="row mt-3">
                <div class="col-md-3 mb-2">
                    <select name="status" class="form-control">
                        <option value="">Tüm durumlar</option>
                        <option value="unread" @selected(request('status') === 'unread')>Okunmamış</option>
                        <option value="read" @selected(request('status') === 'read')>Okunmuş</option>
                    </select>
                </div>
                <div class="col-md-3 mb-2">
                    <select name="level" class="form-control">
                        <option value="">Tüm önem seviyeleri</option>
                        <option value="danger" @selected(request('level') === 'danger')>Kritik</option>
                        <option value="warning" @selected(request('level') === 'warning')>Uyarı</option>
                        <option value="info" @selected(request('level') === 'info')>Bilgi</option>
                        <option value="success" @selected(request('level') === 'success')>Başarılı</option>
                    </select>
                </div>
                <div class="col-md-3 mb-2">
                    <select name="category" class="form-control">
                        <option value="">Tüm kategoriler</option>
                        <option value="api" @selected(request('category') === 'api')>API</option>
                        <option value="mail" @selected(request('category') === 'mail')>Mail</option>
                        <option value="alert" @selected(request('category') === 'alert')>Uyarı</option>
                        <option value="health" @selected(request('category') === 'health')>Sağlık Skoru</option>
                        <option value="operation" @selected(request('category') === 'operation')>Operasyon</option>
                    </select>
                </div>
                <div class="col-md-3 mb-2">
                    <button class="btn btn-secondary btn-block">Filtrele</button>
                </div>
            </form>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead>
                    <tr>
                        <th>Durum</th>
                        <th>Bildirim</th>
                        <th>Kategori</th>
                        <th>Müşteri</th>
                        <th>Tarih</th>
                        <th class="text-right">İşlem</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($notifications as $notification)
                        <tr class="{{ $notification->read_at ? '' : 'font-weight-bold' }}">
                            <td><span class="badge {{ $notification->badgeClass() }}">{{ $notification->label() }}</span></td>
                            <td>
                                <a href="{{ route('admin.notifications.show', $notification) }}">{{ $notification->title }}</a>
                                <div class="text-muted small">{{ \Illuminate\Support\Str::limit($notification->message, 120) }}</div>
                            </td>
                            <td>{{ ucfirst($notification->category) }}</td>
                            <td>{{ optional($notification->client)->name ?? '-' }}</td>
                            <td>{{ optional($notification->created_at)->format('d.m.Y H:i') }}</td>
                            <td class="text-right">
                                @if($notification->action_url)
                                    <a href="{{ $notification->action_url }}" class="btn btn-xs btn-outline-primary">{{ $notification->action_label ?: 'Git' }}</a>
                                @endif
                                @unless($notification->read_at)
                                    <form method="POST" action="{{ route('admin.notifications.read', $notification) }}" class="d-inline">
                                        @csrf
                                        <button class="btn btn-xs btn-outline-secondary">Okundu</button>
                                    </form>
                                @endunless
                                <form method="POST" action="{{ route('admin.notifications.dismiss', $notification) }}" class="d-inline">
                                    @csrf
                                    <button class="btn btn-xs btn-outline-danger">Gizle</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="6" class="text-center p-4 text-muted">Aktif bildirim yok.</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        <div class="card-footer">
            {{ $notifications->links() }}
        </div>
    </div>
</div>
@endsection
