<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<style>
.pje-badge-unread { background:#C00000; color:#fff; font-size:10px; border-radius:10px; padding:2px 7px; }
.pje-row-unread   { background:#fff9f0; border-left:3px solid #C55A11; }
.pje-row-read     { background:#fff; opacity:.85; }
.pje-tribunal     { font-size:11px; font-weight:700; padding:2px 7px; border-radius:4px; background:#1F4E79; color:#fff; }
.pje-processo     { font-family:monospace; font-size:12px; color:#333; }
.pje-content-preview { font-size:12px; color:#555; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; max-width:380px; }
.filter-bar { background:#f8f9fa; border:1px solid #e0e0e0; border-radius:10px; padding:14px 18px; margin-bottom:16px; }
.stat-card  { border-radius:10px; padding:14px 18px; color:#fff; text-align:center; }
.stat-card .num { font-size:28px; font-weight:700; line-height:1; }
.stat-card .lbl { font-size:11px; opacity:.85; text-transform:uppercase; letter-spacing:.8px; margin-top:4px; }
</style>

<section class="content-header">
    <h1><i class="fa fa-newspaper-o"></i> Diário Oficial PJe
        <?php if(isset($nao_lidas) && $nao_lidas > 0): ?>
        <span class="pje-badge-unread"><?php echo $nao_lidas?> não lidas</span>
        <?php endif; ?>
    </h1>
    <ol class="breadcrumb"><li class="active">Diário PJe</li></ol>
</section>

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

<!-- STATS -->
<div class="row" style="margin-bottom:16px">
    <div class="col-md-3 col-sm-6">
        <div class="stat-card" style="background:linear-gradient(135deg,#1F4E79,#2E75B6)">
            <div class="num"><?php echo $total?></div>
            <div class="lbl">Total no período</div>
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="stat-card" style="background:linear-gradient(135deg,#C00000,#e74c3c)">
            <div class="num"><?php echo $nao_lidas?></div>
            <div class="lbl">Não lidas</div>
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="stat-card" style="background:linear-gradient(135deg,#1D6B3B,#27ae60)">
            <div class="num"><?php echo $total - $nao_lidas?></div>
            <div class="lbl">Lidas</div>
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="stat-card" style="background:linear-gradient(135deg,#7F6000,#C55A11)">
            <div class="num"><?php echo count($advogados)?></div>
            <div class="lbl">Advogados monitorados</div>
        </div>
    </div>
</div>

<!-- FILTROS -->
<div class="filter-bar">
    <form method="get" action="<?php echo site_url('admin/diario_oficial')?>">
    <div class="row">
        <div class="col-md-2">
            <label style="font-size:11px;font-weight:600;color:#555">DE</label>
            <input type="date" name="dt_ini" class="form-control input-sm" value="<?php echo $filters['f_dt_ini']?>">
        </div>
        <div class="col-md-2">
            <label style="font-size:11px;font-weight:600;color:#555">ATÉ</label>
            <input type="date" name="dt_fim" class="form-control input-sm" value="<?php echo $filters['f_dt_fim']?>">
        </div>
        <?php if($access == 1): ?>
        <div class="col-md-2">
            <label style="font-size:11px;font-weight:600;color:#555">ADVOGADO</label>
            <select name="adv_id" class="form-control input-sm">
                <option value="">Todos</option>
                <?php foreach($advogados as $adv): ?>
                <option value="<?php echo $adv->id?>" <?php echo $filters['f_adv']==$adv->id?'selected':''?>>
                    <?php echo $adv->nome?> (<?php echo $adv->oab?>/<?php echo $adv->uf_oab?>)
                </option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php endif; ?>
        <div class="col-md-2">
            <label style="font-size:11px;font-weight:600;color:#555">TRIBUNAL</label>
            <select name="tribunal" class="form-control input-sm">
                <option value="">Todos</option>
                <?php foreach($tribunais as $t): ?>
                <option value="<?php echo $t?>" <?php echo $filters['f_tribunal']==$t?'selected':''?>><?php echo $t?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-2">
            <label style="font-size:11px;font-weight:600;color:#555">STATUS</label>
            <select name="lida" class="form-control input-sm">
                <option value="">Todas</option>
                <option value="0" <?php echo $filters['f_lida']==='0'?'selected':''?>>Não lidas</option>
                <option value="1" <?php echo $filters['f_lida']==='1'?'selected':''?>>Lidas</option>
            </select>
        </div>
        <div class="col-md-2">
            <label style="font-size:11px;font-weight:600;color:#555">BUSCAR</label>
            <input type="text" name="busca" class="form-control input-sm" placeholder="Processo ou texto..." value="<?php echo htmlspecialchars($filters['f_busca']?:'')?>">
        </div>
    </div>
    <div class="row" style="margin-top:10px">
        <div class="col-md-12">
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-search"></i> Filtrar</button>
            <a href="<?php echo site_url('admin/diario_oficial')?>" class="btn btn-default btn-sm"><i class="fa fa-times"></i> Limpar</a>
            <?php if($nao_lidas > 0): ?>
            <form style="display:inline" method="post" action="<?php echo site_url('admin/diario_oficial/marcar_todas_lidas')?>">
                <input type="hidden" name="adv_id" value="<?php echo $filters['f_adv']?>">
                <button class="btn btn-warning btn-sm"><i class="fa fa-check-double"></i> Marcar todas como lidas</button>
            </form>
            <?php endif; ?>
            <?php if($access==1): ?>
            <a href="<?php echo site_url('admin/diario_oficial/advogados')?>" class="btn btn-default btn-sm pull-right"><i class="fa fa-users"></i> Advogados</a>
            <a href="<?php echo site_url('admin/diario_oficial/config_cookies')?>" class="btn btn-default btn-sm pull-right" style="margin-right:5px"><i class="fa fa-key"></i> Cookies</a>
            <?php endif; ?>
        </div>
    </div>
    </form>
</div>

<!-- TABELA -->
<div class="box" style="border-radius:10px;box-shadow:0 2px 12px rgba(0,0,0,.08)">
    <div class="box-body no-padding">
        <?php if(empty($publicacoes)): ?>
        <div class="text-center text-muted" style="padding:40px">
            <i class="fa fa-newspaper-o" style="font-size:40px;opacity:.3;display:block;margin-bottom:12px"></i>
            Nenhuma publicação encontrada para os filtros selecionados.
        </div>
        <?php else: ?>
        <table class="table table-hover" style="margin:0">
            <thead style="background:#f5f7fa">
                <tr>
                    <th style="width:30px"></th>
                    <th style="width:90px">Data</th>
                    <th style="width:70px">Tribunal</th>
                    <?php if($access==1): ?><th style="width:140px">Advogado</th><?php endif; ?>
                    <th style="width:160px">Processo</th>
                    <th>Publicação</th>
                    <th style="width:80px">Ações</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($publicacoes as $p): ?>
            <tr class="<?php echo $p->lida ? 'pje-row-read' : 'pje-row-unread'?>" id="row-<?php echo $p->id?>">
                <td style="text-align:center;vertical-align:middle">
                    <?php if(!$p->lida): ?>
                    <span class="fa fa-circle text-danger" style="font-size:8px" title="Não lida"></span>
                    <?php else: ?>
                    <span class="fa fa-circle-o text-muted" style="font-size:8px"></span>
                    <?php endif; ?>
                </td>
                <td style="vertical-align:middle;font-size:12px;white-space:nowrap">
                    <?php echo $p->data_publicacao ? date('d/m/Y', strtotime($p->data_publicacao)) : '-'?>
                </td>
                <td style="vertical-align:middle">
                    <?php if($p->tribunal): ?>
                    <span class="pje-tribunal"><?php echo $p->tribunal?></span>
                    <?php endif; ?>
                </td>
                <?php if($access==1): ?>
                <td style="vertical-align:middle;font-size:12px">
                    <strong><?php echo $p->adv_nome?></strong><br>
                    <span class="text-muted">OAB <?php echo $p->oab?>/<?php echo $p->uf_oab?></span>
                </td>
                <?php endif; ?>
                <td style="vertical-align:middle">
                    <span class="pje-processo"><?php echo $p->processo_mascara ?: $p->numero_processo?></span>
                </td>
                <td style="vertical-align:middle">
                    <div style="font-size:13px;font-weight:<?php echo $p->lida?'400':'600'?>;color:#222;margin-bottom:3px">
                        <?php echo htmlspecialchars($p->titulo ?: 'Publicação PJe')?>
                    </div>
                    <div class="pje-content-preview"><?php echo htmlspecialchars(mb_substr(strip_tags($p->conteudo??''),0,150))?></div>
                    <?php if($p->orgao): ?>
                    <small class="text-muted"><i class="fa fa-building-o"></i> <?php echo htmlspecialchars($p->orgao)?></small>
                    <?php endif; ?>
                </td>
                <td style="vertical-align:middle;white-space:nowrap">
                    <a href="<?php echo site_url('admin/diario_oficial/view/'.$p->id)?>" class="btn btn-xs btn-primary" title="Ver"><i class="fa fa-eye"></i></a>
                    <?php if($p->link): ?>
                    <a href="<?php echo $p->link?>" target="_blank" class="btn btn-xs btn-default" title="Abrir no PJe"><i class="fa fa-external-link"></i></a>
                    <?php endif; ?>
                    <?php if(!$p->lida): ?>
                    <button class="btn btn-xs btn-default btn-marcar-lida" data-id="<?php echo $p->id?>" title="Marcar lida"><i class="fa fa-check"></i></button>
                    <?php endif; ?>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
    <?php if($total > $per_page): ?>
    <div class="box-footer">
        <?php
        $pages = ceil($total / $per_page);
        $q = array_merge($filters, ['f_adv'=>$filters['f_adv'],'f_dt_ini'=>$filters['f_dt_ini'],'f_dt_fim'=>$filters['f_dt_fim']]);
        $base = site_url('admin/diario_oficial').'?'.http_build_query(['adv_id'=>$q['f_adv'],'dt_ini'=>$q['f_dt_ini'],'dt_fim'=>$q['f_dt_fim'],'lida'=>$q['f_lida'],'tribunal'=>$q['f_tribunal'],'busca'=>$q['f_busca']]);
        ?>
        <ul class="pagination pagination-sm" style="margin:0">
            <?php for($i=1;$i<=$pages;$i++): ?>
            <li class="<?php echo $i==$page?'active':''?>"><a href="<?php echo $base.'&page='.$i?>"><?php echo $i?></a></li>
            <?php endfor; ?>
        </ul>
        <small class="text-muted" style="float:right;margin-top:6px"><?php echo $total?> publicações</small>
    </div>
    <?php endif; ?>
</div>
</section>

<script>
$(document).on('click', '.btn-marcar-lida', function() {
    var btn = $(this), id = btn.data('id'), row = $('#row-' + id);
    $.post('<?php echo site_url("admin/diario_oficial/marcar_lida_ajax")?>', {id: id}, function(r) {
        if (r.success) {
            row.removeClass('pje-row-unread').addClass('pje-row-read');
            row.find('.fa-circle.text-danger').removeClass('fa-circle text-danger').addClass('fa-circle-o text-muted');
            btn.remove();
        }
    }, 'json');
});
</script>
