<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="content-header">
    <h1><i class="fa fa-university"></i> Contas Bancárias</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">Contas</li>
    </ol>
</section>
<section class="content">
<div class="row">
    <!-- Lista de contas -->
    <div class="col-md-7">
        <div class="box box-primary">
            <div class="box-header with-border"><h3 class="box-title">Contas Cadastradas</h3></div>
            <div class="box-body no-padding">
                <table class="table table-hover">
                    <thead><tr><th>Nome</th><th>Banco</th><th>Tipo</th><th class="text-right">Saldo Atual</th><th>Ações</th></tr></thead>
                    <tbody>
                    <?php $total=0; foreach($contas as $ct): $total+=$ct->saldo_atual; ?>
                    <tr>
                        <td><span class="badge" style="background:<?php echo $ct->cor?>"><?php echo htmlspecialchars($ct->nome)?></span></td>
                        <td><?php echo htmlspecialchars($ct->banco??'-')?></td>
                        <td><?php echo ucfirst($ct->tipo)?></td>
                        <td class="text-right <?php echo $ct->saldo_atual<0?'text-danger':'text-success'?>">
                            <strong>R$ <?php echo number_format($ct->saldo_atual,2,',','.')?></strong>
                        </td>
                        <td>
                            <a href="<?php echo site_url('admin/financeiro/extrato/'.$ct->id)?>" class="btn btn-info btn-xs" title="Extrato"><i class="fa fa-list"></i></a>
                            <button class="btn btn-warning btn-xs btn-editar-conta"
                                data-id="<?php echo $ct->id?>"
                                data-nome="<?php echo htmlspecialchars($ct->nome)?>"
                                data-banco="<?php echo htmlspecialchars($ct->banco??'')?>"
                                data-tipo="<?php echo $ct->tipo?>"
                                data-saldo="<?php echo $ct->saldo_inicial?>"
                                data-cor="<?php echo $ct->cor?>"
                                title="Editar"><i class="fa fa-pencil"></i></button>
                            <a href="<?php echo site_url('admin/financeiro/delete_conta/'.$ct->id)?>" class="btn btn-danger btn-xs" onclick="return confirm('Desativar conta?')" title="Desativar"><i class="fa fa-times"></i></a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    </tbody>
                    <tfoot>
                        <tr style="background:#f4f4f4;font-weight:bold">
                            <td colspan="3" class="text-right">TOTAL GERAL:</td>
                            <td class="text-right text-<?php echo $total>=0?'success':'danger'?>">R$ <?php echo number_format($total,2,',','.')?></td>
                            <td></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
    <!-- Formulário nova conta -->
    <div class="col-md-5">
        <div class="box box-success">
            <div class="box-header with-border"><h3 class="box-title" id="form-title">Nova Conta</h3></div>
            <form method="post" action="<?php echo site_url('admin/financeiro/contas')?>">
            <div class="box-body">
                <input type="hidden" name="id" id="conta-id" value="0">
                <div class="form-group">
                    <label>Nome da Conta <span class="text-danger">*</span></label>
                    <input type="text" name="nome" id="conta-nome" class="form-control" required placeholder="Ex: Banco do Brasil, Caixa...">
                </div>
                <div class="form-group">
                    <label>Banco / Instituição</label>
                    <input type="text" name="banco" id="conta-banco" class="form-control" placeholder="Ex: Caixa Econômica Federal">
                </div>
                <div class="form-group">
                    <label>Tipo</label>
                    <select name="tipo" id="conta-tipo" class="form-control">
                        <option value="corrente">Conta Corrente</option>
                        <option value="poupanca">Poupança</option>
                        <option value="caixa">Caixa (Dinheiro)</option>
                        <option value="cartao">Cartão</option>
                        <option value="aplicacao">Aplicação Financeira</option>
                        <option value="outros">Outros</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>Saldo Inicial (R$)</label>
                    <div class="input-group">
                        <span class="input-group-addon">R$</span>
                        <input type="text" name="saldo_inicial" id="conta-saldo" class="form-control" placeholder="0,00" value="0,00">
                    </div>
                </div>
                <div class="form-group">
                    <label>Cor de identificação</label>
                    <div class="row">
                        <?php foreach(['#00a65a'=>'Verde','#3c8dbc'=>'Azul','#f39c12'=>'Amarelo','#dd4b39'=>'Vermelho','#605ca8'=>'Roxo','#6c757d'=>'Cinza'] as $cor=>$nome): ?>
                        <div class="col-xs-2" style="padding:3px">
                            <div class="conta-cor" data-cor="<?php echo $cor?>" style="background:<?php echo $cor?>;height:30px;border-radius:4px;cursor:pointer;border:3px solid transparent" title="<?php echo $nome?>"></div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <input type="hidden" name="cor" id="conta-cor" value="#3c8dbc">
                </div>
                <input type="hidden" name="submitted" value="1">
            </div>
            <div class="box-footer">
                <button type="submit" class="btn btn-success"><i class="fa fa-save"></i> Salvar</button>
                <button type="button" class="btn btn-default" onclick="resetForm()">Limpar</button>
            </div>
            </form>
        </div>
    </div>
</div>
</section>
<script>
function resetForm(){
    $('#conta-id').val('0'); $('#conta-nome').val(''); $('#conta-banco').val('');
    $('#conta-tipo').val('corrente'); $('#conta-saldo').val('0,00');
    $('#form-title').text('Nova Conta');
}
$(function(){
    $('.conta-cor').on('click',function(){
        $('.conta-cor').css('border-color','transparent');
        $(this).css('border-color','#333');
        $('#conta-cor').val($(this).data('cor'));
    });
    $('.btn-editar-conta').on('click',function(){
        $('#conta-id').val($(this).data('id'));
        $('#conta-nome').val($(this).data('nome'));
        $('#conta-banco').val($(this).data('banco'));
        $('#conta-tipo').val($(this).data('tipo'));
        $('#conta-saldo').val(parseFloat($(this).data('saldo')).toFixed(2).replace('.',','));
        $('#conta-cor').val($(this).data('cor'));
        $('#form-title').text('Editar Conta');
        $('html,body').animate({scrollTop:0},400);
    });
});
</script>
