<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-calendar-check me-2 text-primary"></i>PM Templates</h5>
    <a class="btn btn-sm btn-primary" href="<?= url('/PmTemplates/create') ?>"><i class="fas fa-plus me-1"></i>New Template</a>
</div>

<p class="text-muted small">Preventive Maintenance templates auto-generate work orders on schedule. Run <code>bin/console pm:generate</code> daily via cron.</p>

<?php if (!empty($flash)): ?><div class="alert alert-info py-2 small"><?= e($flash) ?></div><?php endif; ?>

<div class="rs-card p-3">
<?php if (empty($templates)): ?>
    <p class="text-muted small mb-0">No PM templates yet.</p>
<?php else: ?>
    <table class="table table-sm table-hover mb-0">
        <thead><tr><th>Name</th><th>Asset Type</th><th>Interval</th><th>Lead</th><th>Priority</th><th class="text-end">Tasks</th><th class="text-end">Schedules</th><th>Status</th><th></th></tr></thead>
        <tbody>
            <?php foreach ($templates as $t): ?>
                <tr>
                    <td><a href="<?= url('/PmTemplates/show/' . (int) $t['template_id']) ?>" class="fw-semibold text-decoration-none"><?= e($t['name']) ?></a><?php if ($t['code']): ?> <code class="small"><?= e($t['code']) ?></code><?php endif; ?></td>
                    <td class="small text-muted"><?= e($t['asset_type_name'] ?? 'any') ?></td>
                    <td class="small"><?= (int) $t['interval_days'] ?> days</td>
                    <td class="small"><?= (int) $t['lead_days'] ?> days</td>
                    <td><span class="badge bg-secondary text-uppercase"><?= e($t['priority']) ?></span></td>
                    <td class="text-end"><?= (int) $t['task_count'] ?></td>
                    <td class="text-end"><?= (int) $t['active_schedules'] ?></td>
                    <td><span class="badge <?= $t['is_active'] ? 'bg-success' : 'bg-secondary' ?> text-uppercase"><?= $t['is_active'] ? 'active' : 'inactive' ?></span></td>
                    <td class="text-end"><a href="<?= url('/PmTemplates/edit/' . (int) $t['template_id']) ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-pen"></i></a></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
<?php endif; ?>
</div>
