<?php
/**
 * Franchise Plan Create/Edit Form — Gmind Institute ERP
 * Uses $plan for edit mode (null for create)
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$isEdit = !empty($plan);
$pageTitle = $isEdit ? 'Edit Franchise Plan' : 'Create Franchise Plan';
$formAction = $isEdit ? $bp . '/admin/franchise-plans/update/' . (int)$plan['id'] : $bp . '/admin/franchise-plans/store';
$existingFeatures = $isEdit && !empty($plan['features']) ? $plan['features'] : [];
?>
<div class="admin-page-header" style="margin-bottom:24px;">
    <h1 style="margin:0;font-size:22px;font-weight:700;"><?= htmlspecialchars($pageTitle, ENT_QUOTES, 'UTF-8') ?></h1>
</div>

<div class="admin-form-container" style="max-width:720px;">
    <form method="POST" action="<?= htmlspecialchars($formAction, ENT_QUOTES, 'UTF-8') ?>" class="admin-form">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8') ?>">

        <div class="admin-form-group" style="margin-bottom:16px;">
            <label for="name" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Plan Name <span style="color:#c62828;">*</span></label>
            <input type="text" id="name" name="name" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" value="<?= htmlspecialchars($plan['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>" required>
        </div>

        <div class="admin-form-group" style="margin-bottom:16px;">
            <label for="slug" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Slug <span style="color:#c62828;">*</span></label>
            <input type="text" id="slug" name="slug" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" value="<?= htmlspecialchars($plan['slug'] ?? '', ENT_QUOTES, 'UTF-8') ?>" required placeholder="e.g. basic-plan">
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
            <div class="admin-form-group" style="margin-bottom:16px;">
                <label for="price" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Price (₹) <span style="color:#c62828;">*</span></label>
                <input type="number" id="price" name="price" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" value="<?= htmlspecialchars($plan['price'] ?? '', ENT_QUOTES, 'UTF-8') ?>" step="0.01" min="0" required>
            </div>

            <div class="admin-form-group" style="margin-bottom:16px;">
                <label for="duration_months" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Duration (Months) <span style="color:#c62828;">*</span></label>
                <input type="number" id="duration_months" name="duration_months" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" value="<?= htmlspecialchars($plan['duration_months'] ?? '', ENT_QUOTES, 'UTF-8') ?>" min="1" required>
            </div>
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
            <div class="admin-form-group" style="margin-bottom:16px;">
                <label for="max_students" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Max Students <span style="color:#c62828;">*</span></label>
                <input type="number" id="max_students" name="max_students" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" value="<?= htmlspecialchars($plan['max_students'] ?? '', ENT_QUOTES, 'UTF-8') ?>" min="0" required>
            </div>

            <div class="admin-form-group" style="margin-bottom:16px;">
                <label for="max_exams_per_month" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Max Exams Per Month <span style="color:#c62828;">*</span></label>
                <input type="number" id="max_exams_per_month" name="max_exams_per_month" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" value="<?= htmlspecialchars($plan['max_exams_per_month'] ?? '', ENT_QUOTES, 'UTF-8') ?>" min="0" required>
            </div>
        </div>

        <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
            <div class="admin-form-group" style="margin-bottom:16px;">
                <label for="sort_order" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Sort Order</label>
                <input type="number" id="sort_order" name="sort_order" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" value="<?= htmlspecialchars($plan['sort_order'] ?? 0, ENT_QUOTES, 'UTF-8') ?>" min="0">
            </div>

            <div class="admin-form-group" style="margin-bottom:16px;display:flex;align-items:flex-end;padding-bottom:4px;">
                <label style="display:flex;align-items:center;gap:8px;cursor:pointer;font-weight:600;">
                    <input type="checkbox" name="is_active" value="1" <?= !empty($plan['is_active']) || !$isEdit ? 'checked' : '' ?> style="width:18px;height:18px;">
                    Is Active
                </label>
            </div>
        </div>

        <div class="admin-form-group" style="margin-bottom:16px;">
            <label for="description" class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Description</label>
            <textarea id="description" name="description" class="admin-form-control" style="width:100%;padding:8px 12px;border:1px solid #ccc;border-radius:6px;min-height:100px;" rows="4"><?= htmlspecialchars($plan['description'] ?? '', ENT_QUOTES, 'UTF-8') ?></textarea>
        </div>

        <!-- Dynamic Features -->
        <div class="admin-form-group" style="margin-bottom:16px;">
            <label class="admin-form-label" style="display:block;margin-bottom:4px;font-weight:600;">Features</label>
            <div id="features-container">
                <?php if (!empty($existingFeatures)): ?>
                    <?php foreach ($existingFeatures as $i => $feature): ?>
                    <div class="admin-feature-row" style="display:flex;gap:8px;margin-bottom:8px;align-items:center;">
                        <input type="text" name="features[<?= $i ?>][name]" class="admin-form-control" style="flex:1;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" placeholder="Feature name" value="<?= htmlspecialchars($feature['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                        <input type="text" name="features[<?= $i ?>][value]" class="admin-form-control" style="flex:1;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" placeholder="Feature value" value="<?= htmlspecialchars($feature['value'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                        <button type="button" class="admin-btn admin-btn-sm admin-btn-danger remove-feature-btn" onclick="this.closest('.admin-feature-row').remove();" title="Remove">✕</button>
                    </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
            <button type="button" id="add-feature-btn" class="admin-btn admin-btn-sm admin-btn-secondary" style="margin-top:4px;">+ Add Feature</button>
        </div>

        <div style="display:flex;gap:12px;margin-top:24px;">
            <button type="submit" class="admin-btn admin-btn-primary"><?= $isEdit ? 'Update Plan' : 'Create Plan' ?></button>
            <a href="<?= $bp ?>/admin/franchise-plans" class="admin-btn admin-btn-secondary">Cancel</a>
        </div>
    </form>
</div>

<script>
(function() {
    const container = document.getElementById('features-container');
    const addBtn = document.getElementById('add-feature-btn');
    let featureIndex = <?= !empty($existingFeatures) ? count($existingFeatures) : 0 ?>;

    addBtn.addEventListener('click', function() {
        const row = document.createElement('div');
        row.className = 'admin-feature-row';
        row.style.cssText = 'display:flex;gap:8px;margin-bottom:8px;align-items:center;';
        row.innerHTML = '<input type="text" name="features[' + featureIndex + '][name]" class="admin-form-control" style="flex:1;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" placeholder="Feature name">'
            + '<input type="text" name="features[' + featureIndex + '][value]" class="admin-form-control" style="flex:1;padding:8px 12px;border:1px solid #ccc;border-radius:6px;" placeholder="Feature value">'
            + '<button type="button" class="admin-btn admin-btn-sm admin-btn-danger remove-feature-btn" onclick="this.closest(\'.admin-feature-row\').remove();" title="Remove">✕</button>';
        container.appendChild(row);
        featureIndex++;
    });
})();
</script>
