<?php
/**
 * Super Admin — Edit Template (name, type, image, dimensions)
 * Field positions are managed on the show page.
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$template = $template ?? [];
$courses = $courses ?? [];
?>
<div class="admin-section">
    <div style="margin-bottom:20px;">
        <a href="<?= $bp ?>/super-admin/certificate-templates/<?= (int) ($template['id'] ?? 0) ?>" style="color:#6c3fe8;text-decoration:none;font-size:0.85rem;">← Back to Template</a>
    </div>

    <form method="POST" action="<?= $bp ?>/super-admin/certificate-templates/<?= (int) ($template['id'] ?? 0) ?>" 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 20px;font-size:1.1rem;color:#2c1e4a;">Edit Template</h2>

            <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($template['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>" 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</label>
                    <select name="type" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                        <?php foreach (['certificate','marksheet','affiliation','authorization','renewal','franchise_agreement','id_card','admit_card'] as $t): ?>
                            <option value="<?= $t ?>" <?= ($template['type'] ?? '') === $t ? 'selected' : '' ?>><?= ucfirst(str_replace('_',' ',$t)) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Link to Course</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'] ?>" <?= ($template['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;">Replace Background Image (optional)</label>
                <?php if (!empty($template['background_image'])): ?>
                    <img src="<?= $bp ?>/<?= htmlspecialchars($template['background_image'], ENT_QUOTES, 'UTF-8') ?>" alt="Current" style="max-width:200px;max-height:120px;border-radius:6px;border:1px solid #ddd;margin-bottom:6px;display:block;">
                <?php endif; ?>
                <input type="file" name="background_image" accept="image/jpeg,image/png,image/gif,image/webp" style="width:100%;font-size:0.85rem;padding:8px;border:1px solid #ddd;border-radius:6px;">
                <small style="color:#999;font-size:0.76rem;">Leave blank to keep current image.</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;">Width (pt)</label>
                    <input type="number" name="template_width" value="<?= (int) ($template['template_width'] ?? 842) ?>" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;">
                </div>
                <div>
                    <label style="display:block;font-size:0.85rem;font-weight:600;color:#2c1e4a;margin-bottom:6px;">Height (pt)</label>
                    <input type="number" name="template_height" value="<?= (int) ($template['template_height'] ?? 595) ?>" 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</label>
                <textarea name="description" rows="2" style="width:100%;padding:10px 12px;border:1px solid #ddd;border-radius:6px;font-size:0.88rem;resize:vertical;"><?= htmlspecialchars($template['description'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
            </div>

            <div>
                <label style="display:flex;align-items:center;gap:8px;cursor:pointer;">
                    <input type="checkbox" name="is_active" value="1" <?= !empty($template['is_active']) ? 'checked' : '' ?> style="width:18px;height:18px;accent-color:#6c3fe8;">
                    <span style="font-size:0.88rem;font-weight:600;color:#2c1e4a;">Active</span>
                </label>
            </div>
        </div>

        <div style="display:flex;gap:12px;justify-content:flex-end;">
            <a href="<?= $bp ?>/super-admin/certificate-templates/<?= (int) ($template['id'] ?? 0) ?>" 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;">Save Changes</button>
        </div>
    </form>
</div>
