$(function () {
    loadApprovals();
});

function loadApprovals() {
    $.get('api/workflow.php?action=list', function (res) {

        let html = '';

        res.data.forEach(w => {
            html += `
            <tr>
                <td>${w.module}</td>
                <td>${w.status}</td>
                <td>
                    <button class="btn btn-success btn-sm" onclick="approve(${w.step_id})">Approve</button>
                    <button class="btn btn-danger btn-sm" onclick="reject(${w.step_id})">Reject</button>
                </td>
            </tr>`;
        });

        $('#approvalTable').html(html);
    });
}

function approve(id) {
    $.post('api/workflow.php?action=approve', { step_id: id }, loadApprovals);
}

function reject(id) {
    $.post('api/workflow.php?action=reject', { step_id: id }, loadApprovals);
}