@extends('layouts.app')

@section('content')
<div class="bg-body-light">
    <div class="content content-full">
        <div class="d-flex flex-column flex-sm-row justify-content-sm-between align-items-sm-center py-2">
            <div class="flex-grow-1">
                <h1 class="h3 fw-bold mb-2 text-danger">
                    <i class="fa fa-ban me-2"></i>Devoluciones y Anulaciones
                </h1>
                <h2 class="fs-base lh-base fw-medium text-muted mb-0">
                    Módulo administrativo para la reversión de contratos y liberación de lotes.
                </h2>
            </div>
            <div class="flex-shrink-0 mt-3 mt-sm-0 ms-sm-3">
                <span class="badge bg-danger-light text-danger px-3 py-2">
                    <i class="fa fa-shield-alt me-1"></i> ACCESO RESTRINGIDO
                </span>
            </div>
        </div>
    </div>
</div>

<div class="content">
    {{-- Notificaciones --}}
    @if(session('success'))
        <div class="alert alert-success d-flex align-items-center" role="alert">
            <i class="fa fa-check-circle me-2"></i>
            <p class="mb-0">{{ session('success') }}</p>
            <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    @if(session('error'))
        <div class="alert alert-danger d-flex align-items-center" role="alert">
            <i class="fa fa-exclamation-circle me-2"></i>
            <p class="mb-0">{{ session('error') }}</p>
            <button type="button" class="btn-close ms-auto" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    @if(auth()->user()->tipo != 1)
        <div class="alert alert-warning d-flex align-items-center mb-4" role="alert">
            <div class="flex-shrink-0">
                <i class="fa fa-lock fa-2x me-3"></i>
            </div>
            <div class="flex-grow-1">
                <p class="mb-0 fw-semibold">Permisos Insuficientes</p>
                <p class="mb-0 fs-sm">Solo los usuarios con rol de <strong>Administrador</strong> tienen autorización para procesar devoluciones y anular contratos vigentes.</p>
            </div>
        </div>
    @endif

    <div class="block block-rounded block-bordered">
        <div class="block-header block-header-default border-bottom">
            <h3 class="block-title">Contratos Disponibles para Proceso</h3>
            <div class="block-options">
                <span class="fs-sm text-muted me-2">{{ $contratos->total() }} contratos vigentes</span>
            </div>
        </div>
        
        <div class="block-content block-content-full bg-body-light border-bottom">
            {{-- Buscador Focalizado --}}
            <form method="GET" action="{{ route('devoluciones.index') }}">
                <div class="row items-push">
                    <div class="col-md-5">
                        <div class="input-group">
                            <span class="input-group-text bg-white border-0">
                                <i class="fa fa-search text-muted"></i>
                            </span>
                            <input type="number" name="buscar" class="form-control form-control-alt border-0" 
                                   placeholder="Buscar por número de contrato..." 
                                   value="{{ request('buscar') }}">
                            <button type="submit" class="btn btn-danger">
                                Buscar Contrato
                            </button>
                            @if(request('buscar'))
                                <a href="{{ route('devoluciones.index') }}" class="btn btn-alt-secondary">
                                    <i class="fa fa-times"></i>
                                </a>
                            @endif
                        </div>
                    </div>
                </div>
            </form>
        </div>

        <div class="block-content p-0">
            <div class="table-responsive">
                <table class="table table-vcenter table-hover mb-0 fs-sm">
                    <thead class="bg-body-dark">
                        <tr>
                            <th class="text-center" style="width: 100px;">N° Sistema</th>
                            <th class="text-center" style="width: 100px;">N° Manual</th>
                            <th class="text-center">Fecha</th>
                            <th>Ubicación</th>
                            <th class="text-center">Cliente</th>
                            <th class="text-center" style="width: 120px;">Acción</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($contratos as $c)
                        <tr>
                            <td class="text-center fw-bold">
                                <a href="{{ route('devoluciones.show', $c->id) }}" class="text-danger">#{{ $c->id }}</a>
                            </td>
                            <td class="text-center text-muted">
                                {{ $c->numman ?: '—' }}
                            </td>
                            <td class="text-center">
                                {{ \App\Helpers\FechaHelper::bdToLocal($c->fecha) }}
                            </td>
                            <td>
                                <div class="fw-semibold">{{ $c->nom_urb ?? $c->urbanizacion }}</div>
                                <div class="fs-xs text-muted">
                                    Distrito: {{ $c->nom_dis ?? $c->distrito }} | Mzo: {{ $c->manzano }} | Lote: {{ $c->lote }}
                                </div>
                            </td>
                            <td class="text-center">
                                <span class="fw-medium text-dark">{{ $c->idcliente }}</span>
                                <div class="fs-xs text-muted" title="{{ $c->nom_cliente }}">Ver detalles</div>
                            </td>
                            <td class="text-center">
                                <a href="{{ route('devoluciones.show', $c->id) }}" 
                                   class="btn btn-sm btn-alt-warning" 
                                   data-bs-toggle="tooltip" title="Iniciar Proceso de Anulación">
                                    <i class="fa fa-file-signature me-1"></i> Revisar
                                </a>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="text-center py-5">
                                <i class="fa fa-ban fa-3x text-muted mb-3"></i>
                                <p class="text-muted mb-0">No se encontraron contratos vigentes para procesar.</p>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        
        @if($contratos->hasPages())
        <div class="block-content block-content-full bg-body-light border-top">
            <div class="d-flex justify-content-center">
                {{ $contratos->appends(request()->all())->links('pagination::bootstrap-5') }}
            </div>
        </div>
        @endif
    </div>
</div>
@endsection