<div class="sa-card mb-8">
    <div class="flex justify-between items-center mb-8">
        <div>
            <h3 class="text-2xl font-bold">Edit SaaS Plan</h3>
            <p class="text-muted">Update pricing and resource limits for this subscription tier.</p>
        </div>
    </div>

    <form action="<?= APP_URL ?>/superadmin/plans/<?= $plan['id'] ?>/edit" method="POST">
        <?= Security::csrfField() ?>
        
        <div class="grid-2 gap-8 mb-8" style="grid-template-columns: 2fr 1fr;">
            <div>
                <div class="text-xs text-muted font-bold tracking-widest uppercase mb-4 opacity-50">General Info</div>
                <div class="form-group">
                    <label class="form-label text-sm">Plan Name <span class="required">*</span></label>
                    <input type="text" name="name" class="form-control" required value="<?= e($plan['name']) ?>" placeholder="e.g. Professional Pack">
                </div>

                <div class="text-xs text-muted font-bold tracking-widest uppercase mt-8 mb-4 opacity-50">Pricing Configuration (GHS)</div>
                <div class="form-row">
                    <div class="form-group">
                        <label class="form-label text-sm">Monthly Price <span class="required">*</span></label>
                        <div class="input-group">
                            <span class="input-group-text">GH₵</span>
                            <input type="number" name="price_monthly" step="0.01" class="form-control" value="<?= number_format($plan['price_monthly'], 2) ?>" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="form-label text-sm">Yearly Price <span class="required">*</span></label>
                        <div class="input-group">
                            <span class="input-group-text">GH₵</span>
                            <input type="number" name="price_yearly" step="0.01" class="form-control" value="<?= number_format($plan['price_yearly'], 2) ?>" required>
                        </div>
                    </div>
                </div>

                <div class="text-xs text-muted font-bold tracking-widest uppercase mt-8 mb-4 opacity-50">Resource Limits (-1 for unlimited)</div>
                <div class="grid-2 gap-4">
                    <div class="form-group">
                        <label class="form-label text-xs">Max Products</label>
                        <input type="number" name="max_products" class="form-control" value="<?= $plan['max_products'] ?>">
                    </div>
                    <div class="form-group">
                        <label class="form-label text-xs">Max Users</label>
                        <input type="number" name="max_users" class="form-control" value="<?= $plan['max_users'] ?>">
                    </div>
                    <div class="form-group">
                        <label class="form-label text-xs">Max Locations</label>
                        <input type="number" name="max_locations" class="form-control" value="<?= $plan['max_locations'] ?>">
                    </div>
                    <div class="form-group">
                        <label class="form-label text-xs">Storage (MB)</label>
                        <input type="number" name="storage_mb" class="form-control" value="<?= $plan['storage_mb'] ?>">
                    </div>
                </div>
            </div>

            <div>
                 <div class="text-xs text-muted font-bold tracking-widest uppercase mb-4 opacity-50">Feature Gating</div>
                 <div class="sa-card bg-raised">
                    <?php 
                    $features = json_decode($plan['features'], true) ?? [];
                    ?>
                    <div class="form-group flex items-center gap-3 mb-4">
                        <label class="form-switch">
                            <input type="checkbox" name="features[reports_advanced]" value="1" class="switch-input" <?= ($features['reports_advanced'] ?? false) ? 'checked' : '' ?>>
                            <div class="switch-track"></div>
                            <span class="switch-label text-xs">Advanced Reporting</span>
                        </label>
                    </div>
                    <div class="form-group flex items-center gap-3 mb-4">
                        <label class="form-switch">
                            <input type="checkbox" name="features[batch_tracking]" value="1" class="switch-input" <?= ($features['batch_tracking'] ?? false) ? 'checked' : '' ?>>
                            <div class="switch-track"></div>
                            <span class="switch-label text-xs">Batch & Expiry Tracking</span>
                        </label>
                    </div>
                    <div class="form-group flex items-center gap-3 mb-4">
                        <label class="form-switch">
                            <input type="checkbox" name="features[invoice]" value="1" class="switch-input" <?= ($features['invoice'] ?? false) ? 'checked' : '' ?>>
                            <div class="switch-track"></div>
                            <span class="switch-label text-xs">A4 Invoice Customization</span>
                        </label>
                    </div>
                    <div class="form-group flex items-center gap-3 mb-4">
                        <label class="form-switch">
                            <input type="checkbox" name="features[api_access]" value="1" class="switch-input" <?= ($features['api_access'] ?? false) ? 'checked' : '' ?>>
                            <div class="switch-track"></div>
                            <span class="switch-label text-xs">Developer API Access</span>
                        </label>
                    </div>
                    <div class="form-group flex items-center gap-3">
                        <label class="form-switch">
                            <input type="checkbox" name="features[multi_warehouse]" value="1" class="switch-input" <?= ($features['multi_warehouse'] ?? false) ? 'checked' : '' ?>>
                            <div class="switch-track"></div>
                            <span class="switch-label text-xs">Multi-Warehouse Management</span>
                        </label>
                    </div>
                 </div>

                 <p class="text-xs text-muted mt-6 p-4 border border-dashed rounded" style="border-color: var(--sa-border);">
                    Plan changes will take effect immediately for new subscriptions. Existing subscriptions will continue on their current pricing until their next billing cycle.
                 </p>
            </div>
        </div>

        <div class="flex justify-end gap-3 mt-8 pt-8" style="border-top: 1px solid var(--sa-border);">
            <a href="<?= APP_URL ?>/superadmin/plans" class="btn btn-outline" style="padding: 10px 24px;">Cancel</a>
            <button type="submit" class="btn btn-sa-primary py-2 px-8">Update Plan</button>
        </div>
    </form>
</div>
