<?php $__env->startSection('title', env('APP_NAME') . ' :: ' . __('Licitações e Procedimentos')); ?>

<?php $__env->startSection('content'); ?>
    <?php echo $__env->make('errors.messageFlash', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

    <?php
        use App\Models\LicitacoesProcedimentos\Processo;

        $filterParams = ['ano', 'tipo_processo', 'tipo', 'status', 'nr_processo', 'page'];
        $tipoProcessoFiltro = request('tipo_processo', '');
        $tipoFiltro = (string) request('tipo', '');
        $tiposLicitacaoJson = json_encode(
            collect(Processo::TIPOS_LICITACAO)->mapWithKeys(fn (string $label, int $codigo) => [$codigo => __($label)])->all(),
            JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE
        );
        $tiposProcedimentoJson = json_encode(
            collect(Processo::TIPOS_PROCEDIMENTO)->mapWithKeys(fn (string $label, int $codigo) => [$codigo => __($label)])->all(),
            JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE
        );
        $tiposFiltroAtuais = match ($tipoProcessoFiltro) {
            'procedimento' => Processo::TIPOS_PROCEDIMENTO,
            'licitacao' => Processo::TIPOS_LICITACAO,
            default => [],
        };
    ?>

    <div class="card shadow-sm">
        <?php $headLine = "<i class='fa-solid fa-scale-balanced'></i> " . __('Licitações e Procedimentos'); ?>
        <?php echo $__env->make('components.buttons._headLineButtonsBackAdd', [
            'headLine' => __($headLine),
            'routeBack' => 'admin.dashboard',
            'routeBackParams' => [],
            'nameBack' => __('Voltar'),
            'routeAdd' => 'admin.licitacoesProcedimentos.create',
            'routeAddParams' => request()->only($filterParams),
            'nameAdd' => __('Novo registro'),
            'permission' => 'GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS',
        ], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

        <div class="card-body">
            <form method="get" class="row g-2 mb-3 align-items-end" id="form-filtros-licitacoes-procedimentos">
                <div class="col-md-2">
                    <label class="form-label" for="filtro-ano"><?php echo e(__('Ano:')); ?></label>
                    <input type="number" class="form-control" name="ano" id="filtro-ano" value="<?php echo e(request('ano')); ?>" placeholder="<?php echo e(__('Ano')); ?>">
                </div>
                <div class="col-md-2">
                    <label class="form-label" for="lp-filtro-tipo-processo"><?php echo e(__('Tipo de processo:')); ?></label>
                    <select class="form-select" name="tipo_processo" id="lp-filtro-tipo-processo">
                        <option value=""><?php echo e(__('Todos')); ?></option>
                        <option value="licitacao" <?php if($tipoProcessoFiltro === 'licitacao'): echo 'selected'; endif; ?>><?php echo e(__('Licitação')); ?></option>
                        <option value="procedimento" <?php if($tipoProcessoFiltro === 'procedimento'): echo 'selected'; endif; ?>><?php echo e(__('Procedimento')); ?></option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label" for="lp-filtro-tipo"><?php echo e(__('Tipo:')); ?></label>
                    <select
                        class="form-select"
                        name="tipo"
                        id="lp-filtro-tipo"
                        data-tipos-licitacao="<?php echo e($tiposLicitacaoJson); ?>"
                        data-tipos-procedimento="<?php echo e($tiposProcedimentoJson); ?>"
                        data-tipo-selecionado="<?php echo e($tipoFiltro); ?>"
                        data-empty-label="<?php echo e(__('Todos')); ?>"
                        <?php if($tipoProcessoFiltro === ''): echo 'disabled'; endif; ?>
                    >
                        <option value=""><?php echo e(__('Todos')); ?></option>
                        <?php $__currentLoopData = $tiposFiltroAtuais; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $codigo => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($codigo); ?>" <?php if($tipoFiltro === (string) $codigo): echo 'selected'; endif; ?>><?php echo e(__($label)); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label" for="filtro-status"><?php echo e(__('Status:')); ?></label>
                    <select class="form-select" name="status" id="filtro-status">
                        <option value=""><?php echo e(__('Todos')); ?></option>
                        <option value="1" <?php if((string) request('status') === '1'): echo 'selected'; endif; ?>><?php echo e(__('Licitado/Em Andamento')); ?></option>
                        <option value="2" <?php if((string) request('status') === '2'): echo 'selected'; endif; ?>><?php echo e(__('Cancelado')); ?></option>
                        <option value="3" <?php if((string) request('status') === '3'): echo 'selected'; endif; ?>><?php echo e(__('Suspenso')); ?></option>
                        <option value="4" <?php if((string) request('status') === '4'): echo 'selected'; endif; ?>><?php echo e(__('Aguardando Licitação')); ?></option>
                        <option value="5" <?php if((string) request('status') === '5'): echo 'selected'; endif; ?>><?php echo e(__('Não Homologada')); ?></option>
                        <option value="6" <?php if((string) request('status') === '6'): echo 'selected'; endif; ?>><?php echo e(__('Homologada')); ?></option>
                        <option value="7" <?php if((string) request('status') === '7'): echo 'selected'; endif; ?>><?php echo e(__('Fracassado')); ?></option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label" for="filtro-nr-processo"><?php echo e(__('Nº do Processo / Licitação:')); ?></label>
                    <input
                        type="text"
                        class="form-control"
                        name="nr_processo"
                        id="filtro-nr-processo"
                        value="<?php echo e(request('nr_processo')); ?>"
                        placeholder="<?php echo e(__('Processo ou licitação')); ?>"
                    >
                </div>
                <div class="col-12 d-flex justify-content-end my-2 gap-2">
                    <a href="<?php echo e(route('admin.licitacoesProcedimentos.index')); ?>" class="btn btn-outline-secondary px-4"><?php echo e(__('Limpar')); ?></a>
                    <button class="btn btn-success px-4" type="submit"><?php echo e(__('Filtrar')); ?></button>
                </div>
            </form>

            <?php if (isset($component)) { $__componentOriginal0650541a119648223455610148c86d0b = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal0650541a119648223455610148c86d0b = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.tables._table','data' => ['thead' => ['#', __('Tipo de processo'), __('Procedimento'), __('Modalidade'), __('Nº da Licitação'), __('Nº Processo'), __('Status'), __('Data'), __('Uploads'), __('Ações')],'pagination' => $registros->links()]] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('tables._table'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['thead' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute(['#', __('Tipo de processo'), __('Procedimento'), __('Modalidade'), __('Nº da Licitação'), __('Nº Processo'), __('Status'), __('Data'), __('Uploads'), __('Ações')]),'pagination' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute($registros->links())]); ?>
                 <?php $__env->slot('body', null, []); ?> 
                    <?php $index = $registros->firstItem(); ?>
                    <?php $__empty_1 = true; $__currentLoopData = $registros; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $registro): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr>
                            <td><?php echo e($index); ?></td>
                            <td><?php echo e($registro->tipo_processo === 'licitacao' ? __('Licitação') : __('Procedimento')); ?></td>
                            <td><?php echo e($registro->procedimento_label ?: '-'); ?></td>
                            <td><?php echo e($registro->modalidade_label ?: '-'); ?></td>
                            <td><?php echo e($registro->nr_licitacao); ?></td>
                            <td><?php echo e($registro->nr_processo); ?></td>
                            <td><?php echo e($registro->cd_status_label ?? $registro->cd_status); ?></td>
                            <td><?php echo e(optional($registro->dt_data)->format('d/m/Y')); ?></td>
                            <td>
                                <div class="d-flex justify-content-center gap-2 flex-wrap">
                                    <?php $__empty_2 = true; $__currentLoopData = $registro->uploads; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $upload): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_2 = false; ?>
                                        <?php
                                            $rawPath = trim((string) $upload->ds_arquivo);
                                            $normalizedPath = ltrim(str_replace('\\', '/', $rawPath), '/');
                                            $fileName = basename($normalizedPath);

                                            if (preg_match('#^https?://#i', $rawPath)) {
                                                $uploadHref = $rawPath;
                                            } elseif (
                                                str_starts_with($normalizedPath, 'licitacoes_procedimentos/')
                                                || str_starts_with($normalizedPath, 'storage/licitacoes_procedimentos/')
                                            ) {
                                                $uploadHref = str_starts_with($normalizedPath, 'storage/')
                                                    ? asset($normalizedPath)
                                                    : asset('storage/' . $normalizedPath);
                                            } else {
                                                $uploadHref = 'https://angra.rj.gov.br/SAPO/_licitacao/adm/upload/' . rawurlencode($fileName);
                                            }
                                        ?>
                                        <a
                                            href="<?php echo e($uploadHref); ?>"
                                            title="<?php echo e($upload->ds_legenda ?: basename((string) $upload->ds_arquivo)); ?>"
                                            class="text-danger" target="_blank"
                                        >
                                            <i class="fa-solid fa-file-pdf fa-lg"></i>
                                        </a>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_2): ?>
                                        <span class="text-muted">-</span>
                                    <?php endif; ?>
                                </div>
                            </td>
                            <td>
                                <div class="d-flex justify-content-center gap-2">
                                    <?php if(auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS')['editar']): ?>
                                        <a class="btn btn-sm btn-warning" href="<?php echo e(route('admin.licitacoesProcedimentos.edit', [$registro->id] + request()->only($filterParams))); ?>">
                                            <i class="fa-solid fa-pen-to-square"></i>
                                        </a>
                                        <a class="btn btn-sm btn-primary" href="<?php echo e(route('admin.licitacoesProcedimentos.uploads', [$registro->id] + request()->only($filterParams))); ?>" title="<?php echo e(__('Listar arquivos')); ?>">
                                            <i class="fa-solid fa-file-lines"></i>
                                        </a>
                                        <a class="btn btn-sm btn-info text-white" href="<?php echo e(route('admin.licitacoesProcedimentos.homologacao', [$registro->id] + request()->only($filterParams))); ?>" title="<?php echo e(__('Homologação')); ?>">
                                            <i class="fa-solid fa-stamp"></i>
                                        </a>
                                    <?php endif; ?>
                                    <?php if(auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_LICITACOES_E_PROCEDIMENTOS')['excluir']): ?>
                                        <form class="d-inline" action="<?php echo e(route('admin.licitacoesProcedimentos.destroy', $registro->id)); ?>" method="post" onsubmit="return confirmDelete(this)">
                                            <?php echo csrf_field(); ?>
                                            <?php echo method_field('delete'); ?>
                                            <?php $__currentLoopData = request()->only($filterParams); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                <?php if($value !== null && $value !== ''): ?>
                                                    <input type="hidden" name="<?php echo e($key); ?>" value="<?php echo e($value); ?>">
                                                <?php endif; ?>
                                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                            <input type="hidden" name="confirm" value="<?php echo e(__('* Este registro será excluído e esta ação não poderá ser desfeita, deseja continuar?')); ?>">
                                            <button type="submit" class="btn btn-sm btn-danger">
                                                <i class="fa-solid fa-trash"></i>
                                            </button>
                                        </form>
                                    <?php endif; ?>
                                </div>
                            </td>
                        </tr>
                        <?php $index++; ?>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr>
                            <td colspan="10" class="text-center"><?php echo e(__('Nenhum registro encontrado.')); ?></td>
                        </tr>
                    <?php endif; ?>
                 <?php $__env->endSlot(); ?>
             <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal0650541a119648223455610148c86d0b)): ?>
<?php $attributes = $__attributesOriginal0650541a119648223455610148c86d0b; ?>
<?php unset($__attributesOriginal0650541a119648223455610148c86d0b); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal0650541a119648223455610148c86d0b)): ?>
<?php $component = $__componentOriginal0650541a119648223455610148c86d0b; ?>
<?php unset($__componentOriginal0650541a119648223455610148c86d0b); ?>
<?php endif; ?>
        </div>
    </div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('scripts'); ?>
    <script src="<?php echo e(asset('pmar/js/admin/licitacoes-procedimentos.js')); ?>?v=<?php echo e(@filemtime(public_path('pmar/js/admin/licitacoes-procedimentos.js')) ?: time()); ?>"></script>
<?php $__env->stopPush(); ?>

<?php echo $__env->make('layouts.admin.theme', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /var/www/html/portal/resources/views/admin/licitacoesProcedimentos/index.blade.php ENDPATH**/ ?>