@extends('admin.admin_master')
@section('admin')

<div class="page-content">
    <div class="container-fluid">
        <!-- Page Header -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="page-header-card">
                    <div class="page-header-content">
                        <div class="d-flex align-items-center justify-content-between">
                            <div class="d-flex align-items-center">
                                <div class="page-icon me-3">
                                    <div class="avatar-md">
                                        <div class="avatar-title bg-primary-subtle text-primary rounded-circle">
                                            <i class="ri-notification-3-line font-size-24"></i>
                                        </div>
                                    </div>
                                </div>
                                <div class="flex-grow-1">
                                    <h4 class="page-title mb-1">Notifications</h4>
                                    <p class="page-subtitle text-muted mb-0">
                                        View your recent notifications
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Notifications Card -->
        <div class="row">
            <div class="col-12">
                <div class="card shadow-sm border-0">
                    <div class="card-body p-4">
                        <div class="d-flex justify-content-between align-items-center mb-4">
                            <div>
                                <h5 class="card-title font-size-16 mb-1">All Notifications</h5>
                                <p class="card-title-desc text-muted mb-0 font-size-14">List of your notifications</p>
                            </div>
                            <button type="button" class="btn btn-outline-secondary btn-sm" onclick="location.reload()" data-bs-toggle="tooltip" title="Refresh">
                                <i class="ri-refresh-line font-size-12"></i>
                            </button>
                        </div>

                        @if(session('message'))
                            <div class="alert alert-success alert-dismissible fade show shadow-sm border-0" role="alert">
                                <div class="d-flex align-items-center">
                                    <div class="avatar-xs me-3">
                                        <div class="avatar-title bg-success text-white rounded-circle">
                                            <i class="ri-check-line font-size-16"></i>
                                        </div>
                                    </div>
                                    <div class="flex-grow-1">
                                        <h6 class="alert-heading mb-1 font-size-15">Success!</h6>
                                        <div class="font-size-13">{{ session('message') }}</div>
                                    </div>
                                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                </div>
                            </div>
                        @endif

                        @if(session('error'))
                            <div class="alert alert-danger alert-dismissible fade show shadow-sm border-0" role="alert">
                                <div class="d-flex align-items-center">
                                    <div class="avatar-xs me-3">
                                        <div class="avatar-title bg-danger text-white rounded-circle">
                                            <i class="ri-error-warning-line font-size-16"></i>
                                        </div>
                                    </div>
                                    <div class="flex-grow-1">
                                        <h6 class="alert-heading mb-1 font-size-15">Error!</h6>
                                        <div class="font-size-13">{{ session('error') }}</div>
                                    </div>
                                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                </div>
                            </div>
                        @endif

                        @if($notifications->count())
                            <div class="table-responsive mt-4">
                                <table class="table table-striped table-bordered dt-responsive nowrap w-100">
                                    <thead class="table-light">
                                        <tr>
                                            <th>#</th>
                                            <th>Message</th>
                                            <th>Date</th>
                                            <th>Status</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($notifications as $key => $notification)
                                        <tr>
                                            <td class="text-center">
                                                <span class="badge badge-soft-primary font-size-12">{{ $key + 1 }}</span>
                                            </td>
                                            <td>
                                                <span class="font-medium">{{ $notification->data['message'] ?? 'Notification' }}</span>
                                            </td>
                                            <td>
                                                <span class="text-muted font-size-13">{{ $notification->created_at->diffForHumans() }}</span>
                                            </td>
                                            <td>
                                                @if(is_null($notification->read_at))
                                                    <span class="badge bg-warning text-dark">Unread</span>
                                                @else
                                                    <span class="badge bg-success">Read</span>
                                                @endif
                                            </td>
                                            <td>
                                                @if(is_null($notification->read_at))
                                                    <form action="{{ route('notifications.read', $notification->id) }}" method="POST" style="display:inline;">
                                                        @csrf
                                                        <button class="btn btn-sm btn-outline-primary" type="submit">Mark as read</button>
                                                    </form>
                                                @else
                                                    <span class="text-success font-size-13">—</span>
                                                @endif
                                            </td>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                            <div class="mt-4">
                                {{ $notifications->links() }}
                            </div>
                        @else
                            <p class="text-muted">You have no notifications.</p>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
