@extends('admin.layout')

@section('title', 'Cargar Resultados - ' . $evento->nombre_evento)

@section('content')
<div class="page-header">
    <div>
        <h2 class="mb-0"><i class="bi bi-person-lines-fill"></i> Estadísticas de Jugadores</h2>
        <p class="text-muted mb-0">{{ $evento->nombre_evento }} | {{ $evento->fecha_evento->format('d/m/Y') }}</p>
    </div>
    <a href="{{ route('admin.eventos.index') }}" class="btn-admin-outline">
        <i class="bi bi-arrow-left"></i> Volver
    </a>
</div>

<div class="admin-card">
    <div class="card-header bg-white d-flex justify-content-between align-items-center">
        <h5 class="mb-0 text-primary fw-bold">Planilla de Juego</h5>
        <div class="fs-4 fw-bold">
            {{ $evento->marcador_local }} - {{ $evento->marcador_visitante }}
        </div>
    </div>
    <div class="card-body">
        <form action="{{ route('admin.eventos.stats.store', $evento->id_evento) }}" method="POST">
            @csrf
            
            <div class="row">
                <!-- Equipo Local -->
                <div class="col-md-6 border-end">
                    <h5 class="fw-bold mb-3 text-uppercase small tracking-widest">{{ $evento->equipoLocal->club->nombre ?? 'Local' }}</h5>
                    <div class="table-responsive">
                        <table class="table table-sm align-middle">
                            <thead class="bg-light">
                                <tr>
                                    <th>Jugador</th>
                                    <th width="80" class="text-center">PTS</th>
                                    <th width="80" class="text-center">FALTAS</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($evento->equipoLocal->jugadores as $j)
                                @php 
                                    $stat = $stats->get($j->id_jugador); 
                                    $canEdit = (session('admin_role') == 1) || (session('admin_role') == 2 && $j->id_club_actual == session('admin_id_club'));
                                @endphp
                                <tr class="{{ !$canEdit ? 'row-disabled' : '' }}">
                                    <td>
                                        <div class="fw-bold">{{ $j->apellido }}, {{ $j->nombre }}</div>
                                        <small class="text-muted">DNI: {{ $j->documento }}</small>
                                    </td>
                                    <td>
                                        <input type="number" name="stats[{{ $j->id_jugador }}][puntos]" class="form-control form-control-sm text-center {{ !$canEdit ? 'input-disabled' : '' }}" value="{{ $stat->puntos ?? 0 }}" min="0" {{ $canEdit ? '' : 'readonly' }}>
                                    </td>
                                    <td>
                                        <input type="number" name="stats[{{ $j->id_jugador }}][faltas]" class="form-control form-control-sm text-center {{ !$canEdit ? 'input-disabled' : '' }}" value="{{ $stat->faltas ?? 0 }}" min="0" max="5" {{ $canEdit ? '' : 'readonly' }}>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>

                <!-- Equipo Visitante -->
                <div class="col-md-6">
                    <h5 class="fw-bold mb-3 text-uppercase small tracking-widest">{{ $evento->equipoVisitante->club->nombre ?? 'Visitante' }}</h5>
                    <div class="table-responsive">
                        <table class="table table-sm align-middle">
                            <thead class="bg-light">
                                <tr>
                                    <th>Jugador</th>
                                    <th width="80" class="text-center">PTS</th>
                                    <th width="80" class="text-center">FALTAS</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($evento->equipoVisitante->jugadores as $j)
                                @php 
                                    $stat = $stats->get($j->id_jugador);
                                    $canEdit = (session('admin_role') == 1) || (session('admin_role') == 2 && $j->id_club_actual == session('admin_id_club'));
                                @endphp
                                <tr class="{{ !$canEdit ? 'row-disabled' : '' }}">
                                    <td>
                                        <div class="fw-bold">{{ $j->apellido }}, {{ $j->nombre }}</div>
                                        <small class="text-muted">DNI: {{ $j->documento }}</small>
                                    </td>
                                    <td>
                                        <input type="number" name="stats[{{ $j->id_jugador }}][puntos]" class="form-control form-control-sm text-center {{ !$canEdit ? 'input-disabled' : '' }}" value="{{ $stat->puntos ?? 0 }}" min="0" {{ $canEdit ? '' : 'readonly' }}>
                                    </td>
                                    <td>
                                        <input type="number" name="stats[{{ $j->id_jugador }}][faltas]" class="form-control form-control-sm text-center {{ !$canEdit ? 'input-disabled' : '' }}" value="{{ $stat->faltas ?? 0 }}" min="0" max="5" {{ $canEdit ? '' : 'readonly' }}>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>

            <div class="mt-4 pt-3 border-top text-end">
                <button type="submit" class="btn-admin-primary px-5">
                    <i class="bi bi-save me-2"></i> GUARDAR ESTADÍSTICAS
                </button>
            </div>
        </form>
    </div>
</div>
@section('styles')
<style>
    .row-disabled {
        opacity: 0.7;
        background-color: #f8f9fa;
    }
    .input-disabled {
        background-color: #e9ecef !important;
        cursor: not-allowed;
    }
</style>
@endsection
@endsection
