@extends('layouts.admin')
@section('title', 'Submissões')
@section('content')
<div class="page-header" style="display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 1rem;">
    <div><h1>Submissões</h1><p class="subtitle">{{ $submissions->total() }} resultados</p></div>
    <div style="display: flex; gap: 0.5rem; flex-wrap: wrap;">
        <a href="{{ route('admin.export.contabilidade') }}" class="btn btn-sm btn-outline">📥 Export Contab.</a>
        @if(!$isContabilidade)
            <a href="{{ route('admin.export.erp') }}" class="btn btn-sm btn-outline">📥 Export ERP</a>
        @endif
    </div>
</div>

{{-- Filtros --}}
<div class="card" style="padding: 1rem;">
    <form method="GET" class="form-inline">
        <div class="form-group" style="flex: 1; min-width: 150px;">
            <input type="text" name="search" class="form-control" placeholder="Pesquisar nome ou NIF..." value="{{ request('search') }}">
        </div>
        <div class="form-group">
            <select name="status" class="form-control">
                <option value="">Todos os estados</option>
                @foreach(\App\Models\Submission::STATUSES as $key => $label)
                    <option value="{{ $key }}" {{ request('status') == $key ? 'selected' : '' }}>{{ $label }}</option>
                @endforeach
            </select>
        </div>
        <div class="form-group">
            <select name="flag" class="form-control">
                <option value="">Todas as flags</option>
                <option value="sem_correspondencia" {{ request('flag') == 'sem_correspondencia' ? 'selected' : '' }}>Sem correspondência</option>
                <option value="ip_fora_pt" {{ request('flag') == 'ip_fora_pt' ? 'selected' : '' }}>IP fora de PT</option>
                <option value="inconsistencia" {{ request('flag') == 'inconsistencia' ? 'selected' : '' }}>Inconsistência</option>
                <option value="suspeito" {{ request('flag') == 'suspeito' ? 'selected' : '' }}>Suspeito</option>
            </select>
        </div>
        <button type="submit" class="btn btn-sm btn-primary">Filtrar</button>
        <a href="{{ route('admin.submissions.index') }}" class="btn btn-sm btn-outline">Limpar</a>
    </form>
</div>

{{-- Tabela --}}
<div class="card" style="padding: 0;">
    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Nome</th>
                    <th>NIF</th>
                    <th>Tipo</th>
                    <th>Estado</th>
                    <th>Flags</th>
                    <th>Data</th>
                </tr>
            </thead>
            <tbody>
                @forelse($submissions as $s)
                <tr onclick="window.location='{{ route('admin.submissions.show', array_merge(['submission' => $s->id], request()->query())) }}'" style="cursor:pointer;">
                    <td>#{{ $s->id }}</td>
                    <td>{{ $isContabilidade ? $s->nome_completo : $s->nome_completo }}</td>
                    <td>{{ $s->nif }}</td>
                    <td>{{ $s->type === 'update' ? 'Atualiz.' : 'Nova adm.' }}</td>
                    <td>@statusBadge($s->status)</td>
                    <td>
                        @foreach($s->flags as $flag)
                            <span class="badge badge-flag">{{ $flag }}</span>
                        @endforeach
                    </td>
                    <td>{{ $s->created_at->format('d/m/Y H:i') }}</td>
                </tr>
                @empty
                <tr><td colspan="7" style="text-align:center; color: var(--muted); padding: 2rem;">Nenhuma submissão encontrada.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>

<div class="pagination">{{ $submissions->links('components.pagination') }}</div>
@endsection
