@extends('layouts.app')
@section('title', 'Incapacidades')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
    <h5 class="mb-0"><i class="bi bi-bandaid me-2 text-danger"></i>Incapacidades</h5>
    <div class="d-flex gap-2">
        <a href="{{ route('incapacidades.create') }}" class="btn btn-primary btn-sm">
            <i class="bi bi-plus me-1"></i>Registrar
        </a>
        <a href="{{ route('incapacidades.csv', ['anio'=>$anio, 'empleado_id'=>$empleadoId]) }}"
           class="btn btn-outline-success btn-sm">
            <i class="bi bi-file-earmark-spreadsheet me-1"></i>CSV
        </a>
    </div>
</div>

{{-- FILTROS --}}
<form method="GET" class="card mb-3">
    <div class="card-body py-2">
        <div class="row g-2 align-items-end">
            <div class="col-sm-3">
                <label class="form-label small fw-semibold mb-1">Año</label>
                <select name="anio" class="form-select form-select-sm">
                    @foreach($anios as $a)
                    <option value="{{ $a }}" {{ $a==$anio?'selected':'' }}>{{ $a }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-sm-5">
                <label class="form-label small fw-semibold mb-1">Empleado</label>
                <select name="empleado_id" class="form-select form-select-sm">
                    <option value="">Todos</option>
                    @foreach($empleados as $e)
                    <option value="{{ $e->id }}" {{ $e->id==$empleadoId?'selected':'' }}>{{ $e->nombre }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-sm-4">
                <button class="btn btn-sm btn-primary w-100">Filtrar</button>
            </div>
        </div>
    </div>
</form>

{{-- TOTALES --}}
<div class="row g-2 mb-3">
    <div class="col-6 col-md-3">
        <div class="card text-center p-2">
            <div class="fw-bold fs-5 text-primary">{{ $totales['dias'] }}</div>
            <div class="text-muted small">Dias totales</div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card text-center p-2">
            <div class="fw-bold fs-5 text-warning">$ {{ number_format($totales['valor_empleador'],0,'.',',') }}</div>
            <div class="text-muted small">Pago empresa</div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card text-center p-2">
            <div class="fw-bold fs-5 text-info">$ {{ number_format($totales['valor_eps'],0,'.',',') }}</div>
            <div class="text-muted small">Pago EPS</div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="card text-center p-2">
            <div class="fw-bold fs-5 text-success">$ {{ number_format($totales['total_pagado'],0,'.',',') }}</div>
            <div class="text-muted small">Total pagado</div>
        </div>
    </div>
</div>

{{-- TABLA --}}
<div class="card">
    <div class="card-header d-flex justify-content-between">
        <span><i class="bi bi-list-ul me-1"></i>Registros {{ $anio }}</span>
        <span class="fw-normal small opacity-75">{{ $incapacidades->count() }} registros</span>
    </div>
    <div class="table-responsive">
        <table class="table table-hover align-middle mb-0" style="font-size:13px">
            <thead class="table-light">
                <tr>
                    <th>Empleado</th>
                    <th>Tipo</th>
                    <th class="text-center">Inicio</th>
                    <th class="text-center">Fin</th>
                    <th class="text-center">Dias</th>
                    <th class="text-end d-none d-md-table-cell">Empresa</th>
                    <th class="text-end d-none d-md-table-cell">EPS</th>
                    <th class="text-end d-none d-md-table-cell">ARL</th>
                    <th class="text-end fw-semibold">Total</th>
                    <th class="no-print"></th>
                </tr>
            </thead>
            <tbody>
                @forelse($incapacidades as $inc)
                <tr>
                    <td>
                        <div class="fw-semibold">{{ $inc->empleado->nombre }}</div>
                        <small class="text-muted">{{ $inc->empleado->cedula }}</small>
                    </td>
                    <td>
                        @php
                            $tipoColor = [
                                'enfermedad_general'=>'secondary',
                                'accidente_trabajo'=>'danger',
                                'enfermedad_profesional'=>'warning',
                                'maternidad'=>'pink',
                                'paternidad'=>'info',
                            ][$inc->tipo] ?? 'secondary';
                        @endphp
                        <span class="badge bg-{{ $tipoColor }}">{{ $inc->tipo_label }}</span>
                        @if($inc->diagnostico)
                        <div class="text-muted" style="font-size:11px">{{ $inc->diagnostico }}</div>
                        @endif
                    </td>
                    <td class="text-center">{{ $inc->fecha_inicio->format('d/m/Y') }}</td>
                    <td class="text-center">{{ $inc->fecha_fin->format('d/m/Y') }}</td>
                    <td class="text-center fw-bold">{{ $inc->dias }}</td>
                    <td class="text-end d-none d-md-table-cell">
                        @if($inc->valor_empleador > 0)
                        <small class="text-muted">{{ $inc->dias_empleador }}d</small>
                        $ {{ number_format($inc->valor_empleador,0,'.',',') }}
                        @else —
                        @endif
                    </td>
                    <td class="text-end d-none d-md-table-cell">
                        @php $vEps = $inc->valor_eps + $inc->valor_eps2; $dEps = $inc->dias_eps + $inc->dias_eps2; @endphp
                        @if($vEps > 0)
                        <small class="text-muted">{{ $dEps }}d</small>
                        $ {{ number_format($vEps,0,'.',',') }}
                        @else —
                        @endif
                    </td>
                    <td class="text-end d-none d-md-table-cell">
                        @if($inc->valor_arl > 0)
                        <small class="text-muted">{{ $inc->dias_arl }}d</small>
                        $ {{ number_format($inc->valor_arl,0,'.',',') }}
                        @else —
                        @endif
                    </td>
                    <td class="text-end fw-semibold text-success">
                        $ {{ number_format($inc->total_pagado,0,'.',',') }}
                    </td>
                    <td class="no-print">
                        <a href="{{ route('incapacidades.edit', $inc) }}" class="btn btn-sm btn-outline-primary">
                            <i class="bi bi-pencil"></i>
                        </a>
                        <form action="{{ route('incapacidades.destroy', $inc) }}" method="POST" class="d-inline"
                              onsubmit="return confirm('Eliminar este registro?')">
                            @csrf @method('DELETE')
                            <button class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i></button>
                        </form>
                    </td>
                </tr>
                @empty
                <tr><td colspan="10" class="text-center text-muted py-4">No hay incapacidades registradas.</td></tr>
                @endforelse
            </tbody>
            @if($incapacidades->count())
            <tfoot class="table-light fw-semibold">
                <tr>
                    <td colspan="4" class="text-end text-muted small">Totales:</td>
                    <td class="text-center">{{ $totales['dias'] }}</td>
                    <td class="text-end d-none d-md-table-cell">$ {{ number_format($totales['valor_empleador'],0,'.',',') }}</td>
                    <td class="text-end d-none d-md-table-cell">$ {{ number_format($totales['valor_eps'],0,'.',',') }}</td>
                    <td class="text-end d-none d-md-table-cell">$ {{ number_format($totales['valor_arl'],0,'.',',') }}</td>
                    <td class="text-end text-success">$ {{ number_format($totales['total_pagado'],0,'.',',') }}</td>
                    <td></td>
                </tr>
            </tfoot>
            @endif
        </table>
    </div>
</div>
@endsection
