@extends('layouts.app')
@section('title', 'Bolsa de Tiempo')
@section('content')
<div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
    <h4 class="mb-0"><i class="bi bi-hourglass-split me-1"></i> Bolsa de Tiempo</h4>
    <div class="d-flex gap-2 align-items-center no-print">
        <form method="GET" class="d-flex gap-2">
            <select name="periodo_id" class="form-select form-select-sm" style="width:220px" onchange="this.form.submit()">
                <option value="">-- Período --</option>
                @foreach($periodos as $p)
                <option value="{{ $p->id }}" {{ optional($periodo)->id == $p->id ? 'selected' : '' }}>
                    {{ $p->nombre_completo }}
                </option>
                @endforeach
            </select>
        </form>
        <button onclick="window.print()" class="btn btn-sm btn-outline-secondary">
            <i class="bi bi-printer me-1"></i> Imprimir
        </button>
    </div>
</div>

@if($periodo)
<div class="alert alert-light border">
    <strong>Período:</strong> {{ $periodo->nombre_completo }}
    &nbsp;|&nbsp; Base: <strong>230 horas</strong>
    &nbsp;|&nbsp; Alerta si saldo &lt; <strong>-24 horas</strong>
</div>
@endif

@if($datos->count())
<div class="card">
    <div class="card-body p-0">
        <table class="table table-hover mb-0">
            <thead class="table-dark">
                <tr>
                    <th>Empleado</th>
                    <th>Cédula</th>
                    <th>Cargo</th>
                    <th class="text-center">Base<br>(h)</th>
                    <th class="text-center">Ausencias<br>(h)</th>
                    <th class="text-center">+ Festivos<br>trabajados</th>
                    <th class="text-center">Saldo<br>Bolsa</th>
                    <th class="text-center">Estado</th>
                </tr>
            </thead>
            <tbody>
                @foreach($datos as $fila)
                @php $b = $fila['bolsa']; @endphp
                <tr class="{{ $b['alerta'] ? 'table-danger' : '' }}">
                    <td>{{ $fila['empleado'] }}</td>
                    <td>{{ $fila['cedula'] }}</td>
                    <td>{{ $fila['cargo'] }}</td>
                    <td class="text-center">{{ $b['base'] }}</td>
                    <td class="text-center text-danger">{{ number_format($b['ausencias'],1) }}</td>
                    <td class="text-center text-success">
                        {{ $b['credito_festivos'] > 0 ? '+'.number_format($b['credito_festivos'],1) : '—' }}
                    </td>
                    <td class="text-center fw-bold fs-6 {{ $b['alerta'] ? 'text-danger' : 'text-success' }}">
                        {{ number_format($b['saldo'],1) }} h
                        @if($b['alerta'])
                            <i class="bi bi-exclamation-triangle-fill"></i>
                        @endif
                    </td>
                    <td class="text-center">
                        <span class="badge
                            {{ $fila['estado'] === 'aprobado' ? 'bg-success' : ($fila['estado'] === 'enviado' ? 'bg-warning text-dark' : 'bg-secondary') }}">
                            {{ strtoupper($fila['estado']) }}
                        </span>
                    </td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>

<div class="mt-3 small text-muted">
    <i class="bi bi-info-circle me-1"></i>
    <strong>Bolsa de tiempo:</strong> 230h base − horas de ausencias (vacaciones, incapacidades, licencias, etc.)
    + horas trabajadas en domingos o festivos. Si el saldo es inferior a −24h se genera alerta.
</div>

@elseif($periodo)
<div class="alert alert-info">No hay hojas registradas para este período.</div>
@else
<div class="alert alert-warning">Seleccione un período para ver el reporte.</div>
@endif
@endsection
