<?php
/**
 * Super Admin — Create Certificate Template
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$courses = $courses ?? [];
$fieldKeys = $fieldKeys ?? [];
?>
<div class="admin-section">
    <div style="margin-bottom:20px;">
        <a href="<?= $bp ?>/super-admin/certificate-templates" style="color:#6c3fe8;text-decoration:none;font-size:0.85rem;">← Back to Templates</a>
    </div>

    <form method="POST" action="<?= $bp ?>/super-admin/certificate-templates" enctype="multipart/form-data" style="max-width:680px;">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">

        <div class="admin-card" style="background:#fff;border-radius:12px;padding:24px;margin-bottom:20px;box-shadow:0 2px 8px rgba(0,0,0,0.06);">
            <h2 style="margin:0 0 6px;font-size:1.1rem;color:#2c1e4a;">New Template</h2>
            <p style="margin:0 0 20px;font-size:0.85rem;color:#666;">Upload a JPG/PNG background image. After creating, you'll define field positions (student name, enrollment no, etc.) on the image.</p>

            <div style="margin-bottom:14px;">
                <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Template Name <span style="color:#e84118;">*</span></label>
                <input type="text" name="name" required value="<?= htmlspecialchars($_POST['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>" placeholder="e.g. ADCA Certificate Template" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
            </div>

            <div style="display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-bottom:14px;">
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Document Type <span style="color:#e84118;">*</span></label>
                    <select name="type" required style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                        <option value="certificate" <?= ($_POST['type'] ?? 'certificate') === 'certificate' ? 'selected' : '' ?>>Certificate</option>
                        <option value="marksheet" <?= ($_POST['type'] ?? '') === 'marksheet' ? 'selected' : '' ?>>Marksheet</option>
                        <option value="affiliation" <?= ($_POST['type'] ?? '') === 'affiliation' ? 'selected' : '' ?>>Affiliation Certificate</option>
                        <option value="authorization" <?= ($_POST['type'] ?? '') === 'authorization' ? 'selected' : '' ?>>Authorization Letter</option>
                        <option value="renewal" <?= ($_POST['type'] ?? '') === 'renewal' ? 'selected' : '' ?>>Renewal Letter</option>
                        <option value="franchise_agreement" <?= ($_POST['type'] ?? '') === 'franchise_agreement' ? 'selected' : '' ?>>Franchise Agreement</option>
                        <option value="id_card" <?= ($_POST['type'] ?? '') === 'id_card' ? 'selected' : '' ?>>ID Card</option>
                        <option value="admit_card" <?= ($_POST['type'] ?? '') === 'admit_card' ? 'selected' : '' ?>>Admit Card</option>
                    </select>
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Link to Course (optional)</label>
                    <select name="course_id" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                        <option value="">— All Courses —</option>
                        <?php foreach ($courses as $c): ?>
                            <option value="<?= (int) $c['id'] ?>" <?= ($_POST['course_id'] ?? '') == $c['id'] ? 'selected' : '' ?>><?= htmlspecialchars($c['name'], ENT_QUOTES, 'UTF-8') ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>

            <div style="margin-bottom:14px;">
                <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Background Image (JPG/PNG) <span style="color:#e84118;">*</span></label>
                <input type="file" name="background_image" accept="image/jpeg,image/png,image/gif,image/webp" required style="width:100%;font-size:0.85rem;padding:8px;border:1px solid #ddd;border-radius:6px;">
                <small style="display:block;margin-top:4px;color:#999;font-size:0.76rem;">Upload the certificate/marksheet JPG template. Max 5MB. Recommended: A4 landscape (3508×2480px) or portrait (2480×3508px).</small>
            </div>

            <div style="display:grid;grid-template-columns:1fr 1fr;gap:14px;margin-bottom:14px;">
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Template Width (points)</label>
                    <input type="number" name="template_width" value="<?= htmlspecialchars($_POST['template_width'] ?? '842', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                    <small style="color:#999;font-size:0.72rem;">A4 = 595×842pt (portrait) or 842×595pt (landscape)</small>
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Template Height (points)</label>
                    <input type="number" name="template_height" value="<?= htmlspecialchars($_POST['template_height'] ?? '595', ENT_QUOTES, 'UTF-8') ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
            </div>

            <div style="margin-bottom:14px;">
                <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Description (optional)</label>
                <textarea name="description" rows="2" placeholder="Admin notes about this template" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;resize:vertical;"><?= htmlspecialchars($_POST['description'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
            </div>

            <div style="margin-bottom:14px;">
                <label style="display:flex;align-items:center;gap:8px;cursor:pointer;">
                    <input type="checkbox" name="is_active" value="1" checked style="width:18px;height:18px;accent-color:#6c3fe8;">
                    <span style="font-size:0.88rem;font-weight:600;color:#2c1e4a;">Active (branches can use this template)</span>
                </label>
            </div>
        </div>

        <div style="display:flex;gap:12px;justify-content:flex-end;">
            <a href="<?= $bp ?>/super-admin/certificate-templates" style="padding:12px 24px;border:1px solid #ddd;border-radius:8px;text-decoration:none;color:#666;font-weight:600;font-size:0.9rem;">Cancel</a>
            <button type="submit" style="padding:12px 28px;background:linear-gradient(135deg,#6c3fe8,#4a2db8);color:#fff;border:none;border-radius:8px;font-weight:600;font-size:0.9rem;cursor:pointer;">Create Template</button>
        </div>
    </form>
</div>
