@extends('super-admin.layout')

@section('title', 'Edit add-on - '.$addon->name)

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

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

    <div class="card section-card">
        <h2>Add-on 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', $addon->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', $addon->description) }}</textarea>
        </div>
        <div class="form-row">
            <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', $addon->price_cents) }}" required>
            <div class="form-hint">9900 = RM99, stored in database, not hard-coded in code.</div>
        </div>
        <label class="d-flex align-items-center gap-2" style="cursor:pointer">
            <input type="checkbox" name="is_active" value="1" @checked(old('is_active', $addon->is_active))>
            Add-on is active
        </label>
    </div>

    <div class="card section-card">
        <h2>Features unlocked</h2>
        <div class="cap-grid">
            @foreach($features as $feature)
                <label class="cap-card">
                    <input type="checkbox" name="feature_ids[]" value="{{ $feature->id }}"
                           @checked(in_array($feature->id, old('feature_ids', $linkedFeatures)))>
                    <div>
                        <div class="cap-title">{{ $feature->name }}</div>
                        <p class="cap-desc mono">{{ $feature->code }}</p>
                    </div>
                </label>
            @endforeach
        </div>
    </div>

    <div class="card section-card">
        <h2>Eligible plans</h2>
        <p class="panel-lead">Which plans can purchase this add-on.</p>
        <div class="cap-grid">
            @foreach($plans as $plan)
                <label class="cap-card">
                    <input type="checkbox" name="plan_ids[]" value="{{ $plan->id }}"
                           @checked(in_array($plan->id, old('plan_ids', $eligiblePlans)))>
                    <div>
                        <div class="cap-title">{{ $plan->name }}</div>
                        <p class="cap-desc mono">{{ $plan->code }}</p>
                    </div>
                </label>
            @endforeach
        </div>
    </div>

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