@extends('layouts.admin.theme')
@section('title', env('APP_NAME') . ' :: ' . __('Arquivos da Licitação/Procedimento'))

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

    <div class="card shadow-sm">
        @php $headLine = "<i class='fa-solid fa-folder-open'></i> " . __('Arquivos do Registro'); @endphp
        @include('components.buttons._headLineButtonsBackAdd', [
            'headLine' => __($headLine),
            'routeBack' => 'admin.licitacoesProcedimentos.index',
            'routeBackParams' => request()->only(['ano', 'tipo_processo', 'tipo', 'status', 'nr_processo', 'page']),
            'nameBack' => __('Voltar'),
            'permission' => 'GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS',
        ])

        <div class="card-body">
            <form method="post" action="{{ route('admin.licitacoesProcedimentos.uploads.store', [$registro->id] + request()->only(['ano', 'tipo_processo', 'tipo', 'status', 'nr_processo', 'page'])) }}" enctype="multipart/form-data" class="mb-4">
                @csrf
                @foreach(['ano', 'tipo_processo', 'tipo', 'status', 'nr_processo', 'page'] as $filterKey)
                    @if(request()->filled($filterKey))
                        <input type="hidden" name="{{ $filterKey }}" value="{{ request($filterKey) }}">
                    @endif
                @endforeach
                <div class="row g-2 align-items-end">
                    <div class="col-md-8">
                        <label class="form-label">{{ __('Novo arquivo') }}</label>
                        <input type="file" name="uploads[]" class="form-control" accept=".pdf,.mp4,application/pdf,video/mp4" multiple>
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">{{ __('Legenda') }}</label>
                        <input type="text" name="uploads_legendas[]" maxlength="100" class="form-control">
                    </div>
                </div>
                <div class="mt-3">
                    <button class="btn btn-success" type="submit">{{ __('Adicionar arquivo') }}</button>
                </div>
            </form>

            <div class="table-responsive mt-3">
                <table class="table table-sm table-striped table-bordered align-middle">
                    @include('components.tables._thead', ['thead' => [__('Arquivo'), __('Legenda'), __('Cliques'), __('Ações')]])
                    <tbody>
                        @forelse($registro->uploads as $upload)
                            <tr>
                                <td>{{ basename($upload->ds_arquivo) }}</td>
                                <td>
                                    <form method="post" action="{{ route('admin.licitacoesProcedimentos.uploads.update', [$registro->id, $upload->id] + request()->only(['ano', 'tipo_processo', 'tipo', 'status', 'nr_processo', 'page'])) }}" enctype="multipart/form-data" class="row g-2">
                                        @csrf
                                        @method('put')
                                        @foreach(['ano', 'tipo_processo', 'tipo', 'status', 'nr_processo', 'page'] as $filterKey)
                                            @if(request()->filled($filterKey))
                                                <input type="hidden" name="{{ $filterKey }}" value="{{ request($filterKey) }}">
                                            @endif
                                        @endforeach
                                        <div class="col-md-5">
                                            <input type="text" name="ds_legenda" maxlength="100" class="form-control form-control-sm" value="{{ $upload->ds_legenda }}">
                                        </div>
                                        <div class="col-md-4">
                                            <input type="file" name="arquivo" class="form-control form-control-sm" accept=".pdf,.mp4,application/pdf,video/mp4">
                                        </div>
                                        <div class="col-md-3 d-flex gap-1">
                                            <button type="submit" class="btn btn-sm btn-warning">{{ __('Salvar') }}</button>
                                            <a class="btn btn-sm btn-primary" href="{{ route('admin.licitacoesProcedimentos.uploads.download', [$registro->id, $upload->id]) }}">
                                                <i class="fa-solid fa-download"></i>
                                            </a>
                                        </div>
                                    </form>
                                </td>
                                <td>{{ $upload->qt_cliques }}</td>
                                <td class="text-center">
                                    <form class="d-inline" action="{{ route('admin.licitacoesProcedimentos.uploads.destroy', [$registro->id, $upload->id]) }}" method="post" onsubmit="return confirmDelete(this)">
                                        @csrf
                                        @method('delete')
                                        <input type="hidden" name="confirm" value="{{ __('* Este upload será removido e esta ação não poderá ser desfeita, deseja continuar?') }}">
                                        @foreach(['ano', 'tipo_processo', 'tipo', 'status', 'nr_processo', 'page'] as $filterKey)
                                            @if(request()->filled($filterKey))
                                                <input type="hidden" name="{{ $filterKey }}" value="{{ request($filterKey) }}">
                                            @endif
                                        @endforeach
                                        <button type="submit" class="btn btn-sm btn-danger">
                                            <i class="fa-solid fa-trash"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="4" class="text-center">{{ __('Nenhum arquivo vinculado.') }}</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
