@extends('layouts.app')

@section('title', 'Spaces')

@section('content')
    <x-breadcrumbs :items="[
        ['label' => 'Dashboard', 'url' => route('dashboard')],
        ['label' => 'Spaces'],
    ]" />

    <x-page-header title="Spaces" subtitle="Manage spaces">
        @can('create', \App\Models\Space::class)
            <a href="{{ route('spaces.create') }}" class="btn btn-primary btn-sm">
                <i class="bi bi-plus-lg me-1" aria-hidden="true"></i> Add Space
            </a>
        @endcan
    </x-page-header>

    <div class="vem-filter-bar">
        <form method="GET" action="{{ route('spaces.index') }}" class="row g-3 align-items-end">
            <div class="col-md-4">
                <label class="form-label" for="search">Search</label>
                <input type="search" name="search" id="search" class="form-control form-control-sm" value="{{ request('search') }}" placeholder="Name, code, location...">
            </div>
            <div class="col-md-3">
                <label class="form-label" for="space_type_id">Type</label>
                <select name="space_type_id" id="space_type_id" class="form-select form-select-sm">
                    <option value="">All types</option>
                    @foreach ($spaceTypes as $type)
                        <option value="{{ $type->id }}" @selected((string) request('space_type_id') === (string) $type->id)>{{ $type->name }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label" for="operational_status">Status</label>
                <select name="operational_status" id="operational_status" class="form-select form-select-sm">
                    <option value="">All statuses</option>
                    @foreach (\App\Enums\SpaceOperationalStatus::cases() as $status)
                        <option value="{{ $status->value }}" @selected(request('operational_status') === $status->value)>{{ $status->label() }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-2 d-flex gap-2">
                <button type="submit" class="btn btn-primary btn-sm">Filter</button>
                <a href="{{ route('spaces.index') }}" class="btn btn-outline-secondary btn-sm">Reset</a>
            </div>
        </form>
    </div>

    <x-card flush>
        @if ($spaces->isEmpty())
            <x-empty-state icon="door-open" title="No spaces yet" description="Records will appear here once created.">
                @can('create', \App\Models\Space::class)
                    <a href="{{ route('spaces.create') }}" class="btn btn-primary btn-sm">Add Space</a>
                @endcan
            </x-empty-state>
        @else
            <x-table :headers="['Name', 'Type', 'Location', 'Capacity', 'Standard Rate', 'Status', 'Actions']">
                @foreach ($spaces as $space)
                    <tr>
                        <td>
                            <a href="{{ route('spaces.show', $space) }}" class="fw-medium">{{ $space->name }}</a>
                            <div class="text-muted small">{{ $space->code }}</div>
                        </td>
                        <td>{{ $space->spaceType->name ?? '—' }}</td>
                        <td>{{ $space->location ?? '—' }}</td>
                        <td>{{ $space->max_capacity ?? '—' }}</td>
                        <td><x-money :amount="$space->standard_rate" /></td>
                        <td><x-badge :status="$space->operational_status->value" :label="$space->operational_status->label()" /></td>
                        <td>
                            <div class="btn-group btn-group-sm">
                                <a href="{{ route('spaces.show', $space) }}" class="btn btn-outline-primary">View</a>
                                @can('update', $space)
                                    <a href="{{ route('spaces.edit', $space) }}" class="btn btn-outline-secondary">Edit</a>
                                @endcan
                                @can('delete', $space)
                                    <form method="POST" action="{{ route('spaces.destroy', $space) }}" class="d-inline" onsubmit="return confirm('Archive this space?');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="btn btn-outline-danger">Delete</button>
                                    </form>
                                @endcan
                            </div>
                        </td>
                    </tr>
                @endforeach
            </x-table>
            @if ($spaces->hasPages())
                <div class="p-3 border-top">{{ $spaces->links() }}</div>
            @endif
        @endif
    </x-card>
@endsection
