@extends('super-admin.layout')

@section('title', $organiser ? 'Edit organiser' : 'Create organiser')

@section('content')
<div class="pg-head">
    <div>
        <h1>{{ $organiser ? 'Edit organiser' : 'Create organiser' }}</h1>
    </div>
    <a href="{{ route('mexco.super.organisers') }}" class="btn-outline">← Back</a>
</div>

<form method="POST" action="{{ $organiser ? route('mexco.super.organisers.update', $organiser) : route('mexco.super.organisers.store') }}" class="form-stack">
    @csrf
    @if($organiser) @method('PUT') @endif

    <div class="card section-card">
        <div class="form-row">
            <label class="form-label" for="organisation_id">Organisation</label>
            <select name="organisation_id" id="organisation_id" class="form-input" required>
                @foreach($organisations as $org)
                    <option value="{{ $org->id }}" @selected(old('organisation_id', $organiser->organisation_id ?? '') == $org->id)>{{ $org->name }}</option>
                @endforeach
            </select>
        </div>
        <div class="form-row">
            <label class="form-label" for="name">Full name</label>
            <input type="text" name="name" id="name" class="form-input" value="{{ old('name', $organiser->name ?? '') }}" required>
        </div>
        <div class="form-row">
            <label class="form-label" for="email">Email (login)</label>
            <input type="email" name="email" id="email" class="form-input" value="{{ old('email', $organiser->email ?? '') }}" required>
        </div>
        <div class="row g-3">
            <div class="col-md-6">
                <div class="form-row mb-0">
                    <label class="form-label" for="password">Password</label>
                    <input type="password" name="password" id="password" class="form-input" minlength="6" {{ $organiser ? '' : 'required' }}>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-row mb-0">
                    <label class="form-label" for="password_confirmation">Confirm password</label>
                    <input type="password" name="password_confirmation" id="password_confirmation" class="form-input" minlength="6">
                </div>
            </div>
        </div>
        <div class="row g-3 mt-1">
            <div class="col-md-6">
                <div class="form-row mb-0">
                    <label class="form-label" for="role">Role</label>
                    <select name="role" id="role" class="form-input">
                        @foreach($roles as $key => $label)
                            <option value="{{ $key }}" @selected(old('role', $organiser->role ?? 'event_manager') === $key)>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-row mb-0">
                    <label class="form-label" for="status">Status</label>
                    <select name="status" id="status" class="form-input">
                        @foreach(['active','suspended','invited'] as $s)
                            <option value="{{ $s }}" @selected(old('status', $organiser->status ?? 'active') === $s)>{{ ucfirst($s) }}</option>
                        @endforeach
                    </select>
                </div>
            </div>
        </div>
    </div>

    <div class="card section-card">
        <h2>Assigned events</h2>
        <p class="panel-lead">Organisation Admins automatically access all events in their organisation.</p>
        <div class="cap-grid">
            @foreach($events as $event)
                <label class="cap-card">
                    <input type="checkbox" name="event_ids[]" value="{{ $event->id }}"
                        @checked(in_array($event->id, old('event_ids', $organiser?->assignedEvents->pluck('id')->all() ?? [])))>
                    <div>
                        <div class="cap-title">{{ $event->name }}</div>
                        <p class="cap-desc mono">{{ $event->id }} · {{ $event->organisation?->name }}</p>
                    </div>
                </label>
            @endforeach
        </div>
    </div>

    <div class="d-flex justify-content-end">
        <button type="submit" class="btn-purple">{{ $organiser ? 'Save organiser' : 'Create organiser' }}</button>
    </div>
</form>
@endsection
