<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="content-header">
    <h1><i class="fa fa-file-text"></i> Gerar Recibo</h1>
    <ol class="breadcrumb">
        <li><a href="<?php echo site_url('admin/financeiro')?>"><i class="fa fa-money"></i> Financeiro</a></li>
        <li><a href="<?php echo site_url('admin/financeiro/recibos')?>">Recibos</a></li>
        <li class="active">Gerar Recibo</li>
    </ol>
</section>
<section class="content">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="box box-primary">
    <div class="box-header with-border"><h3 class="box-title">Dados do Recibo</h3></div>
    <form method="post" action="<?php echo site_url('admin/financeiro/gerar_recibo/'.($lancamento?$lancamento->id:''))?>">
    <div class="box-body">
        <?php if($lancamento): ?>
        <div class="callout callout-info">
            <strong>Lançamento vinculado #<?php echo $lancamento->id?>:</strong>
            <?php echo htmlspecialchars($lancamento->descricao)?> —
            R$ <?php echo number_format($lancamento->valor,2,',','.')?>
        </div>
        <?php endif; ?>

        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label>Nome do Pagador <span class="text-danger">*</span></label>
                    <input type="text" name="pagador_nome" class="form-control" required
                           value="<?php echo htmlspecialchars($lancamento->client_nome??'')?>"
                           placeholder="Nome completo ou razão social">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>CPF / CNPJ</label>
                    <input type="text" name="pagador_cpf_cnpj" class="form-control" placeholder="000.000.000-00">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>Valor (R$) <span class="text-danger">*</span></label>
                    <input type="text" name="valor" id="inp-valor" class="form-control" required
                           value="<?php echo $lancamento?number_format($lancamento->valor,2,',','.'):''?>"
                           placeholder="0,00">
                </div>
            </div>
        </div>

        <div class="form-group">
            <label>Endereço do Pagador</label>
            <input type="text" name="pagador_endereco" class="form-control" placeholder="Rua, número, bairro, cidade - UF">
        </div>

        <div class="form-group">
            <label>Referente a <span class="text-danger">*</span></label>
            <textarea name="referente" class="form-control" rows="3" required
                      placeholder="Descreva o serviço ou pagamento"><?php echo htmlspecialchars($lancamento->descricao??'')?></textarea>
        </div>

        <div class="row">
            <div class="col-md-5">
                <div class="form-group">
                    <label>Recebido por <span class="text-danger">*</span></label>
                    <input type="text" name="recebedor_nome" class="form-control" required
                           value="<?php echo htmlspecialchars($setting->name??'Breno Paiva Advocacia')?>">
                </div>
            </div>
            <div class="col-md-3">
                <div class="form-group">
                    <label>OAB do Recebedor</label>
                    <input type="text" name="recebedor_oab" class="form-control" placeholder="123456/PE">
                </div>
            </div>
            <div class="col-md-2">
                <div class="form-group">
                    <label>Data <span class="text-danger">*</span></label>
                    <input type="date" name="data_emissao" class="form-control" required value="<?php echo date('Y-m-d')?>">
                </div>
            </div>
            <div class="col-md-2">
                <div class="form-group">
                    <label>Local</label>
                    <input type="text" name="local_emissao" class="form-control" value="Recife - PE">
                </div>
            </div>
        </div>

        <input type="hidden" name="lancamento_id" value="<?php echo $lancamento?$lancamento->id:''?>">
        <input type="hidden" name="submitted" value="1">
    </div>
    <div class="box-footer">
        <button type="submit" class="btn btn-primary"><i class="fa fa-file-text"></i> Gerar Recibo</button>
        <a href="<?php echo site_url('admin/financeiro/recibos')?>" class="btn btn-default">Cancelar</a>
    </div>
    </form>
</div>
</div>
</div>
</section>
<script>
$(function(){
    $('#inp-valor').on('input',function(){
        var v=$(this).val().replace(/\D/g,'');
        v=(parseInt(v||0)/100).toFixed(2).replace('.',',').replace(/(\d)(?=(\d{3})+(?!\d))/g,'$1.');
        $(this).val(v);
    });
});
</script>
