<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="content-header">
    <h1><i class="fa fa-money"></i> Sistema Financeiro <small><?php echo date('d/m/Y')?></small></h1>
    <ol class="breadcrumb"><li class="active"><i class="fa fa-dashboard"></i> Financeiro</li></ol>
</section>
<section class="content">

<?php if(!empty($vencidos)): ?>
<div class="alert alert-danger alert-dismissible">
    <button type="button" class="close" data-dismiss="alert">&times;</button>
    <i class="fa fa-exclamation-triangle"></i>
    <strong><?php echo count($vencidos)?> lançamento(s) VENCIDO(s)</strong> sem baixa.
    <a href="#sec-vencidos" class="btn btn-danger btn-xs pull-right">Ver</a>
</div>
<?php endif; ?>

<!-- Filtro de período -->
<div class="box box-default collapsed-box">
    <div class="box-header with-border">
        <h3 class="box-title"><i class="fa fa-filter"></i> Período</h3>
        <div class="box-tools pull-right"><button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-plus"></i></button></div>
    </div>
    <div class="box-body">
        <form method="get" class="form-inline">
            <div class="form-group" style="margin-right:10px">
                <label>De</label>
                <input type="date" name="data_ini" class="form-control" value="<?php echo $data_ini?>">
            </div>
            <div class="form-group" style="margin-right:10px">
                <label>Até</label>
                <input type="date" name="data_fim" class="form-control" value="<?php echo $data_fim?>">
            </div>
            <div class="form-group" style="margin-right:10px">
                <label>Cliente</label>
                <select name="client_id" class="form-control">
                    <option value="">Todos</option>
                    <?php foreach($contas_clientes??[] as $cl): ?>
                    <option value="<?php echo $cl->id?>" <?php echo ($this->input->get('client_id')==$cl->id)?'selected':''?>><?php echo htmlspecialchars($cl->name)?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i> Atualizar</button>
        </form>
    </div>
</div>

<!-- Saldos por Conta -->
<div class="row">
<?php foreach($contas as $conta): ?>
<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12" style="margin-bottom:10px">
    <div class="info-box" style="background:<?php echo $conta->cor?>">
        <span class="info-box-icon" style="background:rgba(0,0,0,0.15)">
            <i class="fa <?php echo $conta->icone ?? 'fa-university'?>"></i>
        </span>
        <div class="info-box-content" style="color:#fff">
            <span class="info-box-text" style="color:#fff"><?php echo htmlspecialchars($conta->nome)?></span>
            <span class="info-box-number" style="color:#fff">
                R$ <?php echo number_format($conta->saldo_atual,2,',','.')?>
            </span>
            <span style="font-size:11px;color:rgba(255,255,255,0.8)"><?php echo $conta->banco ?? $conta->tipo?></span>
        </div>
        <a href="<?php echo site_url('admin/financeiro/extrato/'.$conta->id)?>" style="position:absolute;bottom:5px;right:10px;color:rgba(255,255,255,0.7);font-size:11px">
            Extrato <i class="fa fa-arrow-right"></i>
        </a>
    </div>
</div>
<?php endforeach; ?>
<div class="col-lg-3 col-md-4 col-sm-6 col-xs-12" style="margin-bottom:10px">
    <a href="<?php echo site_url('admin/financeiro/contas')?>" class="info-box" style="background:#6c757d;color:#fff;text-decoration:none">
        <span class="info-box-icon" style="background:rgba(0,0,0,0.15)"><i class="fa fa-plus"></i></span>
        <div class="info-box-content" style="color:#fff">
            <span class="info-box-text" style="color:#fff">Nova Conta</span>
            <span class="info-box-number" style="color:#fff">Gerenciar</span>
        </div>
    </a>
</div>
</div>

<!-- Totais do período -->
<div class="row">
    <div class="col-md-3 col-sm-6">
        <div class="small-box bg-green">
            <div class="inner">
                <h3>R$ <?php echo number_format($totais->total_receber ?? 0,2,',','.')?></h3>
                <p>A Receber no Período</p>
            </div>
            <div class="icon"><i class="fa fa-arrow-down"></i></div>
            <a href="<?php echo site_url('admin/financeiro/lancamentos/receber?data_ini='.$data_ini.'&data_fim='.$data_fim)?>" class="small-box-footer">
                Ver detalhes <i class="fa fa-arrow-circle-right"></i>
            </a>
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="small-box bg-red">
            <div class="inner">
                <h3>R$ <?php echo number_format($totais->total_pagar ?? 0,2,',','.')?></h3>
                <p>A Pagar no Período</p>
            </div>
            <div class="icon"><i class="fa fa-arrow-up"></i></div>
            <a href="<?php echo site_url('admin/financeiro/lancamentos/pagar?data_ini='.$data_ini.'&data_fim='.$data_fim)?>" class="small-box-footer">
                Ver detalhes <i class="fa fa-arrow-circle-right"></i>
            </a>
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="small-box bg-blue">
            <div class="inner">
                <h3>R$ <?php echo number_format($totais->total_recebido ?? 0,2,',','.')?></h3>
                <p>Recebido</p>
            </div>
            <div class="icon"><i class="fa fa-check"></i></div>
            <a href="<?php echo site_url('admin/financeiro/lancamentos/receber?status=pago&data_ini='.$data_ini.'&data_fim='.$data_fim)?>" class="small-box-footer">
                Ver <i class="fa fa-arrow-circle-right"></i>
            </a>
        </div>
    </div>
    <div class="col-md-3 col-sm-6">
        <div class="small-box bg-yellow">
            <div class="inner">
                <h3>R$ <?php echo number_format($totais->total_pendente_receber ?? 0,2,',','.')?></h3>
                <p>Pendente a Receber</p>
            </div>
            <div class="icon"><i class="fa fa-clock-o"></i></div>
            <a href="<?php echo site_url('admin/financeiro/lancamentos/receber?status=pendente&data_ini='.$data_ini.'&data_fim='.$data_fim)?>" class="small-box-footer">
                Ver <i class="fa fa-arrow-circle-right"></i>
            </a>
        </div>
    </div>
