<!-- application/modules/admin/views/diario_oficial/advogados.php -->
<div class="container-fluid">
  <div class="row">
    <div class="col-md-12">

      <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
          <h4 class="mb-0"><i class="fas fa-user-tie"></i> Advogados Monitorados — PJe / Diário Oficial</h4>
          <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#modalAdvogado" onclick="novoAdvogado()">
            <i class="fas fa-plus"></i> Novo Advogado
          </button>
        </div>

        <div class="card-body">
          <?php if (empty($advogados)): ?>
            <div class="alert alert-info">
              Nenhum advogado cadastrado ainda. Clique em <strong>Novo Advogado</strong> para começar.
            </div>
          <?php else: ?>
          <div class="table-responsive">
            <table class="table table-hover table-sm">
              <thead class="thead-light">
                <tr>
                  <th>#</th>
                  <th>Nome</th>
                  <th>OAB</th>
                  <th>E-mail Recorte Digital</th>
                  <th>Status</th>
                  <th>Ações</th>
                </tr>
              </thead>
              <tbody>
                <?php foreach ($advogados as $adv): ?>
                <tr>
                  <td><?= $adv['id'] ?></td>
                  <td><?= htmlspecialchars($adv['nome']) ?></td>
                  <td><span class="badge badge-primary"><?= $adv['oab'] ?>/<?= $adv['uf_oab'] ?></span></td>
                  <td>
                    <?php if (!empty($adv['email_recorte'])): ?>
                      <small class="text-muted"><i class="fas fa-envelope"></i> <?= htmlspecialchars($adv['email_recorte']) ?></small>
                    <?php else: ?>
                      <small class="text-danger"><i class="fas fa-exclamation-triangle"></i> Não configurado</small>
                    <?php endif; ?>
                  </td>
                  <td>
                    <?php if ($adv['ativo']): ?>
                      <span class="badge badge-success">Ativo</span>
                    <?php else: ?>
                      <span class="badge badge-secondary">Inativo</span>
                    <?php endif; ?>
                  </td>
                  <td>
                    <button class="btn btn-xs btn-warning"
                      onclick="editarAdvogado(<?= htmlspecialchars(json_encode($adv)) ?>)">
                      <i class="fas fa-edit"></i>
                    </button>
                  </td>
                </tr>
                <?php endforeach; ?>
              </tbody>
            </table>
          </div>
          <?php endif; ?>
        </div>
      </div>

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

<!-- Modal Cadastro/Edição -->
<div class="modal fade" id="modalAdvogado" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <form method="POST" action="<?= site_url('admin/diario_oficial/salvar_advogado') ?>">
        <input type="hidden" name="id" id="adv_id" value="">

        <div class="modal-header">
          <h5 class="modal-title" id="modalTitulo">Novo Advogado</h5>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>

        <div class="modal-body">

          <div class="form-group">
            <label>Nome completo <span class="text-danger">*</span></label>
            <input type="text" name="nome" id="adv_nome" class="form-control" required>
          </div>

          <div class="form-row">
            <div class="form-group col-md-8">
              <label>Número OAB <span class="text-danger">*</span></label>
              <input type="text" name="oab" id="adv_oab" class="form-control" required placeholder="31577">
            </div>
            <div class="form-group col-md-4">
              <label>UF <span class="text-danger">*</span></label>
              <select name="uf_oab" id="adv_uf" class="form-control" required>
                <?php
                $ufs = ['AC','AL','AP','AM','BA','CE','DF','ES','GO','MA','MT','MS','MG','PA','PB','PR','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO'];
                foreach ($ufs as $uf) {
                    $sel = ($uf === 'PE') ? 'selected' : '';
                    echo "<option value=\"$uf\" $sel>$uf</option>";
                }
                ?>
              </select>
            </div>
          </div>

          <div class="form-group">
            <label>
              <i class="fas fa-envelope text-info"></i>
              E-mail que recebe o Recorte Digital
            </label>
            <input type="email" name="email_recorte" id="adv_email_recorte" class="form-control"
              placeholder="Ex: agendaadvbp@gmail.com">
            <small class="form-text text-muted">
              E-mail cadastrado no serviço de Recorte Digital da OAB. O sistema usará este e-mail 
              para capturar automaticamente as publicações diárias.
            </small>
          </div>

          <div class="form-group">
            <div class="custom-control custom-switch">
              <input type="checkbox" class="custom-control-input" id="adv_ativo" name="ativo" value="1" checked>
              <label class="custom-control-label" for="adv_ativo">Monitoramento ativo</label>
            </div>
          </div>

        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
          <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Salvar</button>
        </div>

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

<script>
function novoAdvogado() {
  document.getElementById('adv_id').value = '';
  document.getElementById('adv_nome').value = '';
  document.getElementById('adv_oab').value = '';
  document.getElementById('adv_uf').value = 'PE';
  document.getElementById('adv_email_recorte').value = '';
  document.getElementById('adv_ativo').checked = true;
  document.getElementById('modalTitulo').textContent = 'Novo Advogado';
}

function editarAdvogado(adv) {
  document.getElementById('adv_id').value           = adv.id;
  document.getElementById('adv_nome').value         = adv.nome;
  document.getElementById('adv_oab').value          = adv.oab;
  document.getElementById('adv_uf').value           = adv.uf_oab;
  document.getElementById('adv_email_recorte').value = adv.email_recorte || '';
  document.getElementById('adv_ativo').checked      = adv.ativo == 1;
  document.getElementById('modalTitulo').textContent = 'Editar Advogado';
  $('#modalAdvogado').modal('show');
}
</script>
