@extends('admin.layouts.app')

@section('title', __('dashboard.game_details'))

@section('content')
<x-admin.page-header title="{{ __('dashboard.game_details') }}" :breadcrumbs="[__('dashboard.games') => route('admin.games.index'), __('dashboard.details') => '']" />

<div class="row justify-content-center">
    <div class="col-xl-12">
        <div class="card bg-primary bg-gradient mb-4">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="avatar-lg bg-white bg-opacity-25 rounded d-flex align-items-center justify-content-center me-4" style="width: 80px; height: 80px;">
                        @if($game->image)
                            <img src="{{ asset('storage/' . $game->image) }}" alt="{{ $game->name }}" class="rounded" style="width: 100%; height: 100%; object-fit: cover;">
                        @else
                            <i class="ri-gamepad-line text-white fs-1"></i>
                        @endif
                    </div>
                    <div>
                        <h3 class="mb-1 text-white">{{ $game->name }}</h3>
                        <p class="text-white-50 mb-0">{{ $game->min_players }}-{{ $game->max_players }} {{ __('dashboard.players') }} | {{ $game->duration }} {{ __('dashboard.minutes') }}</p>
                    </div>
                    <div class="ms-auto text-end">
                        {!! \App\Enums\GameStatusEnum::badge($game->status) !!}
                    </div>
                </div>
            </div>
        </div>

        <div class="card mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">{{ __('dashboard.general_info') }}</h5>
            </div>
            <div class="card-body">
                <div class="row">
                    <x-view-col label="{{ __('dashboard.id') }}" :value="$game->id"/>
                    <x-view-col label="{{ __('dashboard.name') }}" :value="$game->name"/>
                    <x-view-col label="{{ __('dashboard.min_players') }}" :value="$game->min_players"/>
                    <x-view-col label="{{ __('dashboard.max_players') }}" :value="$game->max_players"/>
                    <x-view-col label="{{ __('dashboard.duration') }}" :value="$game->duration . ' ' . __('dashboard.minutes')"/>
                    <x-view-col label="{{ __('dashboard.price_per_session') }}" :value="$game->price_per_session ?? '-'"/>
                    <x-view-col label="{{ __('dashboard.description') }}" :value="$game->description ?? '-'"/>
                    <x-view-col label="{{ __('dashboard.age_limit') }}" :value="($game->age_limit_min ?? '-') . ' - ' . ($game->age_limit_max ?? '-')"/>
                    <x-view-col label="{{ __('dashboard.created_at') }}" :value="$game->created_at"/>
                    <x-view-col label="{{ __('dashboard.updated_at') }}" :value="$game->updated_at"/>
                </div>
            </div>
        </div>

        @if($game->courts->isNotEmpty())
        <div class="card mb-4">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="card-title mb-0">{{ __('dashboard.courts') }}</h5>
                <a href="{{ route('admin.courts.create') }}?game_id={{ $game->id }}" class="btn btn-sm btn-success">
                    <i class="ri-add-line"></i> {{ __('dashboard.add_court') }}
                </a>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>{{ __('dashboard.name') }}</th>
                                <th>{{ __('dashboard.status') }}</th>
                                <th>{{ __('dashboard.actions') }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($game->courts as $court)
                            <tr>
                                <td>{{ $court->name }}</td>
                                <td>
                                    @if($court->is_active)
                                        <span class="badge bg-success">{{ __('dashboard.active') }}</span>
                                    @else
                                        <span class="badge bg-danger">{{ __('dashboard.inactive') }}</span>
                                    @endif
                                </td>
                                <td>
                                    <a href="{{ route('admin.courts.show', $court) }}" class="btn btn-sm btn-soft-info"><i class="ri-eye-line"></i></a>
                                    <a href="{{ route('admin.courts.edit', $court) }}" class="btn btn-sm btn-soft-primary"><i class="ri-pencil-line"></i></a>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        @endif

        {{-- ===== SLOTS SECTION ===== --}}
        <div class="card mb-4">
            <div class="card-header d-flex justify-content-between align-items-center flex-wrap gap-2">
                <h5 class="card-title mb-0">
                    <i class="ri-time-line me-1"></i>{{ __('dashboard.slots') }}
                </h5>
                <div class="d-flex align-items-center gap-2">
                    <input type="date" id="slot-date-filter" class="form-control form-control-sm" style="width:160px" value="{{ now()->toDateString() }}">
                    <input type="hidden" id="slot-game-filter" value="{{ $game->id }}">
                    <a href="{{ route('admin.slots.index') }}?game_id={{ $game->id }}" class="btn btn-sm btn-soft-primary">
                        {{ __('dashboard.view_all') }}
                    </a>
                </div>
            </div>
            <div class="card-body">
                <div class="table-responsive custom-scrollbar server-side-table">
                    {!! $dataTable->table(['class' => 'table table-bordered', 'id' => 'slots-table']) !!}
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
{!! $dataTable->scripts() !!}
<script>
document.addEventListener('DOMContentLoaded', function () {
    const table = $('#slots-table').DataTable();

    // The filter elements will be automatically picked up by dtFilters 
    // when 'draw' happens.
    $('#slot-date-filter').on('change', function () {
        table.draw();
    });
});
</script>
@endpush
