<?php $__env->startSection('title', env('APP_NAME') . ' :: ' . __('PPPs e Convênios')); ?>

<?php $__env->startPush('after-styles'); ?>
<style>
    /* Card principal */
    .card-prefeitura {
        border: none;
        border-radius: 12px;
    }

    /* Abas modernas */
    .nav-tabs .nav-link {
        border: none;
        color: #495057;
        font-weight: 500;
        padding: 10px 20px;
        border-radius: 8px 8px 0 0;
        transition: all 0.2s ease;
    }

    .nav-tabs .nav-link.active {
        background-color: #0d6efd;
        color: #fff !important;
        font-weight: 600;
    }

    .nav-tabs .nav-link:hover {
        background-color: #e9ecef;
    }

    /* Campo de pesquisa */
    .search-box {
        max-width: 420px;
    }

    .search-box input {
        border-radius: 8px 0 0 8px;
    }

    .search-box button {
        border-radius: 0 8px 8px 0;
    }

    /* Cabeçalho interno */
    .section-header {
        border-left: 4px solid #0d6efd;
        padding-left: 12px;
    }

    /* Tabela institucional */
    .table thead {
        background-color: #f8f9fa;
        font-size: 0.8rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    .table-hover tbody tr:hover {
        background-color: #f1f5ff;
    }

    /* Botões */
    .btn-group .btn {
        border-radius: 6px !important;
    }
</style>
<?php $__env->stopPush(); ?>

<?php $__env->startSection('content'); ?>

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

<div class="card card-prefeitura shadow-sm">

    <?php $headLine = "<i class='fa-solid fa-handshake'></i> " . __('PPPs e Convênios'); ?>
    <?php echo $__env->make('components.buttons._headLineButtonsBackAdd', [
        'headLine' => $headLine,
        'routeBack' => 'admin.dashboard',
        'nameBack' => __('Voltar'),
        'routeAdd' => 'admin.ppps.create',
        'nameAdd' => __('Nova PPP'),
        'routeOther' => 'admin.convenios.create',
        'nameOther' => __('Novo Convênio'),
        'permission' => 'GERENCIADOR_DE_PPPS',
    ], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>

    <div class="card-body pt-4">

        <!-- ABAS -->
        <ul class="nav nav-tabs mb-4" id="pppTab" role="tablist">
            <li class="nav-item">
                <button class="nav-link <?php echo e($activeTab === 'ppps' ? 'active' : ''); ?>"
                        data-bs-toggle="tab"
                        data-bs-target="#ppps-pane"
                        type="button">
                    <i class="fa-solid fa-handshake me-1"></i> <?php echo e(__('PPPs')); ?>

                </button>
            </li>

            <li class="nav-item">
                <button class="nav-link <?php echo e($activeTab === 'convenios' ? 'active' : ''); ?>"
                        data-bs-toggle="tab"
                        data-bs-target="#convenios-pane"
                        type="button">
                    <i class="fa-solid fa-file-contract me-1"></i> <?php echo e(__('Convênios')); ?>

                </button>
            </li>
        </ul>

        <div class="tab-content">

            <!-- ================= PPPs ================= -->
            <div class="tab-pane fade <?php echo e($activeTab === 'ppps' ? 'show active' : ''); ?>" id="ppps-pane">

                <div class="d-flex justify-content-between align-items-center mb-3">
                    <div class="section-header">
                        <h5 class="mb-0 fw-semibold text-primary">
                            <i class="fa-solid fa-handshake me-2"></i>
                            PPPs Cadastradas
                        </h5>
                    </div>

                    <div class="d-flex align-items-center gap-2">
                        <a href="<?php echo e(route('admin.ppps.exportAll')); ?>" target="_blank" class="btn btn-outline-danger" title="Exportar Relatório Geral de PPPs (PDF)">
                            <i class="fa-solid fa-file-pdf"></i> Relatório Geral
                        </a>
                        <form class="d-flex search-box" method="get">
                            <input type="hidden" name="tab" value="ppps">
                            <input type="hidden" name="page_ppps" value="1">
                            <input type="hidden" name="q_convenios" value="<?php echo e(request('q_convenios')); ?>">
                            <input type="hidden" name="page_convenios" value="<?php echo e(request('page_convenios', 1)); ?>">

                            <input type="text"
                                class="form-control"
                                name="q_ppps"
                                placeholder="<?php echo e(__('Pesquisar por projeto ou empresa...')); ?>"
                                value="<?php echo e(request('q_ppps')); ?>">

                            <button class="btn btn-primary" type="submit">
                                <i class="fa-solid fa-magnifying-glass"></i>
                            </button>
                        </form>
                    </div>
                </div>

                <div class="table-responsive">
                    <table class="table table-hover table-bordered align-middle text-center">
                        <?php echo $__env->make('components.tables._thead', [
                            'thead' => [__('Projeto'), __('Empresa'), __('Status'), __('Ações')]
                        ], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                        <tbody>
                            <?php $index = 0; ?>
                            <?php $__empty_1 = true; $__currentLoopData = $ppps; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ppp): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td class="text-start"><?php echo e(Str::limit($ppp->projeto, 80)); ?></td>
                                    <td class="text-start"><?php echo e(Str::limit($ppp->empresa, 50)); ?></td>

                                    <td>
                                        <?php echo $__env->make('components.buttons._status', [
                                            'index' => $index,
                                            'route' => 'admin.ppps.status',
                                            'id' => $ppp->id,
                                            'status' => $ppp->status,
                                            'permission' => 'GERENCIADOR_DE_PPPS',
                                        ], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                                    </td>

                                    <td class="text-nowrap">
                                        <div class="btn-group">
                                            <a href="<?php echo e(route('admin.ppps.edit', $ppp->id)); ?>"
                                               class="btn btn-sm btn-warning"
                                               title="<?php echo e(__('Editar')); ?>">
                                                <i class="fa-solid fa-pen-to-square"></i>
                                            </a>

                                            <?php if(auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_PPPS')['excluir']): ?>
                                                <form class="d-inline"
                                                        action="<?php echo e(route('admin.ppps.destroy', $ppp->id)); ?>"
                                                        method="post">
                                                        <?php echo csrf_field(); ?>
                                                        <?php echo method_field('delete'); ?>

                                                        <button type="button"
                                                            onclick="confirmDeletePpps(this)"
                                                            class="btn btn-sm btn-danger"
                                                            title="<?php echo e(__('Excluir')); ?>">
                                                        <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="4" class="text-muted fst-italic">
                                        <?php echo e(__('Nenhuma PPP encontrada.')); ?>

                                    </td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <div class="d-flex justify-content-center mt-4">
                    <?php echo $ppps->appends([
                        'tab' => 'ppps',
                        'q_ppps' => request('q_ppps'),
                        'q_convenios' => request('q_convenios'),
                        'page_convenios' => request('page_convenios', 1)
                    ])->links(); ?>

                </div>
            </div>

            <!-- ================= CONVÊNIOS ================= -->
            <div class="tab-pane fade <?php echo e($activeTab === 'convenios' ? 'show active' : ''); ?>" id="convenios-pane">

                <div class="d-flex justify-content-between align-items-center mb-3">
                    <div class="section-header">
                        <h5 class="mb-0 fw-semibold text-primary">
                            <i class="fa-solid fa-file-contract me-2"></i>
                            Convênios Cadastrados
                        </h5>
                    </div>

                    <div class="d-flex align-items-center gap-2">
                        <a href="<?php echo e(route('admin.convenios.exportAll')); ?>" target="_blank" class="btn btn-outline-danger" title="Exportar Relatório Geral de Convênios (PDF)">
                            <i class="fa-solid fa-file-pdf"></i> Relatório Geral
                        </a>
                        <form class="d-flex search-box" method="get">
                            <input type="hidden" name="tab" value="convenios">
                            <input type="hidden" name="page_convenios" value="1">
                            <input type="hidden" name="q_ppps" value="<?php echo e(request('q_ppps')); ?>">
                            <input type="hidden" name="page_ppps" value="<?php echo e(request('page_ppps', 1)); ?>">

                            <input type="text"
                                class="form-control"
                                name="q_convenios"
                                placeholder="<?php echo e(__('Pesquisar por objeto, instrumento, SEI, executor ou concedente...')); ?>"
                                value="<?php echo e(request('q_convenios')); ?>">

                            <button class="btn btn-primary" type="submit">
                                <i class="fa-solid fa-magnifying-glass"></i>
                            </button>
                        </form>
                    </div>
                </div>

                <div class="table-responsive">
                    <table class="table table-hover table-bordered align-middle text-center">
                        <?php echo $__env->make('components.tables._thead', [
                            'thead' => [__('Objeto'), __('Instrumento'), __('Executor'), __('Concedente'), __('Alertas'), __('Status'), __('Ações')]
                        ], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                        <tbody>
                            <?php $index = 1000; ?>
                            <?php $__empty_1 = true; $__currentLoopData = $convenios; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $convenio): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <?php
                                    $alertasConvenio = \App\Http\Resources\ConvenioResource::alertasAtivos($convenio);
                                ?>
                                <tr>
                                    <td class="text-start"><?php echo e(Str::limit($convenio->objeto, 80)); ?></td>
                                    <td class="text-start"><?php echo e($convenio->numero_instrumento ?: '—'); ?></td>
                                    <td class="text-start"><?php echo e(Str::limit($convenio->executor_ordenador, 30)); ?></td>
                                    <td class="text-start"><?php echo e(Str::limit($convenio->concedente, 30)); ?></td>
                                    <td>
                                        <?php if(count($alertasConvenio) > 0): ?>
                                            <div class="d-flex justify-content-center">
                                                <button type="button"
                                                        class="btn btn-sm btn-warning"
                                                        data-bs-toggle="modal"
                                                        data-bs-target="#modalAlertasConvenio-<?php echo e($convenio->id); ?>"
                                                        aria-label="<?php echo e(__('Ver alertas do convênio')); ?>">
                                                    <i class="fa-solid fa-bell me-1" aria-hidden="true"></i>
                                                    <?php echo e(__('Alertas')); ?> (<?php echo e(count($alertasConvenio)); ?>)
                                                </button>
                                            </div>

                                            <div class="modal fade"
                                                 id="modalAlertasConvenio-<?php echo e($convenio->id); ?>"
                                                 tabindex="-1"
                                                 aria-labelledby="modalAlertasConvenioLabel-<?php echo e($convenio->id); ?>"
                                                 aria-hidden="true">
                                                <div class="modal-dialog modal-dialog-centered">
                                                    <div class="modal-content">
                                                        <div class="modal-header">
                                                            <h5 class="modal-title" id="modalAlertasConvenioLabel-<?php echo e($convenio->id); ?>">
                                                                <?php echo e(__('Alertas do Convênio')); ?>

                                                            </h5>
                                                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<?php echo e(__('Fechar')); ?>"></button>
                                                        </div>
                                                        <div class="modal-body text-start">
                                                            <p class="mb-2">
                                                                <strong><?php echo e(__('Instrumento:')); ?></strong>
                                                                <?php echo e($convenio->numero_instrumento ?: __('Não informado')); ?>

                                                            </p>

                                                            <ul class="list-group">
                                                                <?php $__currentLoopData = $alertasConvenio; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $alertaConvenio): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                                                    <li class="list-group-item d-flex align-items-start">
                                                                        <i class="fa-solid fa-triangle-exclamation text-warning mt-1 me-2" aria-hidden="true"></i>
                                                                        <span><?php echo e($alertaConvenio['mensagem']); ?></span>
                                                                    </li>
                                                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                                            </ul>
                                                        </div>
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                                                                <?php echo e(__('Fechar')); ?>

                                                            </button>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        <?php else: ?>
                                            <span class="text-muted"><?php echo e(__('—')); ?></span>
                                        <?php endif; ?>
                                    </td>

                                    <td>
                                        <?php echo $__env->make('components.buttons._status', [
                                            'index' => $index,
                                            'route' => 'admin.convenios.status',
                                            'id' => $convenio->id,
                                            'status' => $convenio->status,
                                            'permission' => 'GERENCIADOR_DE_PPPS',
                                        ], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
                                    </td>

                                    <td class="text-nowrap">
                                        <div class="btn-group">
                                            <a href="<?php echo e(route('admin.convenios.edit', $convenio->id)); ?>"
                                               class="btn btn-sm btn-warning">
                                                <i class="fa-solid fa-pen-to-square"></i>
                                            </a>

                                            <?php if(auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_PPPS')['excluir']): ?>
                                                <form class="d-inline"
                                                      action="<?php echo e(route('admin.convenios.destroy', $convenio->id)); ?>"
                                                      method="post">
                                                    <?php echo csrf_field(); ?>
                                                    <?php echo method_field('delete'); ?>

                                                    <button type="button"
                                                            onclick="confirmDeleteConvenios(this)"
                                                            class="btn btn-sm btn-danger"
                                                            title="<?php echo e(__('Excluir')); ?>">
                                                        <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="7" class="text-muted fst-italic">
                                        <?php echo e(__('Nenhum convênio encontrado.')); ?>

                                    </td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>

                <div class="d-flex justify-content-center mt-4">
                    <?php echo $convenios->appends([
                        'tab' => 'convenios',
                        'q_convenios' => request('q_convenios'),
                        'q_ppps' => request('q_ppps'),
                        'page_ppps' => request('page_ppps', 1)
                    ])->links(); ?>

                </div>
            </div>

        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script>
    function confirmDeletePpps(button) {
        Swal.fire({
            title: 'Tem certeza?',
            text: "Essa ação não poderá ser desfeita!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#21409A',
            cancelButtonColor: '#6c757d',
            confirmButtonText: 'Sim, excluir!',
            cancelButtonText: 'Cancelar'
        }).then((result) => {
            if (result.isConfirmed) {
                button.closest('form').submit();
            }
        });
    }
</script>

<script>
    function confirmDeleteConvenios(button) {
        Swal.fire({
            title: 'Tem certeza?',
            text: "Essa ação não poderá ser desfeita!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#21409A',
            cancelButtonColor: '#6c757d',
            confirmButtonText: 'Sim, excluir!',
            cancelButtonText: 'Cancelar'
        }).then((result) => {
            if (result.isConfirmed) {
                button.closest('form').submit();
            }
        });
    }
</script>
<?php $__env->stopSection(); ?>
<?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/ppps/index.blade.php ENDPATH**/ ?>