</div>

<!-- Ações Rápidas -->
<div class="row" style="margin-bottom:15px">
    <div class="col-md-12">
        <a href="<?php echo site_url('admin/financeiro/add_lancamento?tipo=receber')?>" class="btn btn-success btn-sm">
            <i class="fa fa-plus"></i> Nova Receita
        </a>
        <a href="<?php echo site_url('admin/financeiro/add_lancamento?tipo=pagar')?>" class="btn btn-danger btn-sm">
            <i class="fa fa-plus"></i> Nova Despesa
        </a>
        <a href="<?php echo site_url('admin/financeiro/transferencias')?>" class="btn btn-warning btn-sm">
            <i class="fa fa-exchange"></i> Transferência
        </a>
        <a href="<?php echo site_url('admin/financeiro/relatorio')?>" class="btn btn-info btn-sm">
            <i class="fa fa-bar-chart"></i> Relatórios
        </a>
        <a href="<?php echo site_url('admin/financeiro/bpo')?>" class="btn btn-primary btn-sm">
            <i class="fa fa-university"></i> BPO Financeiro
        </a>
    </div>
</div>

<!-- A Receber no período -->
<div class="box box-success">
    <div class="box-header with-border" style="background:#00a65a;color:#fff">
        <h3 class="box-title" style="color:#fff"><i class="fa fa-arrow-down"></i> Total a Receber no período: R$ <?php echo number_format($totais->total_receber??0,2,',','.')?></h3>
    </div>
    <div class="box-body no-padding">
        <table class="table table-striped table-hover" id="tbl-receber">
            <thead>
                <tr style="background:#f4f4f4">
                    <th>Data</th><th>Tipo de Receita</th><th>Processo/Mês</th>
                    <th>Cliente</th><th>Data Vcto</th><th class="text-right">Valor R$</th>
                    <th>Status</th><th>Ação</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($a_receber as $l): ?>
            <tr class="<?php echo $l->status==='pago'?'success':($l->status==='pendente'&&strtotime($l->vencimento)<time()?'danger':'')?>">
                <td><?php echo date('d/m/y',strtotime($l->created_at))?></td>
                <td><?php echo htmlspecialchars($l->categoria??$l->subcategoria??'-')?></td>
                <td><?php echo htmlspecialchars($l->competencia_mes??$l->case_no??'-')?></td>
                <td><?php echo htmlspecialchars($l->client_nome??'-')?></td>
                <td><?php echo date('d/m/y',strtotime($l->vencimento))?></td>
                <td class="text-right"><strong>R$ <?php echo number_format($l->valor,2,',','.')?></strong></td>
                <td>
                    <span class="label label-<?php echo $l->status==='pago'?'success':($l->status==='cancelado'?'default':'warning')?>">
                        <?php echo $l->status==='pago'?'Recebido':($l->status==='cancelado'?'Cancelado':'Pendente')?>
                    </span>
                    <?php if($l->status==='pendente'&&strtotime($l->vencimento)<time()): ?>
                    <span class="label label-danger">Vencido</span>
                    <?php endif; ?>
                </td>
                <td>
                    <?php if($l->status==='pendente'): ?>
                    <button class="btn btn-success btn-xs btn-baixar" data-id="<?php echo $l->id?>" title="Dar Baixa"><i class="fa fa-check"></i></button>
                    <?php endif; ?>
                    <a href="<?php echo site_url('admin/financeiro/add_lancamento/'.$l->id)?>" class="btn btn-warning btn-xs" title="Editar"><i class="fa fa-pencil"></i></a>
                    <a href="<?php echo site_url('admin/financeiro/gerar_recibo/'.$l->id)?>" class="btn btn-info btn-xs" title="Recibo"><i class="fa fa-file-text"></i></a>
                    <a href="<?php echo site_url('admin/financeiro/delete_lancamento/'.$l->id)?>" class="btn btn-danger btn-xs" onclick="return confirm('Excluir?')" title="Excluir"><i class="fa fa-trash"></i></a>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<!-- A Pagar no período -->
