@extends('admin.layouts.app')

@section('title', $title)

@section('content')
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <div class="card border-0 shadow-sm mt-3">
                    <!-- Page Heading -->
                    <div class="card-header bg-white d-flex justify-content-between align-items-center py-3">
                        <h4 class="mb-0">Notifications</h4>
                        <div>
                            @if(auth()->user()->unreadNotifications->count())
                            <button type="button" class="btn btn-sm btn-primary me-2" onclick="markAllAsRead()">
                                <i class="fa fa-solid fa-check"></i> Mark All as Read
                            </button>
                            @endif
                            @if(auth()->user()->notifications->count())
                                <button type="button" class="btn btn-sm btn-danger" onclick="deleteAllNotifications()">
                                    <i class="fa fa-regular fa-trash"></i> Delete All
                                </button>
                            @endif
                        </div>
                    </div>
                    <!-- Notifications Sections -->
                    <div class="card-body p-0">
                        <ul class="list-unstyled mb-0" id="notification-list">
                            <!-- Dynamic Notifications -->
                            @forelse(auth()->user()->notifications()->latest()->get() as $notification)
                                <!-- Order Created Notification -->
                                @if($notification->type == 'App\Notifications\Order\OrderCreated')
                                    <li class="notification-item d-flex align-items-start px-4 py-3 @if(isset($notification->read_at)) bg-light @endif">
                                        <div class="avatar rounded-circle me-3" style="width: 50px; height: 50px; background: #eaeaea; display: flex; align-items: center; justify-content: center;">
                                            <i class="fa fa-solid fa-shopping-cart text-aqua"></i>
                                        </div>
                                        <div class="notification-content flex-grow-1">
                                            <a href="{{route('ch-admin.order.show', [$notification->data['order_id']])}}" class="text-decoration-none text-reset">
                                                <p class="mb-1">
                                                    You have received a new Order [{{$notification->data['order_reference']}}].
                                                    <span class="text-muted d-block small">{{$notification->created_at->diffForHumans()}}</span>
                                                </p>
                                            </a>
                                        </div>
                                        @if(!isset($notification->read_at))
                                            <button type="button" class="btn btn-sm btn-success" onclick="markAsRead({{ $notification->id }})">Mark as Read</button>
                                        @endif
                                    </li>
                                @endif

                                <!-- Ticket Created Notification -->
                                @if($notification->type == 'App\Notifications\Ticket\TicketCreatedAdminNotification')
                                    <li class="notification-item d-flex align-items-start px-4 py-3 @if(isset($notification->read_at)) bg-light @endif">
                                        <div class="avatar rounded-circle me-3" style="width: 50px; height: 50px; background: #eaeaea; display: flex; align-items: center; justify-content: center;">
                                            <i class="fa fa-solid fa-ticket text-aqua"></i>
                                        </div>
                                        <div class="notification-content flex-grow-1">
                                            <a href="{{route('ch-admin.ticket.show', [$notification->data['id']])}}" class="text-decoration-none text-reset">
                                                <p class="mb-1">
                                                    New Ticket [{{$notification->data['reference_id']}}] has been created.
                                                    <span class="text-muted d-block small">{{$notification->created_at->diffForHumans()}}</span>
                                                </p>
                                            </a>
                                        </div>
                                        @if(!isset($notification->read_at))
                                            <button type="button" class="btn btn-sm btn-success" onclick="markAsRead({{ $notification->id }})">Mark as Read</button>
                                        @endif
                                    </li>
                                @endif

                                <!-- Customer Response on Ticket Notification -->
                                @if($notification->type == 'App\Notifications\Ticket\TicketCreatedAdminNotification')
                                    <li class="notification-item d-flex align-items-start px-4 py-3 @if(isset($notification->read_at)) bg-light @endif">
                                        <div class="avatar rounded-circle me-3" style="width: 50px; height: 50px; background: #eaeaea; display: flex; align-items: center; justify-content: center;">
                                            <i class="fa fa-solid fa-ticket text-aqua"></i>
                                        </div>
                                        <div class="notification-content flex-grow-1">
                                            <a href="{{route('ch-admin.ticket.show', [$notification->data['id']])}}" class="text-decoration-none text-reset">
                                                <p class="mb-1">
                                                    New repsonse on Ticket [{{$notification->data['reference_id']}}] has been submitted.
                                                    <span class="text-muted d-block small">{{$notification->created_at->diffForHumans()}}</span>
                                                </p>
                                            </a>
                                        </div>
                                        @if(!isset($notification->read_at))
                                            <button type="button" class="btn btn-sm btn-success" onclick="markAsRead({{ $notification->id }})">Mark as Read</button>
                                        @endif
                                    </li>
                                @endif

                                @if(!$loop->last)
                                    <hr class="my-0 border border-dark border-end-0 border-start-0 border-bottom-0">
                                @endif
                            @empty
                                <!-- No Notification Message -->
                                <li class="py-3 text-center">No new notifications.</li>
                            @endforelse
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script>
        function markAsRead(notificationId) {
            $.ajax({
                url: "{{ route('ch-admin.ch_admin_notifications') }}",
                type: "GET",
                data: {
                    mark_as_read: true,
                    notification_id: notificationId
                },
                success: function (response) {
                    if (response.success) {
                        location.reload();
                    }
                },
                error: function () {
                    alert("There was an error marking notifications as read.");
                }
            });
        }

        function deleteAllNotifications() {
            $.ajax({
                url: "{{ route('ch-admin.ch_admin_notifications') }}",
                type: "GET",
                data: {
                    delete_notifications: true
                },
                success: function (response) {
                    if (response.success) {
                        location.reload();
                    }
                },
                error: function () {
                    alert("There was an error deleting notifications.");
                }
            });
        }
    </script>
@endpush
