@extends('super-admin.layout')

@section('title', 'Edit plan - '.$plan->name)

@section('content')
<div class="pg-head">
    <div>
        <h1>{{ $plan->name }}</h1>
        <p class="cell-sub mono">{{ $plan->code }}</p>
    </div>
    <a href="{{ route('mexco.super.plans') }}" class="btn-outline">← Back to plans</a>
</div>

<form method="POST" action="{{ route('mexco.super.plans.update', $plan) }}" class="form-stack">
    @csrf @method('PUT')

    <div class="card section-card">
        <h2>Plan details</h2>
        <div class="form-row">
            <label class="form-label" for="name">Name</label>
            <input type="text" name="name" id="name" class="form-input" value="{{ old('name', $plan->name) }}" required>
        </div>
        <div class="form-row">
            <label class="form-label" for="description">Description</label>
            <textarea name="description" id="description" class="form-input" rows="2">{{ old('description', $plan->description) }}</textarea>
        </div>
        <div class="row g-3">
            <div class="col-md-4">
                <div class="form-row mb-0">
                    <label class="form-label" for="price_cents">Price (sen)</label>
                    <input type="number" name="price_cents" id="price_cents" class="form-input" min="0"
                           value="{{ old('price_cents', $plan->price_cents) }}" {{ $plan->is_custom_pricing ? 'disabled' : '' }}>
                    <div class="form-hint">19900 = RM199</div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-row mb-0">
                    <label class="form-label" for="participant_limit">Participant limit</label>
                    <input type="number" name="participant_limit" id="participant_limit" class="form-input" min="0"
                           value="{{ old('participant_limit', $plan->participant_limit) }}">
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-row mb-0">
                    <label class="form-label" for="retention_days">Retention (days)</label>
                    <input type="number" name="retention_days" id="retention_days" class="form-input" min="0"
                           value="{{ old('retention_days', $plan->retention_days) }}">
                </div>
            </div>
        </div>
        <div class="row g-3 mt-1">
            <div class="col-md-4">
                <div class="form-row mb-0">
                    <label class="form-label" for="session_limit">Session limit</label>
                    <input type="number" name="session_limit" id="session_limit" class="form-input" min="0"
                           value="{{ old('session_limit', $plan->session_limit) }}">
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-row mb-0">
                    <label class="form-label" for="booth_limit">Booth limit</label>
                    <input type="number" name="booth_limit" id="booth_limit" class="form-input" min="0"
                           value="{{ old('booth_limit', $plan->booth_limit) }}">
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-row mb-0">
                    <label class="form-label" for="storage_limit_mb">Storage (MB)</label>
                    <input type="number" name="storage_limit_mb" id="storage_limit_mb" class="form-input" min="0"
                           value="{{ old('storage_limit_mb', $plan->storage_limit_mb) }}">
                </div>
            </div>
        </div>
        <div class="form-row mt-2">
            <label class="d-flex align-items-center gap-2" style="cursor:pointer">
                <input type="checkbox" name="is_custom_pricing" value="1" @checked(old('is_custom_pricing', $plan->is_custom_pricing))>
                Custom pricing (Enterprise)
            </label>
            <label class="d-flex align-items-center gap-2 mt-2" style="cursor:pointer">
                <input type="checkbox" name="is_active" value="1" @checked(old('is_active', $plan->is_active))>
                Plan is active
            </label>
        </div>
    </div>

    <div class="card section-card">
        <h2>Feature entitlements</h2>
        <p class="panel-lead">Enable features included in this plan. Changes apply to events on next entitlement rebuild.</p>
        <div class="cap-grid">
            @foreach($features as $feature)
                @php $on = old('features.'.$feature->id, $enabled->get($feature->id)?->enabled ?? false); @endphp
                <label class="cap-card">
                    <input type="checkbox" name="features[{{ $feature->id }}]" value="1" @checked($on)>
                    <div>
                        <div class="cap-title">{{ $feature->name }}</div>
                        <p class="cap-desc mono">{{ $feature->code }} · {{ $feature->category }}</p>
                    </div>
                </label>
            @endforeach
        </div>
    </div>

    <div class="d-flex justify-content-end">
        <button type="submit" class="btn-purple">Save plan</button>
    </div>
</form>
@endsection
