<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="content-header">
    <h1><i class="fa fa-newspaper-o"></i> Diário Oficial / PJe Comunica
        <?php if(!empty($nao_lidas) && $nao_lidas > 0): ?>
        <span class="badge" style="background:#dd4b39"><?php echo $nao_lidas?> não lida(s)</span>
        <?php endif; ?>
    </h1>
    <ol class="breadcrumb"><li class="active">Diário Oficial</li></ol>
</section>
<section class="content">

<?php if($this->session->flashdata('message')): ?>
<div class="alert alert-success alert-dismissible"><button type="button" class="close" data-dismiss="alert">&times;</button><?php echo $this->session->flashdata('message')?></div>
<?php endif; ?>
<?php if($this->session->flashdata('error')): ?>
<div class="alert alert-danger alert-dismissible"><button type="button" class="close" data-dismiss="alert">&times;</button><?php echo $this->session->flashdata('error')?></div>
<?php endif; ?>

<div class="row">
    <div class="col-md-3">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">Advogados</h3>
                <div class="box-tools pull-right">
                    <a href="<?php echo site_url('admin/diario_oficial/add_advogado')?>" class="btn btn-xs btn-success"><i class="fa fa-plus"></i></a>
                </div>
            </div>
            <div class="box-body no-padding">
                <ul class="nav nav-pills nav-stacked">
                    <li class="<?php echo !$advogado_sel?'active':''?>">
                        <a href="<?php echo site_url('admin/diario_oficial')?>">Todos <span class="badge pull-right"><?php echo $nao_lidas??0?></span></a>
                    </li>
                    <?php foreach($advogados as $adv): ?>
                    <li class="<?php echo $advogado_sel==$adv->id?'active':''?>">
                        <a href="<?php echo site_url('admin/diario_oficial?advogado_id='.$adv->id)?>">
                            <i class="fa fa-user"></i> <?php echo htmlspecialchars($adv->nome)?>
                            <br><small class="text-muted">OAB <?php echo $adv->oab?>/<?php echo $adv->uf_oab?></small>
                        </a>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>

        <div class="box box-default">
            <div class="box-header"><h3 class="box-title">Ações</h3></div>
            <div class="box-body">
                <a href="<?php echo site_url('admin/diario_oficial/importar_manual')?>" class="btn btn-warning btn-block btn-sm" style="margin-bottom:5px">
                    <i class="fa fa-upload"></i> Importar JSON Manual
                </a>
                <a href="<?php echo site_url('admin/diario_oficial/config_make')?>" class="btn btn-info btn-block btn-sm" style="margin-bottom:5px">
                    <i class="fa fa-cog"></i> Configurar Make.com
                </a>
                <a href="<?php echo site_url('admin/diario_oficial/marcar_todas_lidas')?>" class="btn btn-default btn-block btn-sm">
                    <i class="fa fa-check"></i> Marcar Todas Lidas
                </a>
            </div>
        </div>

        <div class="box box-warning">
            <div class="box-header"><h3 class="box-title"><i class="fa fa-info-circle"></i> Status API</h3></div>
            <div class="box-body" style="font-size:12px">
                <p><i class="fa fa-times text-danger"></i> API PJe direta: <strong>Bloqueada</strong></p>
                <p><i class="fa fa-check text-warning"></i> Make.com webhook: <strong>Disponível</strong></p>
                <p><i class="fa fa-check text-success"></i> Importação manual: <strong>Ativa</strong></p>
            </div>
        </div>
    </div>

    <div class="col-md-9">
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title"><i class="fa fa-list"></i> <?php echo count($publicacoes)?> publicação(ões)</h3>
            </div>
            <div class="box-body no-padding">
                <?php if(empty($publicacoes)): ?>
                <div class="callout callout-info" style="margin:15px">
                    <h4><i class="fa fa-info-circle"></i> Nenhuma publicação encontrada</h4>
                    <p>Configure o <strong>Make.com</strong> para receber publicações automáticas, ou use a <strong>Importação Manual</strong>.</p>
                    <a href="<?php echo site_url('admin/diario_oficial/config_make')?>" class="btn btn-info btn-sm">
                        <i class="fa fa-cog"></i> Configurar Make.com
                    </a>
                    &nbsp;
                    <a href="<?php echo site_url('admin/diario_oficial/importar_manual')?>" class="btn btn-warning btn-sm">
                        <i class="fa fa-upload"></i> Importar Manual
                    </a>
                </div>
                <?php else: ?>
                <table class="table table-striped table-hover" id="tbl-pub">
                    <thead>
                        <tr>
                            <th width="20"></th>
                            <th>Data</th>
                            <th>Tipo</th>
                            <th>Processo</th>
                            <th>Tribunal</th>
                            <th>Advogado</th>
                            <th>Ações</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach($publicacoes as $pub): ?>
                    <tr class="<?php echo empty($pub->lida)?'warning':''?>">
                        <td><?php echo empty($pub->lida)?'<span class="badge" style="background:#dd4b39">N</span>':''?></td>
                        <td><?php echo date('d/m/Y',strtotime($pub->data_publicacao))?></td>
                        <td><?php echo htmlspecialchars(mb_substr($pub->titulo??'Publicação',0,40))?></td>
                        <td><?php echo htmlspecialchars($pub->numero_processo??'-')?></td>
                        <td><?php echo htmlspecialchars($pub->tribunal??'-')?></td>
                        <td><?php echo htmlspecialchars($pub->advogado_nome??'-')?></td>
                        <td>
                            <a href="<?php echo site_url('admin/diario_oficial/view_publicacao/'.$pub->id)?>" class="btn btn-primary btn-xs"><i class="fa fa-eye"></i> Ver</a>
                            <?php if(empty($pub->lida)): ?>
                            <a href="<?php echo site_url('admin/diario_oficial/marcar_lida/'.$pub->id)?>" class="btn btn-default btn-xs"><i class="fa fa-check"></i></a>
                            <?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
</section>
<link href="<?php echo base_url('assets/css/datatables/dataTables.bootstrap.css')?>" rel="stylesheet">
<script src="<?php echo base_url('assets/js/plugins/datatables/jquery.dataTables.js')?>"></script>
<script src="<?php echo base_url('assets/js/plugins/datatables/dataTables.bootstrap.js')?>"></script>
<script>$(function(){ if($('#tbl-pub').length) $('#tbl-pub').dataTable({"order":[[1,'desc']],"pageLength":25,"language":{"url":"//cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json"}}); });</script>
