<?php $isEdit = !empty($course); ?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-graduation-cap me-2 text-primary"></i><?= $isEdit ? 'Edit' : 'New' ?> training course</h5>
    <a href="<?= url('/Training') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<div class="rs-card p-3">
<form method="post" action="<?= url('/Training/saveCourse') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="course_id" value="<?= (int) ($course['course_id'] ?? 0) ?>">
    <div class="row g-2">
        <div class="col-md-3"><label class="form-label">Code</label>
            <input type="text" class="form-control form-control-sm" name="code" value="<?= e($course['code'] ?? '') ?>" placeholder="e.g. WORK-HEIGHT">
        </div>
        <div class="col-md-6"><label class="form-label">Name *</label>
            <input type="text" class="form-control form-control-sm" name="name" required value="<?= e($course['name'] ?? '') ?>">
        </div>
        <div class="col-md-3"><label class="form-label">Validity (months)</label>
            <input type="number" class="form-control form-control-sm" name="validity_months" min="1" value="<?= (int) ($course['validity_months'] ?? 12) ?>">
        </div>
        <div class="col-md-6"><label class="form-label">Issuer</label>
            <input type="text" class="form-control form-control-sm" name="issuer" value="<?= e($course['issuer'] ?? '') ?>" placeholder="External training body / Internal">
        </div>
        <div class="col-md-3 d-flex align-items-end">
            <div class="form-check">
                <input type="checkbox" class="form-check-input" name="is_active" value="1" id="crActive" <?= !$isEdit || $course['is_active'] ? 'checked' : '' ?>>
                <label for="crActive" 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="3"><?= e($course['description'] ?? '') ?></textarea>
        </div>
    </div>
    <div class="mt-3">
        <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-save me-1"></i>Save</button>
    </div>
</form>
</div>

<?php if ($isEdit): ?>
    <form method="post" action="<?= url('/Training/deleteCourse/' . (int) $course['course_id']) ?>"
          class="mt-3 rs-confirm-submit"
          data-confirm-title="Delete course?"
          data-confirm-body="Soft-delete course <strong><?= e($course['name']) ?></strong>? Existing records stay in the history but no new records may be logged against it."
          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 course</button>
    </form>
<?php endif; ?>
