@extends('pemerintah.main')

@section('content')

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Notifikasi Penawaran Program</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                background-color: #fafafa;
                margin: 0;
                padding: 0;
                padding-top: 80px;
            }

            .notification-container {
                max-width: 600px;
                margin: 50px auto;
                background-color: #fff;
                border-radius: 8px;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                padding: 20px;
            }

            h2 {
                text-align: center;
                color: #333;
                margin-bottom: 20px;
            }

            .notification-list {
                list-style-type: none;
                padding: 0;
                margin: 0;
            }

            .notification-item {
                display: flex;
                align-items: center;
                padding: 15px 10px;
                border-bottom: 1px solid #eee;
            }

            .notification-item:last-child {
                border-bottom: none;
            }

            .user-avatar {
                width: 80px;
                height: 80px;
                border-radius: 50%;
                margin-right: 15px;
                object-fit: cover;
            }

            .notification-content {
                flex-grow: 1;
            }

            .notification-content p {
                margin: 0;
                color: #555;
            }

            .action-buttons {
                display: flex;
                gap: 10px;
            }

            .action-btn {
                padding: 8px 15px;
                border-radius: 5px;
                border: none;
                cursor: pointer;
                transition: background-color 0.3s;
                color: #fff;
            }

            .accept-btn {
                background-color: #3897f0;
                /* Warna untuk menerima */
            }

            .decline-btn {
                background-color: #e74c3c;
                /* Warna untuk menolak */
            }



            .response-btn.accepted {
                background-color: #3897f0;
                /* Warna biru untuk menyetujui */
            }

            .response-btn.declined {
                background-color: #e74c3c;
                /* Warna merah untuk menolak */
            }


            .action-btn:hover {
                opacity: 0.8;
                /* Efek hover */
            }
        </style>
    </head>

    <body>
        <div class="notification-container ">
            <h2>Notifikasi Penawaran Program</h2>

            <form action="{{ route('notification.penawaran') }}" method="GET" class="mb-4">
                <div class="input-group">
                    <input type="text" name="search" value="{{ request('search') }}" class="form-control" placeholder="Cari..." aria-label="Cari...">
                    <div class="input-group-append">
                        <button type="submit" class="btn btn-primary">Cari</button>
                    </div>
                </div>
            </form>

            <div class="notification-list">
                @foreach ($notifications as $notification)
                    <div class="notification-item">
                        <a href="{{ route('profile.detail', ['id' => $notification->user->id]) }}"
                            style="display: flex; align-items: center; text-decoration: none; color: inherit; flex-grow: 1;">
                            <img src="{{ asset('storage/' . $notification->user->foto) }}"
                                alt="{{ $notification->user->full_name }}" class="user-avatar">
                            <div class="notification-content">
                                <p>
                                    <strong>{{ $notification->user->full_name }}</strong> ingin mengikuti program
                                    <strong>{{ $notification->project->judul_project ?? 'Proyek tidak ditemukan' }}</strong>.
                                </p>
                            </div>
                        </a>
                        <div class="action-buttons">
                            @if ($notification->status == 'pending')
                                <button class="action-btn accept-btn"
                                    onclick="handleResponse(this, true, {{ $notification->id }});">Terima</button>
                                <button class="action-btn decline-btn"
                                    onclick="handleResponse(this, false, {{ $notification->id }});">Tolak</button>
                            @else
                                <button
                                    class="action-btn response-btn {{ $notification->status == 'accepted' ? 'accepted' : 'declined' }}"
                                    style="display:inline-block;" disabled>
                                    {{ $notification->status == 'accepted' ? 'Menyetujui Kerjasama' : 'Menolak Kerjasama' }}
                                </button>
                            @endif
                        </div>
                    </div>
                @endforeach
            </div>

            <!-- Tautan Pagination -->
            <div class="mt-4">
                {{ $notifications->links() }}
            </div>
        </div>



        <script>
            function handleResponse(button, isAccepted, notificationId) {
                console.log('Button clicked:', isAccepted, notificationId); // Log untuk debug

                const url = isAccepted ? '{{ route('follow.penawaran.accept', ':id') }}'.replace(':id', notificationId) :
                    '{{ route('follow.penawaran.decline', ':id') }}'.replace(':id', notificationId);

                fetch(url, {
                    method: 'POST',
                    headers: {
                        'X-CSRF-TOKEN': '{{ csrf_token() }}',
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({})
                }).then(response => {
                    if (response.ok) {
                        // Jika permintaan berhasil, reload halaman
                        window.location.href = window.location.href; // Reload halaman
                    } else {
                        // Jika permintaan gagal, tampilkan pesan kesalahan
                        return response.text().then(text => {
                            console.error('Error response from server:', text);
                        });
                    }
                }).catch(error => {
                    console.error('Fetch error:', error);
                });
            }
        </script>
    @endsection
