<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="content-header">
    <h1><i class="fa fa-bar-chart"></i> Relatórios Financeiros</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">Relatórios</li>
    </ol>
</section>
<section class="content">

<!-- Tipos de relatório -->
<div class="row" style="margin-bottom:15px">
    <?php
    $tipos = [
        'receber'    => ['Contas a Receber','success','arrow-down'],
        'pagar'      => ['Contas a Pagar','danger','arrow-up'],
        'geral'      => ['Financeiro Geral','primary','bar-chart'],
        'saldos'     => ['Saldos por Conta','info','university'],
    ];
    foreach($tipos as $t=>$info): ?>
    <div class="col-md-3 col-sm-6" style="margin-bottom:8px">
        <a href="<?php echo site_url('admin/financeiro/relatorio/'.$t.'?data_ini='.$data_ini.'&data_fim='.$data_fim)?>"
           class="btn btn-<?php echo $info[1]?> btn-block <?php echo $tipo===$t?'active':''?>">
            <i class="fa fa-<?php echo $info[2]?>"></i> <?php echo $info[0]?>
        </a>
    </div>
    <?php endforeach; ?>
</div>

<!-- Filtros -->
<div class="box box-default">
    <div class="box-body">
        <form method="get" class="form-inline">
            <div class="form-group" style="margin-right:8px">
                <label>De</label>
                <input type="date" name="data_ini" class="form-control input-sm" value="<?php echo $data_ini?>">
            </div>
            <div class="form-group" style="margin-right:8px">
                <label>Até</label>
                <input type="date" name="data_fim" class="form-control input-sm" value="<?php echo $data_fim?>">
            </div>
            <div class="form-group" style="margin-right:8px">
                <label>Conta</label>
                <select name="conta_id" class="form-control input-sm">
                    <option value="">Todas</option>
                    <?php foreach($contas as $ct): ?>
                    <option value="<?php echo $ct->id?>" <?php echo $conta_id==$ct->id?'selected':''?>><?php echo htmlspecialchars($ct->nome)?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-search"></i> Filtrar</button>
            <button type="button" class="btn btn-default btn-sm" onclick="window.print()"><i class="fa fa-print"></i> Imprimir</button>
        </form>
    </div>
</div>

<?php if($tipo==='saldos'): ?>
<!-- Relatório de Saldos -->
<div class="box box-info">
    <div class="box-header"><h3 class="box-title">Saldos por Conta</h3></div>
    <div class="box-body no-padding">
        <table class="table table-hover">
            <thead><tr><th>Conta</th><th>Banco</th><th>Tipo</th><th>Saldo Inicial</th><th class="text-right">Saldo Atual</th></tr></thead>
            <tbody>
            <?php $total=0; foreach($saldos as $c): $total+=$c->saldo_atual; ?>
            <tr>
                <td><span class="badge" style="background:<?php echo $c->cor?>"><?php echo htmlspecialchars($c->nome)?></span></td>
                <td><?php echo htmlspecialchars($c->banco??'-')?></td>
                <td><?php echo ucfirst($c->tipo)?></td>
                <td class="text-right">R$ <?php echo number_format($c->saldo_inicial,2,',','.')?></td>
                <td class="text-right <?php echo $c->saldo_atual<0?'text-danger':'text-success'?>">
                    <strong>R$ <?php echo number_format($c->saldo_atual,2,',','.')?></strong>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
            <tfoot><tr style="background:#f4f4f4;font-weight:bold">
                <td colspan="4" class="text-right">TOTAL:</td>
                <td class="text-right text-<?php echo $total>=0?'success':'danger'?>">R$ <?php echo number_format($total,2,',','.')?></td>
            </tr></tfoot>
        </table>
    </div>
</div>

