<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="content-header">
    <h1><i class="fa fa-list-alt"></i> <?php echo $page_title?></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"><?php echo $page_title?></li>
    </ol>
</section>
<section class="content">

<!-- Selector de conta -->
<div class="box box-default">
    <div class="box-body">
        <form method="get" class="form-inline">
            <?php foreach($contas as $ct): ?>
            <a href="<?php echo site_url('admin/financeiro/extrato/'.$ct->id)?>" class="btn btn-sm <?php echo $conta&&$conta->id==$ct->id?'btn-primary':'btn-default'?>" style="margin:2px">
                <?php echo htmlspecialchars($ct->nome)?> <span class="badge">R$ <?php echo number_format($ct->saldo_atual,2,',','.')?></span>
            </a>
            <?php endforeach; ?>
            &nbsp;&nbsp;
            <input type="date" name="data_ini" class="form-control input-sm" value="<?php echo $data_ini?>">
            <input type="date" name="data_fim" class="form-control input-sm" value="<?php echo $data_fim?>">
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-search"></i></button>
        </form>
    </div>
</div>

<?php if($conta): ?>
<div class="box box-primary">
    <div class="box-header with-border" style="background:<?php echo $conta->cor??'#3c8dbc'?>;color:#fff">
        <h3 class="box-title" style="color:#fff">
            <i class="fa <?php echo $conta->icone??'fa-university'?>"></i>
            <?php echo htmlspecialchars($conta->nome)?> — Saldo atual: R$ <?php echo number_format($conta->saldo_atual,2,',','.')?>
        </h3>
    </div>
    <div class="box-body no-padding">
        <table class="table table-striped table-hover" id="tbl-extrato">
            <thead>
                <tr><th>Data</th><th>Tipo</th><th>Descrição</th><th>Cliente</th><th>Status</th><th class="text-right">Valor</th></tr>
            </thead>
            <tbody>
            <?php $saldo_periodo=0; foreach($lancamentos as $l):
                $entrada = $l->tipo==='receber' && $l->status==='pago';
                $saida   = $l->tipo==='pagar'   && $l->status==='pago';
                if($entrada) $saldo_periodo += $l->valor;
                if($saida)   $saldo_periodo -= $l->valor;
            ?>
            <tr>
                <td><?php echo date('d/m/Y',strtotime($l->vencimento))?></td>
                <td><span class="label label-<?php echo $l->tipo==='receber'?'success':'danger'?>"><?php echo $l->tipo==='receber'?'Entrada':'Saída'?></span></td>
                <td><?php echo htmlspecialchars($l->descricao??$l->categoria??'-')?></td>
                <td><?php echo htmlspecialchars($l->client_nome??'-')?></td>
                <td><span class="label label-<?php echo $l->status==='pago'?'success':($l->status==='cancelado'?'default':'warning')?>"><?php echo $l->status?></span></td>
                <td class="text-right <?php echo $l->tipo==='receber'?'text-success':'text-danger'?>">
                    <?php echo $l->tipo==='receber'?'+':'-'?> R$ <?php echo number_format($l->valor,2,',','.')?>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr style="background:#f4f4f4;font-weight:bold">
                    <td colspan="5" class="text-right">Saldo do período:</td>
                    <td class="text-right text-<?php echo $saldo_periodo>=0?'success':'danger'?>">
                        R$ <?php echo number_format($saldo_periodo,2,',','.')?>
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
<?php else: ?>
<div class="callout callout-info"><p>Selecione uma conta acima para ver o extrato.</p></div>
<?php endif; ?>
</section>
<link href="<?php echo base_url('assets/css/datatables/dataTables.bootstrap.css')?>" rel="stylesheet">
<script src="<?php echo base_url('assets/js/plugins/datatables/jquery.dataTables.js')?>"></script>
<script src="<?php echo base_url('assets/js/plugins/datatables/dataTables.bootstrap.js')?>"></script>
<script>$(function(){ $('#tbl-extrato').dataTable({"order":[[0,'asc']],"pageLength":50,"language":{"url":"//cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json"}}); });</script>
