<?php
require_once 'config/database.php';
require_once 'app/helpers/auth.php';
requireLogin();
$page_js = 'workflow_setup.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">Settings</span><h2 class="mb-2">Workflow Templates</h2><p class="mb-0 text-muted">Configure multi-step approval workflows for each module.</p></div><div class="col-lg-4 text-lg-end"><button type="button" class="btn btn-primary" id="btnNewTemplate"><i class="bi bi-plus-lg"></i> New Template</button></div></div></div></div>
<div class="card shadow-sm border-0 mb-4"><div class="card-body">
<div class="table-responsive"><table class="table align-middle mb-0"><thead class="table-light"><tr><th>Name</th><th>Module</th><th>Steps</th><th>Active</th><th class="text-end">Actions</th></tr></thead><tbody id="templateTableBody"><tr><td colspan="5" class="text-center text-muted py-4">Loading...</td></tr></tbody></table></div></div></div>

<!-- Module Manager -->
<div class="card shadow-sm border-0"><div class="card-body p-4 p-lg-5"><div class="row g-4 align-items-center mb-4"><div class="col-lg-8"><span class="badge bg-success bg-opacity-10 text-success mb-3 px-3 py-2"><i class="bi bi-puzzle me-1"></i>Modules</span><h2 class="mb-2">Workflow Modules</h2><p class="mb-0 text-muted">Register application modules so the workflow system knows how to update record statuses automatically.</p></div><div class="col-lg-4 text-lg-end"><button type="button" class="btn btn-success" id="btnNewModule"><i class="bi bi-plus-lg"></i> Register Module</button></div></div>
<div class="table-responsive"><table class="table align-middle mb-0"><thead class="table-light"><tr><th>Display Name</th><th>Module ID</th><th>Table</th><th>Creator Column</th><th>Status Column</th><th>Active</th><th class="text-end">Actions</th></tr></thead><tbody id="moduleTableBody"><tr><td colspan="7" class="text-center text-muted py-4">Loading...</td></tr></tbody></table></div></div></div>
</div>
</main>

<!-- Template Modal -->
<div class="modal fade" id="templateModal" tabindex="-1"><div class="modal-dialog modal-lg modal-dialog-centered"><div class="modal-content"><div class="modal-header"><h5 class="modal-title" id="templateModalTitle">New Template</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><input type="hidden" id="templateId" value="0">
<div class="row g-3"><div class="col-md-6"><label class="form-label">Name <span class="text-danger">*</span></label><input type="text" id="templateName" class="form-control" placeholder="e.g. Vehicle Requisition"></div>
<div class="col-md-6"><label class="form-label">Module <span class="text-danger">*</span></label><select id="templateModule" class="form-select"><option value="">-- Select module --</option></select></div>
<div class="col-12"><label class="form-label">Description</label><textarea id="templateDescription" class="form-control" rows="2"></textarea></div>
<div class="col-12"><div class="form-check"><input type="checkbox" id="templateActive" class="form-check-input" checked><label class="form-check-label" for="templateActive">Active</label></div></div>
<div class="col-12"><hr><h6>Approval Steps</h6><div id="stepsContainer"></div><button type="button" class="btn btn-sm btn-outline-primary mt-2" id="btnAddStep"><i class="bi bi-plus"></i> Add Step</button></div></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-primary" id="btnSaveTemplate">Save Template</button></div></div></div></div>

<!-- Module Modal -->
<div class="modal fade" id="moduleModal" tabindex="-1"><div class="modal-dialog modal-dialog-centered"><div class="modal-content"><div class="modal-header"><h5 class="modal-title" id="moduleModalTitle">Register Module</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><input type="hidden" id="moduleId" value="0">
<div class="row g-3"><div class="col-md-6"><label class="form-label">Module ID <span class="text-danger">*</span></label><input type="text" id="moduleKey" class="form-control" placeholder="e.g. purchase_order"><div class="form-text">Unique identifier used in code</div></div>
<div class="col-md-6"><label class="form-label">Display Name <span class="text-danger">*</span></label><input type="text" id="moduleDisplayName" class="form-control" placeholder="e.g. Purchase Order"></div>
<div class="col-md-6"><label class="form-label">Database Table <span class="text-danger">*</span></label><input type="text" id="moduleTable" class="form-control" placeholder="e.g. purchase_orders"></div>
<div class="col-md-6"><label class="form-label">Creator Column</label><input type="text" id="moduleCreatorCol" class="form-control" value="created_by"></div>
<div class="col-md-4"><label class="form-label">Status Column</label><input type="text" id="moduleStatusCol" class="form-control" value="status"></div>
<div class="col-md-4"><label class="form-label">Approved Value</label><input type="text" id="moduleApprovedVal" class="form-control" value="approved"></div>
<div class="col-md-4"><label class="form-label">Rejected Value</label><input type="text" id="moduleRejectedVal" class="form-control" value="rejected"></div>
<div class="col-md-4"><label class="form-label">Returned Value</label><input type="text" id="moduleReturnedVal" class="form-control" value="returned"></div>
<div class="col-12"><div class="form-check"><input type="checkbox" id="moduleActive" class="form-check-input" checked><label class="form-check-label" for="moduleActive">Active</label></div></div>
</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-success" id="btnSaveModule">Save Module</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'; ?>
