<?php
declare(strict_types=1);
require_once 'config/database.php';
require_once 'app/helpers/auth.php';
require_once 'app/helpers/rbac.php';
requireLogin();
if (!can('workflow.approve')) { http_response_code(403); die('Access denied'); }
$page_js = 'workflow_approvals.js';
include 'views/layouts/header.php';
include 'views/layouts/sidebar.php';
$userName = $_SESSION['user_name'] ?? 'User';
?>
<main class="main-content">
<?php include 'views/layouts/topbar.php'; ?>
<div class="container-fluid p-4">
<div id="alertBox" class="alert d-none"></div>
<div class="card shadow-sm border-0 mb-4"><div class="card-body p-4 p-lg-5"><div class="row g-4 align-items-center"><div class="col-lg-8"><span class="badge bg-primary bg-opacity-10 text-primary mb-3 px-3 py-2">Operations</span><h2 class="mb-2">Approvals</h2><p class="mb-0 text-muted">Review and act on pending approval requests.</p></div><div class="col-lg-4 text-lg-end d-flex gap-2 justify-content-lg-end">
<button type="button" class="btn btn-outline-primary" id="btnPending">Pending</button>
<button type="button" class="btn btn-outline-success" id="btnApproved">Approved</button>
<button type="button" class="btn btn-outline-danger" id="btnRejected">Rejected</button>
<button type="button" class="btn btn-outline-info" id="btnReturned">Returned</button>
</div></div></div></div>
<div class="card shadow-sm border-0"><div class="card-body">
<div class="table-responsive"><table class="table align-middle mb-0"><thead class="table-light"><tr><th>Module</th><th>Step</th><th>Requester</th><th>Created</th><th class="text-end">Actions</th></tr></thead><tbody id="approvalTableBody"><tr><td colspan="5" class="text-center text-muted py-4">Loading...</td></tr></tbody></table></div></div></div>
</div>
</main>

<!-- Action Modal -->
<div class="modal fade" id="actionModal" tabindex="-1"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><h5 class="modal-title" id="actionModalTitle">Approval Action</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><input type="hidden" id="actionStepId" value="0">
<p id="actionInfo" class="mb-3"></p>
<div class="mb-3"><label class="form-label">Comment</label><textarea id="actionComment" class="form-control" rows="3" placeholder="Add a comment..."></textarea></div></div>
<div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-info text-white" id="btnReturnAction" style="display:none">Return for Correction</button>
<button type="button" class="btn btn-danger" id="btnRejectAction" style="display:none">Reject</button>
<button type="button" class="btn btn-success" id="btnApproveAction" style="display:none">Approve</button></div></div></div></div>

<script>
window.CSRF_TOKEN = <?= json_encode(generateCsrfToken(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?>;
</script>
<?php include 'views/layouts/footer.php'; ?>
