<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<style>
.peticao-body { background:#fff; border:1px solid #e2e6ea; border-radius:10px; padding:40px; font-family:Georgia,serif; font-size:14px; line-height:1.9; color:#222; max-width:720px; margin:0 auto; }
.peticao-body p { margin-bottom:12px; text-align:justify; }
.peticao-body strong { font-weight:700; }
</style>

<section class="content-header">
    <h1><i class="fa fa-file-text-o"></i> Petição Gerada <small><?php echo htmlspecialchars($exp->devedor_nome) ?></small></h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo site_url('admin') ?>"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        <li><a href="<?php echo site_url('admin/patrimonio') ?>">Busca Patrimonial</a></li>
        <li><a href="<?php echo site_url('admin/patrimonio/view/'.$exp->id) ?>"><?php echo htmlspecialchars($exp->devedor_nome) ?></a></li>
        <li class="active">Petição</li>
    </ol>
</section>

<section class="content">

<!-- Barra de ações -->
<div class="box box-default" style="margin-bottom:14px">
    <div class="box-body" style="padding:10px 14px;display:flex;gap:8px;align-items:center">
        <?php if($pet->status !== 'protocolada'): ?>
        <button data-toggle="modal" data-target="#modal-protocolar" class="btn btn-success btn-sm">
            <i class="fa fa-check"></i> Marcar como Protocolada
        </button>
        <?php else: ?>
        <span class="label label-success" style="padding:6px 10px;font-size:12px">
            <i class="fa fa-check"></i> Protocolada em <?php echo date('d/m/Y', strtotime($pet->data_protocolo)) ?>
            <?php if($pet->num_protocolo): ?> — <?php echo htmlspecialchars($pet->num_protocolo) ?><?php endif; ?>
        </span>
        <?php endif; ?>
        <a href="<?php echo site_url('admin/patrimonio/view/'.$exp->id) ?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> Voltar</a>
        <button onclick="window.print()" class="btn btn-default btn-sm"><i class="fa fa-print"></i> Imprimir</button>
    </div>
</div>

<!-- Conteúdo da petição -->
<div class="peticao-body" id="peticao-conteudo">
    <?php
    $linhas = explode("\n", $pet->conteudo);
    foreach($linhas as $linha) {
        $linha = htmlspecialchars($linha);
        // Negrito via **texto**
        $linha = preg_replace('/\*\*(.+?)\*\*/', '<strong>$1</strong>', $linha);
        if(trim($linha) === '') {
            echo '<br>';
        } else {
            echo '<p>' . $linha . '</p>';
        }
    }
    ?>
</div>

</section>

<!-- Modal protocolar -->
<div class="modal fade" id="modal-protocolar" tabindex="-1">
    <div class="modal-dialog"><div class="modal-content" style="border-radius:10px;overflow:hidden">
        <div class="modal-header" style="background:linear-gradient(135deg,#2e7d32,#27ae60);border-radius:6px 6px 0 0">
            <h4 class="modal-title" style="color:#fff"><i class="fa fa-check"></i> Marcar como Protocolada</h4>
            <button type="button" class="close" data-dismiss="modal" style="color:#fff">&times;</button>
        </div>
        <form method="post" action="<?php echo site_url('admin/patrimonio/peticao_protocolar/'.$pet->id) ?>">
        <div class="modal-body">
            <div class="form-group">
                <label style="font-size:11px;font-weight:700;color:#888;text-transform:uppercase">Número do Protocolo</label>
                <input type="text" name="num_protocolo" class="form-control" placeholder="Ex: 0000001-00.2025.8.17.0001">
            </div>
            <small class="text-muted">Deixe em branco se não tiver o número ainda.</small>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
            <button type="submit" class="btn btn-success"><i class="fa fa-check"></i> Confirmar</button>
        </div>
        </form>
    </div></div>
</div>

<style>
@media print {
    .content-header, .box.box-default, header, aside, .modal { display: none !important; }
    .peticao-body { border: none; padding: 20px; }
    body { background: #fff !important; }
    #bp-sidebar, #bp-topbar { display: none !important; }
    #bp-content { margin: 0 !important; }
}
</style>
