<?php $isEdit = !empty($record); ?>
<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' : 'Log' ?> training completion</h5>
    <a href="<?= url('/Training') ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<p class="text-muted small">
    <em>Expiry auto-computes</em> as completion + course validity. Change the course and expiry recomputes on save.
</p>

<div class="rs-card p-3">
<form method="post" action="<?= url('/Training/saveRecord') ?>">
    <input type="hidden" name="_csrf" value="<?= e($csrf) ?>">
    <input type="hidden" name="record_id" value="<?= (int) ($record['record_id'] ?? 0) ?>">
    <div class="row g-2">
        <div class="col-md-6"><label class="form-label">Course *</label>
            <select name="course_id" class="form-select form-select-sm" required>
                <option value="">— pick course —</option>
                <?php foreach ($courses as $c): ?>
                    <option value="<?= (int) $c['course_id'] ?>" <?= (int) ($record['course_id'] ?? 0) === (int) $c['course_id'] ? 'selected' : '' ?>><?= e($c['code'] ?? '') ?> · <?= e($c['name']) ?> (<?= (int) $c['validity_months'] ?>mo)</option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-3"><label class="form-label">Completed on *</label>
            <input type="date" class="form-control form-control-sm" name="completed_at" required value="<?= e($record['completed_at'] ?? '') ?>">
        </div>
        <div class="col-md-3"><label class="form-label">Certificate #</label>
            <input type="text" class="form-control form-control-sm" name="certificate_number" value="<?= e($record['certificate_number'] ?? '') ?>">
        </div>

        <div class="col-md-6"><label class="form-label">Employee</label>
            <select name="user_id" class="form-select form-select-sm">
                <option value="">— none / external —</option>
                <?php foreach ($users as $u): ?>
                    <option value="<?= (int) $u['user_id'] ?>" <?= (int) ($record['user_id'] ?? 0) === (int) $u['user_id'] ? 'selected' : '' ?>><?= e($u['username']) ?><?php if (!empty($u['f_name']) || !empty($u['l_name'])): ?> · <?= e(trim(($u['f_name'] ?? '') . ' ' . ($u['l_name'] ?? ''))) ?><?php endif; ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-6"><label class="form-label">External name (contractor/visitor)</label>
            <input type="text" class="form-control form-control-sm" name="external_name" value="<?= e($record['external_name'] ?? '') ?>" placeholder="Use when the person isn't a system user">
        </div>

        <div class="col-12"><label class="form-label">Notes</label>
            <textarea class="form-control form-control-sm" name="notes" rows="2"><?= e($record['notes'] ?? '') ?></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/deleteRecord/' . (int) $record['record_id']) ?>"
          class="mt-3 rs-confirm-submit"
          data-confirm-title="Delete record?"
          data-confirm-body="Soft-delete this training record? Attached certificates remain in storage."
          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 record</button>
    </form>
<?php endif; ?>
