<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/iscms_layout.php';

$conn = iscms_init();
$pageTitle = 'Workflow templates';
$activeNav = 'applications';

$sql = "SELECT id, template_name, application_type, sla_days, is_active, version, updated_at, steps
        FROM workflow_templates
        ORDER BY application_type ASC, template_name ASC
        LIMIT 200";
$result = $conn->query($sql);
$rows = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$dbError = $result ? null : $conn->error;

iscms_render_head($pageTitle);
iscms_render_sidebar($activeNav);
iscms_render_topbar($pageTitle);
?>
    <div class="page-header">
        <h1>Workflow templates</h1>
        <p>Reusable step definitions and SLA hints per application type.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Quick links</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="applications.php"><i class="ri-file-list-3-line"></i> Applications</a>
            <a class="qa-btn" href="queue_dashboard.php"><i class="ri-stack-line"></i> Queue</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-flow-chart"></i> Templates</h2>
            <span class="text-muted" style="font-size:.75rem"><?= count($rows) ?> shown</span>
        </div>
        <div class="table-responsive">
            <?php if ($dbError): ?>
                <div class="alert alert-danger m-3" style="font-size:.85rem">Unable to load data: <?= iscms_h($dbError) ?></div>
            <?php else: ?>
            <table class="ssbs-table">
                <thead>
                    <tr>
                        <th>Template</th>
                        <th>Application type</th>
                        <th>Version</th>
                        <th>SLA (days)</th>
                        <th>Steps (preview)</th>
                        <th>Active</th>
                        <th>Updated</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="7"><i class="ri-inbox-line me-2"></i>No workflow templates</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $stepsRaw = (string) ($r['steps'] ?? '');
                            $stepsDisp = $stepsRaw !== '' ? (strlen($stepsRaw) > 100 ? substr($stepsRaw, 0, 97) . '...' : $stepsRaw) : '';
                        ?>
                        <tr>
                            <td style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['template_name']) ?></td>
                            <td class="iscms-mono"><small><?= iscms_h(str_replace('_', ' ', (string) $r['application_type'])) ?></small></td>
                            <td class="iscms-mono"><?= iscms_h((string) ($r['version'] ?? '')) ?></td>
                            <td><?= iscms_h((string) ($r['sla_days'] ?? '')) ?></td>
                            <td><small class="iscms-mono text-muted"><?= $stepsDisp !== '' ? iscms_h($stepsDisp) : '<span class="text-muted">-</span>' ?></small></td>
                            <td><?= !empty($r['is_active']) ? '<span class="badge bg-success-subtle text-success" style="font-size:.65rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.65rem">No</span>' ?></td>
                            <td><small class="text-muted"><?= !empty($r['updated_at']) ? iscms_h(substr((string) $r['updated_at'], 0, 10)) : '-' ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
