<?php
/**
 * Course Pattern Assignment Snippet
 * 
 * Add this to your existing course create/edit form
 * File: /gmind/super-admin/Views/courses/form.php (or wherever your course form is)
 * 
 * This shows a dropdown of LOCKED patterns for both certificate & marksheet
 */

// Get locked patterns via the controller's static method
$lockedPatterns = \SuperAdmin\Controllers\SuperAdminPatternController::getLockedPatterns();

// Current values (if editing)
$certPatternId = $course['cert_pattern_id'] ?? null;
$marksheetPatternId = $course['marksheet_pattern_id'] ?? null;

// Auto-select default if creating new
if (empty($certPatternId)) {
    foreach ($lockedPatterns as $p) {
        if (!empty($p['is_default'])) {
            $certPatternId = $p['id'];
            $marksheetPatternId = $p['id'];
            break;
        }
    }
}
?>

<div class="card mt-3">
    <div class="card-header">
        <h6 class="mb-0"><i class="fas fa-shapes"></i> Certificate & Marksheet Pattern</h6>
    </div>
    <div class="card-body">
        <?php if (empty($lockedPatterns)): ?>
            <div class="alert alert-warning">
                <i class="fas fa-exclamation-triangle"></i>
                No locked patterns available. 
                <a href="/super-admin/patterns" class="alert-link">Create and lock a pattern first</a>.
            </div>
        <?php else: ?>
            <div class="row">
                <div class="col-md-6">
                    <label class="form-label">Certificate Pattern</label>
                    <select name="cert_pattern_id" class="form-select">
                        <option value="">— None —</option>
                        <?php foreach ($lockedPatterns as $p): ?>
                            <option value="<?= $p['id'] ?>" 
                                <?= ($certPatternId == $p['id']) ? 'selected' : '' ?>
                                <?= !empty($p['is_default']) ? 'data-default="1"' : '' ?>>
                                <?= htmlspecialchars($p['name']) ?>
                                <?= !empty($p['is_default']) ? ' ⭐ (Default)' : '' ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <small class="text-muted">Layout for certificate PDF</small>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Marksheet Pattern</label>
                    <select name="marksheet_pattern_id" class="form-select">
                        <option value="">— None —</option>
                        <?php foreach ($lockedPatterns as $p): ?>
                            <option value="<?= $p['id'] ?>" 
                                <?= ($marksheetPatternId == $p['id']) ? 'selected' : '' ?>
                                <?= !empty($p['is_default']) ? 'data-default="1"' : '' ?>>
                                <?= htmlspecialchars($p['name']) ?>
                                <?= !empty($p['is_default']) ? ' ⭐ (Default)' : '' ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                    <small class="text-muted">Layout for marksheet PDF (with subject marks table)</small>
                </div>
            </div>
            <div class="mt-2">
                <a href="/super-admin/patterns" target="_blank" class="btn btn-sm btn-outline-info">
                    <i class="fas fa-external-link-alt"></i> Manage Patterns
                </a>
            </div>
        <?php endif; ?>
    </div>
</div>
