<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="content-header">
    <h1><i class="fa fa-users"></i> Advogados Monitorados no PJe</h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo site_url('admin/diario_oficial')?>">Diário PJe</a></li>
        <li class="active">Advogados</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; ?>
<div class="row">
<div class="col-md-7">
<div class="box box-primary" style="border-radius:12px">
    <div class="box-header with-border"><h3 class="box-title"><i class="fa fa-list"></i> Advogados cadastrados</h3>
        <div class="box-tools pull-right"><button class="btn btn-primary btn-sm" onclick="$('#form-novo').slideToggle()"><i class="fa fa-plus"></i> Novo</button></div>
    </div>
    <div class="box-body">
        <!-- Form novo/editar -->
        <div id="form-novo" style="display:none;background:#f8f9fa;padding:16px;border-radius:8px;margin-bottom:16px">
            <form method="post" action="<?php echo site_url('admin/diario_oficial/advogados')?>">
            <input type="hidden" name="id" id="edit-id" value="0">
            <input type="hidden" name="submitted" value="1">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group"><label>Nome completo</label>
                    <input type="text" name="nome" id="edit-nome" class="form-control" required placeholder="Nome do advogado"></div>
                </div>
                <div class="col-md-3">
                    <div class="form-group"><label>Nº OAB</label>
                    <input type="text" name="oab" id="edit-oab" class="form-control" required placeholder="31577"></div>
                </div>
                <div class="col-md-3">
                    <div class="form-group"><label>UF OAB</label>
                    <input type="text" name="uf_oab" id="edit-uf" class="form-control" required value="PE" maxlength="2" style="text-transform:uppercase"></div>
                </div>
                <div class="col-md-6">
                    <div class="form-group"><label>Vincular ao usuário do sistema</label>
                    <select name="user_id" id="edit-user" class="form-control">
                        <option value="">— Sem vínculo —</option>
                        <?php foreach($usuarios as $u): ?>
                        <option value="<?php echo $u->id?>"><?php echo htmlspecialchars($u->name)?></option>
                        <?php endforeach; ?>
                    </select>
                    <small class="text-muted">Vinculando, o advogado verá apenas suas publicações ao logar</small>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="form-group"><label>Status</label>
                    <select name="ativo" id="edit-ativo" class="form-control">
                        <option value="1">Ativo</option>
                        <option value="0">Inativo</option>
                    </select></div>
                </div>
                <div class="col-md-12">
                    <button type="submit" class="btn btn-primary"><i class="fa fa-save"></i> Salvar</button>
                    <button type="button" class="btn btn-default" onclick="$('#form-novo').slideUp()">Cancelar</button>
                </div>
            </div>
            </form>
        </div>

        <table class="table table-hover">
            <thead><tr><th>Advogado</th><th>OAB</th><th>Usuário vinculado</th><th>Status</th><th></th></tr></thead>
            <tbody>
            <?php foreach($advogados as $adv): ?>
            <tr>
                <td><strong><?php echo htmlspecialchars($adv->nome)?></strong></td>
                <td><code><?php echo $adv->oab?>/<?php echo $adv->uf_oab?></code></td>
                <td>
                    <?php
                    $u = null;
                    foreach($usuarios as $uu) if($uu->id == $adv->user_id) { $u=$uu; break; }
                    echo $u ? '<span class="label label-info">'.htmlspecialchars($u->name).'</span>' : '<span class="text-muted">—</span>';
                    ?>
                </td>
                <td><?php echo $adv->ativo ? '<span class="label label-success">Ativo</span>' : '<span class="label label-default">Inativo</span>'?></td>
                <td>
                    <button class="btn btn-xs btn-default btn-edit"
                        data-id="<?php echo $adv->id?>"
                        data-nome="<?php echo htmlspecialchars($adv->nome)?>"
                        data-oab="<?php echo $adv->oab?>"
                        data-uf="<?php echo $adv->uf_oab?>"
                        data-user="<?php echo $adv->user_id?>"
                        data-ativo="<?php echo $adv->ativo?>">
                        <i class="fa fa-pencil"></i>
                    </button>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <div class="box-footer">
        <a href="<?php echo site_url('admin/diario_oficial')?>" class="btn btn-default"><i class="fa fa-arrow-left"></i> Voltar</a>
    </div>
</div>
</div>
<div class="col-md-5">
<div class="box box-info" style="border-radius:12px">
    <div class="box-header with-border"><h3 class="box-title"><i class="fa fa-info-circle"></i> Como funciona</h3></div>
    <div class="box-body" style="font-size:13px;line-height:1.8">
        <p><strong>Monitoramento automático:</strong> O sistema busca publicações no PJe 2x por dia (0h e 12h) para cada advogado cadastrado.</p>
        <hr>
        <p><strong>Vínculo com usuário:</strong> Se vincular o advogado a um usuário do sistema, quando esse usuário logar ele verá automaticamente apenas suas próprias publicações no filtro.</p>
        <hr>
        <p><strong>Notificação:</strong> Publicações não lidas aparecem destacadas em laranja na listagem.</p>
    </div>
</div>
</div>
</div>
</section>
<script>
$(document).on('click','.btn-edit',function(){
    var d=$(this).data();
    $('#edit-id').val(d.id);
    $('#edit-nome').val(d.nome);
    $('#edit-oab').val(d.oab);
    $('#edit-uf').val(d.uf);
    $('#edit-user').val(d.user);
    $('#edit-ativo').val(d.ativo);
    $('#form-novo').slideDown();
    $('html,body').animate({scrollTop:$('#form-novo').offset().top-80},400);
});
</script>
