<?php
    use App\Enums\MobiangraAlertaCategoria;
?>

<div class="tab-pane fade <?php echo e($tabAtual === 'alertas' ? 'show active' : ''); ?>"
     id="tab-alertas" role="tabpanel" aria-labelledby="tab-alertas-btn">

    <div class="d-flex flex-wrap align-items-center justify-content-between gap-2 mb-3">
        <h5 class="mb-0">
            <i class="fa-solid fa-bell me-1"></i><?php echo e(__('Alertas e avisos')); ?>

            <?php if($alertasAdmin): ?>
                <span class="badge bg-secondary ms-1"><?php echo e($alertasAdmin->total()); ?></span>
            <?php endif; ?>
        </h5>
        <div class="d-flex gap-2 flex-wrap">
            <a href="<?php echo e(route('mobiangra.alertas')); ?>" target="_blank" class="btn btn-sm btn-outline-primary">
                <i class="fa-solid fa-eye me-1"></i><?php echo e(__('Ver no site')); ?>

            </a>
            <?php if(auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_MOBIANGRA')['adicionar'] ?? false): ?>
                <button type="button" class="btn btn-sm btn-success" data-bs-toggle="modal"
                    data-bs-target="#modal-alerta-form" data-alerta-mode="create">
                    <i class="fa-solid fa-plus me-1"></i><?php echo e(__('Novo alerta')); ?>

                </button>
            <?php endif; ?>
        </div>
    </div>

    <?php if($alertasAdmin && $alertasAdmin->isNotEmpty()): ?>
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th scope="col" style="width:120px"><?php echo e(__('Categoria')); ?></th>
                        <th scope="col"><?php echo e(__('Título')); ?></th>
                        <th scope="col" style="width:170px"><?php echo e(__('Publicar em')); ?></th>
                        <th scope="col" style="width:170px"><?php echo e(__('Exibir até')); ?></th>
                        <th scope="col" class="text-center" style="width:100px"><?php echo e(__('Status')); ?></th>
                        <th scope="col" class="text-center" style="width:130px"><?php echo e(__('Ações')); ?></th>
                    </tr>
                </thead>
                <tbody>
                    <?php $__currentLoopData = $alertasAdmin; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $alerta): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <tr>
                            <td>
                                <span class="badge bg-light text-dark border">
                                    <?php echo e($alerta->categoria->label()); ?>

                                </span>
                            </td>
                            <td>
                                <span class="d-block fw-semibold"><?php echo e($alerta->titulo); ?></span>
                                <?php if($alerta->mensagem): ?>
                                    <small class="d-block text-muted mt-1" style="white-space: pre-line;"><?php echo e($alerta->mensagem); ?></small>
                                <?php endif; ?>
                            </td>
                            <td class="text-nowrap"><?php echo e($alerta->data_formatada); ?></td>
                            <td class="text-nowrap">
                                <?php if($alerta->exibir_ate): ?>
                                    <?php echo e($alerta->exibir_ate_formatada); ?>

                                <?php else: ?>
                                    <span class="text-muted"><?php echo e(__('Sem prazo')); ?></span>
                                <?php endif; ?>
                            </td>
                            <td class="text-center">
                                <?php if(auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_MOBIANGRA')['editar'] ?? false): ?>
                                    <form action="<?php echo e(route('admin.mobiangra.alertas.status', $alerta)); ?>" method="post" class="d-inline">
                                        <?php echo csrf_field(); ?>
                                        <input type="hidden" name="tab" value="alertas">
                                        <?php if($alertasAdmin->currentPage() > 1): ?>
                                            <input type="hidden" name="page" value="<?php echo e($alertasAdmin->currentPage()); ?>">
                                        <?php endif; ?>
                                        <button type="submit"
                                            class="btn btn-sm <?php echo e($alerta->publicado ? 'btn-success' : 'btn-outline-secondary'); ?>"
                                            title="<?php echo e($alerta->publicado ? __('Publicado') : __('Rascunho')); ?>">
                                            <i class="fa-solid <?php echo e($alerta->publicado ? 'fa-eye' : 'fa-eye-slash'); ?>"></i>
                                        </button>
                                    </form>
                                <?php else: ?>
                                    <span class="badge <?php echo e($alerta->publicado ? 'bg-success' : 'bg-secondary'); ?>">
                                        <?php echo e($alerta->publicado ? __('Publicado') : __('Rascunho')); ?>

                                    </span>
                                <?php endif; ?>
                            </td>
                            <td class="text-center text-nowrap">
                                <?php if(auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_MOBIANGRA')['editar'] ?? false): ?>
                                    <button type="button" class="btn btn-sm btn-outline-primary"
                                        data-bs-toggle="modal" data-bs-target="#modal-alerta-form"
                                        data-alerta-mode="edit"
                                        data-alerta-id="<?php echo e($alerta->id); ?>"
                                        data-alerta-categoria="<?php echo e($alerta->categoria->value); ?>"
                                        data-alerta-titulo="<?php echo e($alerta->titulo); ?>"
                                        data-alerta-mensagem-b64="<?php echo e(base64_encode($alerta->mensagem ?? '')); ?>"
                                        data-alerta-publicado-em="<?php echo e($alerta->publicado_em?->format('Y-m-d\TH:i')); ?>"
                                        data-alerta-exibir-ate="<?php echo e($alerta->exibir_ate?->format('Y-m-d\TH:i')); ?>"
                                        data-alerta-publicado="<?php echo e($alerta->publicado ? '1' : '0'); ?>"
                                        title="<?php echo e(__('Editar')); ?>">
                                        <i class="fa-solid fa-pen"></i>
                                    </button>
                                <?php endif; ?>
                                <?php if(auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_MOBIANGRA')['excluir'] ?? false): ?>
                                    <form action="<?php echo e(route('admin.mobiangra.alertas.destroy', $alerta)); ?>" method="post"
                                        class="d-inline"
                                        onsubmit="return confirm(<?php echo json_encode(__('Este alerta será excluído permanentemente. Deseja continuar?'), 15, 512) ?>)">
                                        <?php echo csrf_field(); ?>
                                        <?php echo method_field('delete'); ?>
                                        <input type="hidden" name="tab" value="alertas">
                                        <?php if($alertasAdmin->currentPage() > 1): ?>
                                            <input type="hidden" name="page" value="<?php echo e($alertasAdmin->currentPage()); ?>">
                                        <?php endif; ?>
                                        <button type="submit" class="btn btn-sm btn-outline-danger" title="<?php echo e(__('Excluir')); ?>">
                                            <i class="fa-solid fa-trash"></i>
                                        </button>
                                    </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </tbody>
            </table>
        </div>

        <div class="mt-3">
            <?php echo e($alertasAdmin->appends(['tab' => 'alertas'])->links()); ?>

        </div>
    <?php else: ?>
        <div class="alert alert-info mb-0">
            <i class="fa-solid fa-circle-info me-1"></i>
            <?php echo e(__('Nenhum alerta cadastrado. Clique em "Novo alerta" para publicar o primeiro aviso.')); ?>

        </div>
    <?php endif; ?>
</div>

<?php if(auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_MOBIANGRA')['adicionar'] ?? false
    || auth()->user()->getPermissaoDetalhes('GERENCIADOR_DE_MOBIANGRA')['editar'] ?? false): ?>
    <div class="modal fade" id="modal-alerta-form" tabindex="-1" aria-labelledby="modal-alerta-form-label" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <form id="form-alerta" method="post" action="<?php echo e(route('admin.mobiangra.alertas.store')); ?>">
                    <?php echo csrf_field(); ?>
                    <input type="hidden" name="_method" id="alerta-form-method" value="POST">
                    <input type="hidden" name="tab" value="alertas">
                    <?php if($alertasAdmin && $alertasAdmin->currentPage() > 1): ?>
                        <input type="hidden" name="page" value="<?php echo e($alertasAdmin->currentPage()); ?>">
                    <?php endif; ?>

                    <div class="modal-header">
                        <h5 class="modal-title" id="modal-alerta-form-label"><?php echo e(__('Novo alerta')); ?></h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<?php echo e(__('Fechar')); ?>"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="alerta_categoria" class="form-label fw-semibold"><?php echo e(__('Categoria')); ?></label>
                            <select name="categoria" id="alerta_categoria" class="form-select <?php $__errorArgs = ['categoria'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>" required>
                                <option value=""><?php echo e(__('Selecione…')); ?></option>
                                <?php $__currentLoopData = MobiangraAlertaCategoria::cases(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $categoria): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($categoria->value); ?>" <?php if(old('categoria') === $categoria->value): echo 'selected'; endif; ?>>
                                        <?php echo e($categoria->label()); ?>

                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </select>
                            <?php $__errorArgs = ['categoria'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="mb-3">
                            <label for="alerta_titulo" class="form-label fw-semibold"><?php echo e(__('Título')); ?></label>
                            <input type="text" name="titulo" id="alerta_titulo" maxlength="150"
                                class="form-control <?php $__errorArgs = ['titulo'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                value="<?php echo e(old('titulo')); ?>" required
                                placeholder="<?php echo e(__('Resumo curto do alerta (ex.: Alteração na Linha 020)')); ?>">
                            <div class="form-text"><?php echo e(__('Máximo de 150 caracteres. Use o campo Mensagem para o texto completo.')); ?></div>
                            <?php $__errorArgs = ['titulo'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="mb-3">
                            <label for="alerta_mensagem" class="form-label fw-semibold"><?php echo e(__('Mensagem')); ?></label>
                            <textarea name="mensagem" id="alerta_mensagem" rows="5" maxlength="5000"
                                class="form-control <?php $__errorArgs = ['mensagem'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                required placeholder="<?php echo e(__('Descreva o alerta com todos os detalhes para o cidadão.')); ?>"><?php echo e(old('mensagem')); ?></textarea>
                            <?php $__errorArgs = ['mensagem'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <hr class="my-3">

                        <h6 class="fw-semibold mb-3"><?php echo e(__('Programação de exibição')); ?></h6>

                        <div class="mb-3">
                            <label for="alerta_publicado_em" class="form-label fw-semibold"><?php echo e(__('Publicar em')); ?></label>
                            <input type="datetime-local" name="publicado_em" id="alerta_publicado_em"
                                class="form-control <?php $__errorArgs = ['publicado_em'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                value="<?php echo e(old('publicado_em', now()->format('Y-m-d\TH:i'))); ?>" required>
                            <div class="form-text"><?php echo e(__('Data e hora em que o alerta ficará visível no portal.')); ?></div>
                            <?php $__errorArgs = ['publicado_em'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="mb-3">
                            <label for="alerta_exibir_duracao" class="form-label fw-semibold"><?php echo e(__('Tempo de exibição')); ?></label>
                            <select id="alerta_exibir_duracao" class="form-select">
                                <option value="1"><?php echo e(__('1 dia')); ?></option>
                                <option value="3"><?php echo e(__('3 dias')); ?></option>
                                <option value="7" selected><?php echo e(__('7 dias')); ?></option>
                                <option value="15"><?php echo e(__('15 dias')); ?></option>
                                <option value="30"><?php echo e(__('30 dias')); ?></option>
                                <option value="none"><?php echo e(__('Sem prazo definido')); ?></option>
                                <option value="custom"><?php echo e(__('Personalizado')); ?></option>
                            </select>
                            <div class="form-text"><?php echo e(__('Por quanto tempo o alerta permanecerá visível após a publicação.')); ?></div>
                        </div>

                        <div class="mb-3 d-none" id="alerta_exibir_ate_wrap">
                            <label for="alerta_exibir_ate" class="form-label fw-semibold"><?php echo e(__('Exibir até')); ?></label>
                            <input type="datetime-local" name="exibir_ate" id="alerta_exibir_ate"
                                class="form-control <?php $__errorArgs = ['exibir_ate'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?> is-invalid <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>"
                                value="<?php echo e(old('exibir_ate')); ?>">
                            <?php $__errorArgs = ['exibir_ate'];
$__bag = $errors->getBag($__errorArgs[1] ?? 'default');
if ($__bag->has($__errorArgs[0])) :
if (isset($message)) { $__messageOriginal = $message; }
$message = $__bag->first($__errorArgs[0]); ?>
                                <div class="invalid-feedback"><?php echo e($message); ?></div>
                            <?php unset($message);
if (isset($__messageOriginal)) { $message = $__messageOriginal; }
endif;
unset($__errorArgs, $__bag); ?>
                        </div>

                        <div class="form-check">
                            <input type="hidden" name="publicado" value="0">
                            <input type="checkbox" name="publicado" value="1" id="alerta_publicado"
                                class="form-check-input" <?php if(old('publicado', '1') == '1' || old('publicado') === true): echo 'checked'; endif; ?>>
                            <label for="alerta_publicado" class="form-check-label"><?php echo e(__('Publicar no site')); ?></label>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><?php echo e(__('Cancelar')); ?></button>
                        <button type="submit" class="btn btn-primary">
                            <i class="fa-solid fa-floppy-disk me-1"></i><?php echo e(__('Salvar')); ?>

                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
<?php endif; ?>
<?php /**PATH /var/www/html/portal/resources/views/admin/mobiangra/alertas/_tab.blade.php ENDPATH**/ ?>