<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><?= $template ? 'Edit PM Template' : 'New PM Template' ?></h5>
    <div class="d-flex gap-2">
        <?php if (!empty($template['template_id'])): ?>
            <a href="<?= url('/PmTemplates/show/' . (int) $template['template_id']) ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-eye me-1"></i>View · Tasks &amp; Schedules</a>
        <?php endif; ?>
        <a href="<?= url('/PmTemplates') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
    </div>
</div>

<div class="rs-card p-3">
<form method="post" action="<?= url('/PmTemplates/save') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="template_id" value="<?= (int) ($template['template_id'] ?? 0) ?>">
    <div class="row g-2">
        <div class="col-md-8"><label class="form-label">Name *</label><input type="text" class="form-control form-control-sm" name="name" required value="<?= e($template['name'] ?? '') ?>"></div>
        <div class="col-md-4"><label class="form-label">Code</label><input type="text" class="form-control form-control-sm" name="code" value="<?= e($template['code'] ?? '') ?>"></div>
        <div class="col-md-4"><label class="form-label">Applies to Asset Type</label>
            <select name="asset_type_id" class="form-select form-select-sm"><option value="">— any —</option>
                <?php foreach ($assetTypes as $t): ?><option value="<?= (int) $t['asset_type_id'] ?>" <?= (int) ($template['asset_type_id'] ?? 0) === (int) $t['asset_type_id'] ? 'selected' : '' ?>><?= e($t['name']) ?></option><?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2"><label class="form-label">Interval (days)</label><input type="number" class="form-control form-control-sm" name="interval_days" value="<?= (int) ($template['interval_days'] ?? 30) ?>" min="1"></div>
        <div class="col-md-2"><label class="form-label">Lead (days)</label><input type="number" class="form-control form-control-sm" name="lead_days" value="<?= (int) ($template['lead_days'] ?? 3) ?>" min="0"></div>
        <div class="col-md-2"><label class="form-label">Priority</label>
            <select name="priority" class="form-select form-select-sm">
                <?php foreach (['low','medium','high','urgent'] as $p): ?><option <?= ($template['priority'] ?? 'medium') === $p ? 'selected' : '' ?>><?= $p ?></option><?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2 d-flex align-items-end">
            <div class="form-check">
                <input type="checkbox" name="is_active" value="1" class="form-check-input" id="pmActive" <?= ($template['is_active'] ?? 1) ? 'checked' : '' ?>>
                <label for="pmActive" class="form-check-label">Active</label>
            </div>
        </div>
        <div class="col-12"><label class="form-label">Description</label><textarea class="form-control form-control-sm" name="description" rows="2"><?= e($template['description'] ?? '') ?></textarea></div>
    </div>
    <div class="mt-3 d-flex gap-2">
        <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-save me-1"></i>Save</button>
    </div>
</form>
<?php if (!empty($template['template_id'])): ?>
    <form method="post" action="<?= url('/PmTemplates/delete/' . (int) $template['template_id']) ?>"
          class="mt-3 rs-confirm-submit"
          data-confirm-title="Delete template?"
          data-confirm-body="Soft-delete <strong><?= e($template['name']) ?></strong>? Existing work orders generated from this template are not affected."
          data-confirm-ok="Delete">
        <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
        <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash me-1"></i>Delete template</button>
    </form>
<?php endif; ?>
</div>