<?php else: ?>
<!-- Resumo totais -->
<div class="row">
    <div class="col-md-3 col-sm-6"><div class="info-box"><span class="info-box-icon bg-green"><i class="fa fa-arrow-down"></i></span>
        <div class="info-box-content"><span class="info-box-text">Total Receitas</span>
        <span class="info-box-number">R$ <?php echo number_format($totais->total_receber??0,2,',','.')?></span></div>
    </div></div>
    <div class="col-md-3 col-sm-6"><div class="info-box"><span class="info-box-icon bg-red"><i class="fa fa-arrow-up"></i></span>
        <div class="info-box-content"><span class="info-box-text">Total Despesas</span>
        <span class="info-box-number">R$ <?php echo number_format($totais->total_pagar??0,2,',','.')?></span></div>
    </div></div>
    <div class="col-md-3 col-sm-6"><div class="info-box"><span class="info-box-icon bg-blue"><i class="fa fa-check"></i></span>
        <div class="info-box-content"><span class="info-box-text">Recebido</span>
        <span class="info-box-number">R$ <?php echo number_format($totais->total_recebido??0,2,',','.')?></span></div>
    </div></div>
    <div class="col-md-3 col-sm-6"><div class="info-box"><span class="info-box-icon bg-yellow"><i class="fa fa-clock-o"></i></span>
        <div class="info-box-content"><span class="info-box-text">Resultado</span>
        <span class="info-box-number">R$ <?php echo number_format(($totais->total_recebido??0)-($totais->total_pago??0),2,',','.')?></span></div>
    </div></div>
</div>

<!-- Tabela de lançamentos -->
<div class="box box-primary">
    <div class="box-header with-border">
        <h3 class="box-title"><?php echo count($lancamentos)?> lançamento(s)</h3>
    </div>
    <div class="box-body no-padding">
        <table class="table table-striped table-hover" id="tbl-rel">
            <thead><tr>
                <th>Data</th><th>Tipo</th><th>Categoria</th><th>Descrição</th>
                <th>Cliente</th><th>Conta</th><th>Vcto</th>
                <th class="text-right">Valor</th><th>Status</th>
            </tr></thead>
            <tbody>
            <?php foreach($lancamentos as $l): ?>
            <tr>
                <td><?php echo date('d/m/y',strtotime($l->created_at))?></td>
                <td><span class="label label-<?php echo $l->tipo==='receber'?'success':'danger'?>"><?php echo $l->tipo==='receber'?'Receita':'Despesa'?></span></td>
                <td><?php echo htmlspecialchars($l->categoria??'-')?></td>
                <td><?php echo htmlspecialchars($l->descricao??'-')?></td>
                <td><?php echo htmlspecialchars($l->client_nome??'-')?></td>
                <td><?php echo htmlspecialchars($l->conta_nome??'-')?></td>
                <td><?php echo date('d/m/y',strtotime($l->vencimento))?></td>
                <td class="text-right <?php echo $l->tipo==='receber'?'text-success':'text-danger'?>">
                    R$ <?php echo number_format($l->valor,2,',','.')?>
                </td>
                <td><span class="label label-<?php echo $l->status==='pago'?'success':($l->status==='cancelado'?'default':'warning')?>"><?php echo $l->status?></span></td>
            </tr>
            <?php endforeach; ?>
            </tbody>
            <tfoot><tr style="background:#f4f4f4;font-weight:bold">
                <td colspan="7" class="text-right">TOTAL RECEITAS / DESPESAS:</td>
                <td class="text-right">
                    R$ <?php echo number_format($totais->total_receber??0,2,',','.')?> /
                    R$ <?php echo number_format($totais->total_pagar??0,2,',','.')?>
                </td>
                <td></td>
            </tr></tfoot>
        </table>
    </div>
</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-rel').dataTable({"pageLength":50,"language":{"url":"//cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json"}}); });</script>
<style>@media print{.sidebar,.main-header,.content-header ol,.box-tools,form{display:none!important}.content-wrapper{margin:0!important}}</style>