<div class="box box-danger">
    <div class="box-header with-border" style="background:#dd4b39;color:#fff">
        <h3 class="box-title" style="color:#fff"><i class="fa fa-arrow-up"></i> Total a Pagar no período: R$ <?php echo number_format($totais->total_pagar??0,2,',','.')?></h3>
    </div>
    <div class="box-body no-padding">
        <table class="table table-striped table-hover" id="tbl-pagar">
            <thead>
                <tr style="background:#f4f4f4">
                    <th>Data</th><th>Descrição</th><th>Fornecedor</th>
                    <th>Forma Pgto</th><th>Data Vcto</th><th class="text-right">Valor R$</th>
                    <th>Status</th><th>Ação</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($a_pagar as $l): ?>
            <tr class="<?php echo $l->status==='pago'?'success':($l->status==='pendente'&&strtotime($l->vencimento)<time()?'danger':'')?>">
                <td><?php echo date('d/m/y',strtotime($l->created_at))?></td>
                <td><?php echo htmlspecialchars($l->descricao??'-')?></td>
                <td><?php echo htmlspecialchars($l->fornecedor??$l->client_nome??'-')?></td>
                <td><?php echo htmlspecialchars($l->forma_pagamento??'-')?></td>
                <td><?php echo date('d/m/y',strtotime($l->vencimento))?></td>
                <td class="text-right"><strong>R$ <?php echo number_format($l->valor,2,',','.')?></strong></td>
                <td>
                    <span class="label label-<?php echo $l->status==='pago'?'success':($l->status==='cancelado'?'default':'warning')?>">
                        <?php echo $l->status==='pago'?'Pago':($l->status==='cancelado'?'Cancelado':'Pendente')?>
                    </span>
                    <?php if($l->status==='pendente'&&strtotime($l->vencimento)<time()): ?>
                    <span class="label label-danger">Vencido</span>
                    <?php endif; ?>
                </td>
                <td>
                    <?php if($l->status==='pendente'): ?>
                    <button class="btn btn-success btn-xs btn-baixar" data-id="<?php echo $l->id?>" title="Pagar"><i class="fa fa-check"></i></button>
                    <?php endif; ?>
                    <a href="<?php echo site_url('admin/financeiro/add_lancamento/'.$l->id)?>" class="btn btn-warning btn-xs" title="Editar"><i class="fa fa-pencil"></i></a>
                    <a href="<?php echo site_url('admin/financeiro/delete_lancamento/'.$l->id)?>" class="btn btn-danger btn-xs" onclick="return confirm('Excluir?')" title="Excluir"><i class="fa fa-trash"></i></a>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php if(!empty($vencidos)): ?>
<div class="box box-danger" id="sec-vencidos">
    <div class="box-header"><h3 class="box-title text-danger"><i class="fa fa-exclamation-circle"></i> Vencidos sem Baixa</h3></div>
    <div class="box-body no-padding">
        <table class="table table-hover">
            <thead><tr><th>Cliente</th><th>Descrição</th><th>Vencimento</th><th>Atraso</th><th class="text-right">Valor</th><th>Ação</th></tr></thead>
            <tbody>
            <?php foreach($vencidos as $v):
                $dias = (int)floor((time()-strtotime($v->vencimento))/86400); ?>
            <tr>
                <td><?php echo htmlspecialchars($v->client_nome??'-')?></td>
                <td><?php echo htmlspecialchars($v->descricao??'-')?></td>
                <td class="text-danger"><?php echo date('d/m/Y',strtotime($v->vencimento))?></td>
                <td><span class="badge" style="background:#dd4b39"><?php echo $dias?> dias</span></td>
                <td class="text-right"><strong>R$ <?php echo number_format($v->valor,2,',','.')?></strong></td>
                <td>
                    <button class="btn btn-success btn-xs btn-baixar" data-id="<?php echo $v->id?>"><i class="fa fa-check"></i> Baixar</button>
                    <a href="<?php echo site_url('admin/financeiro/gerar_recibo/'.$v->id)?>" class="btn btn-info btn-xs"><i class="fa fa-file-text"></i></a>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

</section>

<!-- Modal Baixar -->
<div class="modal fade" id="modal-baixar">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header" style="background:#00a65a;color:#fff">
                <h4 class="modal-title"><i class="fa fa-check"></i> Confirmar Baixa</h4>
            </div>
            <form id="form-baixar" method="post">
                <div class="modal-body">
                    <div class="form-group">
                        <label>Data do Pagamento</label>
                        <input type="date" name="data_pagamento" class="form-control" value="<?php echo date('Y-m-d')?>">
                    </div>
                </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>

<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-receber,#tbl-pagar').dataTable({"order":[[4,'asc']],"pageLength":25,"language":{"url":"//cdn.datatables.net/plug-ins/1.10.21/i18n/Portuguese-Brasil.json"}});
    $('.btn-baixar').on('click',function(){
        $('#form-baixar').attr('action','<?php echo site_url('admin/financeiro/baixar/')?>'+$(this).data('id'));
        $('#modal-baixar').modal('show');
    });
});
</script>
