<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<style>
.recibo-box { max-width:750px; margin:0 auto; background:#fff; border:1px solid #ddd; border-radius:8px; padding:40px; }
.recibo-header { border-bottom:3px solid #1F4E79; padding-bottom:15px; margin-bottom:20px; }
.recibo-numero { font-size:13px; color:#777; float:right; margin-top:5px; }
.recibo-title  { font-size:22px; font-weight:700; color:#1F4E79; }
.recibo-valor  { background:#1F4E79; color:#fff; border-radius:6px; padding:15px 20px; text-align:center; margin:20px 0; }
.recibo-valor .valor { font-size:28px; font-weight:700; }
.recibo-valor .extenso { font-size:13px; opacity:.85; margin-top:4px; }
.recibo-campo { margin-bottom:12px; }
.recibo-campo label { font-size:11px; text-transform:uppercase; color:#999; font-weight:600; display:block; margin-bottom:2px; }
.recibo-campo span { font-size:14px; color:#333; }
.recibo-assinatura { margin-top:50px; border-top:1px solid #ccc; padding-top:15px; text-align:center; color:#555; font-size:13px; }
.texto-livre-area { background:#fffde7; border:1px dashed #f9a825; border-radius:6px; padding:12px 15px; font-size:13px; color:#555; line-height:1.6; }
#btn-salvar-template { display:none; }
</style>

<section class="content-header">
    <h1><i class="fa fa-file-text-o"></i> Recibo <small><?php echo $numero ?></small></h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo site_url('admin/financeiro') ?>"><i class="fa fa-money"></i> Financeiro</a></li>
        <li class="active">Recibo</li>
    </ol>
</section>

<section class="content">
<div class="row">
    <div class="col-md-10 col-md-offset-1">

        <!-- Ações -->
        <div style="margin-bottom:15px">
            <a href="<?php echo site_url('admin/financeiro/pdf_recibo/' . $lancamento->id) ?>" class="btn btn-primary btn-sm" target="_blank">
                <i class="fa fa-file-pdf-o"></i> Gerar PDF
            </a>
            <button onclick="window.print()" class="btn btn-default btn-sm">
                <i class="fa fa-print"></i> Imprimir
            </button>
            <a href="<?php echo site_url('admin/financeiro') ?>" class="btn btn-default btn-sm">
                <i class="fa fa-arrow-left"></i> Voltar
            </a>
            <button id="btn-salvar-template" class="btn btn-warning btn-sm pull-right">
                <i class="fa fa-save"></i> Salvar texto como padrão
            </button>
        </div>

        <!-- Recibo -->
        <div class="recibo-box" id="recibo-print">

            <div class="recibo-header">
                <div class="recibo-numero">Nº <?php echo $numero ?></div>
                <div class="recibo-title">
                    <?php if (!empty($setting->company_logo)): ?>
                    <img src="<?php echo base_url('assets/uploads/images/' . $setting->company_logo) ?>" height="45" style="vertical-align:middle;margin-right:10px">
                    <?php endif; ?>
                    <?php echo htmlspecialchars($setting->company_name ?? 'Breno Paiva Advocacia') ?>
                </div>
                <div style="font-size:12px;color:#777;margin-top:4px">
                    <?php echo htmlspecialchars($setting->company_address ?? '') ?>
                    <?php if (!empty($setting->company_phone)): ?> | <?php echo $setting->company_phone ?><?php endif; ?>
                </div>
            </div>

            <!-- Valor destaque -->
            <div class="recibo-valor">
                <div class="valor">R$ <?php echo number_format($lancamento->valor, 2, ',', '.') ?></div>
                <div class="extenso"><?php echo $valor_extenso ?></div>
            </div>

            <!-- Dados do pagador -->
            <div class="row">
                <div class="col-sm-8">
                    <div class="recibo-campo">
                        <label>Recebi de</label>
                        <span><strong><?php echo htmlspecialchars($cliente->name ?? 'Cliente não identificado') ?></strong></span>
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="recibo-campo">
                        <label>CPF / CNPJ</label>
                        <span><?php echo htmlspecialchars($cliente->contact ?? '—') ?></span>
                    </div>
                </div>
            </div>

            <!-- Referente a -->
            <div class="recibo-campo">
                <label>Referente a</label>
                <span><?php echo htmlspecialchars($lancamento->descricao ?? $lancamento->categoria ?? '—') ?></span>
                <?php if (!empty($lancamento->competencia_mes)): ?>
                — <span class="text-muted"><?php echo $lancamento->competencia_mes ?></span>
                <?php endif; ?>
            </div>

            <!-- Dados adicionais -->
            <div class="row">
                <div class="col-sm-4">
                    <div class="recibo-campo">
                        <label>Forma de Pagamento</label>
                        <span><?php echo htmlspecialchars($lancamento->forma_pagamento ?? '—') ?></span>
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="recibo-campo">
                        <label>Data do Pagamento</label>
                        <span><?php echo $lancamento->data_pagamento ? date('d/m/Y', strtotime($lancamento->data_pagamento)) : date('d/m/Y') ?></span>
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="recibo-campo">
                        <label>Nº Documento</label>
                        <span><?php echo htmlspecialchars($lancamento->numero_documento ?? '—') ?></span>
                    </div>
                </div>
            </div>

            <!-- TEXTO LIVRE EDITÁVEL (item 4) -->
            <div style="margin-top:20px">
                <label style="font-size:11px;text-transform:uppercase;color:#999;font-weight:600">
                    <i class="fa fa-edit"></i> Observação / Texto do Recibo
                    <small class="text-muted">(clique para editar)</small>
                </label>
                <div class="texto-livre-area" id="texto-livre" contenteditable="true"
                     style="min-height:60px;outline:none;cursor:text"
                     title="Clique para editar"><?php echo nl2br(htmlspecialchars($texto_livre)) ?></div>
            </div>

            <!-- Assinatura -->
            <div class="recibo-assinatura">
                <div style="margin-bottom:40px"></div>
                <div style="border-top:1px solid #333;display:inline-block;min-width:300px;padding-top:8px">
                    <?php echo htmlspecialchars($setting->company_name ?? 'Breno Paiva Advocacia') ?><br>
                    <small><?php echo htmlspecialchars($setting->company_email ?? '') ?></small>
                </div>
                <div style="margin-top:15px;font-size:12px;color:#aaa">
                    <?php echo date('d') ?> de <?php echo strftime('%B', time()) ?> de <?php echo date('Y') ?>
                </div>
            </div>

        </div><!-- /recibo-box -->
    </div>
</div>
</section>

<script>
// Mostra botão salvar quando edita o texto
document.getElementById('texto-livre').addEventListener('input', function() {
    document.getElementById('btn-salvar-template').style.display = 'inline-block';
});

// Salva o template via AJAX
document.getElementById('btn-salvar-template').addEventListener('click', function() {
    var texto = document.getElementById('texto-livre').innerText;
    fetch('<?php echo site_url('admin/financeiro/salvar_template_recibo') ?>', {
        method: 'POST',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        body: 'texto_livre=' + encodeURIComponent(texto)
    })
    .then(function(r) { return r.json(); })
    .then(function(d) {
        if (d.ok) {
            document.getElementById('btn-salvar-template').style.display = 'none';
            alert('Texto salvo como padrão para todos os recibos.');
        }
    });
});
</script>

<style media="print">
.content-header, .main-header, .main-sidebar, .btn, #btn-salvar-template, .texto-livre-area[contenteditable]:focus { display:none !important; }
.recibo-box { border:none; padding:0; max-width:100%; }
body { background:#fff !important; }
</style>
