<!DOCTYPE html>
<html lang="nl">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BAK Verzoeken Valideren</title>
    <%- include('../partials/_favicon') %>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="/styles/main.css">
</head>

<body class="d-flex flex-column min-vh-100">

    <main class="flex-shrink-0">
        <%- include('../partials/_navbar') %>


        <div class="container mt-5">
            <div class="header-content">
                <h1>Ontvangen BAK Verzoeken</h1>
            </div>


            <% if (bakRequests.length> 0) { %>
            <div class="list-group">
                <% bakRequests.forEach(function(request) { %>
                <div class="list-group-item list-group-item-action d-flex justify-content-between align-items-center">
                    <div>
                        <h5 class="mb-1">
                            <%= request.reasonBak %>
                        </h5>
                        <small>Van: <%= request.Requester.name %></small>
                    </div>
                    <div>
                        <button class="btn btn-success btn-sm"
                            onclick="handleResponse('<%= request.id %>', 'approved')">Accepteren</button>
                        <button class="btn btn-danger btn-sm"
                            onclick="handleResponse('<%= request.id %>', 'declined')">Weigeren</button>
                    </div>
                </div>
                <% }); %>
            </div>
            <% } else { %>
            <p>Geen Ontvangen BAK verzoeken.</p>
            <% } %>
        </div>

    </main>

    <%- include('../partials/_footer') %>

    <script>
        function handleResponse(requestId, status) {
            fetch('/bak/validate/' + requestId + '/' + status, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-Token': '<%= csrfToken %>'
                },
                body: JSON.stringify({ status: status })
            }).then(response => {
                if (response.ok) {
                    window.location.reload();
                } else {
                    alert('Het updaten van het BAK-verzoek is mislukt.');
                }
            }).catch(error => {
                console.error('Error:', error);
            })
        }
    </script>


    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>