@extends('layouts.admin.theme')

@section('content')
    <div class="container text-start pb-5 mb-5">
        @if(session('success'))
            <div class="alert alert-success alert-dismissible fade show mb-4" role="alert">
                {{ session('success') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="{{ __('Fechar') }}"></button>
            </div>
        @endif

        @if(session('error'))
            <div class="alert alert-danger alert-dismissible fade show mb-4" role="alert">
                {{ session('error') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="{{ __('Fechar') }}"></button>
            </div>
        @endif

        @if($errors->any())
            <div class="alert alert-danger mb-4">
                @foreach($errors->all() as $e)
                    <div>{{ $e }}</div>
                @endforeach
            </div>
        @endif
        <div class="row mb-4">
            <div class="col text-start">
                <h3>Anexos</h3>
            </div>
            <div class="col text-end d-flex justify-content-end">

                <button type="button" class="btn btn-light bg-mediumGray me-2"
                    onclick="window.location='{{ route('admin.projetosProgramas.index') }}'">
                    <i class="fa fa-list me-1"></i> Início
                </button>
            </div>
        </div>
        @include('admin.projetosProgramas.partials.steps', ['current' => 'anexos', 'project' => $project])
        @include('components.projetosProgramas._stickyProjectHeader', ['project' => $project])

        <div class="card mt-3">
            <div class="card-header">
                <h5 class="mb-0">Gerenciar Anexos</h5>
            </div>
            <div class="card-body">
                <form method="post" action="{{ route('admin.projetosProgramas.upload', $project->id) }}"
                    enctype="multipart/form-data" class="mb-4 text-start">
                    @csrf
                    <div class="row">
                        <div class="col-md-8">
                            <label class="form-label">Selecionar arquivo(s)</label>
                            <input type="file" name="arquivo[]" class="form-control" required multiple
                                accept=".pdf,.doc,.docx,.xls,.xlsx,.jpg,.jpeg,.png">
                            <small class="text-muted">Formatos aceitos: PDF, DOC, DOCX, XLS, XLSX, JPG, JPEG, PNG. Você pode
                                selecionar múltiplos arquivos.</small>
                        </div>
                        <div class="col-md-4 d-flex align-items-end">
                            <button class="btn btn-primary w-100" type="submit">
                                <i class="fa fa-upload me-1"></i> Enviar Arquivo(s)
                            </button>
                        </div>
                    </div>
                </form>

                <hr>

                <h6 class="mb-3">Arquivos anexados ({{ $project->anexos->count() }})</h6>

                @if($project->anexos->count() > 0)
                    <div class="table-responsive">
                        <table class="table table-sm table-bordered">
                            <thead class="table-light">
                                <tr>
                                    <th style="width:50%">Nome do Arquivo</th>
                                    <th style="width:15%">Tamanho</th>
                                    <th style="width:20%">Data de Envio</th>
                                    <th style="width:15%">Ações</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($project->anexos as $anexo)
                                    <tr>
                                        <td>
                                            <a href="{{ $anexo->public_url }}" target="_blank" class="text-decoration-none">
                                                <i class="fa fa-file me-1"></i> {{ $anexo->name }}
                                            </a>
                                        </td>
                                        <td class="small">{{ $anexo->size_human }}</td>
                                        <td class="small">{{ optional($anexo->created_at)->format('d/m/Y H:i') }}</td>
                                        <td>
                                            <form method="post"
                                                action="{{ route('admin.projetosProgramas.anexos.remove', [$project->id, $anexo->id]) }}"
                                                class="d-inline"
                                                onsubmit="return confirm('Tem certeza que deseja remover este anexo?');">
                                                @csrf
                                                <button class="btn btn-sm btn-outline-danger" type="submit" title="Remover">
                                                    <i class="fa fa-trash"></i>
                                                </button>
                                            </form>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="alert alert-info mb-0">
                        <i class="fa fa-info-circle me-1"></i> Nenhum anexo enviado ainda.
                    </div>
                @endif
            </div>
        </div>

        <div class="text-end mt-4 d-flex gap-3 justify-content-end">
            <form method="post" action="{{ route('admin.projetosProgramas.anexos.store', $project->id) }}" class="d-inline">
                @csrf
                <button type="submit" class="btn btn-green">
                    <i class="fa fa-check me-1"></i> Finalizar Cadastro
                </button>
            </form>
        </div>


    </div>
@endsection