@extends('layouts.admin.theme')

@section('title', 'Lista - Fluxo de Ônibus')

@section('content')
@include('errors.messageFlash')

<div class="card shadow-sm border-0">
    <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center">
        <h3 class="my-2 fw-bolder">
            <i class="fa fa-bus me-2"></i> Fluxo de Ônibus
        </h3>

        <div class="d-flex gap-2">
        <a href="{{ route('admin.fluxoOnibus.guias.verify') }}" class="btn btn-light btn">
                <i class="fa fa-plus me-2"></i> Novo Fluxo
            </a>
        </div>
    </div>

    <div class="card-body">
        {{-- Componente de busca --}}
        <div class="row mb-3">
            <div class="col-3">
                <form method="GET" action="{{ route('admin.fluxoOnibus.index') }}">
                    <div class="input-group">
                        <input type="text" name="q" class="form-control"
                            placeholder="Buscar por empresa, origem..."
                            value="{{ request('q') }}">
                        <button type="submit" class="btn btn-primary">
                            <i class="fa fa-search me-1"></i> Buscar
                        </button>
                        @if(request('q'))
                        <a href="{{ route('admin.fluxoOnibus.index') }}" class="btn btn-secondary">
                            <i class="fa fa-times me-1"></i> Limpar
                        </a>
                        @endif
                    </div>
                </form>
            </div>
        </div>

        {{-- Tabela --}}
        <div class="table-responsive mt-3">
            <table class="table table-hover table-bordered align-middle">
                <thead class="table-light">
                    <tr>
                        <th>Data Chegada</th>
                        <th>Origem</th>
                        <th>Empresa</th>
                        <th>Veículo</th>
                        <th>Passageiros</th>
                        <th>Guia</th>
                        <th>Contato</th>
                        <th>Ações</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($fluxos as $fluxo)
                    <tr>
                        <td>
                            <strong>{{ \Carbon\Carbon::parse($fluxo->data_chegada)->format('d/m/Y') }}</strong>
                            <br>
                            <small class="text-muted">{{ $fluxo->horario_chegada }}</small>
                        </td>
                        <td>{{ $fluxo->origem }}</td>
                        <td>
                            <strong>{{ $fluxo->empresa }}</strong>
                            @if($fluxo->cnpj)
                            <br>
                            <small class="text-muted">{{ $fluxo->cnpj }}</small>
                            @endif
                        </td>
                        <td>
                            @php
                            $coresVeiculos = [
                                'Van' => 'info',
                                'Micro-ônibus' => 'primary',
                                'Ônibus' => 'success',
                                'Double Deck' => 'warning'
                            ];
                            $cor = $coresVeiculos[$fluxo->veiculo] ?? 'secondary';
                            @endphp
                            <span class="badge bg-{{ $cor }}">
                                {{ $fluxo->veiculo }}
                            </span>
                            <br>
                            <small class="text-muted">{{ $fluxo->placa_veiculo }}</small>
                        </td>
                        <td>
                            <span class="badge bg-primary rounded-pill">
                                {{ $fluxo->nr_passageiros }} passageiros
                            </span>
                        </td>
                        <td>
                            <strong>{{ $fluxo->nome_guia }}</strong>
                            <br>
                            <small class="text-muted">{{ $fluxo->email_guia }}</small>
                            @if($fluxo->reg_cadastur)
                            <br>
                            <small class="text-muted">CADASTUR: {{ $fluxo->reg_cadastur }}</small>
                            @endif
                        </td>
                        <td>
                            <small>
                                <i class="fa fa-phone text-muted me-1"></i>{{ $fluxo->tel_empresa }}
                                @if($fluxo->tel2)
                                <br>
                                <i class="fa fa-phone text-muted me-1"></i>{{ $fluxo->tel2 }}
                                @endif
                            </small>
                        </td>
                        <td>
                            <div class="d-flex justify-content-center gap-1">
                                <a href="{{ route('admin.fluxoOnibus.show', $fluxo->id) }}"
                                    class="btn btn-info btn-sm" title="Visualizar">
                                    <i class="fa fa-eye"></i>
                                </a>

                                <a href="{{ route('admin.fluxoOnibus.edit', $fluxo->id) }}"
                                    class="btn btn-warning btn-sm" title="Editar">
                                    <i class="fa fa-pen"></i>
                                </a>

                                <form action="{{ route('admin.fluxoOnibus.destroy', $fluxo->id) }}"
                                    method="POST"
                                    onsubmit="return confirm('Tem certeza que deseja excluir este fluxo?')">
                                    @csrf
                                    @method('DELETE')
                                    <button class="btn btn-danger btn-sm" title="Excluir">
                                        <i class="fa fa-trash"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="8" class="text-center text-muted">
                            <i class="fa fa-info-circle"></i> Nenhum fluxo de ônibus cadastrado
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        {{-- Paginação --}}
        <div class="d-flex justify-content-center mt-3">
            {!! $fluxos->links() !!}
        </div>
    </div>
</div>

<script>
    // Função para mostrar alertas
    function showAlert(message, type) {
        const alertClass = type === 'success' ? 'alert-success' : 'alert-danger';
        const alertDiv = document.createElement('div');
        alertDiv.className = `alert ${alertClass} alert-dismissible fade show position-fixed`;
        alertDiv.style.cssText = 'top: 20px; right: 20px; z-index: 9999; min-width: 300px;';
        alertDiv.innerHTML = `
            ${message}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        `;

        document.body.appendChild(alertDiv);

        // Auto-remove após 3 segundos
        setTimeout(() => {
            if (alertDiv.parentElement) {
                alertDiv.remove();
            }
        }, 3000);
    }

    // Mostrar alerta de sucesso se existir
    @if(session('success'))
        showAlert('{{ session('success') }}', 'success');
    @endif
</script>

<style>
    .table td {
        vertical-align: middle;
    }
    
    .badge {
        font-size: 0.75em;
    }
</style>
@endsection