<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<section class="content-header">
    <h1><i class="fa fa-exchange"></i> Transferências entre Contas</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">Transferências</li>
    </ol>
</section>
<section class="content">
<div class="row">
    <div class="col-md-8">
        <div class="box box-primary">
            <div class="box-header with-border"><h3 class="box-title">Histórico de Transferências</h3></div>
            <div class="box-body no-padding">
                <table class="table table-striped table-hover">
                    <thead><tr><th>Data</th><th>Origem</th><th>Destino</th><th>Descrição</th><th class="text-right">Valor</th></tr></thead>
                    <tbody>
                    <?php foreach($transferencias as $t): ?>
                    <tr>
                        <td><?php echo date('d/m/Y',strtotime($t->data_transferencia))?></td>
                        <td><span class="label label-danger"><?php echo htmlspecialchars($t->conta_origem??'-')?></span></td>
                        <td><span class="label label-success"><?php echo htmlspecialchars($t->conta_destino??'-')?></span></td>
                        <td><?php echo htmlspecialchars($t->descricao??'-')?></td>
                        <td class="text-right"><strong>R$ <?php echo number_format($t->valor,2,',','.')?></strong></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if(empty($transferencias)): ?>
                    <tr><td colspan="5" class="text-center text-muted">Nenhuma transferência registrada</td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="box box-warning">
            <div class="box-header with-border"><h3 class="box-title"><i class="fa fa-exchange"></i> Nova Transferência</h3></div>
            <form method="post" action="<?php echo site_url('admin/financeiro/transferencias')?>">
            <div class="box-body">
                <div class="form-group">
                    <label>Conta de Origem <span class="text-danger">*</span></label>
                    <select name="conta_origem_id" class="form-control" required>
                        <option value="">Selecione...</option>
                        <?php foreach($contas as $ct): ?>
                        <option value="<?php echo $ct->id?>"><?php echo htmlspecialchars($ct->nome)?> (R$ <?php echo number_format($ct->saldo_atual,2,',','.')?>)</option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label>Conta de Destino <span class="text-danger">*</span></label>
                    <select name="conta_destino_id" class="form-control" required>
                        <option value="">Selecione...</option>
                        <?php foreach($contas as $ct): ?>
                        <option value="<?php echo $ct->id?>"><?php echo htmlspecialchars($ct->nome)?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label>Valor (R$) <span class="text-danger">*</span></label>
                    <div class="input-group">
                        <span class="input-group-addon">R$</span>
                        <input type="text" name="valor" id="val-transf" class="form-control" required placeholder="0,00">
                    </div>
                </div>
                <div class="form-group">
                    <label>Data</label>
                    <input type="date" name="data_transferencia" class="form-control" value="<?php echo date('Y-m-d')?>">
                </div>
                <div class="form-group">
                    <label>Descrição</label>
                    <input type="text" name="descricao" class="form-control" placeholder="Motivo da transferência">
                </div>
                <input type="hidden" name="submitted" value="1">
            </div>
            <div class="box-footer">
                <button type="submit" class="btn btn-warning btn-block"><i class="fa fa-exchange"></i> Registrar Transferência</button>
            </div>
            </form>
        </div>
    </div>
</div>
</section>
<script>
$(function(){
    $('#val-transf').on('blur',function(){
        var n=parseFloat($(this).val().replace(/\./g,'').replace(',','.'))||0;
        $(this).val(n.toFixed(2).replace('.',',').replace(/(\d)(?=(\d{3})+(?!\d))/g,'$1.'));
    });
});
</script>
